Caching In ASP.NET

In this article, you will learn about caching in ASP.NET.


Caching can be used to temporarily store page output or application data either on the client or on the server, which can then be re-used to satisfy subsequent requests and thus avoid the overhead of re-creating the same information.
Caching is particularly suitable when you expect to return the same information in the same format for many different requests.
ASP.NET provides the following types of caching that can be used to build highly responsive Web applications,
  • Output caching, which caches the dynamic response generated by a request.
  • Partial Page caching, which caches portions of a response generated by a request.
  • Data caching, which allows developers to programmatically retain arbitrary data across requests.
Output Caching
Output caching caches the output of a page (or portions of it) so that a page's content need not be generated every time it is loaded.
Considering the fact that the page does not change for a certain period of time, it is always a good idea to cache whatever is non-static so that the page can be loaded quickly.
Add a new OutputCache page directive to the ASP.NET page,
<%@OutputCache Duration="10" VaryByParam=“None" %>

Caching Application Data

ASP.NET includes a full-featured cache engine that can be used by an application to store arbitrary objects in memory, which can then be retrieved by the same or any other page that is part of the application.
This caching mechanism is implemented via the Cache class, an instance of which exists for each application configured on a Web server.
The ASP.NET cache is private to each Web application, and its lifetime depends on the start and end of any given application. This means that only pages within an application domain can have access to its cache.
When the application starts, an instance of the application's Cache object is created. When the application ends, its cache is cleared.
The application cache provides a simple dictionary interface using key=value pairs that allow programmers to easily store and retrieve objects from the cache.
In the simplest case, placing an item in the cache is just like adding an item to a dictionary,
Cache ( "mykey" ) = myValue
Retrieving the data is just as simple,
myValue = Cache ( "mykey" )
If myValue <> Null Then
DisplayData ( myValue )
End If
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,
  1. void Application_BeginRequest(object sender, EventArgs e)  
  2. {  
  3.    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server);  
  4. }   
The source code given in the above code snippet sets the cache ability programmatically in the BeginRequest event of the application’s global class.

Partial Page Caching

Also known as Fragement Caching, this is a feature that allows specific portions of the web page to be cached rather than caching the entire web page and is useful in situations where a particular web page can contain both static and dynamic content.
The term “fragment” implies one or more user controls in the web page.
Note that each of these user controls can have different cache durations.