Caching in ASP.NET (Part I)

 

Declarative Approach

 
The declarative specification here implies that Page Output Caching is specified at the design time by adding the OutputCache directive in the web page source.
The following is the complete syntax of page output caching directive in ASP.NET.
<%@ OutputCache Duration=”no of seconds”
Location=”Any | Client | Downstream | Server | None”
VaryByControl=”control”
VaryByCustom=”browser |customstring”
VaryByHeader=”headers”
VaryByParam=”parameter” %>
We can specify output caching in an aspx page at design time using the OutputCache directive as shown below.
<%@OutputCache Duration=”25″ VaryByParam=”none” %>
The VaryByHeader and VaryByCustom parameters of the OutputCache directive are used to enable customization of the page’s look or content based on the client that is accessing them. The cache duration is a mandatory parameter and specifies how long (in seconds) the web page remains in the cache memory. The VaryByParam parameter is optional and is used to cache different views of the page, i.e., whether the cached page would have different versions based on a specific parameter. A value of * in the same parameter indicates that the page would be cached based on all the Get/Post parameters. We can also specify one or more Get/Post parameter(s). The VaryByParam parameter is particularly useful in situations where we require caching a page based on some pre-defined criteria. As an example, we might require to cache a specific page based on the PatientID. The OutputCache directive needs to be specified as shown below.
<%@OutputCache Duration=”10″ VaryByParam=”PatientID” %>
The VaryByParam parameter can also have multiple parameters as shown in the example below.
<%@OutputCache Duration=”20″ VaryByParam=”PatientID;ProviderID” %>
The location parameter in the OutputCache directive is used to specify the cache location, i.e., from where the client received the cached page. This should be one of the possible four OutputCacheLocation enumeration values, namely, Any, Client, Downstream, None, Server. An example is given below.
<%@OutputCache Duration=”10″ VaryByParam=”*” Location = “Any”%>
 

Programmatic Approach

 
Page Output Caching can also be specified programmatically using the Response.Cache.SetCacheability method. The parameters to this method can be “NoCache”, “Private”, “Public” or “Server”. Refer to the code example below that illustrates how to set a page’s cache ability programmatically:
Response.Cache.SetCacheability (HttpCacheability.Server);
It is also possible to set the OutputCache on all the pages in an ASP.NET application programmatically in the Global.asax page. Refer to the code snippet that follows:–
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server);
}
The source code given in the above code snippet sets the cache ability programmatically in the BeginRequest event of the application’s global class.

Continues…

Leave a comment

Your email address will not be published.