Export (0) Print
Expand All
5 out of 6 rated this helpful - Rate this topic

Caching Application Data

ASP.NET has a powerful, easy-to-use caching mechanism that allows you to store objects in memory that require extensive server resources to create. Caching these types of resources can significantly improve the performance of your application.

Caching is implemented by the Cache class, with cache instances private to each application. The cache 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. You can place items in the Cache and later retrieve them using simple key/value pairs. For examples of how to do this, see How to: Add Items to the Cache and How to: Retrieve Values of Cached Items.

The Cache class 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 low-priority items to free memory. This technique is referred to as scavenging, and is one of the ways that the cache ensures that out-of-date data does not consume valuable server resources.

You can instruct the Cache object to give certain items priority over other items when it performs scavenging. To indicate an item's importance, you can specify one of the CacheItemPriority enumeration values when you add an item using the Add or Insert methods.

You can also establish an expiration policy for an item when you add it to the cache using the Add or Insert methods. You can define the lifetime for an item by specifying an exact time the item will expire (an absolute expiration) using a DateTime value. Alternatively, you can specify a sliding expiration using a TimeSpan value, which allows you to specify the elapsed time before the item expires based on the time it was last accessed. Once an item expires, it is removed from the cache. Attempts to retrieve its value will return null (Nothing in Visual Basic) 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, you typically 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 another 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 other 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 read the score from the source Web site and cache the new value.

Finally, ASP.NET allows you to define the validity of a cached item based on an external file or directory (a file dependency) or on another cached item (a key dependency). If the item with the associated 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, 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 XML file and puts the refreshed data into the cache.

NoteNote

The Cache object 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 How to: Add Items to the Cache.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.