This article is guided by: in. NET usage is often used in cache objects. There are HttpContext.Current.Cache and Httpruntime.cache,httpruntime.cache are application-level, and HttpContext.Current.Cache is defined for the current web context. HttpRuntime can be used in addition to the Web, non-web programs can also be used.
1, Httpruntime.cache equivalent is a cache specific implementation class, although this class is placed in the System.Web namespace. But non-Web apps can be used as well.
2, Httpcontext.cache is the encapsulation of the above-mentioned cache class, as encapsulated in HttpContext, limited to only know HttpContext under the use, that is, only for WEB applications.
The Httpruntime.cache, in the condition of being able, try to use the Httpcontext.cache.
First, there are several rules for caching data
First, data may be used frequently, and this data can be cached.
Second, the frequency of access to data is very high, or the frequency of access to a data is not high, but it has a long life cycle, such data is best to cache.
The third is a frequently overlooked problem, sometimes we cache too much data, usually on a X86 machine, if you want to cache more than 800M of data, there will be a memory overflow error. So the cache is limited. In other words, you should estimate the size of the cache set, limit the size of the cache set to less than 10, or it may cause problems. In ASP., a memory overflow error is reported if the cache is too large, especially if the large dataset object is cached.
You should carefully analyze your program. According to the actual situation to see where to use, where should not use. For example, using too much cache will increase the pressure on the server. Full-page output caching can also affect data updates. If you really need to cache a lot of data, you can consider static technology.
usingSystem;usingsystem.web;usingSystem.Collections; Public classcookieshelper{/**//// <summary> ///Get data cache/// </summary> /// <param name= "CacheKey" >Key</param> Public Static ObjectGetCache (stringCacheKey) {System.Web.Caching.Cache Objcache=Httpruntime.cache; returnObjcache[cachekey]; } /**//// <summary> ///setting up the data cache/// </summary> Public Static voidSetcache (stringCacheKey,Objectobjobject) {System.Web.Caching.Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, objobject); } /**//// <summary> ///setting up the data cache/// </summary> Public Static voidSetcache (stringCacheKey,ObjectObjobject, TimeSpan Timeout) {System.Web.Caching.Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, Objobject,NULL, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable,NULL); } /**//// <summary> ///setting up the data cache/// </summary> Public Static voidSetcache (stringCacheKey,ObjectObjobject, DateTime absoluteexpiration, TimeSpan slidingexpiration) {System.Web.Caching.Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, Objobject,NULL, absoluteexpiration, slidingexpiration); } /**//// <summary> ///remove the specified data cache/// </summary> Public Static voidRemoveallcache (stringCacheKey) {System.Web.Caching.Cache _cache=Httpruntime.cache; _cache. Remove (CacheKey); } /**//// <summary> ///Removing all caches/// </summary> Public Static voidRemoveallcache () {System.Web.Caching.Cache _cache=Httpruntime.cache; IDictionaryEnumerator Cacheenum=_cache. GetEnumerator (); while(Cacheenum.movenext ()) {_cache. Remove (CacheEnum.Key.ToString ()); } }}
ASP. NET-brought cache