#2437 Optimize querying and caching the permissions

pull/4493/head
Halil İbrahim Kalkan 5 years ago
parent ff3c77b377
commit 92e3eca767

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Authorization.Permissions;
@ -10,17 +11,21 @@ namespace Volo.Abp.PermissionManagement
public class PermissionStore : IPermissionStore, ITransientDependency
{
public ILogger<PermissionStore> Logger { get; set; }
protected IPermissionGrantRepository PermissionGrantRepository { get; }
protected IPermissionDefinitionManager PermissionDefinitionManager { get; }
protected IDistributedCache<PermissionGrantCacheItem> Cache { get; }
public PermissionStore(
IPermissionGrantRepository permissionGrantRepository,
IDistributedCache<PermissionGrantCacheItem> cache)
IDistributedCache<PermissionGrantCacheItem> cache,
IPermissionDefinitionManager permissionDefinitionManager)
{
PermissionGrantRepository = permissionGrantRepository;
Cache = cache;
PermissionDefinitionManager = permissionDefinitionManager;
Logger = NullLogger<PermissionStore>.Instance;
}
@ -29,7 +34,10 @@ namespace Volo.Abp.PermissionManagement
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);
@ -43,23 +51,45 @@ namespace Volo.Abp.PermissionManagement
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(
name,
(await PermissionGrantRepository.FindAsync(name, providerName, providerKey)) != null
);
return cacheItem;
}
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(
cacheKey,
cacheItem
);
var permissionGrants = await PermissionGrantRepository.GetListAsync(providerName, providerKey);
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)
@ -67,4 +97,4 @@ namespace Volo.Abp.PermissionManagement
return PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey);
}
}
}
}
Loading…
Cancel
Save