usingSystem;usingSystem.Collections.Generic;usingsystem.web;usingSystem.Collections;usingSystem.Web.Caching;namespacedmsite.common{/// <summary> ///Server Cache helper Classes/// </summary> Public classCachehelper {/// <summary> ///file dependencies for creating cache entries/// </summary> /// <param name= "key" >Cache Key</param> /// <param name= "obj" >Object Objects</param> /// <param name= "FileName" >File Absolute Path</param> Public Static voidInsertFile (stringKeyObjectObjstringfileName) { //To Create a cache dependencyCacheDependency dep =NewCacheDependency (fileName); //Create CacheHttpRuntime.Cache.Insert (key, obj, DEP); } /// <summary> ///Create cache entry expires/// </summary> /// <param name= "key" >Cache Key</param> /// <param name= "obj" >Object Objects</param> Public Static voidInsert (stringKeyObjectobj) { if(obj! =NULL) { if(Isexist (key)) {Httpruntime.cache[key]=obj; } Else { intexpires = Converthelper.toint (Confighelper.getappsettings ("Timecache")); HttpRuntime.Cache.Insert (key, obj,NULL, System.Web.Caching.Cache.NoAbsoluteExpiration,NewTimeSpan (0, Expires,0)); } } } /// <summary> ///determine if the cache object exists/// </summary> /// <param name= "Strkey" >Cache key Value name</param> /// <returns>whether there is a true, false</returns> Public Static BOOLIsexist (stringstrkey) { returnHttpruntime.cache[strkey]! =NULL; } /// <summary> ///Get Cache Object/// </summary> /// <param name= "key" >Cache Key</param> /// <returns>Object Objects</returns> Public Static ObjectGetCache (stringkey) { if(string. IsNullOrEmpty (key))return NULL; if(Confighelper.getappsettings ("Iscache") =="false") { return NULL; } returnHttpRuntime.Cache.Get (key); } /// <summary> ///Get Cache Object/// </summary> /// <typeparam name= "T" >T Object</typeparam> /// <param name= "key" >Cache Key</param> /// <returns></returns> Public StaticT get<t> (stringkey) { Objectobj =GetCache (key); returnobj = =NULL?default(t): (t) obj; } /// <summary> ///remove the specified data cache/// </summary> Public Static voidRemoveCache (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 ()); } } }}
Cache Helper Class