Added Volo.Abp.Caching package.

pull/216/head
Halil İbrahim Kalkan 8 years ago
parent d2997f98f5
commit 232fa9c8f1

@ -274,6 +274,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Permissions.Applic
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Permissions.Application", "src\Volo.Abp.Permissions.Application\Volo.Abp.Permissions.Application.csproj", "{90197499-CBB6-4C8C-90E6-8718CD755C04}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Caching", "src\Volo.Abp.Caching\Volo.Abp.Caching.csproj", "{A5B650AB-A67F-4A4C-9F81-7B5471CA1331}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -716,6 +718,10 @@ Global
{90197499-CBB6-4C8C-90E6-8718CD755C04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90197499-CBB6-4C8C-90E6-8718CD755C04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90197499-CBB6-4C8C-90E6-8718CD755C04}.Release|Any CPU.Build.0 = Release|Any CPU
{A5B650AB-A67F-4A4C-9F81-7B5471CA1331}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5B650AB-A67F-4A4C-9F81-7B5471CA1331}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5B650AB-A67F-4A4C-9F81-7B5471CA1331}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5B650AB-A67F-4A4C-9F81-7B5471CA1331}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -849,6 +855,7 @@ Global
{8859CFE4-BC7F-4F29-B6B5-A9457435CC32} = {055F4AC0-46C4-4E99-89CD-0F30D4A97382}
{837ABFF5-66EE-486A-8023-8DD3EE7D7D96} = {055F4AC0-46C4-4E99-89CD-0F30D4A97382}
{90197499-CBB6-4C8C-90E6-8718CD755C04} = {055F4AC0-46C4-4E99-89CD-0F30D4A97382}
{A5B650AB-A67F-4A4C-9F81-7B5471CA1331} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5}

@ -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>();
}
}
}

@ -15,8 +15,16 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Authorization\Volo.Abp.Authorization.csproj" />
<ProjectReference Include="..\Volo.Abp.Caching\Volo.Abp.Caching.csproj" />
<ProjectReference Include="..\Volo.Abp.Ddd\Volo.Abp.Ddd.csproj" />
<ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" />
<ProjectReference Include="..\Volo.Abp.Permissions.Domain.Shared\Volo.Abp.Permissions.Domain.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Extensions.Caching.Abstractions">
<HintPath>C:\Users\hikalkan\.nuget\packages\microsoft.extensions.caching.abstractions\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Caching.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

@ -1,5 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Authorization;
using Volo.Abp.Caching;
using Volo.Abp.Json;
using Volo.Abp.Modularity;
namespace Volo.Abp.Permissions
@ -7,6 +9,8 @@ namespace Volo.Abp.Permissions
[DependsOn(typeof(AbpAuthorizationModule))]
[DependsOn(typeof(AbpDddModule))]
[DependsOn(typeof(AbpPermissionsDomainSharedModule))]
[DependsOn(typeof(AbpCachingModule))]
[DependsOn(typeof(AbpJsonModule))]
public class AbpPermissionsDomainModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)

@ -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.DependencyInjection;
using Volo.Abp.Json;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
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
{
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;
_distributedCache = distributedCache;
_cancellationTokenProvider = cancellationTokenProvider;
_currentTenant = currentTenant;
_jsonSerializer = jsonSerializer;
}
public async Task<bool> IsGrantedAsync(string name, string providerName, string providerKey)
{
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;
}
}
}

@ -18,12 +18,19 @@ namespace Volo.Abp.Permissions.EntityFrameworkCore
public async Task<PermissionGrant> FindAsync(string name, string providerName, string providerKey)
{
return await DbSet.FirstOrDefaultAsync(s => s.Name == name && s.ProviderName == providerName && s.ProviderKey == providerKey);
return await DbSet.FirstOrDefaultAsync(s =>
s.Name == name &&
s.ProviderName == providerName &&
s.ProviderKey == providerKey
);
}
public async Task<List<PermissionGrant>> GetListAsync(string providerName, string providerKey)
{
return await DbSet.Where(s => s.ProviderName == providerName && s.ProviderKey == providerKey).ToListAsync();
return await DbSet.Where(s =>
s.ProviderName == providerName &&
s.ProviderKey == providerKey
).ToListAsync();
}
}
}

@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.ApiVersioning.Abstractions\Volo.Abp.ApiVersioning.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Authorization\Volo.Abp.Authorization.csproj" />
<ProjectReference Include="..\Volo.Abp.Caching\Volo.Abp.Caching.csproj" />
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
<ProjectReference Include="..\Volo.Abp.Data\Volo.Abp.Data.csproj" />
<ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" />

@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.ApiVersioning;
using Volo.Abp.Authorization;
using Volo.Abp.Caching;
using Volo.Abp.Data;
using Volo.Abp.Json;
using Volo.Abp.Localization;
@ -27,6 +28,7 @@ namespace Volo.Abp
[DependsOn(typeof(AbpVirtualFileSystemModule))]
[DependsOn(typeof(AbpApiVersioningAbstractionsModule))]
[DependsOn(typeof(AbpMultiTenancyAbstractionsModule))]
[DependsOn(typeof(AbpCachingModule))]
public class AbpCommonModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Permissions;
using Volo.Abp.Session;
using Xunit;
@ -12,10 +13,12 @@ namespace Volo.Abp.Identity
public class PermissionManager_Tests : AbpIdentityDomainTestBase
{
private readonly IPermissionManager _permissionManager;
private readonly IPermissionStore _permissionStore;
public PermissionManager_Tests()
{
_permissionManager = GetRequiredService<IPermissionManager>();
_permissionStore = GetRequiredService<IPermissionStore>();
}
[Fact]
@ -44,8 +47,12 @@ namespace Volo.Abp.Identity
public async Task Should_Grant_Permission_To_Role()
{
(await _permissionManager.GetForRoleAsync("supporter", TestPermissionNames.MyPermission2)).IsGranted.ShouldBeFalse();
(await _permissionStore.IsGrantedAsync(TestPermissionNames.MyPermission2, RolePermissionValueProvider.ProviderName, "supporter")).ShouldBeFalse();
await _permissionManager.SetForRoleAsync("supporter", TestPermissionNames.MyPermission2, true);
(await _permissionManager.GetForRoleAsync("supporter", TestPermissionNames.MyPermission2)).IsGranted.ShouldBeTrue();
(await _permissionStore.IsGrantedAsync(TestPermissionNames.MyPermission2, RolePermissionValueProvider.ProviderName, "supporter")).ShouldBeTrue();
}
[Fact]

Loading…
Cancel
Save