Created RemoteTenantStore

pull/1810/head
Halil İbrahim Kalkan 5 years ago
parent 53987e813b
commit bc757501d2

@ -13,8 +13,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Client
{
public class CachedApplicationConfigurationClient : ICachedApplicationConfigurationClient, ITransientDependency
{
public IHttpContextAccessor HttpContextAccessor { get; set; }
protected IHttpContextAccessor HttpContextAccessor { get; }
protected IHttpClientProxy<IAbpApplicationConfigurationAppService> Proxy { get; }
protected ICurrentUser CurrentUser { get; }
protected IDistributedCache<ApplicationConfigurationDto> Cache { get; }
@ -42,11 +41,11 @@ namespace Volo.Abp.AspNetCore.Mvc.Client
}
configuration = await Cache.GetOrAddAsync(
CreateCacheKey(),
cacheKey,
async () => await Proxy.Service.GetAsync(),
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60) //TODO: Should be configurable. Default value should be higher (5 mins would be good).
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(120) //TODO: Should be configurable. Default value should be higher (5 mins would be good).
}
);

@ -0,0 +1,116 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.AspNetCore.Mvc.MultiTenancy;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client.DynamicProxying;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
namespace Volo.Abp.AspNetCore.Mvc.Client
{
public class RemoteTenantStore : ITenantStore, ITransientDependency
{
protected IHttpClientProxy<IAbpTenantAppService> Proxy { get; }
protected IHttpContextAccessor HttpContextAccessor { get; }
protected IDistributedCache<TenantConfiguration> Cache { get; }
public RemoteTenantStore(
IHttpClientProxy<IAbpTenantAppService> proxy,
IHttpContextAccessor httpContextAccessor,
IDistributedCache<TenantConfiguration> cache)
{
Proxy = proxy;
HttpContextAccessor = httpContextAccessor;
Cache = cache;
}
public async Task<TenantConfiguration> FindAsync(string name)
{
var cacheKey = CreateCacheKey(name);
var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
{
return tenantConfiguration;
}
tenantConfiguration = await Cache.GetOrAddAsync(
cacheKey,
async () => CreateTenantConfiguration(await Proxy.Service.FindTenantByNameAsync(name)),
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow =
TimeSpan.FromMinutes(5) //TODO: Should be configurable.
}
);
if (httpContext != null)
{
httpContext.Items[cacheKey] = tenantConfiguration;
}
return tenantConfiguration;
}
public async Task<TenantConfiguration> FindAsync(Guid id)
{
var cacheKey = CreateCacheKey(id);
var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
{
return tenantConfiguration;
}
tenantConfiguration = await Cache.GetOrAddAsync(
cacheKey,
async () => CreateTenantConfiguration(await Proxy.Service.FindTenantByIdAsync(id)),
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow =
TimeSpan.FromMinutes(5) //TODO: Should be configurable.
}
);
if (httpContext != null)
{
httpContext.Items[cacheKey] = tenantConfiguration;
}
return tenantConfiguration;
}
public TenantConfiguration Find(string name)
{
return AsyncHelper.RunSync(() => FindAsync(name));
}
public TenantConfiguration Find(Guid id)
{
return AsyncHelper.RunSync(() => FindAsync(id));
}
protected virtual TenantConfiguration CreateTenantConfiguration(FindTenantResultDto tenantResultDto)
{
if (!tenantResultDto.Success || tenantResultDto.TenantId == null)
{
return null;
}
return new TenantConfiguration(tenantResultDto.TenantId.Value, tenantResultDto.Name);
}
protected virtual string CreateCacheKey(string tenantName)
{
return $"RemoteTenantStore_Name_{tenantName}";
}
protected virtual string CreateCacheKey(Guid tenantId)
{
return $"RemoteTenantStore_Id_{tenantId:N}";
}
}
}

@ -2,7 +2,8 @@
namespace Volo.Abp.AspNetCore.Mvc.MultiTenancy
{
public class FindTenantResult
[Serializable]
public class FindTenantResultDto
{
public bool Success { get; set; }

@ -6,8 +6,8 @@ namespace Volo.Abp.AspNetCore.Mvc.MultiTenancy
{
public interface IAbpTenantAppService : IApplicationService
{
Task<FindTenantResult> FindTenantByNameAsync(string name);
Task<FindTenantResultDto> FindTenantByNameAsync(string name);
Task<FindTenantResult> FindTenantByIdAsync(Guid id);
Task<FindTenantResultDto> FindTenantByIdAsync(Guid id);
}
}

@ -15,16 +15,16 @@ namespace Pages.Abp.MultiTenancy
TenantStore = tenantStore;
}
public async Task<FindTenantResult> FindTenantByNameAsync(string name)
public async Task<FindTenantResultDto> FindTenantByNameAsync(string name)
{
var tenant = await TenantStore.FindAsync(name);
if (tenant == null)
{
return new FindTenantResult { Success = false };
return new FindTenantResultDto { Success = false };
}
return new FindTenantResult
return new FindTenantResultDto
{
Success = true,
TenantId = tenant.Id,
@ -32,16 +32,16 @@ namespace Pages.Abp.MultiTenancy
};
}
public async Task<FindTenantResult> FindTenantByIdAsync(Guid id)
public async Task<FindTenantResultDto> FindTenantByIdAsync(Guid id)
{
var tenant = await TenantStore.FindAsync(id);
if (tenant == null)
{
return new FindTenantResult { Success = false };
return new FindTenantResultDto { Success = false };
}
return new FindTenantResult
return new FindTenantResultDto
{
Success = true,
TenantId = tenant.Id,

@ -19,14 +19,14 @@ namespace Pages.Abp.MultiTenancy
[HttpGet]
[Route("find-tenant/{name}")] //TODO: Remove on v1.0
[Route("tenants/by-name/{name}")]
public async Task<FindTenantResult> FindTenantByNameAsync(string name)
public async Task<FindTenantResultDto> FindTenantByNameAsync(string name)
{
return await _abpTenantAppService.FindTenantByNameAsync(name);
}
[HttpGet]
[Route("tenants/by-id/{id}")]
public async Task<FindTenantResult> FindTenantByIdAsync(Guid id)
public async Task<FindTenantResultDto> FindTenantByIdAsync(Guid id)
{
return await _abpTenantAppService.FindTenantByIdAsync(id);
}

Loading…
Cancel
Save