// namespace SafeCampus.Cache; /// /// /// 内存缓存 /// public partial class MemoryCacheService : ISimpleCacheService { private readonly MemoryCache _memoryCache; public MemoryCacheService() { _memoryCache = new MemoryCache(); } #region 普通操作 /// public T Get(string key) { var data = _memoryCache.Get(key); return data.ToObject(); } /// public int Remove(params string[] keys) { return _memoryCache.Remove(keys); } /// public bool Set(string key, T value, int expire = -1) { return _memoryCache.Set(key, value.ToJson(), expire); } /// public bool Set(string key, T value, TimeSpan expire) { return _memoryCache.Set(key, value.ToJson(), expire); } /// public bool SetExpire(string key, TimeSpan expire) { return _memoryCache.SetExpire(key, expire); } /// public TimeSpan GetExpire(string key) { return _memoryCache.GetExpire(key); } /// public bool ContainsKey(string key) { return _memoryCache.ContainsKey(key); } /// public void Clear() { _memoryCache.Clear(); } /// public void DelByPattern(string pattern) { var keys = _memoryCache.Keys.ToList();//获取所有key keys.ForEach(it => { if (it.Contains(pattern))//如果匹配 _memoryCache.Remove(pattern); }); } #endregion 普通操作 #region 集合操作 /// public IDictionary GetAll(IEnumerable keys) { IDictionary? result = default;//定义集合 var data = _memoryCache.GetAll(keys);//获取数据 data.ForEach(it => { result.Add(it.Key, it.Value.ToObject());//遍历数据,格式化并加到新的数据集合 }); return result; } /// public void SetAll(IDictionary values, int expire = -1) { IDictionary? result = default;//定义集合 values.ForEach(it => { result.Add(it.Key, it.Value.ToJson());//遍历数据,格式化并加到新的数据集合 }); _memoryCache.SetAll(values, expire); } /// public IDictionary GetDictionary(string key) { IDictionary? result = default;//定义集合 var data = _memoryCache.GetDictionary(key); data.ForEach(it => { result.Add(it.Key, it.Value.ToObject());//遍历数据,格式化并加到新的数据集合 }); return result; } /// public IProducerConsumer GetQueue(string key) { return _memoryCache.GetQueue(key); } /// public IProducerConsumer GetStack(string key) { return _memoryCache.GetStack(key); } /// public ICollection GetSet(string key) { return _memoryCache.GetSet(key); } #endregion 集合操作 #region 高级操作 /// public bool Add(string key, T value, int expire = -1) { return _memoryCache.Add(key, value.ToJson(), expire); } /// public IList GetList(string key) { IList result = default;//定义集合 var data = _memoryCache.GetList(key); data.ForEach(it => { result.Add(it.ToObject());//遍历数据,格式化并加到新的数据集合 }); return result; } /// public T Replace(string key, T value) { return _memoryCache.Replace(key, value); } /// public bool TryGetValue(string key, out T value) { _ = _memoryCache.TryGetValue(key, out var result); value = result.ToObject(); return value == null; } /// public long Decrement(string key, long value) { return _memoryCache.Decrement(key, value); } /// public double Decrement(string key, double value) { return _memoryCache.Decrement(key, value); } /// public long Increment(string key, long value) { return _memoryCache.Increment(key, value); } /// public double Increment(string key, double value) { return _memoryCache.Increment(key, value); } #endregion 高级操作 #region 事务 /// public int Commit() { return _memoryCache.Commit(); } /// public IDisposable AcquireLock(string key, int msTimeout) { return _memoryCache.AcquireLock(key, msTimeout); } /// public IDisposable AcquireLock(string key, int msTimeout, int msExpire, bool throwOnFailure) { return _memoryCache.AcquireLock(key, msTimeout, msExpire, throwOnFailure); } #endregion 事务 }