From 288eedf0b4bb15efa9a8755eaed3b3a277136c39 Mon Sep 17 00:00:00 2001 From: LemonNoCry Date: Fri, 2 Aug 2024 10:08:39 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=F0=9F=8E=A8=20Caching=E4=B8=AD?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C=E8=AD=A6=E5=91=8A,?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=84=8F=E5=A4=96=E6=B3=A8=E5=86=8CIMemoryCa?= =?UTF-8?q?che?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Blog.Core.Common/Caches/Caching.cs | 27 +++++++++++++------ .../Extensions/UntilExtensions.cs | 5 ++++ .../ServiceExtensions/CacheSetup.cs | 2 +- .../IpPolicyRateLimitSetup.cs | 9 ++----- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Blog.Core.Common/Caches/Caching.cs b/Blog.Core.Common/Caches/Caching.cs index 6252d6f..61ff91f 100644 --- a/Blog.Core.Common/Caches/Caching.cs +++ b/Blog.Core.Common/Caches/Caching.cs @@ -1,12 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Concurrent; using System.Text; -using System.Threading.Tasks; using Blog.Core.Common.Caches.Interface; -using Blog.Core.Common.Const; +using Blog.Core.Common.Extensions; using Blog.Core.Common.Option; using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using StackExchange.Redis; @@ -14,12 +13,14 @@ using StackExchange.Redis; namespace Blog.Core.Common.Caches; public class Caching( + ILogger logger, IDistributedCache cache, IOptions redisOptions) : ICaching { + private static readonly ConcurrentDictionary _loggedWarnings = new(); private readonly RedisOptions _redisOptions = redisOptions.Value; - + private const string WarningMessage = "注入的缓存服务不是MemoryCacheManager,请检查注册配置,无法获取所有KEY"; public IDistributedCache Cache => cache; public void DelByPattern(string key) @@ -74,8 +75,18 @@ public class Caching( return keys.Select(u => u.ToString()).ToList(); } - var manage = App.GetService(false); - return manage.GetAllKeys().ToList(); + var memoryCache = App.GetService(); + if (memoryCache is not MemoryCacheManager memoryCacheManager) + { + if (_loggedWarnings.TryAdd(WarningMessage, true)) + { + logger.LogWarning(WarningMessage); + } + + return []; + } + + return memoryCacheManager.GetAllKeys().WhereIf(!key.IsNullOrEmpty(), s => s.StartsWith(key!)).ToList(); } public T Get(string cacheKey) diff --git a/Blog.Core.Common/Extensions/UntilExtensions.cs b/Blog.Core.Common/Extensions/UntilExtensions.cs index 1ae503b..efb17dc 100644 --- a/Blog.Core.Common/Extensions/UntilExtensions.cs +++ b/Blog.Core.Common/Extensions/UntilExtensions.cs @@ -15,4 +15,9 @@ public static class UntilExtensions dic.Add(key, value); } } + + public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate) + { + return condition ? source.Where(predicate) : source; + } } \ No newline at end of file diff --git a/Blog.Core.Extensions/ServiceExtensions/CacheSetup.cs b/Blog.Core.Extensions/ServiceExtensions/CacheSetup.cs index 163c1c5..c7b76f5 100644 --- a/Blog.Core.Extensions/ServiceExtensions/CacheSetup.cs +++ b/Blog.Core.Extensions/ServiceExtensions/CacheSetup.cs @@ -48,7 +48,7 @@ public static class CacheSetup services.AddSingleton(); services.AddSingleton(provider => provider.GetService()); services.AddOptions(); - services.TryAdd(ServiceDescriptor.Singleton()); + services.AddSingleton(); } services.AddSingleton(); diff --git a/Blog.Core.Extensions/ServiceExtensions/IpPolicyRateLimitSetup.cs b/Blog.Core.Extensions/ServiceExtensions/IpPolicyRateLimitSetup.cs index 54562c5..c92c077 100644 --- a/Blog.Core.Extensions/ServiceExtensions/IpPolicyRateLimitSetup.cs +++ b/Blog.Core.Extensions/ServiceExtensions/IpPolicyRateLimitSetup.cs @@ -1,5 +1,6 @@ using AspNetCoreRateLimit; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -13,15 +14,9 @@ namespace Blog.Core.Extensions public static void AddIpPolicyRateLimitSetup(this IServiceCollection services, IConfiguration Configuration) { if (services == null) throw new ArgumentNullException(nameof(services)); - - // CacheSetup unified register - // services.AddMemoryCache(); + //load general configuration from appsettings.json services.Configure(Configuration.GetSection("IpRateLimiting")); - // inject counter and rules stores - // services.AddSingleton(); - // services.AddSingleton(); - // services.AddSingleton(); // inject counter and rules distributed cache stores services.AddSingleton();