cache in c#

Posted Calabash

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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接口

 

以上是关于cache in c#的主要内容,如果未能解决你的问题,请参考以下文章

What's new in C# 7.0

What's new in C# 7.0

C# 最有用的(自定义)代码片段是啥? [关闭]

c#代码片段快速构建代码

此 Canon SDK C++ 代码片段的等效 C# 代码是啥?

是否可以动态编译和执行 C# 代码片段?