13 changed files with 300 additions and 2 deletions
@ -0,0 +1,16 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
|||
|
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net5.0</TargetFramework> |
|||
<RootNamespace /> |
|||
<OutputType>Library</OutputType> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Volo.Abp.AspNetCore.Mvc.Client.Common" Version="4.2.1" /> |
|||
<PackageReference Include="Volo.Abp.EventBus" Version="4.2.1" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<PropertyGroup> |
|||
<ActiveDebugProfile>IIS Express</ActiveDebugProfile> |
|||
</PropertyGroup> |
|||
</Project> |
|||
@ -0,0 +1,16 @@ |
|||
namespace LINGYUN.Abp.AspNetCore.Mvc.Client |
|||
{ |
|||
public class AbpAspNetCoreMvcClientCacheOptions |
|||
{ |
|||
/// <summary>
|
|||
/// 用户缓存过期时间, 单位: 秒
|
|||
/// 默认: 300
|
|||
/// </summary>
|
|||
public int UserCacheExpirationSeconds { get; set; } = 300; |
|||
/// <summary>
|
|||
/// 匿名用户缓存过期时间, 单位: 秒
|
|||
/// 默认: 300
|
|||
/// </summary>
|
|||
public int AnonymousCacheExpirationSeconds { get; set; } = 300; |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using System; |
|||
using Volo.Abp.AspNetCore.Mvc.Client; |
|||
using Volo.Abp.EventBus; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace LINGYUN.Abp.AspNetCore.Mvc.Client |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpAspNetCoreMvcClientCommonModule), |
|||
typeof(AbpEventBusModule) |
|||
)] |
|||
public class AbpAspNetCoreMvcClientModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
var configuration = context.Services.GetConfiguration(); |
|||
Configure<AbpAspNetCoreMvcClientCacheOptions>(configuration.GetSection("AbpMvcClient:Cache")); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,96 @@ |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.Caching.Distributed; |
|||
using Microsoft.Extensions.Options; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; |
|||
using Volo.Abp.AspNetCore.Mvc.Client; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Http.Client.DynamicProxying; |
|||
using Volo.Abp.Threading; |
|||
using Volo.Abp.Users; |
|||
|
|||
namespace LINGYUN.Abp.AspNetCore.Mvc.Client |
|||
{ |
|||
[ExposeServices( |
|||
typeof(MvcCachedApplicationConfigurationClient), |
|||
typeof(ICachedApplicationConfigurationClient), |
|||
typeof(IAsyncInitialize) |
|||
)] |
|||
public class MvcCachedApplicationConfigurationClient : ICachedApplicationConfigurationClient, ITransientDependency |
|||
{ |
|||
protected IHttpContextAccessor HttpContextAccessor { get; } |
|||
protected IHttpClientProxy<IAbpApplicationConfigurationAppService> Proxy { get; } |
|||
protected ICurrentUser CurrentUser { get; } |
|||
protected IDistributedCache<ApplicationConfigurationDto> Cache { get; } |
|||
protected AbpAspNetCoreMvcClientCacheOptions Options { get; } |
|||
|
|||
public MvcCachedApplicationConfigurationClient( |
|||
IDistributedCache<ApplicationConfigurationDto> cache, |
|||
IHttpClientProxy<IAbpApplicationConfigurationAppService> proxy, |
|||
ICurrentUser currentUser, |
|||
IHttpContextAccessor httpContextAccessor, |
|||
IOptions<AbpAspNetCoreMvcClientCacheOptions> options) |
|||
{ |
|||
Proxy = proxy; |
|||
CurrentUser = currentUser; |
|||
HttpContextAccessor = httpContextAccessor; |
|||
Cache = cache; |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public async Task InitializeAsync() |
|||
{ |
|||
await GetAsync(); |
|||
} |
|||
|
|||
public async Task<ApplicationConfigurationDto> GetAsync() |
|||
{ |
|||
var cacheKey = CreateCacheKey(); |
|||
var httpContext = HttpContextAccessor?.HttpContext; |
|||
|
|||
if (httpContext != null && httpContext.Items[cacheKey] is ApplicationConfigurationDto configuration) |
|||
{ |
|||
return configuration; |
|||
} |
|||
|
|||
configuration = await Cache.GetOrAddAsync( |
|||
cacheKey, |
|||
async () => await Proxy.Service.GetAsync(), |
|||
() => new DistributedCacheEntryOptions |
|||
{ |
|||
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(CurrentUser.IsAuthenticated |
|||
? Options.UserCacheExpirationSeconds |
|||
: Options.AnonymousCacheExpirationSeconds) |
|||
} |
|||
); |
|||
|
|||
if (httpContext != null) |
|||
{ |
|||
httpContext.Items[cacheKey] = configuration; |
|||
} |
|||
|
|||
return configuration; |
|||
} |
|||
|
|||
public ApplicationConfigurationDto Get() |
|||
{ |
|||
var cacheKey = CreateCacheKey(); |
|||
var httpContext = HttpContextAccessor?.HttpContext; |
|||
|
|||
if (httpContext != null && httpContext.Items[cacheKey] is ApplicationConfigurationDto configuration) |
|||
{ |
|||
return configuration; |
|||
} |
|||
|
|||
return AsyncHelper.RunSync(GetAsync); |
|||
} |
|||
|
|||
protected virtual string CreateCacheKey() |
|||
{ |
|||
return MvcCachedApplicationConfigurationClientHelper.CreateCacheKey(CurrentUser); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Globalization; |
|||
using Volo.Abp.Users; |
|||
|
|||
namespace LINGYUN.Abp.AspNetCore.Mvc.Client |
|||
{ |
|||
internal static class MvcCachedApplicationConfigurationClientHelper |
|||
{ |
|||
public static string CreateCacheKey(ICurrentUser currentUser) |
|||
{ |
|||
var userKey = currentUser.Id?.ToString("N") ?? "Anonymous"; |
|||
return $"ApplicationConfiguration_{userKey}_{CultureInfo.CurrentUICulture.Name}"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Users; |
|||
|
|||
namespace LINGYUN.Abp.AspNetCore.Mvc.Client |
|||
{ |
|||
public class MvcCurrentApplicationConfigurationCacheResetEventHandler : |
|||
IDistributedEventHandler<CurrentApplicationConfigurationCacheResetEventData>, |
|||
ITransientDependency |
|||
{ |
|||
protected ICurrentUser CurrentUser { get; } |
|||
protected IDistributedCache<ApplicationConfigurationDto> Cache { get; } |
|||
|
|||
public MvcCurrentApplicationConfigurationCacheResetEventHandler(ICurrentUser currentUser, |
|||
IDistributedCache<ApplicationConfigurationDto> cache) |
|||
{ |
|||
CurrentUser = currentUser; |
|||
Cache = cache; |
|||
} |
|||
|
|||
public virtual async Task HandleEventAsync(CurrentApplicationConfigurationCacheResetEventData eventData) |
|||
{ |
|||
await Cache.RemoveAsync(CreateCacheKey()); |
|||
} |
|||
|
|||
protected virtual string CreateCacheKey() |
|||
{ |
|||
return MvcCachedApplicationConfigurationClientHelper.CreateCacheKey(CurrentUser); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
{ |
|||
"iisSettings": { |
|||
"windowsAuthentication": false, |
|||
"anonymousAuthentication": true, |
|||
"iisExpress": { |
|||
"applicationUrl": "http://localhost:41514/", |
|||
"sslPort": 44331 |
|||
} |
|||
}, |
|||
"profiles": { |
|||
"IIS Express": { |
|||
"commandName": "IISExpress", |
|||
"launchBrowser": true, |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
}, |
|||
"LINGYUN.Abp.AspNetCore.Mvc.Client": { |
|||
"commandName": "Project", |
|||
"launchBrowser": true, |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
}, |
|||
"applicationUrl": "https://localhost:5001;http://localhost:5000" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
# LINGYUN.Abp.AspNetCore.Mvc.Client |
|||
|
|||
参照 Volo.Abp.AspNetCore.Mvc.Client 进行重写 |
|||
|
|||
实现可配置的用户配置缓存时间 |
|||
实现订阅配置刷新事件清除用户配置缓存 |
|||
实现基于用户缓存的权限、特性、配置、本地化、语言接口 |
|||
引用 LINGYUN.Abp.MultiTenancy.RemoteService 可实现多租户接口 |
|||
完全脱离具体数据库接口 |
|||
|
|||
#### 注意 |
|||
|
|||
|
|||
## 配置使用 |
|||
|
|||
|
|||
``` json |
|||
|
|||
{ |
|||
"AbpMvcClient": { |
|||
"Cache": { |
|||
"UserCacheExpirationSeconds": 300, |
|||
"AnonymousCacheExpirationSeconds": 300 |
|||
} |
|||
} |
|||
} |
|||
|
|||
``` |
|||
|
|||
```csharp |
|||
[DependsOn(typeof(AbpAspNetCoreMvcClientModule))] |
|||
public class YouProjectModule : AbpModule |
|||
{ |
|||
// other |
|||
} |
|||
``` |
|||
Loading…
Reference in new issue