mirror of https://github.com/abpframework/abp.git
11 changed files with 167 additions and 4 deletions
@ -0,0 +1,24 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netstandard2.0</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.Caching</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Caching</PackageId> |
||||
|
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.Caching |
||||
|
{ |
||||
|
public class AbpCachingModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(IServiceCollection services) |
||||
|
{ |
||||
|
services.AddMemoryCache(); |
||||
|
services.AddDistributedMemoryCache(); |
||||
|
|
||||
|
services.AddAssemblyOf<AbpCachingModule>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Permissions |
||||
|
{ |
||||
|
[Serializable] |
||||
|
public class PermissionGrantCacheItem |
||||
|
{ |
||||
|
public string Name { get; set; } |
||||
|
|
||||
|
public bool IsGranted { get; set; } |
||||
|
|
||||
|
public PermissionGrantCacheItem() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public PermissionGrantCacheItem(string name, bool isGranted) |
||||
|
{ |
||||
|
Name = name; |
||||
|
IsGranted = isGranted; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,21 +1,85 @@ |
|||||
using System.Threading.Tasks; |
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Caching.Distributed; |
||||
using Volo.Abp.Authorization.Permissions; |
using Volo.Abp.Authorization.Permissions; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Json; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.Threading; |
||||
|
|
||||
namespace Volo.Abp.Permissions |
namespace Volo.Abp.Permissions |
||||
{ |
{ |
||||
|
/* TODOS: |
||||
|
* - Wrap distributed cache? |
||||
|
* - Add multi-tenancy |
||||
|
* - Add _cancellationTokenProvider support |
||||
|
* - Add object serialization support |
||||
|
* - Add cache invalidation support..? Maybe it's not cache's job! |
||||
|
*/ |
||||
|
|
||||
public class PermissionStore : AbpServiceBase, IPermissionStore, ITransientDependency |
public class PermissionStore : AbpServiceBase, IPermissionStore, ITransientDependency |
||||
{ |
{ |
||||
private readonly IPermissionGrantRepository _permissionGrantRepository; |
private readonly IPermissionGrantRepository _permissionGrantRepository; |
||||
|
private readonly ICurrentTenant _currentTenant; |
||||
|
private readonly ICancellationTokenProvider _cancellationTokenProvider; |
||||
|
private readonly IDistributedCache _distributedCache; |
||||
|
private readonly IJsonSerializer _jsonSerializer; |
||||
|
|
||||
public PermissionStore(IPermissionGrantRepository permissionGrantRepository) |
public PermissionStore( |
||||
|
IPermissionGrantRepository permissionGrantRepository, |
||||
|
IDistributedCache distributedCache, |
||||
|
ICancellationTokenProvider cancellationTokenProvider, |
||||
|
ICurrentTenant currentTenant, |
||||
|
IJsonSerializer jsonSerializer) |
||||
{ |
{ |
||||
_permissionGrantRepository = permissionGrantRepository; |
_permissionGrantRepository = permissionGrantRepository; |
||||
|
_distributedCache = distributedCache; |
||||
|
_cancellationTokenProvider = cancellationTokenProvider; |
||||
|
_currentTenant = currentTenant; |
||||
|
_jsonSerializer = jsonSerializer; |
||||
} |
} |
||||
|
|
||||
public async Task<bool> IsGrantedAsync(string name, string providerName, string providerKey) |
public async Task<bool> IsGrantedAsync(string name, string providerName, string providerKey) |
||||
{ |
{ |
||||
return await _permissionGrantRepository.FindAsync(name, providerName, providerKey) != null; |
return await _permissionGrantRepository.FindAsync(name, providerName, providerKey) != null; |
||||
|
//return (await GetCacheItemAsync(name, providerName, providerKey)).IsGranted; //TODO: Use cache when invalidation is possible!
|
||||
|
} |
||||
|
|
||||
|
private string CalculateCacheKey(string name, string providerName, string providerKey) |
||||
|
{ |
||||
|
var key = "P:" + providerName + "_K:" + providerKey + "N:" + name; |
||||
|
|
||||
|
if (_currentTenant.Id.HasValue) |
||||
|
{ |
||||
|
key = "T:" + _currentTenant.Id + "_" + key; |
||||
|
} |
||||
|
|
||||
|
return key; |
||||
|
} |
||||
|
|
||||
|
private async Task<PermissionGrantCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey) |
||||
|
{ |
||||
|
var cacheKey = CalculateCacheKey(name, providerName, providerKey); |
||||
|
var cachedString = await _distributedCache.GetStringAsync(cacheKey, _cancellationTokenProvider.Token); |
||||
|
|
||||
|
if (cachedString != null) |
||||
|
{ |
||||
|
return _jsonSerializer.Deserialize<PermissionGrantCacheItem>(cachedString); |
||||
|
} |
||||
|
|
||||
|
var cacheItem = new PermissionGrantCacheItem( |
||||
|
name, |
||||
|
await _permissionGrantRepository.FindAsync(name, providerName, providerKey) != null |
||||
|
); |
||||
|
|
||||
|
await _distributedCache.SetStringAsync( |
||||
|
cacheKey, |
||||
|
_jsonSerializer.Serialize(cacheItem), |
||||
|
new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(20) }, |
||||
|
_cancellationTokenProvider.Token |
||||
|
); |
||||
|
|
||||
|
return cacheItem; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue