Caching Application Data

ASP.NET has a powerful, easy-to-use caching mechanism that allows you to store objects that require a large amount of server resources to create in memory. It is implemented by the Cache class, with instances private to each application, and its lifetime is tied to that of the application. When the application is restarted, the Cache object is recreated.

The Cache class has been designed for ease of use. By using keys paired with values, you can place items in the Cache and later retrieve them. For examples of how to do this, see Adding Items to the Cache and Retrieving Values of Cached Items.

While the Cache class offers a simple interface for you to customize cache settings, it also offers powerful features that allow you to customize how items are cached and how long they are cached. For example, when system memory becomes scarce, the cache automatically removes seldom used or unimportant items to allow memory to be used to process a high volume of requests. This technique is called scavenging. It is one of the ways that the cache ensures that data that is not current does not consume valuable server resources.

You can instruct the Cache to give certain items priority over other items when it performs scavenging. To indicate that a specific item is of greater or lesser importance than another, specify one of the CacheItemPriority enumeration values when you add an item using the Cache.Add method or Cache.Insert method.

You can also establish an expiration policy for an item when you add it to the Cache using the Add method or Insert method. You can define the lifetime for an item by using the absoluteExpiration parameter, which is of the type DateTime and allows you to specify the exact time the item will expire. You can also use the slidingExpiration parameter, which is of the type TimeSpan. It allows you to specify the elapsed time before the item expires based on the time it is accessed. Once the item expires, it is removed from the cache. Attempts to retrieve its value will return null unless the item is added to the Cache again.

For volatile items that are stored in the Cache, such as those that have regular data refreshes, or those that are valid for only a set amount of time, set an expiration policy that keeps those items in the Cache as long as their data remains current. For example, if you are writing an application that tracks sports scores by obtaining the data from a frequently updated Web site, you can cache the scores for a game as long as those scores do not change on the source Web site. In this case, you can set an expiration policy that is based on how often the Web site updates the scores. You can write code that determines if an up-to-date score is in the Cache. If the score is not up to date, the code can update the score from the source Web site.

Finally, ASP.NET allows you to define the validity of a cached item, based on an external file, a directory, or another cached item. These are called file dependencies and key dependencies. If a dependency changes, the cached item is invalidated and removed from the Cache. You can use this technique to remove items from the Cache when their data source changes. For example, if you write an application that processes financial data from an XML file and renders it in a graph, you can insert the data from the file in the Cache and maintain a dependency on that XML file. When the file is updated, the item is removed from the cache, your application rereads the file, and a new version of the item is inserted.

Note   The Cache has no information about the content of the items it contains. It merely holds a reference to those objects. It also provides methods to track their dependencies and set expiration policies.

For more information on how to use these features, see Adding Items to the Cache.

See Also

ASP.NET Caching Features | ASP.NET Optimization | System.Web.Caching Namespace | Cache Class | CacheDependency Class