From 037147ed47588e72f82b0815fa68f0386579dcea Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 8 Dec 2023 14:44:53 +0800 Subject: [PATCH] Add `TenantConfigurationCacheItemInvalidator`. --- .../Abp/TenantManagement/TenantAppService.cs | 20 +-------- ...TenantConfigurationCacheItemInvalidator.cs | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantConfigurationCacheItemInvalidator.cs 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 060e503235..46c85578f4 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,7 +3,6 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Volo.Abp.Application.Dtos; -using Volo.Abp.Caching; using Volo.Abp.Data; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; @@ -18,20 +17,17 @@ 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, - IDistributedCache tenantConfigurationCache) + IDistributedEventBus distributedEventBus) { DataSeeder = dataSeeder; TenantRepository = tenantRepository; TenantManager = tenantManager; DistributedEventBus = distributedEventBus; - TenantConfigurationCache = tenantConfigurationCache; } public virtual async Task GetAsync(Guid id) @@ -103,13 +99,6 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic { var tenant = await TenantRepository.GetAsync(id); - await TenantConfigurationCache.RemoveManyAsync( - new[] - { - TenantConfigurationCacheItem.CalculateCacheKey(tenant.Id, null), - TenantConfigurationCacheItem.CalculateCacheKey(null, tenant.Name), - }); - await TenantManager.ChangeNameAsync(tenant, input.Name); tenant.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp); @@ -130,13 +119,6 @@ public class TenantAppService : TenantManagementAppServiceBase, ITenantAppServic } await TenantRepository.DeleteAsync(tenant); - - await TenantConfigurationCache.RemoveManyAsync( - new[] - { - TenantConfigurationCacheItem.CalculateCacheKey(tenant.Id, null), - TenantConfigurationCacheItem.CalculateCacheKey(null, tenant.Name), - }); } [Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)] diff --git a/modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantConfigurationCacheItemInvalidator.cs b/modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantConfigurationCacheItemInvalidator.cs new file mode 100644 index 0000000000..e629186e20 --- /dev/null +++ b/modules/tenant-management/src/Volo.Abp.TenantManagement.Domain/Volo/Abp/TenantManagement/TenantConfigurationCacheItemInvalidator.cs @@ -0,0 +1,42 @@ +using System; +using System.Threading.Tasks; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events; +using Volo.Abp.EventBus; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.TenantManagement; + + +public class TenantConfigurationCacheItemInvalidator : + ILocalEventHandler>, + ILocalEventHandler>, ITransientDependency +{ + protected IDistributedCache Cache { get; } + + public TenantConfigurationCacheItemInvalidator(IDistributedCache cache) + { + Cache = cache; + } + + public virtual async Task HandleEventAsync(EntityChangedEventData eventData) + { + await ClearCacheAsync(eventData.Entity.Id, eventData.Entity.Name); + } + + public virtual async Task HandleEventAsync(EntityDeletedEventData eventData) + { + await ClearCacheAsync(eventData.Entity.Id, eventData.Entity.Name); + } + + protected virtual async Task ClearCacheAsync(Guid? id, string name) + { + await Cache.RemoveManyAsync( + new[] + { + TenantConfigurationCacheItem.CalculateCacheKey(id, null), + TenantConfigurationCacheItem.CalculateCacheKey(null, name), + }); + } +}