Browse Source

feat(multitenancy): remove redundant and repeated tenant event handler

pull/490/head
cKey 4 years ago
parent
commit
e1dfb3a523
  1. 2
      aspnet-core/modules/common/LINGYUN.Abp.EventBus.CAP/LINGYUN/Abp/EventBus/CAP/AbpCapSerializer.cs
  2. 3
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/ConnectionStringChangedEventHandler.cs
  3. 68
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs
  4. 29
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantDeleteEventHandler.cs
  5. 95
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantSynchronizer.cs
  6. 68
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantUpdateEventHandler.cs
  7. 5
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantConfigurationCacheItem.cs
  8. 47
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/ConnectionStringChangedEventHandler.cs
  9. 45
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs
  10. 29
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantDeleteEventHandler.cs
  11. 78
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantSynchronizer.cs
  12. 45
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantUpdateEventHandler.cs
  13. 3
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantConfigurationCacheItem.cs
  14. 8
      aspnet-core/tests/LINGYUN.Abp.EntityFrameworkCore.Tests/Properties/launchSettings.json

2
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<Message> DeserializeAsync(TransportMessage transportMessage, Type valueType) public Task<Message> 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)); return Task.FromResult(new Message(transportMessage.Headers, null));
} }

3
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)) using (_currentTenant.Change(null))
{ {
await _cache.RemoveManyAsync( await _cache.RemoveManyAsync(
new string[] { new string[]
{
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()),
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name)
}); });

68
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantCreateEventHandler.cs

@ -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<EntityCreatedEto<TenantEto>>, ITransientDependency
{
private readonly ILogger<TenantCreateEventHandler> _logger;
private readonly ICurrentTenant _currentTenant;
private readonly ITenantRepository _tenantRepository;
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantCreateEventHandler(
ICurrentTenant currentTenant,
ITenantRepository tenantRepository,
ILogger<TenantCreateEventHandler> logger,
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
_logger = logger;
_currentTenant = currentTenant;
_tenantRepository = tenantRepository;
}
[UnitOfWork]
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> 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);
}
}
}
}

29
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantDeleteEventHandler.cs

@ -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<EntityDeletedEto<TenantEto>>, ITransientDependency
{
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantDeleteEventHandler(
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
}
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData)
{
await _cache.RemoveAsync(
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()));
await _cache.RemoveAsync(
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name));
}
}
}

95
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<EntityCreatedEto<TenantEto>>,
IDistributedEventHandler<EntityUpdatedEto<TenantEto>>,
IDistributedEventHandler<EntityDeletedEto<TenantEto>>,
ITransientDependency
{
private readonly ILogger<TenantSynchronizer> _logger;
private readonly ICurrentTenant _currentTenant;
private readonly ITenantRepository _tenantRepository;
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantSynchronizer(
ICurrentTenant currentTenant,
ITenantRepository tenantRepository,
ILogger<TenantSynchronizer> logger,
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
_logger = logger;
_currentTenant = currentTenant;
_tenantRepository = tenantRepository;
}
[UnitOfWork]
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData)
{
await UpdateCacheItemAsync(eventData.Entity);
}
[UnitOfWork]
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData)
{
await UpdateCacheItemAsync(eventData.Entity);
}
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> 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);
}
}
}

68
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/EventBus/Distributed/TenantUpdateEventHandler.cs

@ -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<EntityUpdatedEto<TenantEto>>, ITransientDependency
{
private readonly ILogger<TenantUpdateEventHandler> _logger;
private readonly ICurrentTenant _currentTenant;
private readonly ITenantRepository _tenantRepository;
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantUpdateEventHandler(
ICurrentTenant currentTenant,
ITenantRepository tenantRepository,
ILogger<TenantUpdateEventHandler> logger,
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
_logger = logger;
_currentTenant = currentTenant;
_tenantRepository = tenantRepository;
}
[UnitOfWork]
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> 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);
}
}
}
}

5
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 namespace LINGYUN.Abp.MultiTenancy.DbFinder
{ {
public class TenantConfigurationCacheItem public class TenantConfigurationCacheItem
{ {
protected const string FormatKey = "pn:{0},k:{1}";
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
// TODO: 是否需要加密存储? // TODO: 是否需要加密存储?
@ -20,7 +21,7 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder
} }
public static string CalculateCacheKey(string key) public static string CalculateCacheKey(string key)
{ {
return "p:tenant" + ",k:" + key; return string.Format(FormatKey, "tenant", key);
} }
} }
} }

