From e1dfb3a523b6087c799e1bebc1e872685b747ab8 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sat, 12 Feb 2022 17:39:46 +0800 Subject: [PATCH] feat(multitenancy): remove redundant and repeated tenant event handler --- .../Abp/EventBus/CAP/AbpCapSerializer.cs | 2 +- .../ConnectionStringChangedEventHandler.cs | 3 +- .../Distributed/TenantCreateEventHandler.cs | 68 ------------- .../Distributed/TenantDeleteEventHandler.cs | 29 ------ .../Distributed/TenantSynchronizer.cs | 95 +++++++++++++++++++ .../Distributed/TenantUpdateEventHandler.cs | 68 ------------- .../DbFinder/TenantConfigurationCacheItem.cs | 5 +- .../ConnectionStringChangedEventHandler.cs | 47 +++++---- .../Distributed/TenantCreateEventHandler.cs | 45 --------- .../Distributed/TenantDeleteEventHandler.cs | 29 ------ .../Distributed/TenantSynchronizer.cs | 78 +++++++++++++++ .../Distributed/TenantUpdateEventHandler.cs | 45 --------- .../TenantConfigurationCacheItem.cs | 3 +- .../Properties/launchSettings.json | 8 ++ 14 files changed, 218 insertions(+), 307 deletions(-) delete mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs delete mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantDeleteEventHandler.cs create mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantSynchronizer.cs delete mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantUpdateEventHandler.cs delete mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs delete mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantDeleteEventHandler.cs create mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantSynchronizer.cs delete mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantUpdateEventHandler.cs create mode 100644 aspnet-core/tests/LINGYUN.Abp.EntityFrameworkCore.Tests/Properties/launchSettings.json diff --git a/aspnet-core/modules/common/LINGYUN.Abp.EventBus.CAP/LINGYUN/Abp/EventBus/CAP/AbpCapSerializer.cs b/aspnet-core/modules/common/LINGYUN.Abp.EventBus.CAP/LINGYUN/Abp/EventBus/CAP/AbpCapSerializer.cs index 1d96bff04..865e289d9 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.EventBus.CAP/LINGYUN/Abp/EventBus/CAP/AbpCapSerializer.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.EventBus.CAP/LINGYUN/Abp/EventBus/CAP/AbpCapSerializer.cs @@ -43,7 +43,7 @@ namespace LINGYUN.Abp.EventBus.CAP public Task DeserializeAsync(TransportMessage transportMessage, Type valueType) { - if (valueType == null || transportMessage.Body == null) + if (valueType == null || transportMessage.Body == null || transportMessage.Body.Length == 0) { return Task.FromResult(new Message(transportMessage.Headers, null)); } diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/ConnectionStringChangedEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/ConnectionStringChangedEventHandler.cs index 81263f7c5..31085d063 100644 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/ConnectionStringChangedEventHandler.cs +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/ConnectionStringChangedEventHandler.cs @@ -74,7 +74,8 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed using (_currentTenant.Change(null)) { await _cache.RemoveManyAsync( - new string[] { + new string[] + { TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) }); diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs deleted file mode 100644 index c51113e63..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs +++ /dev/null @@ -1,68 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using System.Threading.Tasks; -using Volo.Abp.Caching; -using Volo.Abp.Data; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.MultiTenancy; -using Volo.Abp.TenantManagement; -using Volo.Abp.Uow; - -namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed -{ - public class TenantCreateEventHandler : IDistributedEventHandler>, ITransientDependency - { - private readonly ILogger _logger; - private readonly ICurrentTenant _currentTenant; - private readonly ITenantRepository _tenantRepository; - private readonly IDistributedCache _cache; - - public TenantCreateEventHandler( - ICurrentTenant currentTenant, - ITenantRepository tenantRepository, - ILogger logger, - IDistributedCache cache) - { - _cache = cache; - _logger = logger; - _currentTenant = currentTenant; - _tenantRepository = tenantRepository; - } - - [UnitOfWork] - public virtual async Task HandleEventAsync(EntityCreatedEto eventData) - { - try - { - using (_currentTenant.Change(null)) - { - var tenant = await _tenantRepository.FindAsync(eventData.Entity.Id, true); - if (tenant == null) - { - return; - } - var connectionStrings = new ConnectionStrings(); - foreach (var tenantConnectionString in tenant.ConnectionStrings) - { - connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; - } - var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), - cacheItem); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), - cacheItem); - } - } - catch (Exception ex) - { - _logger.LogException(ex); - } - } - } -} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantDeleteEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantDeleteEventHandler.cs deleted file mode 100644 index 80f898b32..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantDeleteEventHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.Caching; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.TenantManagement; - -namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed -{ - public class TenantDeleteEventHandler : IDistributedEventHandler>, ITransientDependency - { - private readonly IDistributedCache _cache; - - public TenantDeleteEventHandler( - IDistributedCache cache) - { - _cache = cache; - } - - public virtual async Task HandleEventAsync(EntityDeletedEto eventData) - { - await _cache.RemoveAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString())); - - await _cache.RemoveAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name)); - } - } -} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantSynchronizer.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantSynchronizer.cs new file mode 100644 index 000000000..67c338411 --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantSynchronizer.cs @@ -0,0 +1,95 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; +using Volo.Abp.Caching; +using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.MultiTenancy; +using Volo.Abp.TenantManagement; +using Volo.Abp.Uow; + +namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed; + +public class TenantSynchronizer : + IDistributedEventHandler>, + IDistributedEventHandler>, + IDistributedEventHandler>, + ITransientDependency +{ + private readonly ILogger _logger; + private readonly ICurrentTenant _currentTenant; + private readonly ITenantRepository _tenantRepository; + private readonly IDistributedCache _cache; + + public TenantSynchronizer( + ICurrentTenant currentTenant, + ITenantRepository tenantRepository, + ILogger logger, + IDistributedCache cache) + { + _cache = cache; + _logger = logger; + _currentTenant = currentTenant; + _tenantRepository = tenantRepository; + } + + [UnitOfWork] + public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) + { + await UpdateCacheItemAsync(eventData.Entity); + } + + [UnitOfWork] + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + { + await UpdateCacheItemAsync(eventData.Entity); + } + + public virtual async Task HandleEventAsync(EntityDeletedEto eventData) + { + using (_currentTenant.Change(null)) + { + await _cache.RemoveManyAsync( + new string[] + { + TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), + TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), + }); + } + } + + protected virtual async Task UpdateCacheItemAsync(TenantEto tenant) + { + try + { + using (_currentTenant.Change(null)) + { + var findTenant = await _tenantRepository.FindAsync(tenant.Id, true); + if (findTenant == null) + { + return; + } + var connectionStrings = new ConnectionStrings(); + foreach (var tenantConnectionString in findTenant.ConnectionStrings) + { + connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; + } + var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); + + await _cache.SetAsync( + TenantConfigurationCacheItem.CalculateCacheKey(findTenant.Id.ToString()), + cacheItem); + + await _cache.SetAsync( + TenantConfigurationCacheItem.CalculateCacheKey(findTenant.Name), + cacheItem); + } + } + catch (Exception ex) + { + _logger.LogException(ex); + } + } +} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantUpdateEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantUpdateEventHandler.cs deleted file mode 100644 index 610511698..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantUpdateEventHandler.cs +++ /dev/null @@ -1,68 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using System.Threading.Tasks; -using Volo.Abp.Caching; -using Volo.Abp.Data; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.MultiTenancy; -using Volo.Abp.TenantManagement; -using Volo.Abp.Uow; - -namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed -{ - public class TenantUpdateEventHandler : IDistributedEventHandler>, ITransientDependency - { - private readonly ILogger _logger; - private readonly ICurrentTenant _currentTenant; - private readonly ITenantRepository _tenantRepository; - private readonly IDistributedCache _cache; - - public TenantUpdateEventHandler( - ICurrentTenant currentTenant, - ITenantRepository tenantRepository, - ILogger logger, - IDistributedCache cache) - { - _cache = cache; - _logger = logger; - _currentTenant = currentTenant; - _tenantRepository = tenantRepository; - } - - [UnitOfWork] - public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) - { - try - { - using (_currentTenant.Change(null)) - { - var tenant = await _tenantRepository.FindAsync(eventData.Entity.Id, true); - if (tenant == null) - { - return; - } - var connectionStrings = new ConnectionStrings(); - foreach (var tenantConnectionString in tenant.ConnectionStrings) - { - connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; - } - var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), - cacheItem); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), - cacheItem); - } - } - catch(Exception ex) - { - _logger.LogException(ex); - } - } - } -} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantConfigurationCacheItem.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantConfigurationCacheItem.cs index 55616c480..88da5c93c 100644 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantConfigurationCacheItem.cs +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantConfigurationCacheItem.cs @@ -4,7 +4,8 @@ using Volo.Abp.Data; namespace LINGYUN.Abp.MultiTenancy.DbFinder { public class TenantConfigurationCacheItem - { + { + protected const string FormatKey = "pn:{0},k:{1}"; public Guid Id { get; set; } public string Name { get; set; } // TODO: 是否需要加密存储? @@ -20,7 +21,7 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder } public static string CalculateCacheKey(string key) { - return "p:tenant" + ",k:" + key; + return string.Format(FormatKey, "tenant", key); } } } diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/ConnectionStringChangedEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/ConnectionStringChangedEventHandler.cs index f0f791717..8d0c8e8d2 100644 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/ConnectionStringChangedEventHandler.cs +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/ConnectionStringChangedEventHandler.cs @@ -4,6 +4,7 @@ using Volo.Abp.Caching; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.MultiTenancy; namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed { @@ -12,45 +13,55 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed IDistributedEventHandler, ITransientDependency { + private readonly ICurrentTenant _currentTenant; private readonly ITenantAppService _tenantAppService; private readonly IDistributedCache _cache; public ConnectionStringChangedEventHandler( + ICurrentTenant currentTenant, ITenantAppService tenantAppService, IDistributedCache cache) { _cache = cache; + _currentTenant = currentTenant; _tenantAppService = tenantAppService; } public virtual async Task HandleEventAsync(ConnectionStringCreatedEventData eventData) { - var tenantDto = await _tenantAppService.GetAsync(eventData.Id); - var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Id); - var connectionStrings = new ConnectionStrings(); - foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) + using (_currentTenant.Change(null)) { - connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; - } - var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); + var tenantDto = await _tenantAppService.GetAsync(eventData.Id); + var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Id); + var connectionStrings = new ConnectionStrings(); + foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) + { + connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; + } + var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), - cacheItem); + await _cache.SetAsync( + TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), + cacheItem); - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name), - cacheItem); + await _cache.SetAsync( + TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name), + cacheItem); + } } public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) { - await _cache.RemoveManyAsync( - new string[] { - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) - }); + using (_currentTenant.Change(null)) + { + await _cache.RemoveManyAsync( + new string[] + { + TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), + TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) + }); + } } } } diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs deleted file mode 100644 index c34019185..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs +++ /dev/null @@ -1,45 +0,0 @@ -using LINGYUN.Abp.TenantManagement; -using System.Threading.Tasks; -using Volo.Abp.Caching; -using Volo.Abp.Data; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.TenantManagement; - -namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed -{ - public class TenantCreateEventHandler : IDistributedEventHandler>, ITransientDependency - { - private readonly ITenantAppService _tenantAppService; - private readonly IDistributedCache _cache; - - public TenantCreateEventHandler( - ITenantAppService tenantAppService, - IDistributedCache cache) - { - _cache = cache; - _tenantAppService = tenantAppService; - } - - public virtual async Task HandleEventAsync(EntityCreatedEto eventData) - { - var tenantDto = await _tenantAppService.GetAsync(eventData.Entity.Id); - var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Entity.Id); - var connectionStrings = new ConnectionStrings(); - foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) - { - connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; - } - var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), - cacheItem); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), - cacheItem); - } - } -} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantDeleteEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantDeleteEventHandler.cs deleted file mode 100644 index 5ff09a590..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantDeleteEventHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.Caching; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.TenantManagement; - -namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed -{ - public class TenantDeleteEventHandler : IDistributedEventHandler>, ITransientDependency - { - private readonly IDistributedCache _cache; - - public TenantDeleteEventHandler( - IDistributedCache cache) - { - _cache = cache; - } - - public virtual async Task HandleEventAsync(EntityDeletedEto eventData) - { - await _cache.RemoveAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString())); - - await _cache.RemoveAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name)); - } - } -} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantSynchronizer.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantSynchronizer.cs new file mode 100644 index 000000000..6875c71a2 --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantSynchronizer.cs @@ -0,0 +1,78 @@ +using LINGYUN.Abp.TenantManagement; +using System.Threading.Tasks; +using Volo.Abp.Caching; +using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.MultiTenancy; +using Volo.Abp.TenantManagement; + +namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed; + +public class TenantSynchronizer : + IDistributedEventHandler>, + IDistributedEventHandler>, + IDistributedEventHandler>, + ITransientDependency +{ + private readonly ICurrentTenant _currentTenant; + private readonly ITenantAppService _tenantAppService; + private readonly IDistributedCache _cache; + + public TenantSynchronizer( + ICurrentTenant currentTenant, + ITenantAppService tenantAppService, + IDistributedCache cache) + { + _cache = cache; + _currentTenant = currentTenant; + _tenantAppService = tenantAppService; + } + + public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) + { + await UpdateCacheItemAsync(eventData.Entity); + } + + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + { + await UpdateCacheItemAsync(eventData.Entity); + } + + public virtual async Task HandleEventAsync(EntityDeletedEto eventData) + { + using (_currentTenant.Change(null)) + { + await _cache.RemoveManyAsync( + new string[] + { + TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), + TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), + }); + } + } + + protected virtual async Task UpdateCacheItemAsync(TenantEto tenant) + { + using (_currentTenant.Change(null)) + { + var tenantDto = await _tenantAppService.GetAsync(tenant.Id); + var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(tenant.Id); + var connectionStrings = new ConnectionStrings(); + foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) + { + connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; + } + var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); + + await _cache.SetAsync( + TenantConfigurationCacheItem.CalculateCacheKey(tenant.Id.ToString()), + cacheItem); + + await _cache.SetAsync( + TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name), + cacheItem); + } + } +} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantUpdateEventHandler.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantUpdateEventHandler.cs deleted file mode 100644 index 76bd0e80e..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantUpdateEventHandler.cs +++ /dev/null @@ -1,45 +0,0 @@ -using LINGYUN.Abp.TenantManagement; -using System.Threading.Tasks; -using Volo.Abp.Caching; -using Volo.Abp.Data; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events.Distributed; -using Volo.Abp.EventBus.Distributed; -using Volo.Abp.TenantManagement; - -namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed -{ - public class TenantUpdateEventHandler : IDistributedEventHandler>, ITransientDependency - { - private readonly ITenantAppService _tenantAppService; - private readonly IDistributedCache _cache; - - public TenantUpdateEventHandler( - ITenantAppService tenantAppService, - IDistributedCache cache) - { - _cache = cache; - _tenantAppService = tenantAppService; - } - - public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) - { - var tenantDto = await _tenantAppService.GetAsync(eventData.Entity.Id); - var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Entity.Id); - var connectionStrings = new ConnectionStrings(); - foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) - { - connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; - } - var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()), - cacheItem); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name), - cacheItem); - } - } -} diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantConfigurationCacheItem.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantConfigurationCacheItem.cs index 6a1994d2d..8ec83580b 100644 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantConfigurationCacheItem.cs +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantConfigurationCacheItem.cs @@ -5,6 +5,7 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService { public class TenantConfigurationCacheItem { + protected const string FormatKey = "pn:{0},k:{1}"; public Guid Id { get; set; } public string Name { get; set; } // TODO: 是否需要加密存储? @@ -20,7 +21,7 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService } public static string CalculateCacheKey(string key) { - return "p:tenant" + ",k:" + key; + return string.Format(FormatKey, "tenant", key); } } } diff --git a/aspnet-core/tests/LINGYUN.Abp.EntityFrameworkCore.Tests/Properties/launchSettings.json b/aspnet-core/tests/LINGYUN.Abp.EntityFrameworkCore.Tests/Properties/launchSettings.json new file mode 100644 index 000000000..8aa9e542e --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.EntityFrameworkCore.Tests/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "WSL": { + "commandName": "WSL2", + "distributionName": "" + } + } +} \ No newline at end of file