diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index 6e5a7d5..4f0441d 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -43,6 +43,7 @@ namespace ZR.Admin.WebApi.Extensions //...增加其他数据库 }; SugarIocServices.AddSqlSugar(iocList); + ICacheService cache = new SqlSugarCache(); SugarIocServices.ConfigurationSugar(db => { //db0数据过滤 @@ -50,12 +51,12 @@ namespace ZR.Admin.WebApi.Extensions iocList.ForEach(iocConfig => { - SetSugarAop(db, iocConfig); + SetSugarAop(db, iocConfig, cache); }); }); } - private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig) + private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache) { var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig; @@ -76,6 +77,15 @@ namespace ZR.Admin.WebApi.Extensions Console.ForegroundColor = ConsoleColor.Red; logger.Error(e, $"执行SQL出错:{e.Message}"); }; + + db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() + { + IsAutoRemoveDataCache = true + }; + db.GetConnectionScope(configId).CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() + { + DataInfoCacheService = cache + }; } /// diff --git a/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs b/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs new file mode 100644 index 0000000..3e6ccda --- /dev/null +++ b/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs @@ -0,0 +1,66 @@ +using ZR.Common; + +namespace ZR.Admin.WebApi.Extensions +{ + public class SqlSugarCache : SqlSugar.ICacheService + { + public void Add(string key, V value) + { + //RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30)); + CacheHelper.SetCache(key, value); + } + + public void Add(string key, V value, int cacheDurationInSeconds) + { + //RedisServer.Cache.Set(key, value, cacheDurationInSeconds); + CacheHelper.SetCaches(key, value, cacheDurationInSeconds); + } + + public bool ContainsKey(string key) + { + //return RedisServer.Cache.Exists(key); + return CacheHelper.Exists(key); + } + + public V Get(string key) + { + //return RedisServer.Cache.Get(key); + return (V)CacheHelper.Get(key); + } + + public IEnumerable GetAllKey() + { + //return RedisServer.Cache.Keys("*"); + return CacheHelper.GetCacheKeys(); + } + + public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) + { + if (ContainsKey(cacheKey)) + { + var result = Get(cacheKey); + if (result == null) + { + return create(); + } + else + { + return result; + } + } + else + { + var restul = create(); + + Add(cacheKey, restul, cacheDurationInSeconds); + return restul; + } + } + + public void Remove(string key) + { + //RedisServer.Cache.Del(key); + CacheHelper.Remove(key); + } + } +} \ No newline at end of file diff --git a/ZR.Common/Cache/CacheHelper.cs b/ZR.Common/Cache/CacheHelper.cs index 7b1511a..5fa7d32 100644 --- a/ZR.Common/Cache/CacheHelper.cs +++ b/ZR.Common/Cache/CacheHelper.cs @@ -110,6 +110,18 @@ namespace ZR.Common Cache.Remove(key); } + /// + /// 验证缓存项是否存在 + /// + /// 缓存Key + /// + public static bool Exists(string key) + { + if (key == null) + throw new ArgumentNullException(nameof(key)); + return Cache.TryGetValue(key, out _); + } + /// /// 获取所有缓存键 @@ -118,13 +130,18 @@ namespace ZR.Common public static List GetCacheKeys() { const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; - var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache); + //var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache); + + //.net7需要这样写 + var coherentState = Cache.GetType().GetField("_coherentState", flags).GetValue(Cache); + var entries = coherentState.GetType().GetField("_entries", flags).GetValue(coherentState); + var keys = new List(); if (entries is not IDictionary cacheItems) return keys; foreach (DictionaryEntry cacheItem in cacheItems) { keys.Add(cacheItem.Key.ToString()); - Console.WriteLine(cacheItem.Key); + //Console.WriteLine("缓存key=" +cacheItem.Key); } return keys; } diff --git a/ZR.Service/System/SysDictDataService.cs b/ZR.Service/System/SysDictDataService.cs index 4105632..2d4796d 100644 --- a/ZR.Service/System/SysDictDataService.cs +++ b/ZR.Service/System/SysDictDataService.cs @@ -40,25 +40,25 @@ namespace ZR.Service.System public List SelectDictDataByType(string dictType) { string CK = $"SelectDictDataByType_{dictType}"; - if (CacheHelper.GetCache(CK) is not List list) - { - list = Queryable().Where(f => f.Status == "0" && f.DictType == dictType) - .OrderBy(it => it.DictSort) - .ToList(); - CacheHelper.SetCache(CK, list, 30); - } + + var list = Queryable() + .WithCache(CK, 60 * 10) + .Where(f => f.Status == "0" && f.DictType == dictType) + .OrderBy(it => it.DictSort) + .ToList(); + return list; } public List SelectDictDataByTypes(string[] dictTypes) { string CK = $"SelectDictDataByTypes_{dictTypes}"; - if (CacheHelper.GetCache(CK) is not List list) - { - list = Queryable().Where(f => f.Status == "0" && dictTypes.Contains(f.DictType)) - .OrderBy(it => it.DictSort) - .ToList(); - //CacheHelper.SetCache(CK, list, 30); - } + + var list = Queryable() + .WithCache(CK, 60 * 30) + .Where(f => f.Status == "0" && dictTypes.Contains(f.DictType)) + .OrderBy(it => it.DictSort) + .ToList(); + return list; } ///