From 58f31c88a06e062a1eda1ffd46047ffd3c70308c Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 8 Dec 2023 15:53:03 +0800 Subject: [PATCH] Clear tenant cache on name change. --- .../Abp/TenantManagement/TenantManager.cs | 9 +++++++-- .../TenantCacheItemInvalidator_Tests.cs | 19 +++++++++++++++++++ .../AbpTenantManagementTestDataBuilder.cs | 3 +++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantManager.cs b/modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantManager.cs index fa388426d9b..1ad7324c359 100644 --- a/modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantManager.cs +++ b/modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantManager.cs @@ -1,17 +1,21 @@ using System; using System.Threading.Tasks; +using Volo.Abp.Caching; using Volo.Abp.Domain.Services; +using Volo.Abp.MultiTenancy; namespace Volo.Abp.TenantManagement; public class TenantManager : DomainService, ITenantManager { protected ITenantRepository TenantRepository { get; } + protected IDistributedCache Cache { get; } - public TenantManager(ITenantRepository tenantRepository) + public TenantManager(ITenantRepository tenantRepository, + IDistributedCache cache) { TenantRepository = tenantRepository; - + Cache = cache; } public virtual async Task CreateAsync(string name) @@ -28,6 +32,7 @@ public virtual async Task ChangeNameAsync(Tenant tenant, string name) Check.NotNull(name, nameof(name)); await ValidateNameAsync(name, tenant.Id); + await Cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name)); tenant.SetName(name); } diff --git a/modules/tenant-management/test/Volo.Abp.TenantManagement.Domain.Tests/Volo/Abp/TenantManagement/TenantCacheItemInvalidator_Tests.cs b/modules/tenant-management/test/Volo.Abp.TenantManagement.Domain.Tests/Volo/Abp/TenantManagement/TenantCacheItemInvalidator_Tests.cs index 85416c77551..96714025600 100644 --- a/modules/tenant-management/test/Volo.Abp.TenantManagement.Domain.Tests/Volo/Abp/TenantManagement/TenantCacheItemInvalidator_Tests.cs +++ b/modules/tenant-management/test/Volo.Abp.TenantManagement.Domain.Tests/Volo/Abp/TenantManagement/TenantCacheItemInvalidator_Tests.cs @@ -11,12 +11,14 @@ public class TenantConfigurationCacheItemInvalidator_Tests : AbpTenantManagement private readonly IDistributedCache _cache; private readonly ITenantStore _tenantStore; private readonly ITenantRepository _tenantRepository; + private readonly ITenantManager _tenantManager; public TenantConfigurationCacheItemInvalidator_Tests() { _cache = GetRequiredService>(); _tenantStore = GetRequiredService(); _tenantRepository = GetRequiredService(); + _tenantManager = GetRequiredService(); } [Fact] @@ -81,5 +83,22 @@ public async Task Cache_Should_Invalidator_When_Tenant_Changed() (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(volosoft.Id, null))).ShouldBeNull(); (_cache.Get(TenantConfigurationCacheItem.CalculateCacheKey(null, volosoft.Name))).ShouldBeNull(); + + var abp = await _tenantRepository.FindByNameAsync("abp"); + abp.ShouldNotBeNull(); + + // Find will cache tenant. + await _tenantStore.FindAsync(abp.Id); + await _tenantStore.FindAsync(abp.Name); + + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(abp.Id, null))).ShouldNotBeNull(); + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, abp.Name))).ShouldNotBeNull(); + + await _tenantManager.ChangeNameAsync(abp, "abp2"); + await _tenantRepository.UpdateAsync(abp); + + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(abp.Id, null))).ShouldBeNull(); + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, "abp"))).ShouldBeNull(); + (await _cache.GetAsync(TenantConfigurationCacheItem.CalculateCacheKey(null, "abp2"))).ShouldBeNull(); } } diff --git a/modules/tenant-management/test/Volo.Abp.TenantManagement.TestBase/Volo/Abp/TenantManagement/AbpTenantManagementTestDataBuilder.cs b/modules/tenant-management/test/Volo.Abp.TenantManagement.TestBase/Volo/Abp/TenantManagement/AbpTenantManagementTestDataBuilder.cs index 26abc1cd97c..2a4b55ee271 100644 --- a/modules/tenant-management/test/Volo.Abp.TenantManagement.TestBase/Volo/Abp/TenantManagement/AbpTenantManagementTestDataBuilder.cs +++ b/modules/tenant-management/test/Volo.Abp.TenantManagement.TestBase/Volo/Abp/TenantManagement/AbpTenantManagementTestDataBuilder.cs @@ -32,5 +32,8 @@ private async Task AddTenantsAsync() var volosoft = await _tenantManager.CreateAsync("volosoft"); await _tenantRepository.InsertAsync(volosoft); + + var abp = await _tenantManager.CreateAsync("abp"); + await _tenantRepository.InsertAsync(abp); } }