Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 39,909 times

Contents

Related Categories

.NET Data Caching - Using Data Caching

DMarko1

Using Data Caching

The .NET data caching API is comprised of the two classes in the System.Web.Caching namespace. The first class, Cache , is the class we'll be using to add and remove items from the data cache. The second class, CacheDependency , is used when assigning a cache dependency to an item in the data cache (we'll be discussing this in due time).

To add an item to the cache you can simply do:

' In VB.NET
Cache("key") = value

// In C#
Cache["key"] = value;

Note that the C# version uses brackets instead of parenthesis when referencing the items of the cache in the above manner. In the remainder of the examples in this article I will be using VB.NET syntax, but will point out any major differences between the VB.NET and C# syntax.

The above code adds the item value to the data cache with the key key . The key is used to reference the item at some later point. That is, in another ASP.NET Web page we can extract the value inserted above by using:

value = Cache("key")
- or -
value = Cache.Get("key"

To explicitly remove an item from the data cache you can use the Remove method, specifying the key of the cache item you want removed:

Cache.Remove("key")

Now that we've examined the simple form for adding, removing, and selecting an item from the data cache, let's take a more in-depth look at adding items to the cache. In the next section we'll examine the Cache.Insert method in detail, which can be used to enter cache items with cache dependencies, absolute and sliding time expirations, eviction priority, and callback delegates.

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Re: Cache is not Available

    Posted by ojemuyiwa on 15 May 2008

    To Access the Pages Cache from class code c#

     

    Cache ce = new Cache();

     

    ce = System.Web.HttpRuntime.Cache;

    Lol

  • Cache is not Available

    Posted by muthup on 30 Apr 2005

    So , Does That mean this is a Genuine Microsoft Bug??
    No matter What If I Use this Syntax[ Colorede red] to acess the Cache Object I get an Exception...
    internal class EnvUtil :System.Web.UI.Page {...

  • Posted by James Crowley on 30 Apr 2005

    Just because the Cache cannot be inherited, doesn't mean you can't access it when you are inheriting the Page class -

    public class MyClass : System.Web.UI.Page {
    public Object SomeMethod() {
    ...

  • Cache is not Available

    Posted by muthup on 30 Apr 2005

    I stumbled into this problem. The comment is correct The Cache class is a sealed class, which means it cannot be inherited. Since you are inheriting the Page class in your X testClass the Cache object...

  • Posted by DMarko1 on 23 Mar 2005


    Hi Charlie,

    You can use .NET remoting when you need to keep your cache across processes. The process is discussed here [url="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda...