using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Api.Framework.Tools { /// /// 自定义内存缓存助手 /// public sealed class CacheTool { #region 单例模式 //创建私有化静态obj锁 private static readonly object _ObjLock = new object(); //创建私有静态字段,接收类的实例化对象 private static volatile CacheTool _CacheTool = null; //构造函数私有化 private CacheTool() { } //创建单利对象资源并返回 public static CacheTool GetSingleObj() { if (_CacheTool == null) { lock (_ObjLock) { if (_CacheTool == null) _CacheTool = new CacheTool(); } } return _CacheTool; } #endregion /// /// 缓存字典 => 【key|value|time】 /// private static volatile ConcurrentDictionary> _CacheDictionary = new ConcurrentDictionary>(); /// /// 1.主动过期 /// static CacheTool() { Task.Run(() => { while (true) { Thread.Sleep(1000 * 60); //1分钟检查一次 if (_CacheDictionary != null && _CacheDictionary.Keys.Count > 0) { var listKey = new List(); foreach (var key in _CacheDictionary.Keys) { listKey.Add(key); } foreach (var key in listKey) { var valueTime = _CacheDictionary[key]; if (valueTime.Value < DateTime.Now) _CacheDictionary.TryRemove(key, out KeyValuePair value); } } } }); } /// /// 索引器 /// /// /// public object this[string key] { get => _CacheDictionary[key]; set => _CacheDictionary[key] = new KeyValuePair(value, null); } /// /// 设置相对过期缓存 /// /// 键 /// 数据包 /// 相对过期时间 public void Set(string key, object data, int minute = 10) { // var v = string.Empty; // if (data.GetType() != typeof(String)) // { //v = CsharpHttpHelper.HttpHelper.ObjectToJson(); // } _CacheDictionary[key] = new KeyValuePair(data, DateTime.Now.AddMinutes(minute)); } ///// ///// 设置绝对过期缓存 ///// ///// 键< ///// 数据包 //public void Set(string key, object data) //{ // this[key] = data; //_CacheDictionary[key] = new KeyValuePair(data, null); //} /// /// 通过key获取缓存value /// 2.被动过期 /// /// /// /// public T Get(string key) { if (Exist(key)) { var valueTime = _CacheDictionary[key]; return (T)valueTime.Key; //return (T)this[key]; } else { return default(T); } } /// /// 获取缓存个数 /// /// public int Count() { int count = 0; if (_CacheDictionary != null) count = _CacheDictionary.Count; return count; } /// /// 删除指定key的value /// /// public bool Remove(string key) { for (int i = 0; i < 5; i++) { var flag = _CacheDictionary.TryRemove(key, out KeyValuePair value); if (!flag) Thread.Sleep(100); else return true; } return false; } /// /// 清空所有缓存 /// public void Cleaner() { if (_CacheDictionary != null && _CacheDictionary.Count > 0) { foreach (var key in _CacheDictionary.Keys) { _CacheDictionary.TryRemove(key, out KeyValuePair value); } } } /// /// 检查key是否存在 /// 2.被动过期,保证任何过期缓存都无法取值 /// /// /// public bool Exist(string key) { if (string.IsNullOrWhiteSpace(key)) { return false; } else if (_CacheDictionary.ContainsKey(key)) { var valTime = _CacheDictionary[key]; if (valTime.Value != null && valTime.Value > DateTime.Now) { return true; //缓存没过期 } else { _CacheDictionary.TryRemove(key, out KeyValuePair value); //缓存过期清理 return false; } } else { return false; } } } }