基于.NET Standard 2.0标准库下实现C#内存缓存,实例如下:
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Common.Helper { /// <summary> /// 自定义内存缓存助手 /// </summary> public class CustomCacheHelper { #region 单例模式 //创建私有化静态obj锁 private static readonly object _ObjLock = new object(); //创建私有静态字段,接收类的实例化对象 private static CustomCacheHelper _CustomCacheHelper = null; //构造函数私有化 private CustomCacheHelper() { } //创建单利对象资源并返回 public static CustomCacheHelper GetSingleObj() { if (_CustomCacheHelper == null) { lock (_ObjLock) { if (_CustomCacheHelper == null) { _CustomCacheHelper = new CustomCacheHelper(); } } } return _CustomCacheHelper; } #endregion /// <summary> /// 缓存字典 => 【key|value|time】 /// </summary> private static Dictionary<string, KeyValuePair<object, DateTime?>> _CacheDictionary = new Dictionary<string, KeyValuePair<object, DateTime?>>(); /// <summary> /// 1.主动过期 /// </summary> static CustomCacheHelper() { Task.Run(() => { while (true) { Thread.Sleep(1000); //1秒检查一次 if (_CacheDictionary != null) { if (_CacheDictionary.Keys.Count > 0) { var listKey = new List<string>(); foreach (var key in _CacheDictionary.Keys) { listKey.Add(key); } foreach (var key in listKey) { var valueTime = _CacheDictionary[key]; if (valueTime.Value < DateTime.Now) { _CacheDictionary.Remove(key); } } } } } }); } /// <summary> /// 索引器 /// </summary> /// <param name="key"></param> /// <returns></returns> public object this[string key] { get => _CacheDictionary[key]; set => _CacheDictionary[key] = new KeyValuePair<object, DateTime?>(value, null); } /// <summary> /// 设置相对过期缓存 /// </summary> /// <param name="key">键</param> /// <param name="data">数据包</param> /// <param name="seconds">相对过期时间</param> public void Set(string key,object data,int seconds) { _CacheDictionary[key] = new KeyValuePair<object, DateTime?>(data, DateTime.Now.AddSeconds(seconds)); } /// <summary> /// 设置绝对过期缓存 /// </summary> /// <param name="key">键<</param> /// <param name="data">数据包</param> public void Set(string key, object data) { this[key] = data; //_CacheDictionary[key] = new KeyValuePair<object, DateTime?>(data, null); } /// <summary> /// 通过key获取缓存value /// 2.被动过期 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T Get<T>(string key) { if (!Exists(key)) return default(T); var valueTime = _CacheDictionary[key]; if (valueTime.Value != null && valueTime.Value < DateTime.Now) { _CacheDictionary.Remove(key); return default(T); } else return (T)valueTime.Key; //return (T)this[key]; } /// <summary> /// 获取缓存个数 /// </summary> /// <returns></returns> public int Count() { int count = 0; if (_CacheDictionary != null) count = _CacheDictionary.Count; return count; } /// <summary> /// 删除指定key的value /// </summary> /// <param name="key"></param> public void Remove(string key) { if (Exists(key)) _CacheDictionary.Remove(key); } /// <summary> /// 清空所有缓存 /// </summary> public void Cleaner() { if (_CacheDictionary != null && _CacheDictionary.Count > 0) { foreach (var key in _CacheDictionary.Keys) { _CacheDictionary.Remove(key); } } } /// <summary> /// 检查key是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> public bool Exists(string key) { if (string.IsNullOrWhiteSpace(key)) return false; else if (_CacheDictionary.ContainsKey(key)) return true; else return false; } } }