The ability to store data in the main memory and then allow for retrieval of the same as and when they are requested.

Caching is a technique of persisting the data in memory for immediate access to requesting program calls.

 

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

 

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.

 

In a typical ASP.NET page, every time the user views the page, the Web server will have to dynamically generate the content of the page and perform the relevant database queries (which is a very expensive task to do).

 

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.

 

In Page Output Caching, the entire page is cached in memory so all the subsequent requests for the same page are addressed from the cache itself.

 

In Page Fragment Caching, a specific a portion of the page is cached and not the entire page. Page Output or Fragment Caching can be enabled or disabled at the Page, Application or even the Machine levels.

 

Data Caching allows us to cache frequently used data and then retrieve the same data from the cache as and when it is needed. We can also set dependencies so that the data in the cache gets refreshed whenever there is a change in the external data store. The external data store can be a file or even a database. Accordingly, there are two types to dependencies, namely, file based and Sql Server based. There are also differing cache expiration policies

 

Syntax: <%@ OutputCache Duration="60" VaryByParam="none" %>

 

The above syntax specifies that the page be cached for duration of 60 seconds and the value "none" for VaryByParam* attribute makes sure that there is a single cached page available for this duration specified.

 

* VaryByParam can take various "key" parameter names in query string. Also there are other attributes like VaryByHeader, VaryByCustom etc.

 

 

The following is the complete syntax of page output caching directive in ASP.NET.

 

<%@ OutputCache Duration="no of seconds"

Location="Any | Client | Downstream | Server | None"

VaryByControl="control"

VaryByCustom="browser |customstring"

VaryByHeader="headers"

VaryByParam="parameter" %>

 

 

To store the output cache for a specified duration

 

Declarative Approach:

 

<%@ OutputCache Duration="60" VaryByParam="None" %>

 

Programmatic Approach:

 

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

Response.Cache.SetCacheability(HttpCacheability.Public);

 

To store the output cache on the browser client where the request originated

 

Declarative Approach:

 

<%@ OutputCache Duration="60" Location="Client" VaryByParam="None" %>

 

Programmatic Approach:

 

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

Response.Cache.SetCacheability(HttpCacheability.Private);

 

To store the output cache on any HTTP 1.1 cache-capable devices including the proxy servers and the client that made request

 

Declarative Approach:

 

<%@ OutputCache Duration="60" Location="Downstream" VaryByParam="None" %>

 

Programmatic Approach:

 

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

Response.Cache.SetCacheability(HttpCacheability.Public);

Response.Cache.SetNoServerCaching();

 

To store the output cache on the Web server

 

Declarative Approach:

 

<%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %>

 

Programmatic Approach:

 

TimeSpan freshness = new TimeSpan(0,0,0,60);

DateTime now = DateTime.Now;

Response.Cache.SetExpires(now.Add(freshness));

Response.Cache.SetMaxAge(freshness);

Response.Cache.SetCacheability(HttpCacheability.Server);

Response.Cache.SetValidUntilExpires(true);

 

To cache the output for each HTTP request that arrives with a different City:

 

Declarative Approach:

 

<%@ OutputCache duration="60" varybyparam="City" %>

 

Programmatic Approach:

 

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

Response.Cache.SetCacheability(HttpCacheability.Public);

Response.Cache.VaryByParams["City"] = true;

 

For the VaryByCustom attribute, the VaryByHeader attribute, and the VaryByParam attribute in the @ OutputCache directive, the HttpCachePolicy class provides the VaryByHeaders property and the VaryByParams

property, and the SetVaryByCustom method.

 

1. Page Output Caching

 

In Page Output Caching, the entire page is cached in memory so all the subsequent requests for the same page are addressed from the cache itself.

 

Partial-Page Output Caching/ Page Fragment Caching

 

In Page Fragment Caching, a specific a portion of the page is cached and not the entire page. Page Output or Fragment Caching can be enabled or disabled at the Page, Application or even the Machine levels.

 

More often than not, it is impractical to cache entire pages. For example, you may have some content on your page that is fairly static, such as a listing of current inventory, but you may have other information, such as the user's shopping cart, or the current stock price of the company, that you wish to not be cached at all. Since Output Caching caches the HTML of the entire ASP.NET Web page, clearly Output Caching cannot be used for these scenarios: enter Partial-Page Output Caching.

 

Partial-Page Output Caching, or page fragment caching, allows specific regions of pages to be cached.

 

fragment caching comes from the attribute "VaryByControl". Using this attribute one can cache a user control based on the properties exposed.

 

Syntax: <%@ OutputCache Duration="60" VaryByControl="DepartmentId" %>

 

The above syntax when declared within an *.ascx file ensures that the control is cached for 60 seconds and the number of representations of cached control is dependant on the property "DepartmentId" declared in the control.

 

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, similar to the storing of objects in Application scope in classic ASP.

 

Realize that this data cache is kept in memory and "lives" as long as the host application does. In other words, when the ASP.NET application using data caching is restarted, the cache is destroyed and recreated. Data Caching is almost as easy to use as Output Caching or Fragment caching: you simply interact with it as you would any simple dictionary object.

 

Note that the Insert method allows you to simply add items to the cache using a key and value notation as well. For example to simply add an instance of the object bar to the cache named foo, use syntax like this:

 

Cache.Insert("foo", bar); // C#

Cache.Insert("foo", bar) ' VB.NET