From 4035c96c836eb356d9d9a7894b9322704e6eaa2f Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Wed, 26 Jan 2022 20:47:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20ICacheManager=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapAdmin.Caching/CacheManager.cs | 9 ++++++++- .../Extensions/ServiceCollectionExtensions.cs | 12 +++++++++-- .../Services/DefaultCacheManager.cs | 20 +++---------------- .../ApplicationBuilderExtensions.cs | 3 +++ 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs b/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs index 388a3bdb..d19efa09 100644 --- a/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs +++ b/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs @@ -12,7 +12,14 @@ namespace BootstrapAdmin.Caching; /// public static class CacheManager { - private static ICacheManager Cache { get; } = DefaultCacheManager.Instance; + [NotNull] + private static ICacheManager? Cache { get; set; } + + /// + /// 由服务调用 + /// + /// + internal static void Init(ICacheManager cache) => Cache = cache; /// /// 获得或者新建数据 diff --git a/src/blazor/admin/BootstrapAdmin.Caching/Extensions/ServiceCollectionExtensions.cs b/src/blazor/admin/BootstrapAdmin.Caching/Extensions/ServiceCollectionExtensions.cs index 2484753c..6877c84b 100644 --- a/src/blazor/admin/BootstrapAdmin.Caching/Extensions/ServiceCollectionExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Caching/Extensions/ServiceCollectionExtensions.cs @@ -4,6 +4,7 @@ using BootstrapAdmin.Caching; using BootstrapAdmin.Caching.Services; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.Extensions.DependencyInjection; @@ -14,13 +15,20 @@ namespace Microsoft.Extensions.DependencyInjection; public static class ServiceCollectionExtensions { /// - /// + /// 注入 ICacheManager 服务 /// /// /// public static IServiceCollection AddCacheManager(this IServiceCollection services) { - services.TryAddSingleton(provider => DefaultCacheManager.Instance); + services.AddMemoryCache(); + services.TryAddSingleton(provider => + { + var cache = provider.GetRequiredService(); + var cacheManager = new DefaultCacheManager(cache); + CacheManager.Init(cacheManager); + return cacheManager; + }); return services; } } diff --git a/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs b/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs index f68c4c17..37a49ed2 100644 --- a/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs +++ b/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs @@ -10,20 +10,12 @@ namespace BootstrapAdmin.Caching.Services; class DefaultCacheManager : ICacheManager { [NotNull] - private MemoryCache? Cache { get; set; } + private IMemoryCache? Cache { get; set; } /// /// /// - private DefaultCacheManager() - { - Init(); - } - - private void Init() - { - Cache = new MemoryCache(new MemoryCacheOptions()); - } + public DefaultCacheManager(IMemoryCache cache) => Cache = cache; /// /// @@ -77,17 +69,11 @@ class DefaultCacheManager : ICacheManager { if (!string.IsNullOrEmpty(key)) { - // 通过 TokenManager 管理依赖 Cache.Remove(key); } else { - Cache.Compact(100); + //Cache.Compact(100); } } - - #region 静态方法 - [NotNull] - internal static ICacheManager? Instance { get; } = new DefaultCacheManager(); - #endregion } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs index a14b7fac..20e08505 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ApplicationBuilderExtensions.cs @@ -28,6 +28,9 @@ public static class ApplicationBuilderExtensions builder.UseSwagger(builder.Configuration["SwaggerPathBase"].TrimEnd('/')); + // 激活 ICacheManager + builder.Services.GetRequiredService(); + return builder; } }