refactor: 尝试使用 Token 解决依赖问题改造 CacheManager 接口
This commit is contained in:
parent
943c620c55
commit
c19cd5ca85
|
@ -17,11 +17,10 @@ public static class CacheManager
|
|||
/// <typeparam name="TItem"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="valueFactory"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public static TItem GetOrAdd<TItem>(string key, Func<ICacheEntry, TItem> valueFactory, IChangeToken? token = null)
|
||||
public static TItem GetOrAdd<TItem>(string key, Func<ICacheEntry, TItem> valueFactory)
|
||||
{
|
||||
return Cache.GetOrAdd(key, valueFactory, token);
|
||||
return Cache.GetOrAdd(key, valueFactory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -18,9 +18,8 @@ public interface ICacheManager
|
|||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="factory"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
T GetOrAdd<T>(string key, Func<ICacheEntry, T> factory, IChangeToken? token = null);
|
||||
T GetOrAdd<T>(string key, Func<ICacheEntry, T> factory);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
|
|
@ -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) =>
|
||||
{
|
||||
|
||||
|
|
|
@ -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; }
|
||||
|
||||
/// <summary>
|
||||
|
@ -18,14 +30,20 @@ class GroupService : IGroup
|
|||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<Group> GetAll() => Database.Fetch<Group>();
|
||||
public List<Group> GetAll() => CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => Database.Fetch<Group>());
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public List<string> GetGroupsByUserId(string? userId) => Database.Fetch<string>("select GroupID from UserGroup where UserID = @0", userId);
|
||||
public List<string> 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<string>("select GroupID from UserGroup where UserID = @0", userId);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -49,6 +67,11 @@ class GroupService : IGroup
|
|||
Database.AbortTransaction();
|
||||
throw;
|
||||
}
|
||||
|
||||
if (ret)
|
||||
{
|
||||
GetGroupsByUserIdCancellationTokenSource?.Cancel();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -57,7 +80,13 @@ class GroupService : IGroup
|
|||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <returns></returns>
|
||||
public List<string> GetGroupsByRoleId(string? roleId) => Database.Fetch<string>("select GroupID from RoleGroup where RoleID = @0", roleId);
|
||||
public List<string> 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<string>("select GroupID from RoleGroup where RoleID = @0", roleId);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue