cache in c#

//接口
    public interface ICacheManager:IDisposable
    {
        /// <summary>
        /// 根据键获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        T Get<T>(string key);

        /// <summary>
        /// 添加一个键值对缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        /// <param name="cacheTime"></param>
        void Set(string key, object data, int cacheTime);

        /// <summary>
        /// 获取键对应的值是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool IsSet(string key);

        /// <summary>
        /// 移除键值对缓存
        /// </summary>
        /// <param name="key"></param>
        void Remove(string key);

        void RemoveByPattern(string pattern);

        /// <summary>
        /// 清空所有的缓存数据
        /// </summary>
        void Clear();
    }
View Code
//扩展
    public static  class CacheExtensions
    {
        public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire)
        {
            return Get(cacheManager, key, 60, acquire);
        }

        public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire)
        {
            if (cacheManager.IsSet(key))
            {
                return cacheManager.Get<T>(key);
            }
            var result = acquire();
            if(cacheTime>0)
                cacheManager.Set(key,result,cacheTime);
            return result;
        }

        /// <summary>
        /// 批量移除
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="pattern">Pattern</param>
        /// <param name="keys">All keys in the cache</param>
        public static void RemoveByPattern(this ICacheManager cacheManager, string pattern, IEnumerable<string> keys)
        {
            var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            foreach (var key in keys.Where(p => regex.IsMatch(p.ToString())).ToList())
                cacheManager.Remove(key);
        }
    }
View Code
//实现
    public partial  class MemoryCacheManager:ICacheManager
    {
        protected ObjectCache Cache
        {
            get { return MemoryCache.Default; }
        }

        #region Method

        public virtual T Get<T>(string key)
        {
            return (T) Cache[key];
        }

        public virtual void Set(string key, object data, int cacheTime)
        {
            if(data==null)
                return;
            var policy = new CacheItemPolicy {AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime)};
            Cache.Add(new CacheItem(key, data), policy);
        }

        public virtual bool IsSet(string key)
        {
            return Cache.Contains(key);
        }

        public virtual void Remove(string key)
        {
            Cache.Remove(key);
        }

        public virtual void RemoveByPattern(string pattern)
        {
            this.RemoveByPattern(pattern,Cache.Select(p=>p.Key));
        }

        public void Clear()
        {
            foreach (var item in Cache)
                Remove(item.Key);
        }
        #endregion

        public void Dispose()
        {
            throw new NotImplementedException();
        }


    }
View Code

 

//注入

builder.RegisterType<MemoryCacheManager>()
.As<ICacheManager>()
.Named<ICacheManager>("cala_cache_static")
.SingleInstance();

 

     //设置key
private const string Calabash_All_key = "cala.calabash.all"; public IList<Core.Domain.Calabash> GetAll() { //cache var key = string.Format(Calabash_All_key); return _cacheManager.Get(key, () => { var query = _calabashRepository.Table.ToList(); return query; }); }

 

//上一篇redis使用和cache是一样的 因为都实现了 icachemanager接口

 

posted @ 2017-02-24 14:49  CalabashSun  阅读(171)  评论(1编辑  收藏  举报