36 lines
973 B
C#
36 lines
973 B
C#
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace SimpleAdmin.Api.Auth;
|
|
|
|
public sealed class RbacAuthCache(IMemoryCache memoryCache) : IRbacAuthCache
|
|
{
|
|
private const string EpochKey = "rbac:auth:epoch";
|
|
private const string UserInfoKeyPrefix = "rbac:userinfo:v3:";
|
|
private const string AllMenusKeyPrefix = "rbac:menus:all:v3:";
|
|
|
|
private string GetEpoch()
|
|
{
|
|
return memoryCache.GetOrCreate(
|
|
EpochKey,
|
|
entry =>
|
|
{
|
|
entry.Priority = CacheItemPriority.NeverRemove;
|
|
return "0";
|
|
})!;
|
|
}
|
|
|
|
public string UserInfoKey(Guid userId) => UserInfoKeyPrefix + GetEpoch() + ":" + userId;
|
|
|
|
public string AllMenusKey() => AllMenusKeyPrefix + GetEpoch();
|
|
|
|
public void InvalidateUser(Guid userId)
|
|
{
|
|
memoryCache.Remove(UserInfoKey(userId));
|
|
}
|
|
|
|
public void InvalidateAll()
|
|
{
|
|
memoryCache.Set(EpochKey, Guid.NewGuid().ToString("N"));
|
|
}
|
|
}
|