diff --git a/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs b/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs index 25787f21..d9dcbbde 100644 --- a/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs +++ b/src/blazor/admin/BootStarpAdmin.DataAccess.FreeSql/Service/DictService.cs @@ -7,6 +7,8 @@ namespace BootStarpAdmin.DataAccess.FreeSql.Service; class DictService : IDict { + private const string DictServiceCacheKey = "DictService-GetAll"; + private IFreeSql FreeSql { get; } private string? AppId { get; set; } @@ -33,10 +35,11 @@ class DictService : IDict return ret; } - public List GetAll() - { - return FreeSql.Select().ToList(); - } + /// + /// + /// + /// + public List GetAll() => CacheManager.GetOrCreate>(DictServiceCacheKey, entry => FreeSql.Select().ToList()); public Dictionary GetApps() { @@ -122,7 +125,7 @@ class DictService : IDict if (ret) { // 更新缓存 - CacheManager.Remove(DictServiceCacheKey); + CacheManager.Clear(DictServiceCacheKey); } return ret; } diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/Extensions/ServiceCollectionExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/Extensions/ServiceCollectionExtensions.cs index 3e66a69a..b13ffc6f 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/Extensions/ServiceCollectionExtensions.cs @@ -16,8 +16,7 @@ public static class ServiceCollectionExtensions /// public static IServiceCollection AddCacheManager(this IServiceCollection services) { - services.AddMemoryCache(); - services.TryAddSingleton(DefaultCacheManager.Instance); + services.TryAddSingleton(provider => DefaultCacheManager.Instance); return services; } } diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/ICacheManager.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/ICacheManager.cs index 0d5d1914..6f23c3e8 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/ICacheManager.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/ICacheManager.cs @@ -20,6 +20,15 @@ public interface ICacheManager /// T GetOrCreate(string key, Func factory); + /// + /// + /// + /// + /// + /// + /// + Task GetOrCreateAsync(string key, Func> factory); + /// /// /// diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/Services/DefaultCacheManager.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/Services/DefaultCacheManager.cs index 279ae9be..ef211653 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/Services/DefaultCacheManager.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/Services/DefaultCacheManager.cs @@ -5,17 +5,58 @@ namespace BootstrapAdmin.Web.Core.Services; class DefaultCacheManager : ICacheManager { - private static readonly Lazy cache = new(() => new DefaultCacheManager()); + private IMemoryCache Cache { get; } - public static ICacheManager Instance { get; } = cache.Value; + private List Keys { get; } = new List(256); - public T GetOrCreate(string key, Func factory) + /// + /// + /// + public DefaultCacheManager() { - throw new NotImplementedException(); + Cache = new MemoryCache(new MemoryCacheOptions()); + } + + public T GetOrCreate(string key, Func factory) => Cache.GetOrCreate(key, entry => + { + HandlerEntry(key, entry); + return factory(entry); + }); + + /// + /// + /// + /// + /// + /// + /// + public Task GetOrCreateAsync(string key, Func> factory) => Cache.GetOrCreate(key, entry => + { + HandlerEntry(key, entry); + return factory(entry); + }); + + private void HandlerEntry(string key, ICacheEntry entry) + { + Keys.Add(key); + entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10)); + entry.RegisterPostEvictionCallback((key, value, reason, state) => + { + var k = key.ToString(); + if (!string.IsNullOrEmpty(k)) + { + Keys.Remove(k); + } + }); } public void Clear(string? key) { throw new NotImplementedException(); } + + #region 静态方法 + [NotNull] + internal static ICacheManager? Instance { get; } = new DefaultCacheManager(); + #endregion } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Healths.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Healths.razor.cs index af57b0af..f0642498 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Healths.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Admin/Healths.razor.cs @@ -33,6 +33,10 @@ public partial class Healths [NotNull] private DialogService? DialogService { get; set; } + [Inject] + [NotNull] + private BootstrapBlazor.Web.Core.ICacheManager? CacheManager { get; set; } + [NotNull] private HttpClient? Client { get; set; } @@ -49,8 +53,11 @@ public partial class Healths private async Task> OnQueryAsync(QueryPageOptions options) { - var payload = await Client.GetStringAsync("/Healths"); - var report = HealthCheckHelper.Parse(payload); + var report = await CacheManager.GetOrCreateAsync("Health", async entry => + { + var payload = await Client.GetStringAsync("/Healths"); + return HealthCheckHelper.Parse(payload); + }); var ret = new QueryData() {