Caching in ASP.NET (Part II)


File Based Expiration

This is implemented by using a file as a cache dependency. Whenever the contents of the dependent file are changed, the cached is invalidated. As shown in the code snippet below.
Cache.Insert(“Country”, obj, new CacheDependancy(Server.MapPath(“CountryList.xml”)));
Cache.Insert(“Country”, xmldocumentObject,cacheDependencyObj);

Key Based Expiration

The third type of dependency is the key dependency.  This implies that you can map a cache entry to an existing dependency.  Now, when the depended-upon entry changes or expires, the dependent entry will also be expired.  You can have an array of keys that can be specified as a single CacheDependency. Refer to the code snippet below:
string[] keys = new string[] {“key”};
CacheDependency cacheDependencyObj = new CacheDependency(null,keys);
Cache.Insert(“Country”, xmldocumentObject,cacheDependencyObj);


Library Caching Application Block

The Patterns and Practices Group of Microsoft’ came up with the reusable, configurable and extensible Enterprise Library Caching Block that enables you to incorporate local cache capabilities in your applications. Microsoft recommends usage of this block when the volume of relatively static data transport is high in your application and performance is a major concern.
The Enterprise Library Caching Application Block facilitates storage of cached data in the memory, isolated storage (i.e., an XML file) or in a database. The Enterprise Library Caching Application Block encapsulates efficient, consistent caching strategies and, hence, simplifies implementation of caching strategies in enterprise application development.
The salient features of the Caching Application Block include:
Support for efficient caching strategies in web applications
Thread Safety


Caching is a great tool that can be used to boost the performance of your web applications. Caching in ASP.NET is an effective and powerful way of boosting application performance while minimizing the usage of precious server resources. However, choosing the appropriate type of caching and caching the right type of data goes a long way in designing and developing high performing applications. The fourth and final article in this series will discuss the best practices for Caching in ASP.NET and demonstrates a sample application that will present all the concepts that we have learnt so far in this series.]]>

Leave a comment

Your email address will not be published.