C # syntax sugar cache operation class ASP.

Source: Internet
Author: User

Because considering me below I will write session cookies and other operation classes, with the cache has a common. So they all inherit the unity of the Ihttpstorageobject abstract class to keep the function style, but in order to make the call convenient, the abstract also uses a singleton to simplify the call.

The way to use it is simple:

Fame a data set            var liststring = new list<string> () {"A", "B", "C"};            Cache key            String key = "Cmkey";            Get instance            var CacheManager = Cachemanager<list<string>>. GetInstance ();            Insert Cache            Cachemanager.add (key,liststring, cachemanager.minutes * 30);//Expires 30 minutes             //add has other overloads above is the most basic            //Get            list<string> Cachelist=cachemanager[key];            Other methods            Cachemanager.containskey (key);            Cachemanager.remove (key);//delete            Cachemanager.removeall (c=>c.contains ("Sales_"));//delete key contains Sales_ cache            Cachemanager.getallkey ();//Get all keys

Code:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Web;using System.web.caching;using system.collections;using system.linq.expressions;namespace SyntacticSugar{//<summary >//* * Description: Cache operation CLASS//* * Founding Date: 2015-6-9//* * Modified:-//* * Sunkaixuan//* * Instructions://</summary&    Gt <typeparam name= "K" > Keys </typeparam>//<typeparam name= "V" > Value </typeparam> public class Cac Hemanager<v>: ihttpstorageobject<v> {#region global variable private static IHTTPSTORAGEOBJECT&LT;V&GT ;        _instance = null;        private static readonly Object _instancelock = new Object (); #endregion #region Constructor Private CacheManager () {} #endregion #region Properties//<summa              Ry>///Based on key//</summary>//<value></value>           public override V This[string key] { get {return (V) Httpruntime.cache[createkey (key)];} } #endregion #region Public functions///<summary>//Key exists///</summ ary>//<param name= "key" >key</param>///<returns>//Presence <c>t        Rue</c> does not exist <c>false</c>. </returns> public override bool ContainsKey (string key) {return Httprunti Me.        Cache[createkey (key)]! = NULL; }///<summary>//Get Cache value///</summary>//<param Name= "key" >key</param>///<returns></returns> public override V Get (        String key) {return (V) HttpRuntime.Cache.Get (CreateKey (key)); }///<summary>//To get an instance (Singleton mode)///</summary>//<re   Turns></returns>              public static ihttpstorageobject<v> getinstance () {if (_instance = = null) Lock (_instancelock) if (_instance = = null) _instance = new CACHEMANAGER&L T            V> ();        return _instance; }//<summary>//Insert cache///</summary>//<param NA Me= "Key" > key</param>//<param name= "value" >value</param>//<p Aram Name= "Cachedurationinseconds" > Expiry time Unit seconds </param> public override void Add (string key, V value,        int cachedurationinseconds) {ADD (key, value, Cachedurationinseconds, Cacheitempriority.default);                 }///<summary>//Insert cache. </summary>//<param name= "key" >key</param>//<param name= "Val UE ">value</param>                <param name= "Cachedurationinseconds" > Expiry time Unit seconds </param>//<param name= "p Riority "> Cache entry Properties </param> public void Add (string key, V value, int cachedurationinseconds, CacheItem            Priority priority) {string keystring = CreateKey (key); HttpRuntime.Cache.Insert (keystring, value, NULL, DateTime.Now.AddSeconds (cachedurationinseconds),        Cache.noslidingexpiration, priority, NULL);                 }///<summary>//Insert cache. </summary>//<param name= "key" >key</param>//<param name= "Val                 UE ">value</param>//<param name=" cachedurationinseconds "> Expiry time Unit seconds </param> <param name= "Priority" > Cache entry Properties </param> public void Add (string key, V value, int cache  Durationinseconds, CacheDependency dependency, cacheitempriority priority) {          String keystring = CreateKey (key); HttpRuntime.Cache.Insert (keystring, value, dependency, DateTime.Now.AddSeconds (Cachedurationinseconds),        Cache.noslidingexpiration, priority, NULL); }//<summary>//Delete cache///</summary>//<param N Ame= "key" >key</param> public override void Remove (string key) {HTTPRUNTIME.CAC He.        Remove (CreateKey (key));        }///<summary>//Clear all caches///</summary> public override void RemoveAll ()            {System.Web.Caching.Cache Cache = Httpruntime.cache; IDictionaryEnumerator cacheenum = cache.            GetEnumerator ();            ArrayList al = new ArrayList (); while (Cacheenum.movenext ()) {al.            ADD (Cacheenum.key); } foreach (String key in AL) {cache.            Remove (key);   }        }     <summary>///Clear all cache containing keywords///</summary>//<param name= "RemoveKey" > Keywords & lt;/param> public override void RemoveAll (func<string, bool> removeexpression) {System.            Web.Caching.Cache _cache = Httpruntime.cache;            var allkeylist = Getallkey (); var delkeylist = Allkeylist.where (removeexpression).            ToList ();            foreach (var key in delkeylist) {Remove (key); }}///<summary>//Get all cache keys///</summary>//<returns></retu  Rns> public override ienumerable<string> Getallkey () {IDictionaryEnumerator cacheenum =            HttpRuntime.Cache.GetEnumerator ();            while (Cacheenum.movenext ()) {yield return CacheEnum.Key.ToString ();   }} #endregion #region Private Function///<summary>//Create key        </summary>//<param name= "key" >Key</param>//<returns ></returns> private String CreateKey (string key) {return key.        ToString (); } #endregion}}

  

Using System;namespace syntacticsugar{public    abstract class ihttpstorageobject<v>    {public        int Minutes =;        public int Hour = *;        public int day = * *;        public abstract void Add (string key, V value, int cachedurationinseconds);        public abstract bool ContainsKey (string key);        Public abstract V Get (string key);        Public abstract global::system.collections.generic.ienumerable<string> Getallkey ();        public abstract void Remove (string key);        public abstract void RemoveAll ();        public abstract void RemoveAll (func<string, bool> removeexpression);        Public abstract V this[string key] {get;}}    }

  

C # syntax sugar cache operation class ASP.

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.