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
        }
    

      

  • 相关阅读:
    VS2005 Web安装程序 创建程序菜单组
    文件夹 文件 加入/去除 Everyone全控
    [转]asp.net 部署数据库、开始菜单、桌面快捷方式实例
    身边的贵人
    AppCode下的cs类 取得相关路径
    遭遇“windows已经阻止此软件因为无法验证发行者”
    成功不是忽悠
    关于 软件注册授权 防止被大面积免费扩散 的设想
    [转]获取机器的硬件信息(CPU ID序列号, 主板信息,硬盘序列号,系统信息)
    递交辞呈之后
  • 原文地址:https://www.cnblogs.com/marshhu/p/6735055.html
Copyright © 2011-2022 走看看