ASP.net provides you with a powerful, easy-to-use caching mechanism for storing objects that require a large amount of server resources to be created in memory. Caching these types of resources can greatly improve the performance of your application.
Caching is implemented by the cache class, and the cache instance is specific to each application. The cache lifetime relies on the lifetime of the application, and after the application restarts, the cache object is recreated.
The Cache class is designed for ease of use. You can place items in the Cache and retrieve them later using a simple key/value pair.
The cache class provides powerful features that allow you to customize how items are cached and how long they are cached. For example, when system memory is scarce, caching automatically removes less-used or lower-priority items to free memory. This technology is also known as cleanup, which is one of the ways caching ensures that outdated data does not use valuable server resources.
When scavenging is performed, you can instruct the Cache to give some items a higher priority than others. To indicate the importance of an item, you can specify a CacheItemPriority enumeration value when you add an item using the Add or Insert method.
When you add an item to the cache using the Add or Insert method, you can also establish an expiration policy for the item. You can define the lifetime of an item by using a DateTime value to specify the exact expiration time of the item (absolute expiration). You can also specify a flex expiration using the TimeSpan value, which allows you to specify the elapsed time before the item expires, based on the last access time of the item. Once the item expires, it is removed from the cache. The behavior of an attempt to retrieve its value returns Null (Nothing in Visual Basic) unless the item is added back to the cache.
For volatile items that are stored in the cache, such as those that periodically perform data refreshes, or those that are valid only for a period of time, you typically set an expiration policy: keep them in the cache as long as the data for those items remains current. For example, if you are writing an application that retrieves data from another Web site to track sports scores, you can cache these scores as long as the score on the source site does not change. In this case, you can set an expiration policy based on how often the score is updated by other sites. You can write code to determine if the cache is the most recent score. 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 cached items based on external files, directories (file dependencies), or another cache entry (key dependency). If an item with an 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 data from the file into the cache and leave a dependency on the XML file. When the file is updated, the item is removed from the cache, your application reads the XML file again, and then puts the refreshed data in the cache.
To add an item to the cache
You can use the cache object to access items in the application cache. You can use the Insert method of the Cache object to add items to the application cache. This method adds an item to the cache, and with several overloads, you can add items with different options to set dependencies, expiration, and removal notifications. If you add an item to the cache using the Insert method, and an item already exists with the same name as an existing item, the existing item in the cache is replaced.
You can also add items to the cache using the Add method. With this method, you can set all the options that are the same as the Insert method; However, the Add method returns the object that you added to the cache. In addition, if you use the Add method and the cache already has an item with the same name as an existing item, the method does not replace the item, and no exception is thrown.
To add items to the cache by setting keys and values directly
Adds an item to the cache as if it were added to the dictionary by specifying its key and value.
The following code example adds an item named CacheItem1 to the Cache object:
cache["CacheItem1"] = "Cached Item 1";
To add an item to the cache by using the Insert method
Invokes the Insert method, passing the key and value of the item to be added.
The following code example adds a string named CacheItem2:
Cache.Insert ("CacheItem2", "Cached Item 2");