using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Hotline.Caches; using Hotline.Settings; using XF.Domain.Cache; using XF.Domain.Dependency; namespace Hotline.CacheManager { public class RolePermissionsCacheManager : IRolePermissionsCacheManager, IScopeDependency { private const string RolePermissionCacheKey = "RolePermissionCache:"; private readonly ITypedCache> _cache; private readonly ISystemAuthorityRepository _systemAuthorityRepository; public RolePermissionsCacheManager(ITypedCache> cache, ISystemAuthorityRepository systemAuthorityRepository) { _cache = cache; _systemAuthorityRepository = systemAuthorityRepository; } public IReadOnlyList GetPermissions(string role) { return _cache.GetOrAdd(GetKey(role), d => { //todo 加上全局配置,对未开启模块过滤 var systemAuth = _systemAuthorityRepository.GetAsync(d => d.RoleCode == role).GetAwaiter() .GetResult(); return systemAuth?.GetPermissions() ?? new(); } ); } public void RemovePermissions(string role) { _cache.Remove(GetKey(role)); } private string GetKey(string role) => $"{RolePermissionCacheKey}{role}"; } }