zoukankan      html  css  js  c++  java
  • 简单缓存Cache

    接口

     interface ICache
        {
            /// <summary>
            /// 添加
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="expiratTime"></param>
            void Add(string key, object value, int expiratTime = 30);
            /// <summary>
            /// 获取
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="key"></param>
            /// <returns></returns>
            T Get<T>(string key);
            /// <summary>
            /// 是否包含
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            bool Contains(string key);
            /// <summary>
            /// 删除指定key
            /// </summary>
            /// <param name="key"></param>
            void Remove(string key);
            /// <summary>
            /// 删除所有
            /// </summary>
            void RemoveAll();
            /// <summary>
            /// 索引访问器
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            object this[string key] { get; set; }
            /// <summary>
            /// 数量
            /// </summary>
            int Count { get; }
        }
    

     实现类:

     public class CustomerCache : ICache
        {
            private static Dictionary<string, KeyValuePair<object, DateTime>> _dictionary = new Dictionary<string, KeyValuePair<object, DateTime>>();
    
            static CustomerCache()
            {
                new Action(() =>
                {
                    while (true)
                    {
                        Thread.Sleep(100);
                        foreach (var item in _dictionary.Where(t => t.Value.Value < DateTime.Now))
                        {
                            _dictionary.Remove(item.Key);
                        }
                    }
                }).BeginInvoke(null, null);
            }
    
            public void  Add(string key, object value, int expiratTime = 30)
            {
                KeyValuePair<object, DateTime> keyValue = new KeyValuePair<object, DateTime>(value, DateTime.Now.AddMinutes(expiratTime));
                _dictionary[key] = keyValue;
            }
    
            public bool Contains(string key)
            {
                if (_dictionary.ContainsKey(key))
                {
                    KeyValuePair<object, DateTime> keyValue = _dictionary[key];
                    if (keyValue.Value > DateTime.Now)//没有过期
                    {
                        return true;
                    }
                    else
                    {
                        _dictionary.Remove(key);//过期清除
                        return false;
                    }
                }
                return false;
            }
    
            public T Get<T>(string key)
            {
                if (_dictionary.ContainsKey(key))
                {
                    KeyValuePair<object, DateTime> keyValue = _dictionary[key];
                    if (keyValue.Value > DateTime.Now)//没有过期
                    {
                        return (T)keyValue.Key;
                    }
                    else
                    {
                        _dictionary.Remove(key);//过期清除
                        return default(T);
                    }
                }
                return default(T);
            }
    
            public void Remove(string key)
            {
                 _dictionary.Remove(key);
            }
    
            public void RemoveAll()
            {
                _dictionary = new Dictionary<string, KeyValuePair<object, DateTime>>();
            }
    
            public object this[string key]
            {
                get
                {
                    return this.Get<object>(key);
                }
                set
                {
                    this.Add(key, value);
                }
            }
    
            public int Count
            {
                get
                {
                    return _dictionary.Values.Where(t => t.Value > DateTime.Now).Count();
                }
            }
    
        }

     缓存管理器:

    public class CacheManager
        {
            private CacheManager()
            {
    
            }
    
            private static ICache cache = null;
    
            static CacheManager()
            {
                cache = (ICache)Activator.CreateInstance(typeof(CustomerCache));
            }
    
            #region ICache
            public static int Count
            {
                get { return cache.Count; }
            }
    
            public static bool Contains(string key)
            {
                return cache.Contains(key);
            }
    
            public static T Get<T>(string key)
            {
                return cache.Get<T>(key);
            }
    
            public static void Add(string key, object value, int expiratTime = 30)
            {
                if (Contains(key))
                    cache.Remove(key);
                cache.Add(key, value, expiratTime);
            }
            /// <summary>
            /// 删除缓存数据项
            /// </summary>
            /// <param name="key"></param>
            public static void Remove(string key)
            {
                cache.Remove(key);
            }
    
            /// <summary>
            /// 删除所有缓存数据项
            /// </summary>
            public static void RemoveAll()
            {
                cache.RemoveAll();
            }
            #endregion
        }
    

      

  • 相关阅读:
    创建供应商-采购模块
    定义容差组
    前台创建供应商-财务角度
    对供应商账户组分配编号范围
    拓端数据tecdat|R语言建立和可视化混合效应模型mixed effect model
    拓端数据tecdat|R语言建模收入不平等:分布函数拟合及洛伦兹曲线(Lorenz curve)
    拓端数据tecdat|R语言中的多项式回归、局部回归、核平滑和平滑样条回归模型
    拓端数据tecdat|R语言ARIMA,SARIMA预测道路交通流量时间序列:季节性、周期性
    拓端数据tecdat|ARIMA模型预测CO2浓度时间序列
    拓端数据tecdat|R语言基于递归神经网络RNN的温度时间序列预测
  • 原文地址:https://www.cnblogs.com/marshhu/p/6735055.html
Copyright © 2011-2022 走看看