Types Of Caching in asp.net

Posted: December 20, 2010 in Asp.net

Types of Caching

Caching in ASP.NET can be of the following types

  1. Page Output Caching
  2. Page Fragment Caching
  3. Data Caching


Page Output Caching

This is a concept by virtue of which the output of pages is cached using an Output Cache engine and all subsequent requests are served from the cache. Whenever a new request comes, this engine would check if there is a corresponding cache entry for this page. If there is a cache hit, i.e., if the engine finds a corresponding cache entry for this page, the page is rendered from the cache, else, the page being requested is rendered dynamically.

This is particularly useful for pages that are static and thus do not change for a considerable period of time.

Page output caching can be implemented in either of the following two ways:

· At design time using the OutputCache directive

· At runtime using the HttpPolicy class

The following is the complete syntax of page output caching directive

<%@ OutputCache Duration="no of seconds"
Location="Any | Client | Downstream | Server | None"
VaryByControl="control"
VaryByCustom="browser |customstring"
VaryByHeader="headers"
VaryByParam="parameter" %>The following statement is used to implement output caching in an aspx page at design time. The directive is placed at the top of the .aspx page.

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


Page Fragment Caching

This allows specific portions of the page to be cached rather than caching the whole page. This is useful in situations where we can have a page that contains both static and dynamic content. The following code depicts how this can be accomplished.

<%@ OutputCache Duration="15"
VaryByControl="EmpID;DeptID" VaryByParam="*"%> This directive is placed at the top of any User Control (.axcx file).



Data Caching

The following code is an implementation of On-Demand caching. The method GetUserInfo checks to see if the data exists in the cache. If it is present, it is returned from the cache, else, the GetUserInfoFromDatabase method fills the DataSet from the database and then populates the Cache.

public DataSet GetUserInfo()
{
string cacheKey = "UserInfo";
DataSet ds = Cache[cacheKey] as DataSet;
if (ds == null)
{
ds = GetUserInfoFromDatabase();
Cache.Insert(cacheKey, ds, null, NoAbsoluteExpiration,
TimeSpan.FromHours(15),CacheItemPriority.High, null);
}
return ds;
}

DataSet GetUserInfoFromDatabase() {
// Usual code to populate a data set from thedatabase. This data set
// object is then returned.
}

Advertisements
Comments
  1. MartinTeew says:

    Obtain and select some good points from you and it helps me to solve a problem, thanks.

    – Henry

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