using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using EntrustSettle.Common.Const; using Microsoft.Extensions.Caching.Distributed; using Newtonsoft.Json; namespace EntrustSettle.Common.Caches; public class Caching : ICaching { private readonly IDistributedCache _cache; public Caching(IDistributedCache cache) { _cache = cache; } private byte[] GetBytes(T source) { return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(source)); } public IDistributedCache Cache => _cache; public void AddCacheKey(string cacheKey) { var res = _cache.GetString(CacheConst.KeyAll); var allkeys = string.IsNullOrWhiteSpace(res) ? new List() : JsonConvert.DeserializeObject>(res); if (!allkeys.Any(m => m == cacheKey)) { allkeys.Add(cacheKey); _cache.SetString(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys)); } } /// /// 增加缓存Key /// /// /// public async Task AddCacheKeyAsync(string cacheKey) { var res = await _cache.GetStringAsync(CacheConst.KeyAll); var allkeys = string.IsNullOrWhiteSpace(res) ? new List() : JsonConvert.DeserializeObject>(res); if (!allkeys.Any(m => m == cacheKey)) { allkeys.Add(cacheKey); await _cache.SetStringAsync(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys)); } } public void DelByPattern(string key) { var allkeys = GetAllCacheKeys(); if (allkeys == null) return; var delAllkeys = allkeys.Where(u => u.Contains(key)).ToList(); delAllkeys.ForEach(u => { _cache.Remove(u); }); // 更新所有缓存键 allkeys = allkeys.Where(u => !u.Contains(key)).ToList(); _cache.SetString(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys)); } /// /// 删除某特征关键字缓存 /// /// /// public async Task DelByPatternAsync(string key) { var allkeys = await GetAllCacheKeysAsync(); if (allkeys == null) return; var delAllkeys = allkeys.Where(u => u.Contains(key)).ToList(); delAllkeys.ForEach(u => { _cache.Remove(u); }); // 更新所有缓存键 allkeys = allkeys.Where(u => !u.Contains(key)).ToList(); await _cache.SetStringAsync(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys)); } /// /// 删除缓存(从缓存中删除Key,而不是直接删除Key)如果要删除Key,使用 /// public void DelCacheKey(string cacheKey) { var res = _cache.GetString(CacheConst.KeyAll); var allkeys = string.IsNullOrWhiteSpace(res) ? new List() : JsonConvert.DeserializeObject>(res); if (allkeys.Any(m => m == cacheKey)) { allkeys.Remove(cacheKey); _cache.SetString(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys)); } } /// /// 删除缓存(从缓存中删除Key,而不是直接删除Key)如果要删除Key,使用 /// public async Task DelCacheKeyAsync(string cacheKey) { var res = await _cache.GetStringAsync(CacheConst.KeyAll); var allkeys = string.IsNullOrWhiteSpace(res) ? new List() : JsonConvert.DeserializeObject>(res); if (allkeys.Any(m => m == cacheKey)) { allkeys.Remove(cacheKey); await _cache.SetStringAsync(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys)); } } public bool Exists(string cacheKey) { var res = _cache.Get(cacheKey); return res != null; } /// /// 检查给定 key 是否存在 /// /// 键 /// public async Task ExistsAsync(string cacheKey) { var res = await _cache.GetAsync(cacheKey); return res != null; } public List GetAllCacheKeys() { var res = _cache.GetString(CacheConst.KeyAll); return string.IsNullOrWhiteSpace(res) ? null : JsonConvert.DeserializeObject>(res); } /// /// 获取所有缓存列表 /// /// public async Task> GetAllCacheKeysAsync() { var res = await _cache.GetStringAsync(CacheConst.KeyAll); return string.IsNullOrWhiteSpace(res) ? null : JsonConvert.DeserializeObject>(res); } public T Get(string cacheKey) { var res = _cache.Get(cacheKey); return res == null ? default : JsonConvert.DeserializeObject(Encoding.UTF8.GetString(res)); } /// /// 获取缓存 /// /// /// /// public async Task GetAsync(string cacheKey) { var res = await _cache.GetAsync(cacheKey); return res == null ? default : JsonConvert.DeserializeObject(Encoding.UTF8.GetString(res)); } public object Get(Type type, string cacheKey) { var res = _cache.Get(cacheKey); return res == null ? default : JsonConvert.DeserializeObject(Encoding.UTF8.GetString(res), type); } public async Task GetAsync(Type type, string cacheKey) { var res = await _cache.GetAsync(cacheKey); return res == null ? default : JsonConvert.DeserializeObject(Encoding.UTF8.GetString(res), type); } public string GetString(string cacheKey) { return _cache.GetString(cacheKey); } /// /// 获取缓存 /// /// /// public async Task GetStringAsync(string cacheKey) { return await _cache.GetStringAsync(cacheKey); } /// /// 删除Key /// public void Remove(string key) { _cache.Remove(key); DelCacheKey(key); } /// /// 删除Key /// public async Task RemoveAsync(string key) { await _cache.RemoveAsync(key); await DelCacheKeyAsync(key); } public void RemoveAll() { var catches = GetAllCacheKeys(); foreach (var @catch in catches) Remove(@catch); catches.Clear(); _cache.SetString(CacheConst.KeyAll, JsonConvert.SerializeObject(catches)); } public async Task RemoveAllAsync() { var catches = await GetAllCacheKeysAsync(); foreach (var @catch in catches) await RemoveAsync(@catch); catches.Clear(); await _cache.SetStringAsync(CacheConst.KeyAll, JsonConvert.SerializeObject(catches)); } public void Set(string cacheKey, T value, TimeSpan? expire = null) { _cache.Set(cacheKey, GetBytes(value), expire == null ? new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(6)} : new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = expire}); AddCacheKey(cacheKey); } /// /// 增加对象缓存 /// /// /// /// public async Task SetAsync(string cacheKey, T value) { await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)), new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(6)}); await AddCacheKeyAsync(cacheKey); } /// /// 增加对象缓存,并设置过期时间 /// /// /// /// /// public async Task SetAsync(string cacheKey, T value, TimeSpan expire) { await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)), new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = expire}); await AddCacheKeyAsync(cacheKey); } public void SetPermanent(string cacheKey, T value) { _cache.Set(cacheKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value))); AddCacheKey(cacheKey); } public async Task SetPermanentAsync(string cacheKey, T value) { await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value))); await AddCacheKeyAsync(cacheKey); } public void SetString(string cacheKey, string value, TimeSpan? expire = null) { if (expire == null) _cache.SetString(cacheKey, value, new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(6)}); else _cache.SetString(cacheKey, value, new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = expire}); AddCacheKey(cacheKey); } /// /// 增加字符串缓存 /// /// /// /// public async Task SetStringAsync(string cacheKey, string value) { await _cache.SetStringAsync(cacheKey, value, new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(6)}); await AddCacheKeyAsync(cacheKey); } /// /// 增加字符串缓存,并设置过期时间 /// /// /// /// /// public async Task SetStringAsync(string cacheKey, string value, TimeSpan expire) { await _cache.SetStringAsync(cacheKey, value, new DistributedCacheEntryOptions() {AbsoluteExpirationRelativeToNow = expire}); await AddCacheKeyAsync(cacheKey); } /// /// 根据父键清空 /// /// /// public async Task DelByParentKeyAsync(string key) { var allkeys = await GetAllCacheKeysAsync(); if (allkeys == null) return; var delAllkeys = allkeys.Where(u => u.StartsWith(key)).ToList(); delAllkeys.ForEach(Remove); // 更新所有缓存键 allkeys = allkeys.Where(u => !u.StartsWith(key)).ToList(); await SetStringAsync(CacheConst.KeyAll, JsonConvert.SerializeObject(allkeys)); } }