From e7fe909fd039668aedcfa43bfc8fcae1ed189d35 Mon Sep 17 00:00:00 2001 From: "YUN-PC5\\user" Date: Sun, 8 Oct 2023 16:36:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99SqlSugar=E4=BA=8C=E7=BA=A7?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=A8=A1=E7=B3=8A=E5=88=A0=E9=99=A4=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=96=B9=E6=B3=95=E8=A6=86=E7=9B=96ICacheService?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=B1=BBSqlSugarCache=E4=B8=AD=E7=9A=84GetAl?= =?UTF-8?q?lKey=E5=92=8CRemove=E6=96=B9=E6=B3=95=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=90=AB=E6=9C=89key=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=E9=97=AE=E9=A2=98=EF=BC=8Cipratelim?= =?UTF-8?q?it=E6=8E=A5=E5=85=A5=E5=88=86=E5=B8=83=E5=BC=8F=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/Cache/RedisServer.cs | 5 ++--- Infrastructure/WebExtensions/IPRateExtension.cs | 4 ++-- ZR.Admin.WebApi/Program.cs | 4 +--- ZR.CodeGenerator/ZR.CodeGenerator.csproj | 2 +- ZR.Model/ZR.Model.csproj | 2 +- ZR.Repository/ZR.Repository.csproj | 2 +- ZR.ServiceCore/SqlSugar/SqlSugarCache.cs | 15 +++++++++------ .../{SqlsugarSetup.cs => SqlSugarSetup.cs} | 13 +++++++++++-- 8 files changed, 28 insertions(+), 19 deletions(-) rename ZR.ServiceCore/SqlSugar/{SqlsugarSetup.cs => SqlSugarSetup.cs} (95%) diff --git a/Infrastructure/Cache/RedisServer.cs b/Infrastructure/Cache/RedisServer.cs index a44c178..53d3357 100644 --- a/Infrastructure/Cache/RedisServer.cs +++ b/Infrastructure/Cache/RedisServer.cs @@ -1,8 +1,7 @@ using CSRedis; using Infrastructure; -using Microsoft.Extensions.Caching.Distributed; -namespace ZR.Common.Cache +namespace ZR.Infrastructure.Cache { public class RedisServer { @@ -13,7 +12,7 @@ namespace ZR.Common.Cache { Cache = new CSRedisClient(AppSettings.GetConfig("RedisServer:Cache")); Session = new CSRedisClient(AppSettings.GetConfig("RedisServer:Session")); - RedisHelper.Initialization(Cache); + RedisHelper.Initialization(Session); } } } diff --git a/Infrastructure/WebExtensions/IPRateExtension.cs b/Infrastructure/WebExtensions/IPRateExtension.cs index 2eccadf..71d3792 100644 --- a/Infrastructure/WebExtensions/IPRateExtension.cs +++ b/Infrastructure/WebExtensions/IPRateExtension.cs @@ -17,8 +17,8 @@ namespace ZR.Infrastructure.WebExtensions //从appsettings.json中加载Ip规则 services.Configure(configuration.GetSection("IpRateLimitPolicies")); //注入计数器和规则存储 - services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); //配置(解析器、计数器密钥生成器) services.AddSingleton(); services.AddSingleton(); diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index 5558e1c..d636823 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -1,4 +1,3 @@ -using System.Reflection; using AspNetCoreRateLimit; using Infrastructure.Converter; using Microsoft.AspNetCore.DataProtection; @@ -8,9 +7,8 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Redis; using Microsoft.Extensions.Options; using Newtonsoft.Json; -using Quartz; using ZR.Admin.WebApi.Extensions; -using ZR.Common.Cache; +using ZR.Infrastructure.Cache; using ZR.Infrastructure.Resolver; using ZR.Infrastructure.WebExtensions; using ZR.ServiceCore.Model; diff --git a/ZR.CodeGenerator/ZR.CodeGenerator.csproj b/ZR.CodeGenerator/ZR.CodeGenerator.csproj index 620ceae..dba0753 100644 --- a/ZR.CodeGenerator/ZR.CodeGenerator.csproj +++ b/ZR.CodeGenerator/ZR.CodeGenerator.csproj @@ -12,6 +12,6 @@ - + diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj index 22b5ee1..8aafae9 100644 --- a/ZR.Model/ZR.Model.csproj +++ b/ZR.Model/ZR.Model.csproj @@ -9,7 +9,7 @@ - + diff --git a/ZR.Repository/ZR.Repository.csproj b/ZR.Repository/ZR.Repository.csproj index d5e0de2..0f43c8c 100644 --- a/ZR.Repository/ZR.Repository.csproj +++ b/ZR.Repository/ZR.Repository.csproj @@ -13,6 +13,6 @@ - + diff --git a/ZR.ServiceCore/SqlSugar/SqlSugarCache.cs b/ZR.ServiceCore/SqlSugar/SqlSugarCache.cs index f9d5e4a..16ce939 100644 --- a/ZR.ServiceCore/SqlSugar/SqlSugarCache.cs +++ b/ZR.ServiceCore/SqlSugar/SqlSugarCache.cs @@ -1,5 +1,4 @@ -using ZR.Common; -using ZR.Common.Cache; +using ZR.Infrastructure.Cache; namespace ZR.ServiceCore.SqlSugar { @@ -31,7 +30,10 @@ namespace ZR.ServiceCore.SqlSugar public IEnumerable GetAllKey() { - return RedisServer.Cache.Keys("*"); + var keys = RedisServer.Cache.Keys("cache:SqlSugarDataCache.*"); + // return RedisServer.Cache.Keys("cache:SqlSugarDataCache.*"); + keys = keys.Select(it => it.Replace("cache:", "")).ToArray(); + return keys; // return CacheHelper.GetCacheKeys(); } @@ -51,15 +53,16 @@ namespace ZR.ServiceCore.SqlSugar } else { - var restul = create(); + var result = create(); - Add(cacheKey, restul, cacheDurationInSeconds); - return restul; + Add(cacheKey, result, cacheDurationInSeconds); + return result; } } public void Remove(string key) { + // key = key.Split("cache:")[1]; RedisServer.Cache.Del(key); // CacheHelper.Remove(key); } diff --git a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs b/ZR.ServiceCore/SqlSugar/SqlSugarSetup.cs similarity index 95% rename from ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs rename to ZR.ServiceCore/SqlSugar/SqlSugarSetup.cs index c161d07..fea608a 100644 --- a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs +++ b/ZR.ServiceCore/SqlSugar/SqlSugarSetup.cs @@ -4,12 +4,12 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SqlSugar.IOC; -using ZR.Model.System; +using ZR.Infrastructure.Cache; using ZR.ServiceCore.Model; namespace ZR.ServiceCore.SqlSugar { - public static class SqlsugarSetup + public static class SqlSugarSetup { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); @@ -56,6 +56,15 @@ namespace ZR.ServiceCore.SqlSugar { InitTable.InitDb(); } + + StaticConfig.CacheRemoveByLikeStringFunc = (service, key) => + { + var keyList = RedisServer.Cache.Keys("cache:SqlSugarDataCache" + key + "*"); + foreach (var item in keyList) + { + RedisServer.Cache.Del(item.Split("cache:")[1]); + } + }; } ///