Click here to Skip to main content
Click here to Skip to main content

Advanced Data Caching features in ASP.NET

, 29 May 2004
Rate this:
Please Sign up or sign in to vote.
An article on data caching features of ASP.NET.

Introduction

This article is a short description on different types of cache dependencies and how we can repopulate the expired cache automatically using callback methods.

Data caching

ASP.NET has a full-featured cache engine that can be used by applications to store arbitrary objects in memory. This mechanism is implemented via the Cache class, an instance of which exists for each application configured on a web server. The application cache provides a simple dictionary interface using key/value pairs that enables you to store and retrieve objects from the cache.

The application cache is global to the application and every user who accesses that application can access the value in the cache (don’t mix it up with session). Placing an item in cache is simple. It is like adding an item to a dictionary.

Adding a DataSet named dstEmployee to the Cache with a key name myEmpDataset is as follows:

 Cache["myEmpDataset"] = dstEmployee

Inserting an Item to Cache

The Add method enables you to add an item to the cache. The Add method ignores any call to add an item if another item with the same key already exists in the cache. The insert method functionally is the same as Add method but it overwrites the item if another item with the same key already exists in the cache.

Let's look at the insert method:

public void Insert(string key, object value,CacheDependency dependencies,
  DateTime absoluteExpiration,
  TimeSpan slidingExpiration,
  CacheItemPriority priority,
  CacheItemRemovedCallback onRemoveCallback );
  • value: The object to be inserted in the cache.
  • key: The cache key used to reference the object.
  • dependencies: The file or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter contains a null reference (Nothing in Visual Basic).
  • absoluteExpiration: The time at which the inserted object expires and is removed from the cache.
  • slidingExpiration: The interval between the time the inserted object was last accessed and when that object expires. If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed.
  • priority: The cost of the object relative to other items stored in the cache, as expressed by the CacheItemPriority enumeration. This value is used by the cache when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.
  • onRemoveCallback: A delegate that, if provided, will be called when an object is removed from the cache. You can use this to notify applications when their objects are deleted from the cache.
Cache.Insert ("DSN", connectionString,new CacheDependency(“filepath”) 
  DateTime.Now.AddMinutes(2), 
  TimeSpan.Zero, 
  CacheItemPriority.High,
  new CacheItemRemovedCallback(this.RemovedCallback)
);

When we are inserting an object to cache, we can set the dependency and a call back function that will be executed when the cache expires. CacheItemRemovedCallback is a delegate, which defines a callback method for notifying applications when a cached item is removed from the Cache. Now, we can do some cool things with these two features. We will first look at setting the dependencies.

What is a dependency?

When we insert an item to the cache, we can set the expiration policy of the item based on other resources such as physical file(s) in the server, another cache item(s), or time. Any change in the resources which our cache item depends on, will make our cache item expire.

There are three types of dependencies:

  • File based dependency
  • Key based dependency
  • Time based dependency
File based dependency

We can set the validity of the cache item based on an external file or files. When this resource changes, the cached object becomes obsolete and is removed from the cache.

CacheDependency dependency = new CacheDependency(Server.MapPath("isbn.xml"));

For multiple file dependencies, we can pass a string array containing the filename to the constructor.

Key based dependency

Key based dependency enables you to associate one cached item with another item in the cache and invalidates the first item when the second item changes. The following code fragment demonstrates how to insert an item into your application's Cache with a dependency on a key to another item placed in the cache. Since this method uses array syntax, you must define the number of keys the item you are adding to the Cache is dependent on.

String[] dependencyKey = new String[1];
dependencyKey[0] = "key1"; 
CacheDependency dependency = new CacheDependency(null, dependencyKey);

You can use multiple key dependencies by increasing the array size and adding the keys to the array.

Time based dependency.

A time based dependency expires the item at a predefined time. There are two types of time-based dependency: Absolute and Sliding. I have explained the two in the insert method.

Creating call back methods for a cached item

In addition to setting dependencies on keys, files, or time, we can also associate a call back method to a cached item. This method is automatically called when the cache item is expired. By using this feature, we can ensure that the cache item is not removed or cache is updated based on new data. To implement a callback notification, we need to use the CacheItemRemovedCallback delegate. An instance of this delegate is passed to the insert method when we insert a cache item. This delegate has the signature as follows:

public delegate void CacheItemRemovedCallback( string key, 
    object value, CacheItemRemovedReason reason );
  • key: The index location for the item removed from the cache.
  • value: The object item removed from the cache.
  • reason: The reason the item was removed from the cache, as specified by the CacheItemRemovedReason enumeration.

The third parameter is of interest to us. Using this parameter, we can get the reason for the removal of the cache item. The CacheItemRemovedReason enumerations has values as follows:

  • DependencyChanged: The item is removed from the cache because a file or key dependency changed.
  • Expired: The item is removed from the cache because it expired.
  • Removed: The item is removed from the cache by a Remove method call, or by an Insert method call that specified the same key.
  • Underused: The item is removed from the cache because the system removed it to free memory.

Example of creating a callback method to handle expiration event:

public void RemovedCallback(String k, 
       Object v,CacheItemRemovedReason r) {
// Write code here to check the reason for expiration
// and to repopulate using new values
}

Points of Interest

There is nothing in this world which is 100% a boon. There is a dark side in caching. When you enable caching, it takes up valuable system resources and can easily eat up all the memory. When server memory runs out, the content of the cache is cleaned automatically. The cleaning up is done based on factors such as priority of the cache item and the usage of the cache item. We can set the priority of each cache item in the insert method using CacheItemPriority enumeration.

When we are repopulating the cache using the callback method, do check the CacheItemRemovedReason parameter. If the reason is Removed or Underused, then don't try repopulating it.

History

Article version 1.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ansil
Software Developer (Senior)
India India
Ansil hails from Trivandrum.
He works for Microsoft india (R&D ).
he blogs at http://ansilf.spaces.live.com/
 

 
"First they ignore you;then they laugh at you ;then they fight you ;then you win "

Comments and Discussions

 
GeneralArticle Comments Pinmemberboss007v2-May-08 1:17 
GeneralProblem with Cachedependency.. PinmemberRajkumarvasan22-Feb-07 1:24 
AnswerRe: Problem with Cachedependency.. PinmemberStiGMaTa_Dev12-Mar-07 23:13 
GeneralNCache supports ASP.NET 2.0 Cache API PinmemberIqbalKhan25-Jul-05 14:40 
GeneralCache Dependencies in ASP.NET 2.0 PinmemberAnsil19-Jun-04 20:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 30 May 2004
Article Copyright 2004 by Ansil
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid