.NET Data Caching

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.

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

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 des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”