Subscribe

RSS Feed (xml)

Inserting Data into the Cache

There are several ways to add items into the Cache API:



Dictionary style-Similar to the way the Session or Application API is used.


Insert method-Inserts the named item into the cache and allows for additional


parameters that specify other cache behavior, such as when the item should be


removed from the cache.


Add method-Same as insert, but adds the item only if it did not already exist.


In the following examples, products is assumed to be a dataset. However, it could be


any .NET class.


' Add an item using the dictionary style API


Cache("MyProducts") = products


' Insert an item using the overloaded Insert method


Cache.Insert("MyProducts", products)


' Insert an item and specify a cache dependency


Cache.Insert("MyProducts", products, cacheDependency)


' Insert an item and specify that the item exists for only 60 minutes


Cache.Insert("MyProducts", products, Nothing,


DateTime.Now.AddMinutes(60), TimeSpan.Zero)


' Insert an item and specify that the item exists for only 60 minutes, has a


' high priority and specify a callback function called when the item is


' removed from the cache


Cache.Insert("MyProducts", products, Nothing, DateTime.Now.AddMinutes(60),


TimeSpan.Zero, CacheItemPriority.High, cacheCallBack)


' Add an item using the Add method


Cache.Add("MyProducts", products, cacheDependency,


DateTime.Now.AddMinutes(60), TimeSpan.Zero,


CacheItemPriority.High, cacheCallBack)


Comments


The Cache API, accessible simply as the Cache property within an ASP.NET page, is the


underlying data store used by page output caching, partial page caching, and Web service


caching.There are many cases when caching the output of the page is not desirable, but


data can still be reused. One example is a listing of products or reports.The data is stored


in memory, but options within the page determine how the data is displayed.


Something to keep in mind when using the cache is that it is scavenging.As memory is


needed, items are removed from the cache using an LRU (least recently used) algorithm.


Therefore, before using an item from the cache it is always wise to verify that the item


exists and is not Nothing:


If (Cache("MyProducts") Is Not Nothing)


… Insert into the cache


End If


The various parameters accepted by Insert() and Add() provide control over how the


cache stores data.You can provide a CacheDependency, which then enforces a relationship


between the item stored within the cache and either a file or cache key.When the


file or cache key changes, the related entry within the cache is removed. It is also possible


to specify time-based constraints using a fixed point in time or a sliding window of


time. It's possible to control the priority of items stored within the cache using the


CacheItemPriority enumeration-this affects how the scavenging algorithm removes


items. Finally, you can specify a callback function that the cache will call when an item is


removed.

No comments:

Archives

Variety in the Web World