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