Caching in Asp.Net

There are 2 ways to do caching in
1. Output Caching

2. Data Caching

1. Output Caching : Ability of ASP.NET to cache entire output from a webpage.

Implementation of output cache –
Add following line below the page directive 

It generate page once, then other all subsequent requests in certain time frame, when user requests page dont run it through just return the HTML that was generated on the previous run.
IF VaryByparameter =”CategoryId” THEN

ASP.NET cache page depends on categoryId, when a userrequest a page with the same categoryId in query string then the cached version of page will be displayed.

If another user request the same page with another categoryId then generates page again.

For 2 query string Parameters – VaryByParam=”ProductId;Quantity”

Substitution Control – When only one part of page need to regenerate every time, use substitution control.

Substitution control defines an area it always be requested on every round trip regardlessly whether page is cached  or not.

2. Data Caching : Write code to programmatically cache an object (e.g. Dataset ,variable). So, in order to reduce the workload and time for data retrieving, you can use a caching functionality.

DataSet ds;
String ObjectName=”CategoryId”+categoryid;
If(Cache[ObjectName] != Null)
-If ds is already cached means(not null) then it shows cached version of ds.
-If ds is not cached in Cache[ObjectName] array then ds binding method called.
As you can see, the usage of the caching functionality is quite simple. And, you can use any appropriate code in your application architecture inside the anonymous method so that it would be easy to add caching support to your application.


Leave a Reply

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

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

Google+ photo

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

Connecting to %s