From bf44f640929144cadf4dbacfaf77de0d01c7f1e0 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 8 Dec 2023 11:58:15 +0800 Subject: [PATCH] Clear the tenant cache when creating/update/deleting. --- .../Mvc/Client/MvcRemoteTenantStore.cs | 18 ++++-------------- .../TenantConfigurationCacheHelper.cs | 16 ++++++++++++++++ ...nantManagement.Application.Contracts.csproj | 1 + ...nantManagementApplicationContractsModule.cs | 4 +++- .../Abp/TenantManagement/TenantAppService.cs | 16 +++++++++++++++- 5 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/TenantConfigurationCacheHelper.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs index aa9fc4beb8..8e163bdd1f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcRemoteTenantStore.cs +++ b/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 FindAsync(string name) { - var cacheKey = CreateCacheKey(name); + var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name); var httpContext = HttpContextAccessor?.HttpContext; if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) @@ -60,7 +60,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency public async Task FindAsync(Guid id) { - var cacheKey = CreateCacheKey(id); + var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id); var httpContext = HttpContextAccessor?.HttpContext; if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) @@ -87,7 +87,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency public TenantConfiguration Find(string name) { - var cacheKey = CreateCacheKey(name); + var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(name); var httpContext = HttpContextAccessor?.HttpContext; if (httpContext != null && httpContext.Items[cacheKey] is TenantConfiguration tenantConfiguration) @@ -114,7 +114,7 @@ public class MvcRemoteTenantStore : ITenantStore, ITransientDependency public TenantConfiguration Find(Guid id) { - var cacheKey = CreateCacheKey(id); + var cacheKey = TenantConfigurationCacheHelper.CreateCacheKey(id); var httpContext = HttpContextAccessor?.HttpContext; 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!); } - - protected virtual string CreateCacheKey(string tenantName) - { - return $"RemoteTenantStore_Name_{tenantName}"; - } - - protected virtual string CreateCacheKey(Guid tenantId) - { - return $"RemoteTenantStore_Id_{tenantId:N}"; - } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/TenantConfigurationCacheHelper.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Contracts/Volo/Abp/AspNetCore/Mvc/MultiTenancy/TenantConfigurationCacheHelper.cs new file mode 100644 index 0000000000..666cda31c0 --- /dev/null +++ b/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}"; + } +} diff --git a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo.Abp.TenantManagement.Application.Contracts.csproj b/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo.Abp.TenantManagement.Application.Contracts.csproj index 8e849785b7..58009adef1 100644 --- a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo.Abp.TenantManagement.Application.Contracts.csproj +++ b/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo.Abp.TenantManagement.Application.Contracts.csproj @@ -18,6 +18,7 @@ + diff --git a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/AbpTenantManagementApplicationContractsModule.cs b/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/AbpTenantManagementApplicationContractsModule.cs index 27bfeaaa36..dddb67ac83 100644 --- a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application.Contracts/Volo/Abp/TenantManagement/AbpTenantManagementApplicationContractsModule.cs +++ b/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.AspNetCore.Mvc; using Volo.Abp.Authorization; using Volo.Abp.Modularity; using Volo.Abp.ObjectExtending; @@ -10,7 +11,8 @@ namespace Volo.Abp.TenantManagement; [DependsOn( typeof(AbpDddApplicationContractsModule), typeof(AbpTenantManagementDomainSharedModule), - typeof(AbpAuthorizationAbstractionsModule) + typeof(AbpAuthorizationAbstractionsModule), + typeof(AbpAspNetCoreMvcContractsModule) )] public class AbpTenantManagementApplicationContractsModule : AbpModule { diff --git a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application/Volo/Abp/TenantManagement/TenantAppService.cs b/modules/tenant-management/src/Volo.Abp.TenantManagement.Application/Volo/Abp/TenantManagement/TenantAppService.cs index 46c85578f4..cc9ce7455d 100644 --- a/modules/tenant-management/src/Volo.Abp.TenantManagement.Application/Volo/Abp/TenantManagement/TenantAppService.cs +++ b/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 Microsoft.AspNetCore.Authorization; using Volo.Abp.Application.Dtos; +using Volo.Abp.AspNetCore.Mvc.MultiTenancy; +using Volo.Abp.Caching; using Volo.Abp.Data; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; @@ -17,17 +19,20 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic protected ITenantRepository TenantRepository { get; } protected ITenantManager TenantManager { get; } protected IDistributedEventBus DistributedEventBus { get; } + protected IDistributedCache TenantConfigurationCache { get; } public TenantAppService( ITenantRepository tenantRepository, ITenantManager tenantManager, IDataSeeder dataSeeder, - IDistributedEventBus distributedEventBus) + IDistributedEventBus distributedEventBus, + IDistributedCache tenantConfigurationCache) { DataSeeder = dataSeeder; TenantRepository = tenantRepository; TenantManager = tenantManager; DistributedEventBus = distributedEventBus; + TenantConfigurationCache = tenantConfigurationCache; } public virtual async Task 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); } @@ -106,6 +114,9 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic await TenantRepository.UpdateAsync(tenant); + await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id)); + await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name)); + return ObjectMapper.Map(tenant); } @@ -119,6 +130,9 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic } await TenantRepository.DeleteAsync(tenant); + + await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Id)); + await TenantConfigurationCache.RemoveAsync(TenantConfigurationCacheHelper.CreateCacheKey(tenant.Name)); } [Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)]