ASP.NET provides programming capabilities that essentially cache information in a ASP.NET application. The functionality is similar to the Application object, but it has the ability to dynamically maintain cached information over the lifetime of ASP.NET application. Caching data in an application can greatly improve performance because the data is local to the server and can be retrieved quickly. Instead of having to requery the original data source. Especially the biggest bottleneck of the current system is the database, and the rational use of cache can reduce a lot of unnecessary database access, so as to optimize the performance of the system.
Cache objects improve the ability to implement relevance-based expiration and callback notifications that allow actions on cached items in the cache by other users of the management application or by the ASP.NET itself. Use the Cache object to store any type (value type or application type) before updating or deleting them. Cache is a more flexible object than an Application object. It supports:
1. Relevance-based expiration and deletion - Cache objects provide the ability to bind automatic expiration and deletion of cache values to a file event, a specific or relative date/time, or another cache key.
2. Automatic lock management - Cache objects include internal lock management, which provides synchronization for atomic updates (updates that can be placed in a calculation operation).
3. Callback mechanism - Cache object provides the ability to set the callback function to be called when deleting cache items.
The Cache addition method includes Add() or Insert(), and the Insert method can use optional parameters to add the cache using both the default parameters:
Cache.Add(KeyName, KeyValue,Dependencies,AbsoluteExpiration,SlidingExpiration,Priority,CacheItemRemoveCallback);
The parameters are: cache name, cached object, dependency, absolute expiration time, relative expiration time, priority, and events caused by cache expiration.
Cache dependencies: file dependencies, other cache dependencies, database dependencies, and expiration time methods are set, and when the dependencies change, the cache is invalid and can raise certain events.
1. File dependencies: cache dependencies on xml files:
- <p>DataSet dsProducts = new DataSet();</p><p>//...dsProducts数y据Y</p><p>CacheDependency fileDepends = new CacheDependency(Server.MapPath("Northwind.xml"));</p><p>Cache.Insert("dsProducts", dsProducts, fileDepends);</p>
Copy code
2. Other cache item dependencies:
- <p>object cacheData;</p><p>string[] fileDependsArray = { Server.MapPath("Northwind.xml") };</p><p>string[] cacheDependsArray = { "Depend0", "Depend1", "Depend2" };</p><p>CacheDependency cacheDepends = new CacheDependency(fileDependsArray, cacheDependsArray);</p><p>Cache.Insert("cacheName", cacheData, cacheDepends);</p>
Copy code
3. Expiration time setting:
AbsoluteExpiration can set the absolute expiration time of the cache, such as setting it to expire after 30 minutes:
- Cache.Insert("cacheName ", "cacheValue", null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration);
Copy code NoSlidingExpiration can set a relative expiration time, if the cache is not accessed within the time set by NoSlidingExpiration, if the cache expires and is not accessed for a certain period of time, the cache will not expire. If you access the cache within 30 minutes, it will not expire:
- Cache.Insert("cacheName", "cacheValue", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30));
Copy code
|