namespace Admin.NET.Core.Service; /// /// 系统缓存服务 /// [ApiDescriptionSettings(Name = "系统缓存", Order = 190)] public class SysCacheService : ISysCacheService, IDynamicApiController, ISingleton { private readonly IDistributedCache _cache; public SysCacheService(IDistributedCache cache) { _cache = cache; } /// /// 获取所有缓存列表 /// /// [HttpGet("/sysCache/keyList")] public async Task> GetAllCacheKeys() { var res = await _cache.GetStringAsync(CacheConst.KeyAll); return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res); } /// /// 增加对象缓存 /// /// /// /// [HttpPost("/sysCache/addObject")] public async Task SetAsync(string cacheKey, object value) { await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JSON.Serialize(value))); await AddCacheKey(cacheKey); } /// /// 增加对象缓存,并设置过期时间 /// /// /// /// /// [HttpPost("/sysCache/addObject/expire")] public async Task SetAsync(string cacheKey, object value, TimeSpan expire) { await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JSON.Serialize(value)), new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = expire }); await AddCacheKey(cacheKey); } /// /// 增加字符串缓存 /// /// /// /// [HttpPost("/sysCache/addString")] public async Task SetStringAsync(string cacheKey, string value) { await _cache.SetStringAsync(cacheKey, value); await AddCacheKey(cacheKey); } /// /// 增加字符串缓存,并设置过期时间 /// /// /// /// /// [HttpPost("/sysCache/addString/expire")] public async Task SetStringAsync(string cacheKey, string value, TimeSpan expire) { await _cache.SetStringAsync(cacheKey, value, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = expire }); await AddCacheKey(cacheKey); } /// /// 获取缓存 /// /// /// [HttpGet("/sysCache/detail")] public async Task GetStringAsync(string cacheKey) { return await _cache.GetStringAsync(cacheKey); } /// /// 删除缓存 /// /// /// [HttpGet("/sysCache/remove")] public async Task RemoveAsync(string key) { await _cache.RemoveAsync(key); await DelCacheKey(key); } /// /// 删除某特征关键字缓存 /// /// /// [NonAction] public async Task DelByPatternAsync(string key) { var allkeys = await 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(); await _cache.SetStringAsync(CacheConst.KeyAll, JSON.Serialize(allkeys)); } /// /// 获取缓存 /// /// /// /// [NonAction] public async Task GetAsync(string cacheKey) { var res = await _cache.GetAsync(cacheKey); return res == null ? default : JSON.Deserialize(Encoding.UTF8.GetString(res)); } /// /// 检查给定 key 是否存在 /// /// 键 /// [NonAction] public async Task ExistsAsync(string cacheKey) { var res = await _cache.GetAsync(cacheKey); return res != null; } /// /// 增加缓存Key /// /// /// [NonAction] public async Task AddCacheKey(string cacheKey) { var res = await _cache.GetStringAsync(CacheConst.KeyAll); var allkeys = string.IsNullOrWhiteSpace(res) ? new List() : JSON.Deserialize>(res); if (!allkeys.Any(m => m == cacheKey)) { allkeys.Add(cacheKey); await _cache.SetStringAsync(CacheConst.KeyAll, JSON.Serialize(allkeys)); } } /// /// 删除缓存 /// /// /// [NonAction] public async Task DelCacheKey(string cacheKey) { var res = await _cache.GetStringAsync(CacheConst.KeyAll); var allkeys = string.IsNullOrWhiteSpace(res) ? new List() : JSON.Deserialize>(res); if (allkeys.Any(m => m == cacheKey)) { allkeys.Remove(cacheKey); await _cache.SetStringAsync(CacheConst.KeyAll, JSON.Serialize(allkeys)); } } /// /// 获取机构Id集合 /// /// /// [NonAction] public async Task> GetOrgIdList(long userId) { var cacheKey = CacheConst.KeyOrgIdList + userId; var res = await _cache.GetStringAsync(cacheKey); return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res); } /// /// 缓存机构Id集合 /// /// /// /// [NonAction] public async Task SetOrgIdList(long userId, List orgIdList) { var cacheKey = CacheConst.KeyOrgIdList + userId; await _cache.SetStringAsync(cacheKey, JSON.Serialize(orgIdList)); await AddCacheKey(cacheKey); } ///// ///// 获取菜单缓存 ///// ///// ///// ///// //[NonAction] //public async Task> GetMenu(long userId, string appCode) //{ // var cacheKey = CacheConst.KeyMenu + $"{userId}-{appCode}"; // var res = await _cache.GetStringAsync(cacheKey); // return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res); //} ///// ///// 缓存菜单 ///// ///// ///// ///// ///// //[NonAction] //public async Task SetMenu(long userId, string appCode, List menus) //{ // var cacheKey = CommonConst.CACHE_KEY_MENU + $"{userId}-{appCode}"; // await _cache.SetStringAsync(cacheKey, JSON.Serialize(menus)); // await AddCacheKey(cacheKey); //} /// /// 获取权限缓存(按钮) /// /// /// [NonAction] public async Task> GetPermission(long userId) { var cacheKey = CacheConst.KeyPermission + userId; var res = await _cache.GetStringAsync(cacheKey); return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res); } /// /// 缓存权限 /// /// /// /// [NonAction] public async Task SetPermission(long userId, List permissions) { var cacheKey = CacheConst.KeyPermission + userId; await _cache.SetStringAsync(cacheKey, JSON.Serialize(permissions)); await AddCacheKey(cacheKey); } /// /// 获取最大角色数据范围 /// /// /// [NonAction] public async Task GetMaxDataScopeType(long userId) { var cacheKey = CacheConst.KeyMaxDataScopeType + userId; var res = await _cache.GetStringAsync(cacheKey); return string.IsNullOrWhiteSpace(res) ? null : int.Parse(res); } /// /// 缓存最大角色数据范围 /// /// /// /// [NonAction] public async Task SetMaxDataScopeType(long userId, int dataScopeType) { var cacheKey = CacheConst.KeyMaxDataScopeType + userId; await _cache.SetStringAsync(cacheKey, dataScopeType.ToString()); await AddCacheKey(cacheKey); } /// /// 根据父键清空 /// /// /// [HttpGet("/sysCache/delByParentKey")] public async Task DelByParentKeyAsync(string key) { var allkeys = await GetAllCacheKeys(); if (allkeys == null) return; var delAllkeys = allkeys.Where(u => u.StartsWith(key)).ToList(); delAllkeys.ForEach(u => { _cache.Remove(u); }); // 更新所有缓存键 allkeys = allkeys.Where(u => !u.StartsWith(key)).ToList(); await _cache.SetStringAsync(CacheConst.KeyAll, JSON.Serialize(allkeys)); } }