47
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.Data;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed
{ {
@ -12,45 +13,55 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService.EventBus.Distributed
IDistributedEventHandler<ConnectionStringDeletedEventData>, IDistributedEventHandler<ConnectionStringDeletedEventData>,
ITransientDependency ITransientDependency
{ {
private readonly ICurrentTenant _currentTenant;
private readonly ITenantAppService _tenantAppService; private readonly ITenantAppService _tenantAppService;
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache; private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public ConnectionStringChangedEventHandler( public ConnectionStringChangedEventHandler(
ICurrentTenant currentTenant,
ITenantAppService tenantAppService, ITenantAppService tenantAppService,
IDistributedCache<TenantConfigurationCacheItem> cache) IDistributedCache<TenantConfigurationCacheItem> cache)
{ {
_cache = cache; _cache = cache;
_currentTenant = currentTenant;
_tenantAppService = tenantAppService; _tenantAppService = tenantAppService;
} }
public virtual async Task HandleEventAsync(ConnectionStringCreatedEventData eventData) public virtual async Task HandleEventAsync(ConnectionStringCreatedEventData eventData)
{ {
var tenantDto = await _tenantAppService.GetAsync(eventData.Id); using (_currentTenant.Change(null))
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Id);
var connectionStrings = new ConnectionStrings();
foreach (var tenantConnectionString in tenantConnectionStringsDto.Items)
{ {
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; var tenantDto = await _tenantAppService.GetAsync(eventData.Id);
} var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Id);
var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings); 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( await _cache.SetAsync(
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()),
cacheItem); cacheItem);
await _cache.SetAsync( await _cache.SetAsync(
TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name), TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name),
cacheItem); cacheItem);
}
} }
public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData) public virtual async Task HandleEventAsync(ConnectionStringDeletedEventData eventData)
{ {
await _cache.RemoveManyAsync( using (_currentTenant.Change(null))
new string[] { {
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()), await _cache.RemoveManyAsync(
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name) new string[]
}); {
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Id.ToString()),
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Name)
});
}
} }
} }
} }

45
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantCreateEventHandler.cs

@ -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<EntityCreatedEto<TenantEto>>, ITransientDependency
{
private readonly ITenantAppService _tenantAppService;
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantCreateEventHandler(
ITenantAppService tenantAppService,
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
_tenantAppService = tenantAppService;
}
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> 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);
}
}
}

29
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantDeleteEventHandler.cs

@ -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<EntityDeletedEto<TenantEto>>, ITransientDependency
{
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantDeleteEventHandler(
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
}
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData)
{
await _cache.RemoveAsync(
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString()));
await _cache.RemoveAsync(
TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Name));
}
}
}

78
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<EntityCreatedEto<TenantEto>>,
IDistributedEventHandler<EntityUpdatedEto<TenantEto>>,
IDistributedEventHandler<EntityDeletedEto<TenantEto>>,
ITransientDependency
{
private readonly ICurrentTenant _currentTenant;
private readonly ITenantAppService _tenantAppService;
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantSynchronizer(
ICurrentTenant currentTenant,
ITenantAppService tenantAppService,
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
_currentTenant = currentTenant;
_tenantAppService = tenantAppService;
}
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData)
{
await UpdateCacheItemAsync(eventData.Entity);
}
public virtual async Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData)
{
await UpdateCacheItemAsync(eventData.Entity);
}
public virtual async Task HandleEventAsync(EntityDeletedEto<TenantEto> 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);
}
}
}

45
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/EventBus/Distributed/TenantUpdateEventHandler.cs

@ -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<EntityUpdatedEto<TenantEto>>, ITransientDependency
{
private readonly ITenantAppService _tenantAppService;
private readonly IDistributedCache<TenantConfigurationCacheItem> _cache;
public TenantUpdateEventHandler(
ITenantAppService tenantAppService,
IDistributedCache<TenantConfigurationCacheItem> cache)
{
_cache = cache;
_tenantAppService = tenantAppService;
}
public virtual async Task HandleEventAsync(EntityUpdatedEto<TenantEto> 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);
}
}
}

3
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 public class TenantConfigurationCacheItem
{ {
protected const string FormatKey = "pn:{0},k:{1}";
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
// TODO: 是否需要加密存储? // TODO: 是否需要加密存储?
@ -20,7 +21,7 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
} }
public static string CalculateCacheKey(string key) public static string CalculateCacheKey(string key)
{ {
return "p:tenant" + ",k:" + key; return string.Format(FormatKey, "tenant", key);
} }
} }
} }

8
aspnet-core/tests/LINGYUN.Abp.EntityFrameworkCore.Tests/Properties/launchSettings.json

@ -0,0 +1,8 @@
{
"profiles": {
"WSL": {
"commandName": "WSL2",
"distributionName": ""
}
}
}
Loading…
Cancel
Save