The ASP.NET Cache API
The final section of this article looks briefly at how you can use the
Cache API provided with ASP.NET and the .NET Framework. Working with the Cache object in its simplest form is similar to using the Session and Application objects; however, the Cache provides many more opportunities to store and retrieve data, and to manage data priority and expiration.
The simplest approach uses the key name directly, thoughas with the Session and Application objectsyou should cast the returned value back to the correct data type:
Cache("key-name") = value-to-cache
retrieved-value = CType(Cache("key-name"), object-type)
If the key name does not match an entry in the cache, it returns
Nothing (
null in C#). Remember that items may be ejected from the cache if memory or disk constraints demand it, and so you should test for the presence of a value and re-create it if it is not availablefor example:
Dim iMyValue As Integer = CType(Cache("myintvalue"), Integer)
If iMyValue Is Nothing Then
' re-create the item
iMyValue =
{your code here to create or fetch the value again}
' and add to cache
Cache("myintvalue") = iMyValue
End If
The Cache API also exposes two methods for adding items to the cache. The
Insert method replaces any existing item with same key:
Cache.Insert("key-name", value-to-cache)
The
Add method fails if an item with the same key already exists in the cache, and returns the existing item instead. If the item does
not exist, it is added to the cache and the method returns
Nothing (
null in C#):
cached-item = Cache.Add("key-name", value-to-cache)
| Author's Note: See the .NET Framework SDK topic "Cache Members" in the "Reference | Class Library" section for more details. |
Working with Cache Dependencies
Items added to the
Cache can be dependent on a range of factors:
- One or more disk files or folders
- A fixed or sliding "last accessed" time span
- An absolute date and/or time for expiration
- Other cache dependencies (chained expiration)
- Individual item priorities when the cache is purged
You can also detect when an item is removed from the cache using a callback, by handling the
CacheItemRemoved event. Table 2 shows the full list of parameters for the
Add and
Insert methods of the Cache object, indicating how you specify the dependencies for an item when adding it to the Cache.
Table 2. The table lists the parameters for the Add and Insert methods of the Cache class.
| Parameter
|
Description
|
| key
|
A String that is the key name for the item in the cache.
|
| value
|
An Object that contains the value to be inserted into the cache.
|
| dependencies
|
A reference to a CacheDependency instance. This can be a new instance based on any of the factors described in the bullet list above (the following sections detail the technique for creating these dependencies). Alternatively, it can be an existing dependencyso that when the existing dependency causes its cached data to be invalidated, the current data will also be invalidated.
|
| absoluteExpiration
|
A DateTime value that defines the time that the cached data will be invalidated.
|
| slidingExpiration
|
A TimeSpan that defines the length of time for which the cached data is valid.
|
| priority
|
A CacheItemPriority value that controls how the item will be handled if the cache becomes full and items must be ejected automatically. The CacheItemPriority enumeration contains the values Default, High, AboveNormal, Normal, BelowNormal, and Low to specify the priority and allow items to be purged when available memory runs out, and the value NotRemovable (the default) to indicate that the item should not be removed.
|
| onRemoveCallback
|
A reference to a CacheItemRemovedCallback instance, which will be executed when this item is invalidated and removed from the cache.
|
For example, to insert a new value, you can write:
Cache.Insert("key", value, Nothing, _
DateTime.Now.AddMinutes(10), _
TimeSpan.Zero, CacheItemPriority.High, Nothing)