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 deleted file mode 100644 index 31085d063..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/ConnectionStringChangedEventHandler.cs +++ /dev/null @@ -1,90 +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.EventBus.Distributed; -using Volo.Abp.MultiTenancy; -using Volo.Abp.TenantManagement; -using Volo.Abp.Uow; - -namespace LINGYUN.Abp.MultiTenancy.DbFinder.EventBus.Distributed -{ - public class ConnectionStringChangedEventHandler : - IDistributedEventHandler, - IDistributedEventHandler, - ITransientDependency - { - private readonly ILogger _logger; - private readonly ICurrentTenant _currentTenant; - private readonly ITenantRepository _tenantRepository; - private readonly IDistributedCache _cache; - - public ConnectionStringChangedEventHandler( - ICurrentTenant currentTenant, - ITenantRepository tenantRepository, - ILogger logger, - IDistributedCache cache) - { - _cache = cache; - _logger = logger; - _currentTenant = currentTenant; - _tenantRepository = tenantRepository; - } - - [UnitOfWork] - public virtual async Task HandleEventAsync(ConnectionStringCreatedEventData eventData) - { - try - { - using (_currentTenant.Change(null)) - { - var tenant = await _tenantRepository.FindAsync(eventData.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.Id.ToString()), - cacheItem); - - await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name), - cacheItem); - } - } - catch (Exception ex) - { - _logger.LogException(ex); - } - } - - public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) - { - try - { - using (_currentTenant.Change(null)) - { - await _cache.RemoveManyAsync( - new string[] - { - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), - TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) - }); - } - } - catch (Exception ex) - { - _logger.LogException(ex); - } - } - } -} 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 index 67c338411..cdb9dd896 100644 --- 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 @@ -16,6 +16,8 @@ public class TenantSynchronizer : IDistributedEventHandler>, IDistributedEventHandler>, IDistributedEventHandler>, + IDistributedEventHandler, + IDistributedEventHandler, ITransientDependency { private readonly ILogger _logger; @@ -35,38 +37,82 @@ public class TenantSynchronizer : _tenantRepository = tenantRepository; } + /// + /// 处理租户变更事件 + /// + /// + /// 更新租户缓存 + /// + /// + /// [UnitOfWork] public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) { - await UpdateCacheItemAsync(eventData.Entity); + await UpdateCacheItemAsync(eventData.Entity.Id, eventData.Entity.Name); } + /// + /// 处理租户新增事件 + /// + /// + /// 更新租户缓存 + /// + /// + /// [UnitOfWork] public virtual async Task HandleEventAsync(EntityCreatedEto eventData) { - await UpdateCacheItemAsync(eventData.Entity); + await UpdateCacheItemAsync(eventData.Entity.Id, eventData.Entity.Name); } + /// + /// 处理租户删除事件 + /// + /// + /// 删除租户缓存 + /// + /// + /// 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()), - }); - } + await RemoveCacheItemAsync(eventData.Entity.Id, eventData.Entity.Name); + } + + /// + /// 处理租户连接字符串创建事件 + /// + /// + /// 更新租户缓存 + /// + /// + /// + [UnitOfWork] + public virtual async Task HandleEventAsync(ConnectionStringCreatedEventData eventData) + { + await UpdateCacheItemAsync(eventData.TenantId, eventData.TenantName); } - protected virtual async Task UpdateCacheItemAsync(TenantEto tenant) + /// + /// 处理租户连接字符串删除事件 + /// + /// + /// 删除租户缓存 + /// + /// + /// + public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) + { + // TODO: 用更新还是用删除? + await RemoveCacheItemAsync(eventData.TenantId, eventData.TenantName); + } + + protected virtual async Task UpdateCacheItemAsync(Guid tenantId, string tenantName = null) { try { using (_currentTenant.Change(null)) { - var findTenant = await _tenantRepository.FindAsync(tenant.Id, true); + var findTenant = await _tenantRepository.FindAsync(tenantId, true); if (findTenant == null) { return; @@ -76,7 +122,7 @@ public class TenantSynchronizer : { connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; } - var cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); + var cacheItem = new TenantConfigurationCacheItem(findTenant.Id, findTenant.Name, connectionStrings); await _cache.SetAsync( TenantConfigurationCacheItem.CalculateCacheKey(findTenant.Id.ToString()), @@ -92,4 +138,16 @@ public class TenantSynchronizer : _logger.LogException(ex); } } + + protected virtual async Task RemoveCacheItemAsync(Guid tenantId, string tenantName = null) + { + using (_currentTenant.Change(null)) + { + await _cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantId.ToString())); + if (!tenantName.IsNullOrWhiteSpace()) + { + await _cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantName)); + } + } + } } 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 deleted file mode 100644 index 8d0c8e8d2..000000000 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/ConnectionStringChangedEventHandler.cs +++ /dev/null @@ -1,67 +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.EventBus.Distributed; -using Volo.Abp.MultiTenancy; - -namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed -{ - public class ConnectionStringChangedEventHandler : - IDistributedEventHandler, - 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) - { - using (_currentTenant.Change(null)) - { - 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(tenantDto.Name), - cacheItem); - } - } - - public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) - { - 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/TenantSynchronizer.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantSynchronizer.cs index 6875c71a2..d45304406 100644 --- 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 @@ -1,4 +1,5 @@ using LINGYUN.Abp.TenantManagement; +using System; using System.Threading.Tasks; using Volo.Abp.Caching; using Volo.Abp.Data; @@ -14,6 +15,8 @@ public class TenantSynchronizer : IDistributedEventHandler>, IDistributedEventHandler>, IDistributedEventHandler>, + IDistributedEventHandler, + IDistributedEventHandler, ITransientDependency { private readonly ICurrentTenant _currentTenant; @@ -32,33 +35,35 @@ public class TenantSynchronizer : public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) { - await UpdateCacheItemAsync(eventData.Entity); + await UpdateCacheItemAsync(eventData.Entity.Id, eventData.Entity.Name); } public virtual async Task HandleEventAsync(EntityCreatedEto eventData) { - await UpdateCacheItemAsync(eventData.Entity); + await UpdateCacheItemAsync(eventData.Entity.Id, eventData.Entity.Name); } 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()), - }); - } + await RemoveCacheItemAsync(eventData.Entity.Id, eventData.Entity.Name); + } + + public virtual async Task HandleEventAsync(ConnectionStringCreatedEventData eventData) + { + await UpdateCacheItemAsync(eventData.TenantId, eventData.TenantName); + } + + public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) + { + await RemoveCacheItemAsync(eventData.TenantId, eventData.TenantName); } - protected virtual async Task UpdateCacheItemAsync(TenantEto tenant) + protected virtual async Task UpdateCacheItemAsync(Guid tenantId, string tenantName = null) { using (_currentTenant.Change(null)) { - var tenantDto = await _tenantAppService.GetAsync(tenant.Id); - var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(tenant.Id); + var tenantDto = await _tenantAppService.GetAsync(tenantId); + var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(tenantId); var connectionStrings = new ConnectionStrings(); foreach (var tenantConnectionString in tenantConnectionStringsDto.Items) { @@ -67,12 +72,27 @@ public class TenantSynchronizer : var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(tenant.Id.ToString()), + TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Id.ToString()), cacheItem); await _cache.SetAsync( - TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name), + TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name), cacheItem); } } + + protected virtual async Task RemoveCacheItemAsync(Guid tenantId, string tenantName = null) + { + using (_currentTenant.Change(null)) + { + using (_currentTenant.Change(null)) + { + await _cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantId.ToString())); + if (!tenantName.IsNullOrWhiteSpace()) + { + await _cache.RemoveAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantName)); + } + } + } + } } diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringCreatedEventData.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringCreatedEventData.cs index b7bd16461..0563244d7 100644 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringCreatedEventData.cs +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringCreatedEventData.cs @@ -4,7 +4,9 @@ namespace LINGYUN.Abp.MultiTenancy { public class ConnectionStringCreatedEventData { - public Guid Id { get; set; } + public Guid TenantId { get; set; } + + public string TenantName { get; set; } public string Name { get; set; } } diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringDeletedEventData.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringDeletedEventData.cs index 6890049c1..a284bd11e 100644 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringDeletedEventData.cs +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy/LINGYUN/Abp/MultiTenancy/ConnectionStringDeletedEventData.cs @@ -4,7 +4,9 @@ namespace LINGYUN.Abp.MultiTenancy { public class ConnectionStringDeletedEventData { - public Guid Id { get; set; } + public Guid TenantId { get; set; } + + public string TenantName { get; set; } public string Name { get; set; } } diff --git a/aspnet-core/modules/tenants/LINGYUN.Abp.TenantManagement.Application/LINGYUN/Abp/TenantManagement/TenantAppService.cs b/aspnet-core/modules/tenants/LINGYUN.Abp.TenantManagement.Application/LINGYUN/Abp/TenantManagement/TenantAppService.cs index b0457bf09..26a24a97f 100644 --- a/aspnet-core/modules/tenants/LINGYUN.Abp.TenantManagement.Application/LINGYUN/Abp/TenantManagement/TenantAppService.cs +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.TenantManagement.Application/LINGYUN/Abp/TenantManagement/TenantAppService.cs @@ -151,7 +151,8 @@ namespace LINGYUN.Abp.TenantManagement { var eventData = new ConnectionStringCreatedEventData { - Id = tenant.Id, + TenantId = tenant.Id, + TenantName = tenant.Name, Name = tenantConnectionStringCreateOrUpdate.Name }; @@ -174,12 +175,17 @@ namespace LINGYUN.Abp.TenantManagement tenant.RemoveConnectionString(name); - var eventData = new ConnectionStringDeletedEventData + CurrentUnitOfWork.OnCompleted(async () => { - Id = tenant.Id, - Name = name - }; - await EventBus.PublishAsync(eventData); + var eventData = new ConnectionStringDeletedEventData + { + TenantId = tenant.Id, + TenantName = tenant.Name, + Name = name + }; + + await EventBus.PublishAsync(eventData); + }); await TenantRepository.UpdateAsync(tenant); }