Saturday, July 5, 2008

Application Caching in .Net 2.0

Application Caching in .Net 2.0

What is Application Caching?
Application Caching is a technique in .Net 2.0 for caching different type of objects for faster access. As the name itself depicts, the Cache Object can be used from the entire application. A single cache object lies for the entire application and so can be used between various different user sessions and requests.

How Application Caching works and How to use it?
The main motive for Application Caching is to store cached Objects in memory for faster access. Expensive operation to obtain and persists data or information can lead to application failure or sluggishness at time of heavy traffic. Also the amount of time and resources involved in such operations may not be satisfactionary. So to improve upon the performance and the scalability of your application you can cache the expensive data for faster access.

You can use either Cache.Add() or Cache.Insert() methods for caching your data. The only difference between the two is, Cache.Add() method returns the object which you want to cache.
So let’s say if you want to use the object and cache it as well. You can do so in a single line of code with the help of Cache.Add().

Cache.Insert() methods has 4 different types of overloaded methods while Cache.Add() has only one.



Different Parameters used for the above methods:

Key
It is the name for the cached object by which it will be accessed. You can also use the key to remove the cached object from the memory. Following code will remove the cached object named “customerList” from the memory.

Cache.Remove("customerList");


Object to be cached
It is the object that you want to cache. This object will be stored in the memory for faster retrieval.


Dependency
It is the CacheDependency object that can be a file or cache key. So if the file or cache key is changed than the object in the cached will be invalidated and removed from the cache. This can be used to maintain the freshness of the data. This is an optional parameter. You can set it to null. Incase of multiple dependency conditions you can use AggregateCacheDependency object instead of CacheDependency.


Absolute Expiration DateIt is the time at which the object will be removed from the cache. To remove the object from cache after 10 minutes you can set this parameter to DateTime.Now.AddMinutes(10) as shown in the below code.

Cache.Insert("customerList", objDataSet, new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration,CacheItemPriority.Normal, null);


If you do not want the object to expire forcefully after some time than you may set this parameter to Cache.NoAbsoluteExpiration as shown in below code.
Cache.Insert("customerList", objDataSet,new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,CacheItemPriority.Normal, null);

Time Span
It is the time span after which the object will be removed from the cache if it was not accessed. So lets say if you want to remove the object from cache if it has not been accessed since last 10 minutes than you can use the below code.

Cache.Insert("customerList", objDataSet,new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),Cache.NoAbsoluteExpiration,DateTime.Now.AddMinutes(10),CacheItemPriority.Normal, null);


If you do not want the object to expire forcefully after some time it was last accessed than you may set this parameter to use Cache.NoSlidingExpiration  as shown in below code.
Cache.Insert("customerList", objDataSet,new CacheDependency(Server.MapPath("~/App_Data/customerList.xml")),Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,CacheItemPriority.Normal, null);

Note: If you are using this sliding expiration time parameter than Absolute Expiration Date parameter must be Cache.NoAbsoluteExpiration

Priority
It is the priority at which the objects will be purged from the memory incase if the memory runs low. Lower priority objects will be removed first.

Callback Method
It is the event handler that will run when the object is removed from the cache. If you do not want to use this parameter than you can set this to null.

To download Application caching code sample Click Here.
Note: To introduce sophisticated caching layer in your application you can always use Microsoft’s Enterprise Library - Caching Application Block.

2 comments:

vinay said...

hi i am vinay preparing .net pls provide me material for application management

wesnur said...

Hi,

There is a lot of stuff available on internet talking about the usages of the CAB. But I want to draw the attention of all the readers towards another important issue. And that is the The Drawbacks of using Regular Caching Application Block. Enterprise Library 2.0 provides a stand-alone caching block. However, if your .NET application is running in a server farm then your cache must be distributed in order to avoid data integrity problems in your cache. I would suggest a through research before going ahead.