Home > DotNet > How to use cache in ASP.Net ?

How to use cache in ASP.Net ?


Why need to cache data in ASP.Net? 

Most of the developers don’t know where can use cache mechanisam in ASP.Net.   For good performance of ASP.Net dynamic application we can use cache mechanism. This is nothing but we store some dynamic data into the cache for reusablility of web pages when user request the same again. That means suppose we have a link in our websites, that displays all the items in the database. When user clicks first time, it will fetch all data from the database and displayed to the web page. If a user trying to get same data for the second time, normally we are not able track whether the user tried same data before, so we are doing same procedure again (Fetching data from database and render to the html). As a optimisation part of the we bsite, we need to fetch same data only in single time for a user (Because new items inserted in to the database is rare case). 

Simple codes to cache data in ASP.Net 

To achieve tracking of user’s request, we can use cache mechanism. In the above example when a user trying to get all items from the database, we need to fetch this data and will store to cache object. When the same user trying to get same data for the second time, we need to check is there any data in the cache object, if it is there just displayed to the web page else taken from the db and store to the cache for future. 

How to store a string variable to cache? 

Cache["name"]="Smitha";
 if (Cache["name"] != null)
    Label1.Text= Cache["name"].ToString();

 To insert objects into the cache, the Add method or different versions of the Insert method of the Cache class can be used. These methods allow us to use the more powerful features provided by the Cache class. One of the overloads of the Insert method is used as follows: (How to store data object into cache in ASP.Net ?)

Cache.Insert("Name", strName,
    new CacheDependency(Server.MapPath("name.txt"),
    DateTime.Now.AddMinutes(2), TimeSpan.Zero);
 

The first two parameters are the key and the object to be inserted. The third parameter is of type CacheDependency and helps us set a dependency of this value to the file named name.txt. So whenever this file changes, the value in the cache is removed. We can specify null to indicate no dependency. The fourth parameter specifies the time at which the value should be removed from cache. [See example 5 for an illustration.] The last parameter is the sliding expiration parameter which shows the time interval after which the item is to be removed from the cache after its last accessed time.

The cache automatically removes the least used items from memory, when system memory becomes low. This process is called scavenging. We can specify priority values for items we add to the cache so that some items are given more priority than others:

Cache.Insert("Name", strName,
    new CacheDependency(Server.MapPath("name.txt"),
    DateTime.Now.AddMinutes(2), TimeSpan.Zero,
    CacheItemPriority.High, null);
Categories: DotNet Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: