Overview of ASP. NET cache data adding requirements
ASP. NET uses the cache mechanism to store objects that require a large amount of server resources in the memory. Caching these types of resources will greatly improve the application performance. Cache is implemented by the cache class. You can set the cache priority cacheitempriority to control the "clean" priority when the memory is insufficient. You can also set an expiration Policy for the cache and a dependency for the cache.
Add ASP. NET cache data (add data items to cache)
1. Add a key-Value Pair
Cache ["cacheitem"] = "cached item ";
2. Use the insert method to add
If the insert method adds an item to the cache and an item with the same name as the existing item already exists, the existing item in the cache will be replaced.
Cache. insert ("cacheitem", "cached item ");
3. Specify and add dependencies (specify dependencies for data items added to the buffer)
When a data item depends on a string array object:
string[] dependencies = { "Dependences" }; Cache.Insert("CacheItem","Cached Item", new System.Web.Caching.CacheDependency(null, dependencies));
When a data item depends on an XML file:
Cache.Insert("CacheItem", "Cached Item", new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")));
Where data items depend on multiple dependencies:
System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")); string[] keyDependencies2 = { "CacheItem1" };System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(null, keyDependencies2);System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency();aggDep.Add(dep1);aggDep.Add(dep2);Cache.Insert("CacheItem", "Cached Item", aggDep);
4. Set an expiration Policy and add
Add an absolute expiration time of one minute to the cache:
Cache.Insert("CacheItem", "Cached Item", null, DateTime.Now.AddMinutes(1d), System.Web.Caching.Cache.NoSlidingExpiration);
Add a 10-minute elastic expiration time to the cache:
Cache.Insert("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0));
5. set priority and add
Call the insert method to specify a value from the cacheitempriority enumeration.
Cache.Insert("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
6. Use the add method to add
The add method returns the object you added to the cache. In addition, if the add method is used and an item with the same name as an existing item already exists in the cache, this method will not replace this item and will not cause an exception.
string CachedItem = (string)Cache.Add("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
The preceding describes six methods for adding cache data to ASP. NET. This article is from Bodhi House: cache application data (1)