Cache in Asp.Net


When a user request a server we know asp pages will load the data dynamic content

For example :MI Home page ,there we can able to view differnet modules loaded in the home page dynamically when there is changes in Database. without the need for the administrator to change the page content every time something changes in the data store will reflect into Home page dynamically.

Disadvantage is the overhead in creating the pages for every user request.

ASP.NET provides support for "caching" which will help us solve this problem to a great extend. It can cache [store in memory] the output generated by a page and will serve this cached content for future requests. And this is useful only in the second scenari

In a simple way we can say as on using caching we can able to store the expensive data on a single request for later use (This helps you to avoid multiple request to fetch the data)

Caching a page :

To work with caching we have to define an attributes at the top of the page as :

@OutputCache directive at the top of the page

In Caching we have to use 2 Different attributes

1.Duration (Duration helps that how much time a cache should hold in an cache object)

2.VaryByParam (it handel the querystring parameters to vary the cache Like “ID”)

Syntax :

<%@ OutputCache Duration=5 VaryByParam="id"%>

in the above syntax we need to observe that

Duration is 5 Seconds

VaryByParam is holding the “id” parameter.

If you want to specify multiple varybyparam in a single attribute we have to use “semicoln” to seperate the parameters name .

If we specify the VaryByParam attribute as *, the cached content is varied for all parameters passed through the querystring.

If we want to generate different content for different browsers

VaryByCustom="browser"(This helps you to work with different browser with different dynamic content )

Caching page fragments :

Page fragment cache is used to hold just portions of a page. That means we can able to hold portion of a page .

For Example :

we might have a header for our page which will have the same content for all users. There might be some text/image in the header which might change everyday. In that case, we will want to cache this header for a duration of a day.

In real time senario is User Control

The solution is to put the header contents into a user control and then specify that the user control content should be cached. This technique is called fragment caching.


<%@ OutputCache Duration=10 VaryByParam="None" %>

Data Caching :

If we want to store the data in a string then we can go for data caching .

We can store objects in memory and use them across various pages in our application.

This feature is implemented using the “Cache” class.Objects can be stored as name value pairs in the cache

How to store the Data cachi object as below syntax



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:

Syntax :

Cache.Insert("Name", strName,

new CacheDependency(Server.MapPath("name.txt"),

DateTime.Now.AddMinutes(2), TimeSpan.Zero);

Points of interest

If there are old ASP pages in your website which use the Response.Expires property to cache page output, they can be retained as such. ASP.NET supports this property as well.

The Insert method of the Cache class will overwrite any existing item with the same key name.

The CacheItemPriority.NotRemovable priority value can be used with Cache.Insert method to set the priority level of an item so that the item will not be removed from the cache during scavenging.


One thought on “Cache in Asp.Net

  1. This is a comment to the admin. Your website is missing out on at least 300 visitors per day. I have found a company which offers to dramatically increase your traffic to your website: They offer 1,000 free visitors during their free trial period and I managed to get over 30,000 visitors per month using their services, you could also get lot more targeted traffic than you have now. Hope this helps 🙂 Take care.

Thanks a lot for visiting this site .. Please leave a Comment

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

You are commenting using your 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