diff --git a/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs b/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs
index 550d7337..7f2e57db 100644
--- a/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs
+++ b/src/blazor/admin/BootstrapAdmin.Caching/CacheManager.cs
@@ -17,11 +17,10 @@ public static class CacheManager
///
///
///
- ///
///
- public static TItem GetOrAdd(string key, Func valueFactory, IChangeToken? token = null)
+ public static TItem GetOrAdd(string key, Func valueFactory)
{
- return Cache.GetOrAdd(key, valueFactory, token);
+ return Cache.GetOrAdd(key, valueFactory);
}
///
diff --git a/src/blazor/admin/BootstrapAdmin.Caching/ICacheManager.cs b/src/blazor/admin/BootstrapAdmin.Caching/ICacheManager.cs
index 90806355..0e2fd392 100644
--- a/src/blazor/admin/BootstrapAdmin.Caching/ICacheManager.cs
+++ b/src/blazor/admin/BootstrapAdmin.Caching/ICacheManager.cs
@@ -18,9 +18,8 @@ public interface ICacheManager
///
///
///
- ///
///
- T GetOrAdd(string key, Func factory, IChangeToken? token = null);
+ T GetOrAdd(string key, Func factory);
///
///
diff --git a/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs b/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs
index 8c089b05..ad691f38 100644
--- a/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs
+++ b/src/blazor/admin/BootstrapAdmin.Caching/Services/DefaultCacheManager.cs
@@ -55,6 +55,12 @@ class DefaultCacheManager : ICacheManager
entry.AddExpirationToken(token);
}
+ // 内置缓存策略 缓存相对时间 10 分钟
+ if (entry.AbsoluteExpiration == null && entry.SlidingExpiration == null && !entry.ExpirationTokens.Any())
+ {
+ entry.SlidingExpiration = TimeSpan.FromMinutes(10);
+ }
+
entry.RegisterPostEvictionCallback((key, value, reason, state) =>
{
diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs
index 4320abe0..1557372b 100644
--- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs
+++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/GroupService.cs
@@ -1,11 +1,23 @@
-using BootstrapAdmin.DataAccess.Models;
+using BootstrapAdmin.Caching;
+using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
+using Microsoft.Extensions.Primitives;
using PetaPoco;
namespace BootstrapAdmin.DataAccess.PetaPoco.Services;
-class GroupService : IGroup
+class GroupService : IGroup, IDisposable
{
+ private const string GroupServiceGetAllCacheKey = "GroupService-GetAll";
+
+ private const string GroupServiceGetGroupsByUserIdCacheKey = "GroupService-GetGroupsByUserId";
+
+ private const string GroupServiceGetGroupsByRoleIdCacheKey = "GroupService-GetGroupsByRoleId";
+
+ private CancellationTokenSource? GetGroupsByUserIdCancellationTokenSource { get; set; }
+
+ private CancellationTokenSource? GetGroupsByRoleIdCancellationTokenSource { get; set; }
+
private IDatabase Database { get; }
///
@@ -18,14 +30,20 @@ class GroupService : IGroup
///
///
///
- public List GetAll() => Database.Fetch();
+ public List GetAll() => CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => Database.Fetch());
///
///
///
///
///
- public List GetGroupsByUserId(string? userId) => Database.Fetch("select GroupID from UserGroup where UserID = @0", userId);
+ public List GetGroupsByUserId(string? userId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByUserIdCacheKey}-{userId}", entry =>
+ {
+ GetGroupsByUserIdCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(10));
+ var token = new CancellationChangeToken(GetGroupsByUserIdCancellationTokenSource.Token);
+ entry.ExpirationTokens.Add(token);
+ return Database.Fetch("select GroupID from UserGroup where UserID = @0", userId);
+ });
///
///
@@ -49,6 +67,11 @@ class GroupService : IGroup
Database.AbortTransaction();
throw;
}
+
+ if (ret)
+ {
+ GetGroupsByUserIdCancellationTokenSource?.Cancel();
+ }
return ret;
}
@@ -57,7 +80,13 @@ class GroupService : IGroup
///
///
///
- public List GetGroupsByRoleId(string? roleId) => Database.Fetch("select GroupID from RoleGroup where RoleID = @0", roleId);
+ public List GetGroupsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByRoleIdCacheKey}-{roleId}", entry =>
+ {
+ GetGroupsByRoleIdCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(10));
+ var token = new CancellationChangeToken(GetGroupsByRoleIdCancellationTokenSource.Token);
+ entry.ExpirationTokens.Add(token);
+ return Database.Fetch("select GroupID from RoleGroup where RoleID = @0", roleId);
+ });
///
///
@@ -81,6 +110,33 @@ class GroupService : IGroup
Database.AbortTransaction();
throw;
}
+
+ if (ret)
+ {
+ // reset cache
+ GetGroupsByRoleIdCancellationTokenSource?.Cancel();
+ }
return ret;
}
+
+ private void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ GetGroupsByRoleIdCancellationTokenSource?.Cancel();
+ GetGroupsByRoleIdCancellationTokenSource?.Dispose();
+
+ GetGroupsByUserIdCancellationTokenSource?.Cancel();
+ GetGroupsByUserIdCancellationTokenSource?.Dispose();
+ }
+ }
+
+ ///
+ ///
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
}