diff --git a/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs b/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs index ad691f38..348dbd51 100644 --- a/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs +++ b/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs @@ -27,11 +27,10 @@ class DefaultCacheManager : ICacheManager /// /// /// - /// /// - public T GetOrAdd(string key, Func factory, IChangeToken? token = null) => Cache.GetOrCreate(key, entry => + public T GetOrAdd(string key, Func factory) => Cache.GetOrCreate(key, entry => { - HandlerEntry(key, entry, token); + HandlerEntry(key, entry); return factory(entry); }); diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs index 3e07f49d..18a2704d 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/RoleService.cs @@ -1,4 +1,5 @@ -using BootstrapAdmin.DataAccess.Models; +using BootstrapAdmin.Caching; +using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.Web.Core; using PetaPoco; @@ -6,6 +7,16 @@ namespace BootstrapAdmin.DataAccess.PetaPoco.Services; class RoleService : IRole { + private const string RoleServiceGetAllCacheKey = "RoleService-GetAll"; + + private const string RoleServiceGetRolesByUserIdCacheKey = "RoleService-GetRolesByUserId"; + + private const string RoleServiceGetRolesByGroupIdCacheKey = "RoleService-GetRolesByGroupId"; + + private CancellationTokenSource? GetRolesByUserIdCancellationTokenSource { get; set; } + + private CancellationTokenSource? GetRolesByGroupIdCancellationTokenSource { get; set; } + private IDatabase Database { get; } /// @@ -18,14 +29,14 @@ class RoleService : IRole /// /// /// - public List GetAll() => Database.Fetch(); + public List GetAll() => CacheManager.GetOrAdd(RoleServiceGetAllCacheKey, entry => Database.Fetch()); /// /// /// /// /// - public List GetRolesByGroupId(string? groupId) => Database.Fetch("select RoleID from RoleGroup where GroupID = @0", groupId); + public List GetRolesByGroupId(string? groupId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByGroupIdCacheKey}-{groupId}", entry => Database.Fetch("select RoleID from RoleGroup where GroupID = @0", groupId)); /// /// @@ -57,7 +68,7 @@ class RoleService : IRole /// /// /// - public List GetRolesByUserId(string? userId) => Database.Fetch("select RoleID from UserRole where UserID = @0", userId); + public List GetRolesByUserId(string? userId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByUserIdCacheKey}-{userId}", entry => Database.Fetch("select RoleID from UserRole where UserID = @0", userId)); /// /// @@ -84,7 +95,7 @@ class RoleService : IRole return ret; } - public List GetRolesByMenuId(string? menuId) => Database.Fetch("select RoleID from NavigationRole where NavigationID = @0", menuId); + public List GetRolesByMenuId(string? menuId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByUserIdCacheKey}-{menuId}", entry => Database.Fetch("select RoleID from NavigationRole where NavigationID = @0", menuId)); public bool SaveRolesByMenuId(string? menuId, IEnumerable roleIds) { diff --git a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs index 9f817f49..8bc19d29 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/ServiceCollectionExtensions.cs @@ -28,9 +28,6 @@ namespace Microsoft.Extensions.DependencyInjection services.AddTaskServices(); services.AddHostedService(); - // 增加 缓存管理服务 - services.AddCacheManager(); - // 增加 健康检查服务 services.AddAdminHealthChecks();