using Microsoft.Extensions.Caching.Memory; using System; using System.Collections.Generic; using System.Text; namespace HaBookCms.RedisStorage.Cache { public class MemoryCacheService : ICacheService { protected IMemoryCache _cache; public MemoryCacheService(IMemoryCache cache) { _cache = cache; } public static MemoryCacheService Default { get; private set; } static MemoryCacheService() { Default = new MemoryCacheService(new MemoryCache(new MemoryCacheOptions())); } /// /// 是否存在此缓存 /// /// /// public bool Exists(string key) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); object v = null; return _cache.TryGetValue(key, out v); } ///         /// 取得缓存数据         ///         ///         ///         ///         public T GetCache(string key) where T : class { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); T v = null; _cache.TryGetValue(key, out v); return v; }         ///         /// 设置缓存         ///         ///         ///         public void SetCache(string key, object value) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); if (value == null) throw new ArgumentNullException(nameof(value)); object v = null; if (_cache.TryGetValue(key, out v)) _cache.Remove(key); _cache.Set(key, value); }         ///         /// 设置缓存,绝对过期         ///         ///         ///         /// 间隔分钟         /// MemoryCacheService.Default.SetCache("test", "RedisCache works!", 30);         public void SetCache(string key, object value, double expirationMinute) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); if (value == null) throw new ArgumentNullException(nameof(value)); object v = null; if (_cache.TryGetValue(key, out v)) _cache.Remove(key); DateTime now = DateTime.Now; TimeSpan ts = now.AddMinutes(expirationMinute) - DateTime.Now; _cache.Set(key, value, ts); }         ///         /// 设置缓存,绝对过期         ///         ///         ///         /// DateTimeOffset 结束时间         /// MemoryCacheService.Default.SetCache("test", "RedisCache works!", DateTimeOffset.Now.AddSeconds(30));         public void SetCache(string key, object value, DateTimeOffset expirationTime) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); if (value == null) throw new ArgumentNullException(nameof(value)); object v = null; if (_cache.TryGetValue(key, out v)) _cache.Remove(key); _cache.Set(key, value, expirationTime); }         ///         /// 设置缓存,相对过期时间         ///         ///         ///         ///         /// MemoryCacheService.Default.SetCache("test", "MemoryCache works!",TimeSpan.FromSeconds(30));         public void SetSlidingCache(string key, object value, TimeSpan t) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); if (value == null) throw new ArgumentNullException(nameof(value)); object v = null; if (_cache.TryGetValue(key, out v)) _cache.Remove(key); _cache.Set(key, value, new MemoryCacheEntryOptions() { SlidingExpiration = t }); }         ///         /// 移除缓存         ///         ///         public void RemoveCache(string key) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); _cache.Remove(key); }         ///         /// 释放         ///         public void Dispose() { if (_cache != null) _cache.Dispose(); GC.SuppressFinalize(this); } } }