.netCoders Contact Us
Search:

Caching

Caching refers to the storing of frequently accessed data in memory to increase performance. For web applications, caching could be used to store html pages or database information for lookup tables. In ASP.NET, there are three types of caching:
  1. Output
  2. Fragment
  3. Data
The .NET Framework doesn't just give you the ability to indicate what to and what not to cache. You also can indicate expiration time on a cached item, as well as dependencies on files or other cached items, so that if those items change, the cached item will be recreated.

Output Caching

Output Caching stores the responses from an asp.net page (aspx) or ascx user control (.ascx). This type of caching is also referred to as Page caching. To enable Output Caching, use the OutputCache directive:
<%@ OutputCache Duration="60" VaryByParam="None" %>
See the section below on Caching Directives for more information about this directive.

Fragment Caching

Unlike Output Caching, Fragment Caching only stores a portion of a page. You may have a personalized page where Output Caching would not be suitable, but perhaps you have functionality that runs for a group of users that would be a candidate for fragment caching.

To enabale Fragment Caching, you must put the portion of the page that you want cached in a User Control, and then set a directive to cache that control. The directive is similiar to that used in output caching, with one additional attribute, VaryByControl. This allows you to vary the cached fragment by a control's value inside of it.

<%@ OutputCache Duration="120" VaryByParam="none" VaryByControl="Category" %>

Data Caching

Data Caching is a programmatic way for you to add your own objects to a managed cache. For instance, you may want to store a dataset containing highlighted articles for your homepage.

Cache Directives

To enable Output caching, add the <%@OutputCache%> directive to the top of your page or control. The syntax for this directive is as follows:
<%@ OutputCache Duration="60" VaryByParam="None" %>
Duration indicates, in number of seconds, the length the page should be cached. The VaryByParam attribute is used to indicate whether pages should be differentiated by a querystring or form variable. In an ecommerce site, one page may be used to display all products, with the product ID passed as a parameter. In such a case, the VaryByParam attribute might be set to "ProductID". Multiple parameters can be used by separated their names with semicolons.

Cache settings for a page can also be set programmatically using an System.Web.HttpCachePolicy object accessible via the Response.Cache property. Our directive example could have been written programmatically as:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public)

Cache Object

The Cache object (an instance of System.Web.Caching.Cache) is used to programmatically add and retrieve items from the cache. It has a dictionary interface whereby objects are referenced by a string key. This object has a lifetime tied to the application. When the application is restarted, the cache is recreated as well. The following code snippet shows how to add and retrieve an item from the cache:
//Add item
Cache["TopProducts"] = objTopProductsDataset;

//Retrieve item
objDataset = Cache["TopProducts"];

To more parameters of the cached object, such as the expiration and dependency information, use the Add or Insert method. Full details about the Cache class can be found in the .NET Framework documentation:

Additional Resources

  • Cache Class