CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Caching in ASP.Net Applications

By Bala Murugan
Posted On Jan 06,2011
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

Caching in ASP.Net Applications

Caching is one of the best practices to improve the performance of any application we develop. For example, any business application will be very dynamic, complex and resource intensive in nature. In these scenarios, one can consider caching the output from a resource intensive processing so that the output can be served directly for the subsequent requests without processing it again. Moving forward, we will see how to do caching in ASP.Net and different techniques the framework offers to support caching.

 

ASP.Net supports the below caching techniques,

1.      Page Level Caching(Output Caching)

2.      Fragment caching or Control (Output Caching)

3.      Application or Data Caching(also programmatic caching)

 

Page Level Caching

Using this technique we can cache the whole aspx page output. Since we are caching page output, it is called as page output caching. In this technique, the page will be executed for the first time and will be cached for the duration specified in the expiry setting. The subsequent requests will be served with the cached page output instead of processing the page again. This is achieved by using @OutputCache directive in the aspx page. The below setting will cache the Default.aspx page for 120 seconds.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

 

It is compulsory to have Duration and VaryByParam parameter with some settings in @OutputCache directive for the caching to work. 

There will be situations where we need cache the page for different input parameters on the page. The input will be querystring in GET and form fields in POST requests. It is obvious that the page output will be different for different input parameters. In this case, to cache the output of different versions of page we need to set the parameter name(s) in VaryByParam parameter. For example, if you have querystring called catid and if you like to cache the page for different category values then the below will work,

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ OutputCache Duration="120" VaryByParam="catid"%>

 

The same setting will apply to a POST request with catid as form field. The above setting will cache the page out with different catid values.

The None value in VaryByParam will make ASP.Net to cache the page without any input parameter. You can give multiple parameters by separating the parameter with semicolon(;). If you want to cache the page for all the parameter values, then specify VaryByParam="*".

The other parameters which helps in caching different version of page is,

Ø       VaryByControl

Ø       VaryByHeader

Ø       VaryByCustom

Ø       VaryByContentEncodings

 

VaryByControl

This parameter accepts semicolon separated list of control ids to cache the different versions of page. See below,

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ OutputCache Duration="120" VaryByControl="txtEmpID"%>

 

The above setting will cache the page for different values in txtEmpID.

Note

If you specify the VaryByControl then VaryByParam is optional i.e. Duration and either VaryByControl or VaryByParam is mandatory.

 

VaryByHeader

This setting will cache different versions of the page depending upon the http header set in the request. Refer below,

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ OutputCache Duration="120" VaryByParam="None" VaryByHeader="User-Agent" %>

 

The above setting will cache different version of page when requested from different user agents i.e. IE, firefox, etc. You can specify multiple header values separated by semicolon.

 

VaryByCustom

This can be used to cache with a custom parameter. Refer this post which speaks about the parameter usage.

 

VaryByContentEncodings

This parameter can be used to cache different version of the page depending on different encoding.

 

Where the Cached elements are stored ?

The other useful parameter of @OutputCache directive is Location which dictates where the cached items should be stored. It is an enumeration of type OutputCacheLocation. The possible values are,

Any     

The output cache can be located on the browser client, on a proxy server, or on the server where the request was processed.

Client  

The output cache is located on the browser client where the request is originated.

Downstream  

This make the cache to be stored in proxy servers and the client that made the request other than the processing server.

Server

The output cache is located on the Web server where the request was processed

None   

This dictates that the cache is stored nowhere. In other words, the output cache is disabled for the requested page.

ServerAndClient        

The output cache can be stored only at the origin server or at the requesting client.

 

Fragment caching or Control Caching

Using this technique we can cache a part or fragment of a page in ASP.Net. The fragment that needs to be cached should be a usercontrol so that it can be cached. Again, this is done by using @OutputCache directive. Refer below,

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

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

 

The fragment caching supports VaryByParam, VaryByControl and VaryByCustom parameters.

 




Application or Data Caching (also called programmatic caching)

Using this technique, we can cache data programmatically from a complex and resource intensive calculations/processing. Something like this,

if (Cache["categories"] == null)

        {

            Cache["categories"] = GetCats();

        }

        else

        {

           DataSet ds =  Cache["categories"] as DataSet;

        }

 

Conclusion

Caching is one of the very useful techniques when we are looking at improving our application’s performance. Again, we need to be very cautious on deciding where and when to use it to prevent any backfire since it involves managing the memory of the web server. Stay tuned for my next article which will discuss on adding dependency on cache and cache invalidation techniques.

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments