|
|
|
@ -1,10 +1,8 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Pages.Abp.MultiTenancy.ClientProxies;
|
|
|
|
|
using Volo.Abp.AspNetCore.Mvc.MultiTenancy;
|
|
|
|
|
using Volo.Abp.Caching;
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
using Volo.Abp.MultiTenancy;
|
|
|
|
@ -16,13 +14,13 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
|
|
|
|
|
{
|
|
|
|
|
protected AbpTenantClientProxy TenantAppService { get; }
|
|
|
|
|
protected IHttpContextAccessor HttpContextAccessor { get; }
|
|
|
|
|
protected IDistributedCache<TenantConfiguration> Cache { get; }
|
|
|
|
|
protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
|
|
|
|
|
protected AbpAspNetCoreMvcClientCacheOptions Options { get; }
|
|
|
|
|
|
|
|
|
|
public MvcRemoteTenantStore(
|
|
|
|
|
AbpTenantClientProxy tenantAppService,
|
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
|
IDistributedCache<TenantConfiguration> cache,
|
|
|
|
|
IDistributedCache<TenantConfigurationCacheItem> cache,
|
|
|
|
|
IOptions<AbpAspNetCoreMvcClientCacheOptions> options)
|
|
|
|
|
{
|
|
|
|
|
TenantAppService = tenantAppService;
|
|
|
|
@ -33,119 +31,101 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
|
|
|
|
|
|
|
|
|
|
public async Task<TenantConfiguration?> FindAsync(string name)
|
|
|
|
|
{
|
|
|
|
|
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name);
|
|
|
|
|
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
|
|
|
|
|
var httpContext = HttpContextAccessor?.HttpContext;
|
|
|
|
|
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
|
|
|
|
|
{
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
return tenantConfigurationInHttpContext?.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tenantConfiguration = (await Cache.GetOrAddAsync(
|
|
|
|
|
cacheKey,
|
|
|
|
|
async () => CreateTenantConfiguration(await TenantAppService.FindTenantByNameAsync(name))!,
|
|
|
|
|
() => new DistributedCacheEntryOptions
|
|
|
|
|
{
|
|
|
|
|
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration
|
|
|
|
|
}
|
|
|
|
|
))!;
|
|
|
|
|
var tenantConfiguration = await Cache.GetAsync(cacheKey);
|
|
|
|
|
if (tenantConfiguration == null)
|
|
|
|
|
{
|
|
|
|
|
await TenantAppService.FindTenantByNameAsync(name);
|
|
|
|
|
tenantConfiguration = await Cache.GetAsync(cacheKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (httpContext != null)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Items[cacheKey] = tenantConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
return tenantConfiguration?.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<TenantConfiguration?> FindAsync(Guid id)
|
|
|
|
|
{
|
|
|
|
|
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id);
|
|
|
|
|
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id);
|
|
|
|
|
var httpContext = HttpContextAccessor?.HttpContext;
|
|
|
|
|
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
|
|
|
|
|
{
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
return tenantConfigurationInHttpContext?.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tenantConfiguration = (await Cache.GetOrAddAsync(
|
|
|
|
|
cacheKey,
|
|
|
|
|
async () => CreateTenantConfiguration(await TenantAppService.FindTenantByIdAsync(id))!,
|
|
|
|
|
() => new DistributedCacheEntryOptions
|
|
|
|
|
{
|
|
|
|
|
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration
|
|
|
|
|
}
|
|
|
|
|
))!;
|
|
|
|
|
var tenantConfiguration = await Cache.GetAsync(cacheKey);
|
|
|
|
|
if (tenantConfiguration == null)
|
|
|
|
|
{
|
|
|
|
|
await TenantAppService.FindTenantByIdAsync(id);
|
|
|
|
|
tenantConfiguration = await Cache.GetAsync(cacheKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (httpContext != null)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Items[cacheKey] = tenantConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
return tenantConfiguration?.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TenantConfiguration Find(string name)
|
|
|
|
|
public TenantConfiguration? Find(string name)
|
|
|
|
|
{
|
|
|
|
|
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name);
|
|
|
|
|
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
|
|
|
|
|
var httpContext = HttpContextAccessor?.HttpContext;
|
|
|
|
|
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
|
|
|
|
|
{
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
return tenantConfigurationInHttpContext?.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tenantConfiguration = Cache.GetOrAdd(
|
|
|
|
|
cacheKey,
|
|
|
|
|
() => AsyncHelper.RunSync(async () => CreateTenantConfiguration(await TenantAppService.FindTenantByNameAsync(name))!),
|
|
|
|
|
() => new DistributedCacheEntryOptions
|
|
|
|
|
{
|
|
|
|
|
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration
|
|
|
|
|
}
|
|
|
|
|
)!;
|
|
|
|
|
var tenantConfiguration = Cache.Get(cacheKey);
|
|
|
|
|
if (tenantConfiguration == null)
|
|
|
|
|
{
|
|
|
|
|
AsyncHelper.RunSync(async () => await TenantAppService.FindTenantByNameAsync(name));
|
|
|
|
|
tenantConfiguration = Cache.Get(cacheKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (httpContext != null)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Items[cacheKey] = tenantConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
return tenantConfiguration?.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TenantConfiguration Find(Guid id)
|
|
|
|
|
public TenantConfiguration? Find(Guid id)
|
|
|
|
|
{
|
|
|
|
|
var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id);
|
|
|
|
|
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id);
|
|
|
|
|
var httpContext = HttpContextAccessor?.HttpContext;
|
|
|
|
|
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
|
|
|
|
|
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfigurationCacheItem tenantConfigurationInHttpContext)
|
|
|
|
|
{
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
return tenantConfigurationInHttpContext?.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tenantConfiguration = Cache.GetOrAdd(
|
|
|
|
|
cacheKey,
|
|
|
|
|
() => AsyncHelper.RunSync(async () => CreateTenantConfiguration(await TenantAppService.FindTenantByIdAsync(id))!),
|
|
|
|
|
() => new DistributedCacheEntryOptions
|
|
|
|
|
{
|
|
|
|
|
AbsoluteExpirationRelativeToNow = Options.TenantConfigurationCacheAbsoluteExpiration
|
|
|
|
|
}
|
|
|
|
|
)!;
|
|
|
|
|
|
|
|
|
|
if (httpContext != null)
|
|
|
|
|
var tenantConfiguration = Cache.Get(cacheKey);
|
|
|
|
|
if (tenantConfiguration == null)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Items[cacheKey] = tenantConfiguration;
|
|
|
|
|
AsyncHelper.RunSync(async () => await TenantAppService.FindTenantByIdAsync(id));
|
|
|
|
|
tenantConfiguration = Cache.Get(cacheKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tenantConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual TenantConfiguration? CreateTenantConfiguration(FindTenantResultDto tenantResultDto)
|
|
|
|
|
{
|
|
|
|
|
if (!tenantResultDto.Success || tenantResultDto.TenantId == null)
|
|
|
|
|
if (httpContext != null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
httpContext.Items[cacheKey] = tenantConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new TenantConfiguration(tenantResultDto.TenantId.Value, tenantResultDto.Name!);
|
|
|
|
|
return tenantConfiguration?.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|