. NET comes with a cache of two, one is the Httpcontext.cacheof the ASP. NET application-level caching, Httpruntime.cache.
There is an explanation on MSDN that:
HttpContext.Current.Cache: Gets the Cache object for the current HTTP request.
Httpruntime.cache: Gets the Cache for the current application.
Through the source view can be informed that HttpContext.Current.Cache call unexpectedly is also Httpruntime.cache and HttpContext can only be used in the Web, and Httpruntimecache can be used in any application set, so here I directly encapsulated Httprunt Imecache is used as a cache class.
The code is as follows:
1 usingSystem;2 usingSystem.Collections;3 usingsystem.web;4 usingSystem.Web.Caching;5 /**6 * Author:qixiao7 * create2017-6-6 11:54:078 * */9 namespaceQX_FRAME.HELPER_DGTen { One Public classHTTPRUNTIMECACHE_HELPER_DG A { - /// <summary> - ///Cache_get the /// </summary> - /// <param name= "CacheKey" >CacheKey</param> - /// <returns></returns> - Public Static ObjectCache_get (stringCacheKey) + { - returnHttpruntime.cache[cachekey]; + } A at #regionCache ADD - - /// <summary> - ///Cache_add - /// </summary> - /// <param name= "CacheKey" >Key</param> in /// <param name= "Cachevalue" >Object Value</param> - /// <param name= "keepminutes" ></param> to /// <param name= "dependencies" >A cached dependency, that is, a change to this item, means that the cached content has expired. If there are no dependencies, you can set this value to NULL. </param> + /// <param name= "CacheItemRemovedCallback" >represents the event that is called when the cache deletes a data object, and is typically used as a notifier. </param> - /// <returns></returns> the Public StaticBoolean Cache_add (stringCacheKey,ObjectCachevalue,intKeepminutes =Ten, cachedependency dependencies =NULL, CacheItemRemovedCallback CacheItemRemovedCallback =NULL) * { $ HttpRuntime.Cache.Insert (CacheKey, Cachevalue, dependencies, Cache.noabsoluteexpiration, Timespan.fromminu TES (keepminutes), cacheitempriority.notremovable, cacheitemremovedcallback);Panax Notoginseng return true; - } the + /// <summary> A ///Cache_add the /// </summary> + /// <param name= "CacheKey" >Key</param> - /// <param name= "Cachevalue" >Object Value</param> $ /// <param name= "keepminutes" ></param> $ /// <param name= "dependencies" >A cached dependency, that is, a change to this item, means that the cached content has expired. If there are no dependencies, you can set this value to NULL. </param> - /// <param name= "CacheItemRemovedCallback" >represents the event that is called when the cache deletes a data object, and is typically used as a notifier. </param> - /// <returns></returns> the Public StaticBoolean Cache_add (stringCacheKey,ObjectCachevalue, DateTime expiretime, cachedependency dependencies =NULL, CacheItemRemovedCallback CacheItemRemovedCallback =NULL) - {Wuyi HttpRuntime.Cache.Insert (CacheKey, Cachevalue, dependencies, Expiretime, Cache.noslidingexpiration, Cachei Tempriority.notremovable, CacheItemRemovedCallback); the return true; - } Wu - /// <summary> About ///Cache_add $ /// </summary> - /// <param name= "CacheKey" >Key</param> - /// <param name= "Cachevalue" >Object Value</param> - /// <param name= "dependencies" >A cached dependency, that is, a change to this item, means that the cached content has expired. If there are no dependencies, you can set this value to NULL. </param> A /// <param name= "absoluteexpiration" >if slidingexpiration is set, the entry must be set to DateTime.MaxValue. is date data, which indicates when the cache expires, the cache provided by. NET 2.0 can be used after expiration, and how long it takes to see the settings for this parameter. </param> + /// <param name= "slidingexpiration" >if Absoluteexpiration is set, the entry must be set to TimeSpan.Zero. Represents a time interval that indicates how long the cache parameter will be deleted after this parameter is associated with the absoluteexpiration parameter. </param> the /// <param name= "CacheItemPriority" >represents the priority value of the undo cache, the value of this parameter is taken from the enumeration variable "CacheItemPriority", and the low-priority data item is first deleted. This parameter is primarily used when the cache exits an object. </param> - /// <param name= "CacheItemRemovedCallback" >represents the event that is called when the cache deletes a data object, and is typically used as a notifier. </param> $ //Public static Boolean Cache_add (String CacheKey, Object cachevalue, cachedependency dependencies = null, DateTime ABSO Luteexpiration = Default (DateTime), TimeSpan slidingexpiration = Default (TimeSpan), cacheitempriority CacheItemPriority = cacheitempriority.notremovable, cacheitemremovedcallback cacheitemremovedcallback = null) the //{ the //datetime absoluteexpirationtime = Default (datetime); the //if (! Datetime.tryparse (Absoluteexpiration.tostring (), out absoluteexpirationtime) | | Absoluteexpiration.equals (Default (DateTime ))) the //absoluteexpirationtime = DateTime.MaxValue; - //Else in //slidingexpiration = TimeSpan.Zero; the the //TimeSpan slidingexpirationtime = Default (TimeSpan); About //if (! Timespan.tryparse (Slidingexpiration.tostring (), out slidingexpirationtime) | | Slidingexpiration.equals (Default (TimeSpan))) the //slidingexpirationtime = TimeSpan.Zero; the the //HttpRuntime.Cache.Insert (CacheKey, Cachevalue, dependencies, Absoluteexpirationtime, Slidingexpirationtime, CacheItemPriority, CacheItemRemovedCallback); + //return true; - //} the Bayi #endregion the the /// <summary> - ///Cache_delete - /// </summary> the /// <param name= "CacheKey" >CacheKey</param> the Public StaticBoolean Cache_delete (stringCacheKey) the { the HttpRuntime.Cache.Remove (CacheKey); - return true; the } the the /// <summary>94 ///Cache_deleteall the /// </summary> the Public StaticBoolean Cache_deleteall () the {98Cache _cache =Httpruntime.cache; AboutIDictionaryEnumerator Cacheenum =_cache. GetEnumerator (); - while(Cacheenum.movenext ())101 {102 _cache. Remove (CacheEnum.Key.ToString ());103 }104 return true; the }106 107 /// <summary>108 ///Cache Count109 /// </summary> the Public Static intCachecount111 { the Get{returnHttpRuntime.Cache.Count;}113 } the } the}
Httpruntime.cache. NET-brought cache classes