From 00382e9d4ed122a44927e10b6ad7d302d5ae5cd6 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sat, 4 Jul 2020 09:11:23 +0800 Subject: [PATCH] Increase event subscriptions for tenants --- .../Distributed/TenantCreateEventHandler.cs | 50 +++++++++++++++++++ .../Distributed/TenantDeleteEventHandler.cs | 26 ++++++++++ .../Distributed/TenantUpdateEventHandler.cs | 50 +++++++++++++++++++ .../Distributed/TenantCreateEventHandler.cs | 40 +++++++++++++++ .../Distributed/TenantDeleteEventHandler.cs | 26 ++++++++++ .../Distributed/TenantUpdateEventHandler.cs | 40 +++++++++++++++ 6 files changed, 232 insertions(+) create mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs create 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/TenantUpdateEventHandler.cs create mode 100644 aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs create 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/TenantUpdateEventHandler.cs 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 new file mode 100644 index 000000000..aeeeb16a6 --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs @@ -0,0 +1,50 @@ +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.DbFinder.EventBus.Distributed +{ + public class TenantCreateEventHandler : IDistributedEventHandler>, ITransientDependency + { + private readonly IDataFilter _dataFilter; + private readonly ITenantRepository _tenantRepository; + private readonly IDistributedCache _cache; + + public TenantCreateEventHandler( + IDataFilter dataFilter, + ITenantRepository tenantRepository, + IDistributedCache cache) + { + _cache = cache; + _dataFilter = dataFilter; + _tenantRepository = tenantRepository; + } + + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + { + // 禁用租户过滤器 + using (_dataFilter.Disable()) + { + 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); + + var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); + await _cache.SetAsync(cacheKey, cacheItem); + } + } + } +} 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 new file mode 100644 index 000000000..3d77fdad3 --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantDeleteEventHandler.cs @@ -0,0 +1,26 @@ +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) + { + var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); + await _cache.RemoveAsync(cacheKey); + } + } +} 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 new file mode 100644 index 000000000..2d0c597a2 --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantUpdateEventHandler.cs @@ -0,0 +1,50 @@ +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.DbFinder.EventBus.Distributed +{ + public class TenantUpdateEventHandler : IDistributedEventHandler>, ITransientDependency + { + private readonly IDataFilter _dataFilter; + private readonly ITenantRepository _tenantRepository; + private readonly IDistributedCache _cache; + + public TenantUpdateEventHandler( + IDataFilter dataFilter, + ITenantRepository tenantRepository, + IDistributedCache cache) + { + _cache = cache; + _dataFilter = dataFilter; + _tenantRepository = tenantRepository; + } + + public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) + { + // 禁用租户过滤器 + using (_dataFilter.Disable()) + { + 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); + + var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); + await _cache.SetAsync(cacheKey, cacheItem); + } + } + } +} 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 new file mode 100644 index 000000000..8b29c7971 --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs @@ -0,0 +1,40 @@ +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); + + var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); + await _cache.SetAsync(cacheKey, 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 new file mode 100644 index 000000000..0edee7cfc --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantDeleteEventHandler.cs @@ -0,0 +1,26 @@ +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) + { + var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); + await _cache.RemoveAsync(cacheKey); + } + } +} 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 new file mode 100644 index 000000000..2a052b0f0 --- /dev/null +++ b/aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantUpdateEventHandler.cs @@ -0,0 +1,40 @@ +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); + + var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()); + await _cache.SetAsync(cacheKey, cacheItem); + } + } +}