Caching in ASP.net

The main benefits of caching are performance-related: operations like accessing database information can be one of the most expensive operations of an ASP page’s life cycle. If the database information is fairly static, this database-information can be cached.

Caching helps us to achieve three important aspects of QoS (Quality of Service):

  1. Performance – Caching improves application performance by minimizing data retrieval and formatting operations.
  2. Scalability – Since caching minimizes data retrieval and formatting operations, it reduces the load on server resources thus increasing the scalability of the application.
  3. Availability – Since the application uses data from a cache, the application will survive failures in other systems and databases.

Caching Options in ASP.NET

ASP.NET supports three types of caching for Web-based applications:

  1. Page Level Caching (called Output Caching)
  2. Page Fragment Caching (often called Partial-Page Output Caching)
  3. Programmatic or Data Caching

Output Caching

Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages. The way ASP.NET implements this (roughly) is through an Output Cache engine. Each time an incoming ASP.NET page request comes in, this engine checks to see if the page being requested has a cached output entry. If it does, this cached HTML is sent as a response; otherwise, the page is dynamically rendered, its output is stored in the Output Cache engine.

Output caching is easy to implement. By simply using the @OuputCache page directive, ASP.NET Web pages can take advantage of this powerful technique. The syntax looks like this:

Partial-Page Output Caching

Partial-Page Output Caching, or page fragment caching, allows specific regions of pages to be cached. ASP.NET provides a way to take advantage of this powerful technique, requiring that the part(s) of the page you wish to have cached appear in a User Control. One way to specify that the contents of a User Control should be cached is to supply an OutputCache directive at the top of the User Control. That’s it! The content inside the User Control will now be cached for the specified period, while the ASP.NET Web page that contains the User Control will continue to serve dynamic content.

Data Caching

Programmatic or data caching takes advantage of the .NET Runtime cache engine to store any data or object between responses. That is, you can store objects into a cache

To store a value in the cache, use syntax like this:

Cache[“myblogpage”] = bar; or Cache.Insert(“myblogpage”, bar);

To retrieve a value, simply reverse the syntax like this:

sampleVar = Cache[“myblogpage”];

References

Caching explained here:

http://aspnet.4guysfromrolla.com/articles/022802-1.aspx

Caching including other state managements explained here:

http://www.codeproject.com/KB/web-cache/cachemanagementinaspnet.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s