Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Best practices in caching in ASP.Net

Joydip Kanjilal | Aug. 28, 2015
Caching enables the web page to be rendered faster, and proper use of caching minimizes or reduces database hits or consumption of server's resources.


Caching is a state management strategy often adopted in ASP.Net to improve the application's performance by minimizing the consumption of resources in your system. If used properly, it can improve the performance of your application considerably by storing the wWb page in its entirety or partially, or even store the application's data across the HTTP requests. Caching enables the Web page to be rendered faster, and proper use of caching minimizes or reduces database hits or consumption of server's resources.

Caching in ASP.Net is of the following three types:

  1. page output caching
  2. page fragment caching
  3. data caching

Page output caching

This is a form of caching in ASP.Net that stores a copy of your Web page in the memory cache so that subsequent requests for the same Web page can be fetched directly from the cache -- the cached output is sent to the application. This improves the application's performance considerably. The following code snippet shows how you can implement page output caching.

<%@ OutputCache Duration="30" VaryByParam="*" %>

The VaryByParam option helps you to specify the variables in the Http Request that would need a new cache entry. Other possible options include: VaryByHeader and VaryByCustom. You can also specify Location and Duration in the OutputCache directive -- you can use these to specify the location of the cache and also the duration for which the Web page should be cached respectively.

Page fragment caching

Page fragment caching is a caching strategy in which the Web page is cached partially – only fragments of the Web page are cached, not the entire Web page. You can use the same syntax as page output caching. However, you need to apply the OutputCache attribute to a user control instead of the Web page. Fragment caching is helpful when you would need to cache only portions of your Web page -- typically in situations where your Web page contains a mix of common and dynamic sections. As an example, you can have a Web page that contains a mix of menu items and also certain dynamic sections that need to be populated and updated from the database often.

Data caching

ASP.Net exposes the Cache API for you to store data in the cache for retrieval later. The syntax for storing data in the Cache using the Cache API is given below.

Cache["key"] = "value";

You can also use the Add or the Insert methods. To remote an entry from the cache, you can use the Remove() method of the Cache class. The Insert() method of the Cache class enables you to specify cache dependency. Cache dependency is a strategy that ensures that when the data in the data store (from which the cache has been populated) changes, the cache would then be re-populated immediately. When data in the data store changes, the cache would expire, resulting in a re-populating of the cache with the latest data. You can read more on this from this MSDN article.


1  2  Next Page 

Sign up for CIO Asia eNewsletters.