Tuesday, March 12, 2013

Caching in ASP.NET

learn asp.net caching


               CACHING IN ASP.NET


Caching means to store something in memory that is being used frequently to provide better performance.
Types of Caching
There are two types of caching available in ASP.NET:
•Page Output Caching
•Application Caching

Page Output Caching

Page output caching refer to the ability of the web server to cache a certain webpage after user request in its memory so that further requests for the same page will check for the cached page's validity and will not result in resource usage (DB access or file access) and the page will be returned to user from cache.
Parameters that we can use to customize caching behavior.
VaryByParam: List of strings that are sent to server via POST that are checked to   validate cache
VaryByControl: List of controls whose value will determine the validity of cache
SqlDependency: Defines the Database-tablename pair on which the validity of cache depends
VaryByCustom: Used for custom output cache requirements
VaryByHeader: HTTPs header that determines the cache validity


If we need different output pages based on query string, then we can specify the list of querystringparameters here or we can simply say VaryByParam ="*" to do the same for all querystring parameters. 

Example


<%@ OutputCache Duration="30" VaryByParam="name" %>
 protected void Button2_Click(object sender, EventArgs e)
 {
int name;
if (Request.QueryString["name"] == null)
 {
name = 1;
 }
 else {
name = Convert.ToInt32(Request.QueryString["name"]) + 1; }
Response.Redirect("varybyqs.aspx?name=" + name.ToString());
}

Partial Page Caching

Also, if we have a requirement for partial page caching (Fragment caching), we can simply use the controls that require caching and put them in a user control. We can then use the same above mentioned technique to enable caching behavior in the custom control and we have partial page caching in place.  
Application Caching
Application data caching is a mechanism for storing the Data objects on cache. It has nothing to do with the page caching. ASP.NET allows us to store the object in a Key-Value based cache. We can use this to store the data that need to cached. Let us work on the same example and try to store the DateTime object in the Application Cache now.

protected void Page_Load(object sender, EventArgs e)
 {
 if (IsPostBack == false)
 {
Cache["time"] = DateTime.Now;
}
Label1.Text = ((DateTime)Cache["time"]).ToLongTimeString();
}
What this code will do is that it will store the DateTime object in application cache and will keep displaying the time of initial request by using the object from the cache.

Points to remember:
•The ASP.NET provides caching features so that the developers can use them instead of writing all the caching logic manually.
•Page output caching should be used when we want to cache complete web pages based on some criteria and for some specific time.
•Application caching should be used when we want to cache custom objects to use later.
•Application caching provides a lot of parameters to customize its behavior to have more fine grained control.



0 comments:

Post a Comment