Effectively use the Cache object in ASP.NET designs

ASP.NET's Cache object allows developers to easily implement advanced caching. See how you can control its dependencies and expiration and apply priorities to items in the cache.

By Tim Landgrave

An effective caching strategy is a key element of any application design. Effective caching increases performance by returning data more quickly to a larger number of users. It also increases scalability by limiting the number of calls required to back-end systems by providing data from a local source instead of requiring a call to the back end. Beyond the Application and Session objects, classic ASP provided little support for caching mechanisms. ASP.NET, however, provides advanced caching capabilities in the form of a programmable Cache object.

Implementing the Cache object
Your program can access the Cache object using the System.Web.Caching namespace. You can set a reference to the Cache object using the Cache property of the HttpContext class in the System.Web namespace or the Cache property of the Page object. The Cache object allows you to store key-value pairs that become accessible by any user or page in the application domain. For example, you can store a list of state abbreviation and state names in a Dataset called ShortState and then store that Dataset object in the Cache for use by any page that needs to allow state selection or display by abbreviation. Loading the page from the cache eliminates the need to regenerate the database programmatically or to load it from a back-end database.

Cache object memory management
The inability to automatically manage cached objects is one of the most significant limitations of the classic ASP-caching mechanisms. Developers using the Application or Session objects had to write code to manage the creation and destruction of the data they managed. But the ASP.NET Cache object adds dependency and expiration policies to make cache management automatic. Whenever you add an item to the ASP.NET cache, you have the option of telling the common language runtime (CLR) when to remove the item automatically. This automatic removal depends on certain conditions and is therefore called a cache dependency. When an item is to be removed from the cache, it’s said to be invalidated. Invalidating a cached item tells the CLR that it should remove the item from the cache. Built in ASP.NET cache dependencies include file, key, and time-based features. Let’s look at each of these in more detail.

File dependency
In many cases, you may want a cached item to be invalidated when a disk-based file changes. For example, if you use data generated or uploaded by an external system, you need to refresh the cached data whenever that external system sends new data. Suppose that your ASP.NET application relies on a customer list supplied by a mainframe and that the mainframe sends new data in a file named Customers.xml to an FTP site whenever the customer list changes. The code fragment shown in Listing A uses a custom method called BuildDSFromXml to load the file from the FTP site and the CacheDependency object to introduce a file dependency into the cache

Key dependency
In many cases, you may want cached items to depend on or relate to one another. If your application caches calculated values based on the values in other cached keys, you would want the calculated values to be invalidated whenever the values upon which they were based have been changed. The code fragment shown in Listing B demonstrates how to introduce a key dependency between cached objects.

Time expiration
In many cases, the values in the cache don’t depend on other circumstances but instead depend on the amount of time that has passed since they were placed in the cache. When determining the amount of time that an item should remain in the cache before it’s invalidated, you can either specify an exact time at which it should be invalidated or the amount of time that it can stay in the cache without being accessed.

You can use the first method to ensure that cached items are refreshed on a regular schedule. For example, if you want data to be cached each morning that reflects activity from the prior day, you can programmatically set the cache to invalidate at 8 A.M. on the following morning, thus forcing your system to use and cache new data after that time. Using the second method—commonly called the sliding window—you can ensure that items are kept in the cache only when being used regularly. For example, using the command shown in Listing C, you can cache a list of hot selling items in a commerce site and specify that the list of items be regenerated if the list hasn’t been accessed from the cache in the last 30 seconds.

Effective caching helps manage resources
Effective use of the caching capabilities in ASP.NET will allow you to balance the use of precious resources like machine memory and database connections with the need to generate client pages quickly. The ASP.NET Cache object adds another tool to the architect’s arsenal that you can use when designing caching strategies for Web-based systems.


Editor's Picks