C#文法糖之 cache操作類 asp.net

來源:互聯網
上載者:User

標籤:

因為考慮到我下面我將寫session cookies 等 操作類 ,與cache具有共性。 所以都統一繼承了IHttpStorageObject  abstract class 來保函數風格的統一 ,但是又為了調用方便,抽象中又使用了單例來簡化調用。

 

 

使用方法很簡單:

 //聲名一個資料集合            var listString = new List<string>() { "a", "b", "c" };            //緩衝key            string key = "cmkey";            //擷取執行個體            var cacheManager = CacheManager<List<string>>.GetInstance();            //插入緩衝            cacheManager.Add(key,listString, cacheManager.Minutes * 30);//到期30分鐘             //add有其它重載 上面是最基本的            //擷取            List<string> cacheList=cacheManager[key];            //其它方法            cacheManager.ContainsKey(key);            cacheManager.Remove(key);//刪除            cacheManager.RemoveAll(c=>c.Contains("sales_"));//刪除key包含sales_緩衝            cacheManager.GetAllKey();//擷取所有key

 

代碼:

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>    /// ** 描述:快取作業類    /// ** 創始時間:2015-6-9    /// ** 修改時間:-    /// ** sunkaixuan    /// ** 使用說明:    /// </summary>    /// <typeparam name="K">鍵</typeparam>    /// <typeparam name="V">值</typeparam>    public class CacheManager<V> : IHttpStorageObject<V>    {        #region 全域變數        private static IHttpStorageObject<V> _instance = null;        private static readonly object _instanceLock = new object();        #endregion        #region 建構函式        private CacheManager() { }        #endregion        #region  屬性        /// <summary>                 ///根據key擷取value             /// </summary>                 /// <value></value>              public override V this[string key]        {            get { return (V)HttpRuntime.Cache[CreateKey(key)]; }        }        #endregion        #region 公用函數        /// <summary>                 /// key是否存在               /// </summary>                 /// <param name="key">key</param>                 /// <returns> /// 存在<c>true</c> 不存在<c>false</c>.        /// /// </returns>                 public override bool ContainsKey(string key)        {            return HttpRuntime.Cache[CreateKey(key)] != null;        }        /// <summary>                 /// 擷取緩衝值                 /// </summary>                 /// <param name="key">key</param>                 /// <returns></returns>                 public override V Get(string key)        {            return (V)HttpRuntime.Cache.Get(CreateKey(key));        }        /// <summary>                 /// 擷取執行個體 (單例模式)               /// </summary>                 /// <returns></returns>                 public static IHttpStorageObject<V> GetInstance()        {            if (_instance == null)                lock (_instanceLock)                    if (_instance == null)                        _instance = new CacheManager<V>();            return _instance;        }        /// <summary>                 /// 插入緩衝                /// </summary>                 /// <param name="key"> key</param>                 /// <param name="value">value</param>                 /// <param name="cacheDurationInSeconds">到期時間單位秒</param>                 public override void Add(string key, V value, int cacheDurationInSeconds)        {            Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default);        }        /// <summary>                 /// 插入緩衝.                 /// </summary>                 /// <param name="key">key</param>                 /// <param name="value">value</param>                 /// <param name="cacheDurationInSeconds">到期時間單位秒</param>                 /// <param name="priority">快取項目屬性</param>                 public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority)        {            string keyString = CreateKey(key);            HttpRuntime.Cache.Insert(keyString, value, null, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);        }        /// <summary>                 /// 插入緩衝.                 /// </summary>                 /// <param name="key">key</param>                 /// <param name="value">value</param>                 /// <param name="cacheDurationInSeconds">到期時間單位秒</param>                 /// <param name="priority">快取項目屬性</param>                 public  void Add(string key, V value, int cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority)        {            string keyString = CreateKey(key);            HttpRuntime.Cache.Insert(keyString, value, dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);        }        /// <summary>                 /// 刪除緩衝                 /// </summary>                 /// <param name="key">key</param>                 public override void Remove(string key)        {            HttpRuntime.Cache.Remove(CreateKey(key));        }        /// <summary>        /// 清除所有緩衝        /// </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>        /// 清除所有包含關鍵字的緩衝        /// </summary>        /// <param name="removeKey">關鍵字</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>        /// 擷取所有緩衝key        /// </summary>        /// <returns></returns>        public override IEnumerable<string> GetAllKey()        {            IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();            while (CacheEnum.MoveNext())            {                yield return CacheEnum.Key.ToString();            }        }        #endregion        #region 私人函數        /// <summary>                 ///建立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 = 60;        public int Hour = 60 * 60;        public int Day = 60 * 60 * 24;        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#文法糖之 cache操作類 asp.net

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.