ASP.NET Caching Tutorial – Part 2

Continuing from ASP.NET Caching Part 1

Caching is an essential feature of ASP.NET that reduces latency and the network traffic by storing frequently used data and pages in the Cache for quick retrieval later; hence greatly boosting the application’s performance. We have had a detailed look at the concepts of Caching in the first part of this series of articles on Caching in ASP.NET. This article presents Data Caching, a feature of ASP.NET that allows you to store your data in the cache memory for faster retrieval. It also discusses the Cache API and the Cache expiration strategies. As in the earlier articles in this series, it also throws light on the related newly added features in ASP.NET 2.0 Caching.

Data Caching using the Cache API

Data Caching is a feature that enables us to store frequently used data in the Cache. The Cache API was introduced in ASP.NET 1.x and was quite exhaustive and powerful. However, it did not allow you to invalidate an item in the Cache based on a change of data in a Sql Server database table. With ASP.NET 2.0, the Cache API provides you a database triggered Cache invalidation technique that enables you to invalidate the data in the Cache based on any change of data in the Sql Server database table. Besides this, you can also create custom cache dependencies.

The Cache class contains a numerous properties and methods. Of these, the Add, Insert, Remove methods and the Count property are the most frequently used. The Add/Insert method of the Cache class is used to add/insert an item into the cache. The Remove method removes a specified item from the cache. The Count property returns the current number of objects in the Cache.

The Cache property of the Page.HttpContext class can be used to store and retrieve data in the cache. The following code snippet illustrates the simplest way of storage and retrieval of data to and from the cache using the Cache class.

//Storing data

Cache ["key"] = objValue;

//Retrieving the data

object  obj = Cache ["key"];

We can also check for the existence of the data in the cache prior to retrieving the same. This is shown in the code snippet below:

object obj = null;
 if(Cache["key"] != null)

obj = Cache["key"];

The following code snippet illustrates how we can make use of the Cache API to store and retrieve data from the Cache. The method GetCountryList checks to see if the data (list of countries) exists in the cache. If so, it is retrieved from the cache; else, the GetCountryListFromDatabase method fills the DataSet from the database and then populates the Cache.

public DataSet GetCountryList()

{
string key = "Country";
DataSet ds = Cache[key] as DataSet;

if (ds == null)
{
ds = GetCountryListFromDatabase ();
Cache.Insert(cacheKey, ds, null, NoAbsoluteExpiration,
TimeSpan.FromHours(5),CacheItemPriority.High, null);
}

else
{
return ds;
}
} //End of method GetCountryList

public DataSet GetCountryListFromDatabase ()

{
// Necessary code to retrieve the list of countries from the database and
// store the same in a DataSet instance, which in turn is returned to the
// GetCountryList method.

}

Cache Dependency and Cache Expiration Strategies

Cache dependency implies a logical dependency between the data in the Cache and physical data storage.
Continues…

One thought on “ASP.NET Caching Tutorial – Part 2

  1. Pingback: ASP.NET Caching Tutorial – Part 1 | ASP.NET 101

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>