RolePermissionsCacheManager.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Hotline.Caches;
  7. using Hotline.Settings;
  8. using XF.Domain.Cache;
  9. using XF.Domain.Dependency;
  10. namespace Hotline.CacheManager
  11. {
  12. public class RolePermissionsCacheManager : IRolePermissionsCacheManager, IScopeDependency
  13. {
  14. private const string RolePermissionCacheKey = "RolePermissionCache:";
  15. private readonly ITypedCache<IReadOnlyList<string>> _cache;
  16. private readonly ISystemAuthorityRepository _systemAuthorityRepository;
  17. public RolePermissionsCacheManager(ITypedCache<IReadOnlyList<string>> cache, ISystemAuthorityRepository systemAuthorityRepository)
  18. {
  19. _cache = cache;
  20. _systemAuthorityRepository = systemAuthorityRepository;
  21. }
  22. public IReadOnlyList<string> GetPermissions(string role)
  23. {
  24. return _cache.GetOrAdd(GetKey(role), d =>
  25. {
  26. //todo 加上全局配置,对未开启模块过滤
  27. var systemAuth = _systemAuthorityRepository.GetAsync(d => d.RoleCode == role).GetAwaiter()
  28. .GetResult();
  29. return systemAuth?.GetPermissions() ?? new();
  30. }
  31. );
  32. }
  33. public void RemovePermissions(string role)
  34. {
  35. _cache.Remove(GetKey(role));
  36. }
  37. private string GetKey(string role) =>
  38. $"{RolePermissionCacheKey}{role}";
  39. }
  40. }