zoukankan      html  css  js  c++  java
  • 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接口

  • 相关阅读:
    php 网络图片转base64
    uniapp微信小程序拒绝授权后,重新调起授权页
    uniapp获取用户信息 getuserinfo
    基于JSP和Mybatis框架技术的应用总结
    Java第16周作业集
    Java第15周作业集
    Java第14周作业集
    Java第13周作业集
    软件工程结课作业
    Java第13次作业--邮箱的正则表达式
  • 原文地址:https://www.cnblogs.com/sunzgod/p/6438544.html
Copyright © 2011-2022 走看看