ASP.NET Caching Tutorial – Part 1

The ability to store data in the main memory and then allow for retrieval of the same as and when they are requested is one of the major factors that can yield high performance and scalable applications. This results in a gain in the application’s overall throughput by serving the subsequent requests for data from the Cache rather than recreating them or retrieving them from the database or any other storage device that works much slower compared to the main memory. This is one of the most striking features in Microsoft’s ASP.NET compared to its earlier counterparts and has been enhanced further with the introduction of the new release of ASP.NET (ASP.NET 2.0). This is the first in the series of articles on Caching in ASP.NET and discusses Caching, its types, benefits and the new features of Caching in ASP.NET 2.0.

Caching types, Cache Dependencies and Cache Expirations

ASP.NET features several different types of caching namely  Page Output Caching, Page Fragment Caching and Data 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. 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.

New Caching features in ASP.NET 2.0

ASP.NET 2.0 has introduced a lot of new features in Caching. This new version of ASP.NET includes an excellent feature called automatic database server cache invalidation which is in turn based on database triggered cache invalidation technique. This powerful and easy-to-use feature enables us to output cache database-driven page and partial page content within a site and then leave it to ASP.NET invalidate these cache entries automatically and refresh the content whenever the there are any changes in the database. This ensures that the data in the Cache is in sync with that of the database. Note that the Cache API in ASP.NET 1.x does not allow you to invalidate an item in the cache when the data in the database changes. It is possible to invalidate cached data based on pre-defined conditions that relate to any change in the data in an external data store on which the dependency has been set. The data gets deleted from the Cache with any change in the data in the external data storage. However, the cached data cannot be refreshed directly.

ASP.NET 2.0 also features numerous enhancements to the Cache API. The CacheDependency class of ASP.NET 1.x was sealed to prevent further inheritance.
Thus preventing extension of this class to provide custom cache dependency features. ASP.NET 2.0 Cache API provides the ability to create custom cache dependency classes that are inherited from the CacheDependency class. I would discuss all these issues in details in the subsequent articles in this series of articles on Caching in ASP.NET applications.

Page Caching in ASP.NET

There are two types of Page caching, namely, Page Output Caching and Page Fragment Caching. Page Fragment Caching is also known Partial Page Caching.

Page Output Caching

Page Output Caching is the most basic form of caching. The output caching simply maintains a copy of the HTML which was sent in response to a memory request. Subsequent requests are then given the cached output until the cache expires.
Continues…

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…