Cache in C #

Source: Internet
Author: User

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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.