Browse Source

#2437 Optimize querying and caching the permissions

pull/4493/head
Halil İbrahim Kalkan 6 years ago
parent
commit
92e3eca767
  1. 66
      modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs

66
modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Authorization.Permissions; using Volo.Abp.Authorization.Permissions;
@ -10,17 +11,21 @@ namespace Volo.Abp.PermissionManagement
public class PermissionStore : IPermissionStore, ITransientDependency public class PermissionStore : IPermissionStore, ITransientDependency
{ {
public ILogger<PermissionStore> Logger { get; set; } public ILogger<PermissionStore> Logger { get; set; }
protected IPermissionGrantRepository PermissionGrantRepository { get; } protected IPermissionGrantRepository PermissionGrantRepository { get; }
protected IPermissionDefinitionManager PermissionDefinitionManager { get; }
protected IDistributedCache<PermissionGrantCacheItem> Cache { get; } protected IDistributedCache<PermissionGrantCacheItem> Cache { get; }
public PermissionStore( public PermissionStore(
IPermissionGrantRepository permissionGrantRepository, IPermissionGrantRepository permissionGrantRepository,
IDistributedCache<PermissionGrantCacheItem> cache) IDistributedCache<PermissionGrantCacheItem> cache,
IPermissionDefinitionManager permissionDefinitionManager)
{ {
PermissionGrantRepository = permissionGrantRepository; PermissionGrantRepository = permissionGrantRepository;
Cache = cache; Cache = cache;
PermissionDefinitionManager = permissionDefinitionManager;
Logger = NullLogger<PermissionStore>.Instance; Logger = NullLogger<PermissionStore>.Instance;
} }
@ -29,7 +34,10 @@ namespace Volo.Abp.PermissionManagement
return (await GetCacheItemAsync(name, providerName, providerKey)).IsGranted; return (await GetCacheItemAsync(name, providerName, providerKey)).IsGranted;
} }
protected virtual async Task<PermissionGrantCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey) protected virtual async Task<PermissionGrantCacheItem> GetCacheItemAsync(
string name,
string providerName,
string providerKey)
{ {
var cacheKey = CalculateCacheKey(name, providerName, providerKey); var cacheKey = CalculateCacheKey(name, providerName, providerKey);
@ -43,23 +51,45 @@ namespace Volo.Abp.PermissionManagement
return cacheItem; return cacheItem;
} }
Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}"); Logger.LogDebug($"Not found in the cache: {cacheKey}");
cacheItem = new PermissionGrantCacheItem(name, false);
await SetCacheItemsAsync(providerName, providerKey, name, cacheItem);
cacheItem = new PermissionGrantCacheItem( return cacheItem;
name, }
(await PermissionGrantRepository.FindAsync(name, providerName, providerKey)) != null
);
Logger.LogDebug($"Setting the cache item: {cacheKey}"); protected virtual async Task SetCacheItemsAsync(
string providerName,
string providerKey,
string currentName,
PermissionGrantCacheItem currentCacheItem)
{
var permissions = PermissionDefinitionManager.GetPermissions();
Logger.LogDebug($"Getting all granted permissions from the repository for this provider name,key: {providerName},{providerKey}");
await Cache.SetAsync( var permissionGrants = await PermissionGrantRepository.GetListAsync(providerName, providerKey);
cacheKey,
cacheItem
);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); Logger.LogDebug($"Setting the cache items. Count: {permissions.Count}");
return cacheItem; foreach (var permission in permissions)
{
var isGranted = permissionGrants.Any(pg => pg.Name == permission.Name); //TODO: Optimize? Dictionary/Hash
await Cache.SetAsync(
CalculateCacheKey(permission.Name, providerName, providerKey),
new PermissionGrantCacheItem(permission.Name, isGranted)
);
if (permission.Name == currentName)
{
currentCacheItem.IsGranted = isGranted;
}
}
Logger.LogDebug($"Finished setting the cache items. Count: {permissions.Count}");
} }
protected virtual string CalculateCacheKey(string name, string providerName, string providerKey) protected virtual string CalculateCacheKey(string name, string providerName, string providerKey)
@ -67,4 +97,4 @@ namespace Volo.Abp.PermissionManagement
return PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey); return PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey);
} }
} }
} }
Loading…
Cancel
Save