Interface
Public Interfaceicachemanager:idisposable {/// <summary> ///get values by key/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "key" ></param> /// <returns></returns>T get<t> (stringkey); /// <summary> ///Add a key-value pair cache/// </summary> /// <param name= "key" ></param> /// <param name= "Data" ></param> /// <param name= "CacheTime" ></param> voidSet (stringKeyObjectDataintcacheTime); /// <summary> ///gets whether the value corresponding to the key exists/// </summary> /// <param name= "key" ></param> /// <returns></returns> BOOLIsSet (stringkey); /// <summary> ///removing a key-value pair cache/// </summary> /// <param name= "key" ></param> voidRemove (stringkey); voidRemovebypattern (stringpattern); /// <summary> ///empty all cached data/// </summary> voidClear (); }View CodeExtended
Public Static classcacheextensions { Public StaticT get<t> ( ThisIcachemanager CacheManager,stringKey, func<t>acquire) { returnGet (CacheManager, Key, -, acquire); } Public StaticT get<t> ( ThisIcachemanager CacheManager,stringKeyintCacheTime, func<t>acquire) { if(Cachemanager.isset (key)) {returnCachemanager.get<t>(key); } varresult =acquire (); if(cachetime>0) Cachemanager.set (key,result,cachetime); returnresult; } /// <summary> ///Batch Removal/// </summary> /// <param name= "CacheManager" >Cache Manager</param> /// <param name= "pattern" >Pattern</param> /// <param name= "Keys" >All keys in the cache</param> Public Static voidRemovebypattern ( ThisIcachemanager CacheManager,stringPattern, ienumerable<string>keys) { varRegex =NewRegex (Pattern, Regexoptions.singleline | regexoptions.compiled |regexoptions.ignorecase); foreach(varKeyinchKeys. Where (p =regex. IsMatch (P.tostring ())). ToList ()) Cachemanager.remove (key); } }View CodeRealize
Public Partial classMemorycachemanager:icachemanager {protectedObjectCache Cache {Get{returnMemorycache.default;} } #regionMethod Public VirtualT get<t> (stringkey) { return(T) Cache[key]; } Public Virtual voidSet (stringKeyObjectDataintcacheTime) { if(data==NULL) return; varPolicy =Newcacheitempolicy {absoluteexpiration = DateTime.Now +timespan.fromminutes (CacheTime)}; Cache.Add (NewCacheItem (key, data), policy); } Public Virtual BOOLIsSet (stringkey) { returncache.contains (key); } Public Virtual voidRemove (stringkey) {Cache.remove (key); } Public Virtual voidRemovebypattern (stringpattern) { This. Removebypattern (Pattern,cache.select (p=>P.key)); } Public voidClear () {foreach(varIteminchCache) Remove (item. Key); } #endregion Public voidDispose () {Throw Newnotimplementedexception (); } }View Code
Injection
Builder. Registertype<memorycachemanager> ()
. As<icachemanager> ()
. Named<icachemanager> ("Cala_cache_static")
. SingleInstance ();
Set key
Private Const stringCalabash_all_key ="Cala.calabash.all"; PublicIlist<core.domain.calabash>GetAll () {//Cache varKey =string. Format (Calabash_all_key); return_cachemanager.get (key, () = { varquery =_calabashrepository.table.tolist (); returnquery; }); }
The previous Redis use is the same as the cache because all implementations of the Icachemanager interface
Cache in C #