Browse Source

Clear the tenant cache when creating/update/deleting.

pull/18412/head
maliming 3 years ago
parent
commit
bf44f64092
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 18
      framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs
  2. 16
      framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/TenantConfigurationCacheHelper.cs
  3. 1
      modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo.Abp.TenantManagement.Application.Contracts.csproj
  4. 4
      modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/AbpTenantManagementApplicationContractsModule.cs
  5. 16
      modules/tenant-management/src/Volo.Abp.TenantManagement.Application/Volo/Abp/TenantManagement/TenantAppService.cs

18
framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs

@ -33,7 +33,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
public async Task<TenantConfiguration?> FindAsync(string name) public async Task<TenantConfiguration?> FindAsync(string name)
{ {
var cacheKey = CreateCacheKey(name); var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
@ -60,7 +60,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
public async Task<TenantConfiguration?> FindAsync(Guid id) public async Task<TenantConfiguration?> FindAsync(Guid id)
{ {
var cacheKey = CreateCacheKey(id); var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
@ -87,7 +87,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
public TenantConfiguration Find(string name) public TenantConfiguration Find(string name)
{ {
var cacheKey = CreateCacheKey(name); var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
@ -114,7 +114,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
public TenantConfiguration Find(Guid id) public TenantConfiguration Find(Guid id)
{ {
var cacheKey = CreateCacheKey(id); var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id);
var httpContext = HttpContextAccessor?.HttpContext; var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration)
@ -148,14 +148,4 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency
return new TenantConfiguration(tenantResultDto.TenantId.Value, tenantResultDto.Name!); 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}";
}
} }

16
framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/TenantConfigurationCacheHelper.cs

@ -0,0 +1,16 @@
using System;
namespace Volo.Abp.AspNetCore.Mvc.MultiTenancy;
public static class TenantConfigurationCacheHelper
{
public static string CreateCacheKey(string tenantName)
{
return $"RemoteTenantStore_Name_{tenantName}";
}
public static string CreateCacheKey(Guid tenantId)
{
return $"RemoteTenantStore_Id_{tenantId:N}";
}
}

1
modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo.Abp.TenantManagement.Application.Contracts.csproj

@ -18,6 +18,7 @@
<ProjectReference Include="..\Volo.Abp.TenantManagement.Domain.Shared\Volo.Abp.TenantManagement.Domain.Shared.csproj" /> <ProjectReference Include="..\Volo.Abp.TenantManagement.Domain.Shared\Volo.Abp.TenantManagement.Domain.Shared.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Application.Contracts\Volo.Abp.Ddd.Application.Contracts.csproj" /> <ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Application.Contracts\Volo.Abp.Ddd.Application.Contracts.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Authorization.Abstractions\Volo.Abp.Authorization.Abstractions.csproj" /> <ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Authorization.Abstractions\Volo.Abp.Authorization.Abstractions.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AspNetCore.Mvc.Contracts\Volo.Abp.AspNetCore.Mvc.Contracts.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

4
modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/AbpTenantManagementApplicationContractsModule.cs

@ -1,4 +1,5 @@
using Volo.Abp.Application; using Volo.Abp.Application;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Authorization; using Volo.Abp.Authorization;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.ObjectExtending; using Volo.Abp.ObjectExtending;
@ -10,7 +11,8 @@ namespace Volo.Abp.TenantManagement;
[DependsOn( [DependsOn(
typeof(AbpDddApplicationContractsModule), typeof(AbpDddApplicationContractsModule),
typeof(AbpTenantManagementDomainSharedModule), typeof(AbpTenantManagementDomainSharedModule),
typeof(AbpAuthorizationAbstractionsModule) typeof(AbpAuthorizationAbstractionsModule),
typeof(AbpAspNetCoreMvcContractsModule)
)] )]
public class AbpTenantManagementApplicationContractsModule : AbpModule public class AbpTenantManagementApplicationContractsModule : AbpModule
{ {

16
modules/tenant-management/src/Volo.Abp.TenantManagement.Application/Volo/Abp/TenantManagement/TenantAppService.cs

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc.MultiTenancy;
using Volo.Abp.Caching;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -17,17 +19,20 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic
protected ITenantRepository TenantRepository { get; } protected ITenantRepository TenantRepository { get; }
protected ITenantManager TenantManager { get; } protected ITenantManager TenantManager { get; }
protected IDistributedEventBus DistributedEventBus { get; } protected IDistributedEventBus DistributedEventBus { get; }
protected IDistributedCache<TenantConfiguration> TenantConfigurationCache { get; }
public TenantAppService( public TenantAppService(
ITenantRepository tenantRepository, ITenantRepository tenantRepository,
ITenantManager tenantManager, ITenantManager tenantManager,
IDataSeeder dataSeeder, IDataSeeder dataSeeder,
IDistributedEventBus distributedEventBus) IDistributedEventBus distributedEventBus,
IDistributedCache<TenantConfiguration> tenantConfigurationCache)
{ {
DataSeeder = dataSeeder; DataSeeder = dataSeeder;
TenantRepository = tenantRepository; TenantRepository = tenantRepository;
TenantManager = tenantManager; TenantManager = tenantManager;
DistributedEventBus = distributedEventBus; DistributedEventBus = distributedEventBus;
TenantConfigurationCache = tenantConfigurationCache;
} }
public virtual async Task<TenantDto> GetAsync(Guid id) public virtual async Task<TenantDto> GetAsync(Guid id)
@ -91,6 +96,9 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic
); );
} }
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id));
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name));
return ObjectMapper.Map<Tenant, TenantDto>(tenant); return ObjectMapper.Map<Tenant, TenantDto>(tenant);
} }
@ -106,6 +114,9 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic
await TenantRepository.UpdateAsync(tenant); await TenantRepository.UpdateAsync(tenant);
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id));
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name));
return ObjectMapper.Map<Tenant, TenantDto>(tenant); return ObjectMapper.Map<Tenant, TenantDto>(tenant);
} }
@ -119,6 +130,9 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic
} }
await TenantRepository.DeleteAsync(tenant); await TenantRepository.DeleteAsync(tenant);
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id));
await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name));
} }
[Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)] [Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)]

Loading…
Cancel
Save