Mobile Content

Saturday, December 24, 2011

Caching in asp.net - Output Caching

Caching:

Caching is the process of storing frequently used data, usually data that is costly to generate, for reuse. Typically this data is stored in memory since retrieving data from memory is much more efficient than retrieving the data from other locations, such as a database.

Caching enables you to store the expensive data into Cache object and later retrieve it without doing expensive operations. A very common example where you want to use caching is datagrid paging. I am sure you all are familiar with datagrid paging which enables you to view the records in multiple pages. Each time you visit a different page all the records are fetched from the database. This becomes very expensive operation. Caching can save a lot of expensive operations since you can store all the records in the cache object and use the cache object as the data source. In this article we will see some important features that caching provides.

There are 3 Caching Type

a. Output Caching

b. Data Caching

c. Fragment Caching

Output Caching:

The rendered html page is stored into the cache before sending it to the client. Now, if the same page is requested by some other client the already rendered htm page is retrieved from the server memory and sent to the client, which saves the time requires rendering and processing the complete page.

.Declaration:To use output caching, declare the below line at the page:

<@ OutputCache Duration="20" VaryByParam="None">

It means that the page will be cached for the 20 seconds. If any client requests for the same page under this time, the rendered page is sent to it. After 20 seconds the updated/latest version of rendered page replaces the earlier version.

Note: The page is automatically removed when the application is recompiled.

2.Output Caching and Query String:Output caching also supports the query string values to cache the specific pages. On basis of query string parameter values, different pages are cached. For example, in the below declaration:

<@ OutputCache Duration="20" VaryByParam="ProductID">

Different version of page is cached based on different ProductId value. And later when the page is requested with specific query string value (ProductId value), the matching page is retrieved.

It is also possible to specify more than one query string parameter. In those cases the combined value of both the parameter work as a key for pages to be cached. There is also a special case, where "*" is passed as a parameter. In this case, pages are cached for all the separate combinations of the query string arguments.

In the sample project, I am passing a different “ID” in the query string. ASP.NET stores a separate copy for each “ID” and this technique is good in this scenario. But this technique has some problems, if page is accepting wide range of different query strings. In this case, ASP.NET caches separate a copy of page according to query string parameters and potentially lowers the reusability. In this case, you can specify important query string variable names in VarByParam attribute.

User has defined a custom string that will be used to generate the custom cache string. In this case, user wants to cache pages based on browser version. It requires the below method implementation under global.asax page (or should have the below structure):

Public override function GetVaryByCustomString(byval context as HttpContext, byval arg as string) as string