简单缓存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 }