// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! namespace Admin.NET.Core.Service; /// /// 系统缓存服务 🧩 /// [ApiDescriptionSettings(Order = 400, Description = "系统缓存")] public class SysCacheService : IDynamicApiController, ISingleton { private static ICacheProvider _cacheProvider; private readonly CacheOptions _cacheOptions; public SysCacheService( ICacheProvider cacheProvider, IOptions cacheOptions) { _cacheProvider = cacheProvider; _cacheOptions = cacheOptions.Value; } /// /// 申请分布式锁 🔖 /// /// 要锁定的key /// 申请锁等待的时间,单位毫秒 /// 锁过期时间,超过该时间没有主动是放则自动是放,必须整数秒,单位毫秒 /// 失败时是否抛出异常,如不抛出异常,可通过判断返回null得知申请锁失败 /// [DisplayName("申请分布式锁")] public IDisposable? BeginCacheLock(string key, int msTimeout = 500, int msExpire = 10000, bool throwOnFailure = true) { try { return _cacheProvider.Cache.AcquireLock(key, msTimeout, msExpire, throwOnFailure); } catch { return null; } } /// /// 获取缓存键名集合 🔖 /// /// [DisplayName("获取缓存键名集合")] public List GetKeyList() { return _cacheProvider.Cache == Cache.Default ? [.. _cacheProvider.Cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u)] : [.. ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u)]; } /// /// 增加缓存 /// /// /// /// [NonAction] public bool Set(string key, object value) { return !string.IsNullOrWhiteSpace(key) && _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value); } /// /// 增加缓存并设置过期时间 /// /// /// /// /// [NonAction] public bool Set(string key, object value, TimeSpan expire) { return !string.IsNullOrWhiteSpace(key) && _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value, expire); } public async Task AdGetAsync(String cacheName, Func> del, TimeSpan? expiry = default) where TR : class { return await AdGetAsync(cacheName, del, [], expiry); } public async Task AdGetAsync(String cacheName, Func> del, T1 t1, TimeSpan? expiry = default) where TR : class { return await AdGetAsync(cacheName, del, [t1], expiry); } public async Task AdGetAsync(String cacheName, Func> del, T1 t1, T2 t2, TimeSpan? expiry = default) where TR : class { return await AdGetAsync(cacheName, del, [t1, t2], expiry); } public async Task AdGetAsync(String cacheName, Func> del, T1 t1, T2 t2, T3 t3, TimeSpan? expiry = default) where TR : class { return await AdGetAsync(cacheName, del, [t1, t2, t3], expiry); } private async Task AdGetAsync(string cacheName, Delegate del, Object[] obs, TimeSpan? expiry) where T : class { var key = Key(cacheName, obs); // 使用分布式锁 using (_cacheProvider.Cache.AcquireLock($@"lock:AdGetAsync:{cacheName}", 1000)) { var value = Get(key); if (value == null) { value = await ((dynamic)del).DynamicInvoke(obs); _ = expiry == null ? Set(key, value) : Set(key, value, (TimeSpan)expiry); } return value; } } public T Get(String cacheName, object t1) { return Get(cacheName, [t1]); } public T Get(String cacheName, object t1, object t2) { return Get(cacheName, [t1, t2]); } public T Get(String cacheName, object t1, object t2, object t3) { return Get(cacheName, [t1, t2, t3]); } private T Get(String cacheName, Object[] obs) { var key = cacheName + ":" + obs.Aggregate(string.Empty, (current, o) => current + $"<{o}>"); return Get(key); } private static string Key(string cacheName, object[] obs) { if (obs.OfType().Any()) throw new Exception("缓存参数类型不能能是:TimeSpan类型"); StringBuilder sb = new(cacheName + ":"); foreach (var a in obs) sb.Append($"<{KeySingle(a)}>"); return sb.ToString(); } private static string KeySingle(object t) { return t.GetType().IsClass && !t.GetType().IsPrimitive ? JSON.Serialize(t) : t.ToString(); } /// /// 获取缓存的剩余生存时间 /// /// /// [NonAction] public static TimeSpan GetExpire(string key) { return _cacheProvider.Cache.GetExpire(key); } /// /// 获取缓存 /// /// /// /// [NonAction] public T Get(string key) { return _cacheProvider.Cache.Get($"{_cacheOptions.Prefix}{key}"); } /// /// 批量获取缓存值(普通键值结构)🔖 /// /// 值类型 /// 缓存键集合 /// 与键顺序对应的值列表 [NonAction] public List GetBatch(IEnumerable keys) { var prefixedKeys = keys.Select(k => $"{_cacheOptions.Prefix}{k}"); return prefixedKeys.Select(k => _cacheProvider.Cache.Get(k)).ToList(); } /// /// 批量设置缓存项(兼容现有键规则)❄️,方法只对雪花Id有效 /// /// 实体类型(需包含Id属性) /// 待缓存数据集合 /// 统一过期时间 /// 批次大小(默认500) [NonAction] public void SetList(IEnumerable items, TimeSpan? expire = null, int batchSize = 500) where T : class { if (items == null) return; var itemList = items.ToList(); if (itemList.Count == 0) return; // 获取雪花ID属性 var idProperty = typeof(T).GetProperty("Id") ?? throw new ArgumentException("实体必须包含Id属性"); // 分批次处理 foreach (var batch in itemList.Batch(batchSize)) { var dic = batch.ToDictionary( item => $"{_cacheOptions.Prefix}{idProperty.GetValue(item)}", item => item ); if (_cacheProvider.Cache is Redis redis) { // Redis管道批量设置 redis.StartPipeline(); try { foreach (var kv in dic) { redis.Set(kv.Key, kv.Value, expire ?? TimeSpan.Zero); } } finally { redis.StopPipeline(true); } } else { // 通用缓存实现 foreach (var kv in dic) { _cacheProvider.Cache.Set(kv.Key, kv.Value, expire ?? TimeSpan.Zero); } } } } /// /// 异步批量获取(当前为同步实现,未来可升级) /// 实体类型 /// 雪花ID集合 /// 数据加载方法 /// 是否缓存空值(防穿透) /// 空值缓存时间(默认永久) /// [NonAction] public async Task> GetListAsync(IEnumerable ids, Func, Task>> loadFromDb, bool cacheNull = true, TimeSpan? nullExpire = null) where T : class { var idList = ids.Distinct().ToList(); if (idList.Count == 0) return []; // 1. 批量获取缓存(保持同步,假设缓存操作快速) var cachedItems = GetFromCache(idList); // 2. 识别未命中ID var missedIds = new List(); var resultDict = new Dictionary(); for (int i = 0; i < idList.Count; i++) { if (cachedItems[i] != null) { resultDict[idList[i]] = cachedItems[i]; } else { missedIds.Add(idList[i]); } } // 3. 异步加载缺失数据 if (missedIds.Count > 0) { var dbItems = await loadFromDb(missedIds).ConfigureAwait(false); // 异步等待 var dbDict = dbItems.ToDictionary(GetId); // 4. 缓存回填 var toCache = new List(); foreach (var id in missedIds) { if (dbDict.TryGetValue(id, out var item)) { resultDict[id] = item; toCache.Add(item); } //else if (cacheNull) //{ // // 使用 default(T) 作为空值标记 // toCache.Add(default(T)); //} } if (toCache.Count > 0) SetList(toCache, cacheNull ? nullExpire : null); // 保持同步缓存写入 } // 5. 按原始顺序返回 return idList.Select(id => resultDict.TryGetValue(id, out var item) ? (item ?? null) : null).ToList(); } /// /// 批量获取(自动加载缺失数据+缓存回填)🔁 /// /// 实体类型 /// 雪花ID集合 /// 数据加载方法 /// 是否缓存空值(防穿透) /// 空值缓存时间(默认永久) [NonAction] public List GetList(IEnumerable ids, Func, List> loadFromDb, bool cacheNull = true, TimeSpan? nullExpire = null) where T : class { var idList = ids.Distinct().ToList(); if (idList.Count == 0) return []; // 1. 批量获取缓存 var cachedItems = GetFromCache(idList); // 2. 识别未命中ID var missedIds = new List(); var resultDict = new Dictionary(); for (int i = 0; i < idList.Count; i++) { if (cachedItems[i] != null) { resultDict[idList[i]] = cachedItems[i]; } else { missedIds.Add(idList[i]); } } // 3. 加载缺失数据 if (missedIds.Count > 0) { var dbItems = loadFromDb(missedIds); var dbDict = dbItems.ToDictionary(GetId); // 4. 缓存回填 var toCache = new List(); foreach (var id in missedIds) { if (dbDict.TryGetValue(id, out var item)) { resultDict[id] = item; toCache.Add(item); } //else if (cacheNull) //{ // // 缓存空值标记 // toCache.Add(default(T)); //} } //SetList(toCache, cacheNull ? (nullExpire ?? TimeSpan.FromMinutes(5)) : null); // 将默认过期时间改为null(一直存储) if (toCache.Count > 0) SetList(toCache, cacheNull ? nullExpire : null); } // 5. 按原始顺序返回 return idList.Select(id => resultDict.TryGetValue(id, out var item) ? (item ?? null) : null).ToList(); } private long GetId(T item) { var prop = typeof(T).GetProperty("Id"); return (long)prop.GetValue(item); } /// /// 基础方法:仅从缓存获取数据 /// [NonAction] public List GetFromCache(List ids) where T : class { if (ids == null || ids.Count == 0) return []; var keys = ids.Select(id => $"{_cacheOptions.Prefix}{id}").ToList(); if (_cacheProvider.Cache is FullRedis redis) { var result = redis.GetAll(keys); return keys.Select(k => result.TryGetValue(k, out var val) ? val : null).ToList(); } return keys.Select(k => _cacheProvider.Cache.Get(k)).ToList(); } /// /// 批量获取哈希缓存字段值(哈希结构)🔖 /// /// 值类型 /// 哈希键名 /// 要获取的字段集合 /// 与字段顺序对应的值列表 [NonAction] public List HashGetBatch(string key, IEnumerable fields) { var hash = GetHashMap($"{_cacheOptions.Prefix}{key}"); return fields.Select(f => hash.TryGetValue(f, out T val) ? val : default).ToList(); } /// /// 删除缓存 🔖 /// /// /// [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除缓存")] public int Remove(string key) { return _cacheProvider.Cache.Remove($"{_cacheOptions.Prefix}{key}"); } /// /// 清空所有缓存 🔖 /// /// [DisplayName("清空所有缓存")] [ApiDescriptionSettings(Name = "Clear"), HttpPost] public void Clear() { // 超管用户操作,清空所有缓存 if (LazyHelper.GetService().SuperAdmin) { _cacheProvider.Cache.Clear(); Cache.Default.Clear(); return; } // 排除非本租户、以及超管的Session缓存 var sysUserRep = App.GetService>(); var userIds = sysUserRep.AsQueryable().Where(u => u.AccountType != AccountTypeEnum.SuperAdmin).Select(u => u.Id).ToList().Select(u => u.ToString()).ToList(); var keys = _cacheProvider.Cache.Keys.Where(key => !key.StartsWith(CacheConst.KeyUserSession) || userIds.Any(key.EndsWith)).ToList(); keys.ForEach(key => _cacheProvider.Cache.Remove(key)); } /// /// 检查缓存是否存在 /// /// 键 /// [NonAction] public bool ExistKey(string key) { return _cacheProvider.Cache.ContainsKey($"{_cacheOptions.Prefix}{key}"); } /// /// 检查缓存是否不存在 /// /// 键 /// [NonAction] public bool NotExistKey(string key) { return !_cacheProvider.Cache.ContainsKey($"{_cacheOptions.Prefix}{key}"); } /// /// 根据键名前缀删除缓存 🔖 /// /// 键名前缀 /// [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost] [DisplayName("根据键名前缀删除缓存")] public int RemoveByPrefixKey(string prefixKey) { var delKeys = _cacheProvider.Cache == Cache.Default ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray() : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray(); return _cacheProvider.Cache.Remove(delKeys); } /// /// 根据键名前缀获取键名集合 🔖 /// /// 键名前缀 /// [DisplayName("根据键名前缀获取键名集合")] public List GetKeysByPrefixKey(string prefixKey) { return _cacheProvider.Cache == Cache.Default ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList() : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList(); } /// /// 获取缓存值 🔖 /// /// /// [DisplayName("获取缓存值")] public object GetValue(string key) { if (string.IsNullOrEmpty(key)) return null; // 若Key经过URL编码则进行解码 if (Regex.IsMatch(key, @"%[0-9a-fA-F]{2}")) key = HttpUtility.UrlDecode(key); var fullKey = $"{_cacheOptions.Prefix}{key}"; if (_cacheProvider.Cache == Cache.Default) return _cacheProvider.Cache.Get(fullKey); if (_cacheProvider.Cache is FullRedis redisCache) { if (!redisCache.ContainsKey(fullKey)) return null; try { var keyType = redisCache.TYPE(fullKey)?.ToLower(); switch (keyType) { case "string": return redisCache.Get(fullKey); case "list": var list = redisCache.GetList(fullKey); return list?.ToList(); case "hash": var hash = redisCache.GetDictionary(fullKey); return hash?.ToDictionary(k => k.Key, v => v.Value); case "set": var set = redisCache.GetSet(fullKey); return set?.ToArray(); case "zset": var sortedSet = redisCache.GetSortedSet(fullKey); return sortedSet?.Range(0, -1)?.ToList(); case "none": return null; default: // 未知类型或特殊类型 return new Dictionary { { "key", key }, { "type", keyType ?? "unknown" }, { "message", "无法使用标准方式获取此类型数据" } }; } } catch (Exception ex) { return new Dictionary { { "key", key }, { "error", ex.Message }, { "type", "exception" } }; } } return _cacheProvider.Cache.Get(fullKey); } /// /// 获取或添加缓存(在数据不存在时执行委托请求数据) /// /// /// /// /// 过期时间,单位秒 /// [NonAction] public T GetOrAdd(string key, Func callback, int expire = -1) { if (string.IsNullOrWhiteSpace(key)) return default; return _cacheProvider.Cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire); } /// /// Hash匹配 /// /// /// /// [NonAction] public static IDictionary GetHashMap(string key) { return _cacheProvider.Cache.GetDictionary(key); } /// /// 批量添加HASH /// /// /// /// /// [NonAction] public static bool HashSet(string key, Dictionary dic) { var hash = GetHashMap(key); foreach (var v in dic) { hash.Add(v); } return true; } /// /// 添加一条HASH /// /// /// /// /// [NonAction] public static void HashAdd(string key, string hashKey, T value) { var hash = GetHashMap(key); hash.Add(hashKey, value); } /// /// 添加或更新一条HASH /// /// /// /// /// [NonAction] public static void HashAddOrUpdate(string key, string hashKey, T value) { var hash = GetHashMap(key); if (hash.ContainsKey(hashKey)) hash[hashKey] = value; else hash.Add(hashKey, value); } /// /// 获取多条HASH /// /// /// /// /// [NonAction] public static List HashGet(string key, params string[] fields) { var hash = GetHashMap(key); return hash.Where(t => fields.Any(c => t.Key == c)).Select(t => t.Value).ToList(); } /// /// 获取一条HASH /// /// /// /// /// [NonAction] public static T HashGetOne(string key, string field) { var hash = GetHashMap(key); return hash.TryGetValue(field, out T value) ? value : default; } // 新增方法:获取哈希表所有键 public static List HashGetAllKeys(string key) { var hash = GetHashMap(key); // 假设值为任意类型 return hash.Keys.ToList(); } // 增强的哈希设置方法(带过期时间) public static bool HashSet(string key, Dictionary items, TimeSpan? expiry = null) { var hash = GetHashMap(key); foreach (var item in items) { hash[item.Key] = item.Value; } if (expiry.HasValue) { _cacheProvider.Cache.SetExpire(key, expiry.Value); } return true; } // 异步批量设置哈希,目前没有,先保留扩展 public static async Task HashSetAsync(string key, Dictionary items, TimeSpan? expiry = null) { var hash = GetHashMap(key); foreach (var item in items) { hash[item.Key] = item.Value; } if (expiry.HasValue) { _cacheProvider.Cache.SetExpire(key, expiry.Value); } await Task.CompletedTask; return true; } // 异步设置过期时间 /// /// 根据KEY获取所有HASH /// /// /// /// [NonAction] public static IDictionary HashGetAll(string key) { var hash = GetHashMap(key); return hash; } /// /// 删除HASH /// /// /// /// /// [NonAction] public static int HashDel(string key, params string[] fields) { var hash = GetHashMap(key); fields.ToList().ForEach(t => hash.Remove(t)); return fields.Length; } ///// ///// 搜索HASH ///// ///// ///// ///// ///// //[NonAction] //public List> HashSearch(string key, SearchModel searchModel) //{ // var hash = GetHashMap(key); // return hash.Search(searchModel).ToList(); //} ///// ///// 搜索HASH ///// ///// ///// ///// ///// ///// //[NonAction] //public List> HashSearch(string key, string pattern, int count) //{ // var hash = GetHashMap(key); // return hash.Search(pattern, count).ToList(); //} } public class CacheItem { public T Value { get; set; } public bool IsNull { get; set; } }