Browse Source

feat: Optimize the query for retrieving all tenant caches

pull/1503/head
colin 4 months ago
parent
commit
3580ec806a
  1. 13
      aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/Handlers/Distributed/NotificationEventHandler.cs
  2. 70
      aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/Handlers/Distributed/TenantSynchronizer.cs
  3. 12
      aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/MultiTenancy/ITenantConfigurationCache.cs
  4. 62
      aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/MultiTenancy/TenantConfigurationCache.cs
  5. 20
      aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/MultiTenancy/TenantConfigurationCacheItem.cs
  6. 9
      aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/BackgroundWorkers/BlobTempCleanupBackgroundWorker.cs
  7. 8
      aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/MultiTenancy/ITenantConfigurationCache.cs
  8. 47
      aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/MultiTenancy/TenantConfigurationCache.cs
  9. 19
      aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/MultiTenancy/TenantConfigurationCacheItem.cs
  10. 21
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantCacheItemInvalidator.cs
  11. 33
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantStore.cs
  12. 28
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantsCacheItem.cs
  13. 20
      aspnet-core/services/LY.MicroService.Applications.Single/EventBus/Distributed/NotificationEventHandler.cs
  14. 46
      aspnet-core/services/LY.MicroService.Applications.Single/EventBus/Distributed/TenantEventHandler.cs
  15. 10
      aspnet-core/services/LY.MicroService.Applications.Single/MultiTenancy/ITenantConfigurationCache.cs
  16. 59
      aspnet-core/services/LY.MicroService.Applications.Single/MultiTenancy/TenantConfigurationCache.cs
  17. 19
      aspnet-core/services/LY.MicroService.Applications.Single/MultiTenancy/TenantConfigurationCacheItem.cs
  18. 3
      aspnet-core/services/LY.MicroService.Applications.Single/appsettings.json
  19. 10
      aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/BackgroundWorkers/BlobTempCleanupBackgroundWorker.cs
  20. 10
      aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/MultiTenancy/ITenantConfigurationCache.cs
  21. 50
      aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/MultiTenancy/TenantConfigurationCache.cs
  22. 20
      aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/MultiTenancy/TenantConfigurationCacheItem.cs
  23. 13
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs
  24. 70
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/TenantSynchronizer.cs
  25. 12
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/ITenantConfigurationCache.cs
  26. 62
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCache.cs
  27. 20
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCacheItem.cs

13
aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/Handlers/Distributed/NotificationEventHandler.cs

@ -1,5 +1,4 @@
using LINGYUN.Abp.MicroService.MessageService.BackgroundJobs;
using LINGYUN.Abp.MicroService.MessageService.MultiTenancy;
using LINGYUN.Abp.Notifications;
using LINGYUN.Abp.Notifications.Templating;
using Microsoft.Extensions.Localization;
@ -57,9 +56,9 @@ namespace LINGYUN.Abp.MicroService.MessageService.Handlers.Distributed
/// </summary>
protected ICurrentTenant CurrentTenant { get; }
/// <summary>
/// Reference to <see cref="ITenantConfigurationCache"/>.
/// Reference to <see cref="ITenantStore"/>.
/// </summary>
protected ITenantConfigurationCache TenantConfigurationCache { get; }
protected ITenantStore TenantStore { get; }
/// <summary>
/// Reference to <see cref="IJsonSerializer"/>.
/// </summary>
@ -108,7 +107,7 @@ namespace LINGYUN.Abp.MicroService.MessageService.Handlers.Distributed
IClock clock,
ICurrentTenant currentTenant,
ISettingProvider settingProvider,
ITenantConfigurationCache tenantConfigurationCache,
ITenantStore tenantStore,
IJsonSerializer jsonSerializer,
ITemplateRenderer templateRenderer,
IBackgroundJobManager backgroundJobManager,
@ -123,7 +122,7 @@ namespace LINGYUN.Abp.MicroService.MessageService.Handlers.Distributed
{
Clock = clock;
Options = options.Value;
TenantConfigurationCache = tenantConfigurationCache;
TenantStore = tenantStore;
CurrentTenant = currentTenant;
SettingProvider = settingProvider;
JsonSerializer = jsonSerializer;
@ -165,7 +164,7 @@ namespace LINGYUN.Abp.MicroService.MessageService.Handlers.Distributed
{
await SendToTenantAsync(null, notification, eventData, result);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{
@ -202,7 +201,7 @@ namespace LINGYUN.Abp.MicroService.MessageService.Handlers.Distributed
{
await SendToTenantAsync(null, notification, eventData);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{

70
aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/Handlers/Distributed/TenantSynchronizer.cs

@ -1,70 +0,0 @@
using LINGYUN.Abp.Data.DbMigrator;
using LINGYUN.Abp.MicroService.MessageService.MultiTenancy;
using LINGYUN.Abp.Notifications;
using LINGYUN.Abp.Saas.Tenants;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace LINGYUN.Abp.MicroService.MessageService.Handlers.Distributed
{
public class TenantSynchronizer :
IDistributedEventHandler<EntityCreatedEto<TenantEto>>,
IDistributedEventHandler<EntityUpdatedEto<TenantEto>>,
IDistributedEventHandler<EntityDeletedEto<TenantEto>>,
IDistributedEventHandler<TenantConnectionStringUpdatedEto>,
ITransientDependency
{
protected ILogger<TenantSynchronizer> Logger { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IDbSchemaMigrator DbSchemaMigrator { get; }
protected INotificationSender NotificationSender { get; }
protected ITenantConfigurationCache TenantConfigurationCache { get; }
protected INotificationSubscriptionManager NotificationSubscriptionManager { get; }
public TenantSynchronizer(
ICurrentTenant currentTenant,
IDbSchemaMigrator dbSchemaMigrator,
IUnitOfWorkManager unitOfWorkManager,
INotificationSender notificationSender,
ITenantConfigurationCache tenantConfigurationCache,
INotificationSubscriptionManager notificationSubscriptionManager,
ILogger<TenantSynchronizer> logger)
{
Logger = logger;
CurrentTenant = currentTenant;
DbSchemaMigrator = dbSchemaMigrator;
UnitOfWorkManager = unitOfWorkManager;
TenantConfigurationCache = tenantConfigurationCache;
NotificationSender = notificationSender;
NotificationSubscriptionManager = notificationSubscriptionManager;
}
public async virtual Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(TenantConnectionStringUpdatedEto eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
}
}

12
aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/MultiTenancy/ITenantConfigurationCache.cs

@ -1,12 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.MicroService.MessageService.MultiTenancy;
public interface ITenantConfigurationCache
{
Task RefreshAsync();
Task<List<TenantConfiguration>> GetTenantsAsync();
}

62
aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/MultiTenancy/TenantConfigurationCache.cs

@ -1,62 +0,0 @@
using LINGYUN.Abp.Saas.Tenants;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.MicroService.MessageService.MultiTenancy;
public class TenantConfigurationCache : ITenantConfigurationCache, ITransientDependency
{
protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> TenantCache { get; }
public TenantConfigurationCache(
ITenantRepository tenantRepository,
IDistributedCache<TenantConfigurationCacheItem> tenantCache)
{
TenantRepository = tenantRepository;
TenantCache = tenantCache;
}
public async virtual Task RefreshAsync()
{
var cacheKey = GetCacheKey();
await TenantCache.RemoveAsync(cacheKey);
}
public async virtual Task<List<TenantConfiguration>> GetTenantsAsync()
{
return (await GetForCacheItemAsync()).Tenants;
}
protected async virtual Task<TenantConfigurationCacheItem> GetForCacheItemAsync()
{
var cacheKey = GetCacheKey();
var cacheItem = await TenantCache.GetAsync(cacheKey);
if (cacheItem == null)
{
var allActiveTenants = await TenantRepository.GetListAsync();
cacheItem = new TenantConfigurationCacheItem(
allActiveTenants
.Where(t => t.IsActive)
.Select(t => new TenantConfiguration(t.Id, t.Name)
{
IsActive = t.IsActive,
}).ToList());
await TenantCache.SetAsync(cacheKey, cacheItem);
}
return cacheItem;
}
protected virtual string GetCacheKey()
{
return "_Abp_Tenant_Configuration";
}
}

20
aspnet-core/aspire/LINGYUN.Abp.MicroService.MessageService/MultiTenancy/TenantConfigurationCacheItem.cs

@ -1,20 +0,0 @@
using System.Collections.Generic;
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.MicroService.MessageService.MultiTenancy;
[IgnoreMultiTenancy]
public class TenantConfigurationCacheItem
{
public List<TenantConfiguration> Tenants { get; set; }
public TenantConfigurationCacheItem()
{
Tenants = new List<TenantConfiguration>();
}
public TenantConfigurationCacheItem(List<TenantConfiguration> tenants)
{
Tenants = tenants;
}
}

9
aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/BackgroundWorkers/BlobTempCleanupBackgroundWorker.cs

@ -1,5 +1,4 @@
using LINGYUN.Abp.BlobManagement;
using LINGYUN.Abp.MicroService.PlatformService.MultiTenancy;
using Microsoft.Extensions.Options;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.DistributedLocking;
@ -12,7 +11,7 @@ public class BlobTempCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
{
protected ICurrentTenant CurrentTenant { get; }
protected IAbpDistributedLock DistributedLock { get; }
protected ITenantConfigurationCache TenantConfigurationCache { get; }
protected ITenantStore TenantStore { get; }
public BlobTempCleanupBackgroundWorker(
AbpAsyncTimer timer,
@ -20,12 +19,12 @@ public class BlobTempCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
IOptionsMonitor<AbpBlobManagementOptions> cleanupOptions,
IAbpDistributedLock distributedLock,
ICurrentTenant currentTenant,
ITenantConfigurationCache tenantConfigurationCache)
ITenantStore tenantStore)
: base(timer, serviceScopeFactory)
{
CurrentTenant = currentTenant;
DistributedLock = distributedLock;
TenantConfigurationCache = tenantConfigurationCache;
TenantStore = tenantStore;
timer.Period = cleanupOptions.CurrentValue.CleanupPeriod;
}
@ -41,7 +40,7 @@ public class BlobTempCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
{
await ExecuteCleanService(workerContext);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{

8
aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/MultiTenancy/ITenantConfigurationCache.cs

@ -1,8 +0,0 @@
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.MicroService.PlatformService.MultiTenancy;
public interface ITenantConfigurationCache
{
Task<List<TenantConfiguration>> GetTenantsAsync();
}

47
aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/MultiTenancy/TenantConfigurationCache.cs

@ -1,47 +0,0 @@
using LINGYUN.Abp.Saas.Tenants;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.MicroService.PlatformService.MultiTenancy;
public class TenantConfigurationCache : ITenantConfigurationCache, ITransientDependency
{
protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> TenantCache { get; }
public TenantConfigurationCache(
ITenantRepository tenantRepository,
IDistributedCache<TenantConfigurationCacheItem> tenantCache)
{
TenantRepository = tenantRepository;
TenantCache = tenantCache;
}
public async virtual Task<List<TenantConfiguration>> GetTenantsAsync()
{
return (await GetForCacheItemAsync()).Tenants;
}
protected async virtual Task<TenantConfigurationCacheItem> GetForCacheItemAsync()
{
var cacheKey = "_Abp_Tenant_Configuration";
var cacheItem = await TenantCache.GetAsync(cacheKey);
if (cacheItem == null)
{
var allActiveTenants = await TenantRepository.GetListAsync();
cacheItem = new TenantConfigurationCacheItem(
allActiveTenants
.Where(t => t.IsActive)
.Select(t => new TenantConfiguration(t.Id, t.Name)
{
IsActive = t.IsActive,
}).ToList());
await TenantCache.SetAsync(cacheKey, cacheItem);
}
return cacheItem;
}
}

19
aspnet-core/aspire/LINGYUN.Abp.MicroService.PlatformService/MultiTenancy/TenantConfigurationCacheItem.cs

@ -1,19 +0,0 @@
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.MicroService.PlatformService.MultiTenancy;
[IgnoreMultiTenancy]
public class TenantConfigurationCacheItem
{
public List<TenantConfiguration> Tenants { get; set; }
public TenantConfigurationCacheItem()
{
Tenants = new List<TenantConfiguration>();
}
public TenantConfigurationCacheItem(List<TenantConfiguration> tenants)
{
Tenants = tenants;
}
}

21
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantCacheItemInvalidator.cs

@ -17,11 +17,15 @@ public class TenantCacheItemInvalidator :
ILocalEventHandler<EntityChangedEventData<Tenant>>,
ITransientDependency
{
protected IDistributedCache<TenantCacheItem> Cache { get; }
protected IDistributedCache<TenantCacheItem> TenantCache { get; }
protected IDistributedCache<TenantsCacheItem> TenantsCache { get; }
public TenantCacheItemInvalidator(IDistributedCache<TenantCacheItem> cache)
public TenantCacheItemInvalidator(
IDistributedCache<TenantCacheItem> tenantCache,
IDistributedCache<TenantsCacheItem> tenantsCache)
{
Cache = cache;
TenantCache = tenantCache;
TenantsCache = tenantsCache;
}
public async virtual Task HandleEventAsync(EntityCreatedEto<TenantEto> eto)
@ -46,7 +50,7 @@ public class TenantCacheItemInvalidator :
protected async virtual Task RemoveTenantCache(Guid tenantId, string tenantName = null)
{
var keys = new string[]
var removeTenantKeys = new string[]
{
TenantCacheItem.CalculateCacheKey(null, tenantName),
TenantCacheItem.CalculateCacheKey(tenantId, null),
@ -55,6 +59,13 @@ public class TenantCacheItemInvalidator :
// 同时移除租户版本缓存
EditionCacheItem.CalculateCacheKey(tenantId)
};
await Cache.RemoveManyAsync(keys, considerUow: true);
await TenantCache.RemoveManyAsync(removeTenantKeys, considerUow: true);
var removeTenantsKeys = new string[]
{
TenantsCacheItem.CalculateCacheKey(true),
TenantsCacheItem.CalculateCacheKey(false)
};
await TenantsCache.RemoveManyAsync(removeTenantsKeys, considerUow: true);
}
}

33
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantStore.cs

@ -1,7 +1,7 @@
using System;
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
@ -15,18 +15,21 @@ public class TenantStore : ITenantStore, ITransientDependency
protected ITenantRepository TenantRepository { get; }
protected IObjectMapper<AbpSaasDomainModule> ObjectMapper { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IDistributedCache<TenantCacheItem> Cache { get; }
protected IDistributedCache<TenantCacheItem> TenantCache { get; }
protected IDistributedCache<TenantsCacheItem> TenantsCache { get; }
public TenantStore(
ITenantRepository tenantRepository,
IObjectMapper<AbpSaasDomainModule> objectMapper,
ICurrentTenant currentTenant,
IDistributedCache<TenantCacheItem> cache)
IDistributedCache<TenantCacheItem> tenantCache,
IDistributedCache<TenantsCacheItem> tenantsCache)
{
TenantRepository = tenantRepository;
ObjectMapper = objectMapper;
CurrentTenant = currentTenant;
Cache = cache;
TenantCache = tenantCache;
TenantsCache = tenantsCache;
}
public async virtual Task<TenantConfiguration> FindAsync(string name)
@ -41,8 +44,16 @@ public class TenantStore : ITenantStore, ITransientDependency
public async virtual Task<IReadOnlyList<TenantConfiguration>> GetListAsync(bool includeDetails = false)
{
return ObjectMapper.Map<List<Tenant>, List<TenantConfiguration>>(
await TenantRepository.GetListAsync(includeDetails));
var cacheKey = TenantsCacheItem.CalculateCacheKey(includeDetails);
var cacheItem = await TenantsCache.GetAsync(cacheKey);
if (cacheItem == null)
{
var tenants = await TenantRepository.GetListAsync(includeDetails);
var tenantConfiurations = ObjectMapper.Map<List<Tenant>, List<TenantConfiguration>>(tenants);
cacheItem = new TenantsCacheItem(tenantConfiurations);
await TenantsCache.SetAsync(cacheKey, cacheItem, considerUow: true);
}
return [.. cacheItem.Tenants];
}
[Obsolete("Use FindAsync method.")]
@ -61,7 +72,7 @@ public class TenantStore : ITenantStore, ITransientDependency
{
var cacheKey = CalculateCacheKey(id, name);
var cacheItem = await Cache.GetAsync(cacheKey, considerUow: true);
var cacheItem = await TenantCache.GetAsync(cacheKey, considerUow: true);
if (cacheItem != null)
{
return cacheItem;
@ -92,7 +103,7 @@ public class TenantStore : ITenantStore, ITransientDependency
{
var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null;
var cacheItem = new TenantCacheItem(tenantConfiguration);
await Cache.SetAsync(cacheKey, cacheItem, considerUow: true);
await TenantCache.SetAsync(cacheKey, cacheItem, considerUow: true);
return cacheItem;
}
@ -101,7 +112,7 @@ public class TenantStore : ITenantStore, ITransientDependency
{
var cacheKey = CalculateCacheKey(id, name);
var cacheItem = Cache.Get(cacheKey, considerUow: true);
var cacheItem = TenantCache.Get(cacheKey, considerUow: true);
if (cacheItem != null)
{
return cacheItem;
@ -133,7 +144,7 @@ public class TenantStore : ITenantStore, ITransientDependency
{
var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null;
var cacheItem = new TenantCacheItem(tenantConfiguration);
Cache.Set(cacheKey, cacheItem, considerUow: true);
TenantCache.Set(cacheKey, cacheItem, considerUow: true);
return cacheItem;
}

28
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain/LINGYUN/Abp/Saas/Tenants/TenantsCacheItem.cs

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using Volo.Abp.Caching;
using Volo.Abp.MultiTenancy;
namespace LINGYUN.Abp.Saas.Tenants;
[Serializable]
[IgnoreMultiTenancy]
[CacheName("AbpSaasTenantsAll")]
public class TenantsCacheItem
{
private const string CacheKeyFormat = "id:{0}";
public List<TenantConfiguration> Tenants { get; set; }
public TenantsCacheItem()
{
}
public TenantsCacheItem(List<TenantConfiguration> tenants)
{
Tenants = tenants;
}
public static string CalculateCacheKey(bool includeDetails = false)
{
return string.Format(CacheKeyFormat, includeDetails.ToString());
}
}

20
aspnet-core/services/LY.MicroService.Applications.Single/EventBus/Distributed/NotificationEventHandler.cs

@ -1,7 +1,4 @@
using LINGYUN.Abp.Notifications;
using LINGYUN.Abp.Notifications.Templating;
using LY.MicroService.Applications.Single.BackgroundJobs;
using LY.MicroService.Applications.Single.MultiTenancy;
using LY.MicroService.Applications.Single.BackgroundJobs;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
@ -9,9 +6,6 @@ using System.Globalization;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Json;
using Volo.Abp.Localization;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TextTemplating;
using Volo.Abp.Timing;
using Volo.Abp.Uow;
@ -47,9 +41,9 @@ namespace LY.MicroService.Applications.Single.EventBus.Distributed
/// </summary>
protected ICurrentTenant CurrentTenant { get; }
/// <summary>
/// Reference to <see cref="ITenantConfigurationCache"/>.
/// Reference to <see cref="ITenantStore"/>.
/// </summary>
protected ITenantConfigurationCache TenantConfigurationCache { get; }
protected ITenantStore TenantStore { get; }
/// <summary>
/// Reference to <see cref="IJsonSerializer"/>.
/// </summary>
@ -97,7 +91,7 @@ namespace LY.MicroService.Applications.Single.EventBus.Distributed
public NotificationEventHandler(
IClock clock,
ICurrentTenant currentTenant,
ITenantConfigurationCache tenantConfigurationCache,
ITenantStore tenantStore,
IJsonSerializer jsonSerializer,
ITemplateRenderer templateRenderer,
IBackgroundJobManager backgroundJobManager,
@ -112,7 +106,7 @@ namespace LY.MicroService.Applications.Single.EventBus.Distributed
{
Clock = clock;
Options = options.Value;
TenantConfigurationCache = tenantConfigurationCache;
TenantStore = tenantStore;
CurrentTenant = currentTenant;
JsonSerializer = jsonSerializer;
TemplateRenderer = templateRenderer;
@ -152,7 +146,7 @@ namespace LY.MicroService.Applications.Single.EventBus.Distributed
{
await SendToTenantAsync(null, notification, eventData, result);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{
@ -182,7 +176,7 @@ namespace LY.MicroService.Applications.Single.EventBus.Distributed
{
await SendToTenantAsync(null, notification, eventData);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{

46
aspnet-core/services/LY.MicroService.Applications.Single/EventBus/Distributed/TenantEventHandler.cs

@ -1,46 +0,0 @@
using LINGYUN.Abp.Saas.Tenants;
using LY.MicroService.Applications.Single.MultiTenancy;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Uow;
namespace LY.MicroService.Applications.Single.EventBus.Distributed
{
public class TenantEventHandler :
IDistributedEventHandler<EntityCreatedEto<TenantEto>>,
IDistributedEventHandler<EntityUpdatedEto<TenantEto>>,
IDistributedEventHandler<EntityDeletedEto<TenantEto>>,
IDistributedEventHandler<TenantConnectionStringUpdatedEto>,
ITransientDependency
{
protected ITenantConfigurationCache TenantConfigurationCache { get; }
public TenantEventHandler(
ITenantConfigurationCache tenantConfigurationCache)
{
TenantConfigurationCache = tenantConfigurationCache;
}
[UnitOfWork]
public async virtual Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(TenantConnectionStringUpdatedEto eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
}
}

10
aspnet-core/services/LY.MicroService.Applications.Single/MultiTenancy/ITenantConfigurationCache.cs

@ -1,10 +0,0 @@
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.Applications.Single.MultiTenancy;
public interface ITenantConfigurationCache
{
Task RefreshAsync();
Task<List<TenantConfiguration>> GetTenantsAsync();
}

59
aspnet-core/services/LY.MicroService.Applications.Single/MultiTenancy/TenantConfigurationCache.cs

@ -1,59 +0,0 @@
using LINGYUN.Abp.Saas.Tenants;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.Applications.Single.MultiTenancy;
public class TenantConfigurationCache : ITenantConfigurationCache, ITransientDependency
{
protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> TenantCache { get; }
public TenantConfigurationCache(
ITenantRepository tenantRepository,
IDistributedCache<TenantConfigurationCacheItem> tenantCache)
{
TenantRepository = tenantRepository;
TenantCache = tenantCache;
}
public async virtual Task RefreshAsync()
{
var cacheKey = GetCacheKey();
await TenantCache.RemoveAsync(cacheKey);
}
public async virtual Task<List<TenantConfiguration>> GetTenantsAsync()
{
return (await GetForCacheItemAsync()).Tenants;
}
protected async virtual Task<TenantConfigurationCacheItem> GetForCacheItemAsync()
{
var cacheKey = GetCacheKey();
var cacheItem = await TenantCache.GetAsync(cacheKey);
if (cacheItem == null)
{
var allActiveTenants = await TenantRepository.GetListAsync();
cacheItem = new TenantConfigurationCacheItem(
allActiveTenants
.Where(t => t.IsActive)
.Select(t => new TenantConfiguration(t.Id, t.Name)
{
IsActive = t.IsActive,
}).ToList());
await TenantCache.SetAsync(cacheKey, cacheItem);
}
return cacheItem;
}
protected virtual string GetCacheKey()
{
return "_Abp_Tenant_Configuration";
}
}

19
aspnet-core/services/LY.MicroService.Applications.Single/MultiTenancy/TenantConfigurationCacheItem.cs

@ -1,19 +0,0 @@
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.Applications.Single.MultiTenancy;
[IgnoreMultiTenancy]
public class TenantConfigurationCacheItem
{
public List<TenantConfiguration> Tenants { get; set; }
public TenantConfigurationCacheItem()
{
Tenants = new List<TenantConfiguration>();
}
public TenantConfigurationCacheItem(List<TenantConfiguration> tenants)
{
Tenants = tenants;
}
}

3
aspnet-core/services/LY.MicroService.Applications.Single/appsettings.json

@ -36,7 +36,8 @@
"Override": {
"System": "Warning",
"Microsoft": "Warning",
"DotNetCore": "Information"
"DotNetCore": "Information",
"LINGYUN.Abp.BlobManagement": "Debug"
}
},
"Enrich": [ "FromLogContext", "WithProcessId", "WithThreadId", "WithEnvironmentName", "WithMachineName", "WithApplicationName", "WithUniqueId" ],

10
aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/BackgroundWorkers/BlobTempCleanupBackgroundWorker.cs

@ -1,8 +1,8 @@
using LINGYUN.Abp.BlobManagement;
using LY.MicroService.PlatformManagement.MultiTenancy;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.DistributedLocking;
@ -13,9 +13,9 @@ namespace LY.MicroService.PlatformManagement.BackgroundWorkers;
public class BlobTempCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
{
protected ITenantStore TenantStore { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IAbpDistributedLock DistributedLock { get; }
protected ITenantConfigurationCache TenantConfigurationCache { get; }
public BlobTempCleanupBackgroundWorker(
AbpAsyncTimer timer,
@ -23,12 +23,12 @@ public class BlobTempCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
IOptionsMonitor<AbpBlobManagementOptions> cleanupOptions,
IAbpDistributedLock distributedLock,
ICurrentTenant currentTenant,
ITenantConfigurationCache tenantConfigurationCache)
ITenantStore tenantStore)
: base(timer, serviceScopeFactory)
{
TenantStore = tenantStore;
CurrentTenant = currentTenant;
DistributedLock = distributedLock;
TenantConfigurationCache = tenantConfigurationCache;
timer.Period = cleanupOptions.CurrentValue.CleanupPeriod;
}
@ -44,7 +44,7 @@ public class BlobTempCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
{
await ExecuteCleanService(workerContext);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{

10
aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/MultiTenancy/ITenantConfigurationCache.cs

@ -1,10 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.PlatformManagement.MultiTenancy;
public interface ITenantConfigurationCache
{
Task<List<TenantConfiguration>> GetTenantsAsync();
}

50
aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/MultiTenancy/TenantConfigurationCache.cs

@ -1,50 +0,0 @@
using LINGYUN.Abp.Saas.Tenants;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.PlatformManagement.MultiTenancy;
public class TenantConfigurationCache : ITenantConfigurationCache, ITransientDependency
{
protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> TenantCache { get; }
public TenantConfigurationCache(
ITenantRepository tenantRepository,
IDistributedCache<TenantConfigurationCacheItem> tenantCache)
{
TenantRepository = tenantRepository;
TenantCache = tenantCache;
}
public async virtual Task<List<TenantConfiguration>> GetTenantsAsync()
{
return (await GetForCacheItemAsync()).Tenants;
}
protected async virtual Task<TenantConfigurationCacheItem> GetForCacheItemAsync()
{
var cacheKey = "_Abp_Tenant_Configuration";
var cacheItem = await TenantCache.GetAsync(cacheKey);
if (cacheItem == null)
{
var allActiveTenants = await TenantRepository.GetListAsync();
cacheItem = new TenantConfigurationCacheItem(
allActiveTenants
.Where(t => t.IsActive)
.Select(t => new TenantConfiguration(t.Id, t.Name)
{
IsActive = t.IsActive,
}).ToList());
await TenantCache.SetAsync(cacheKey, cacheItem);
}
return cacheItem;
}
}

20
aspnet-core/services/LY.MicroService.PlatformManagement.HttpApi.Host/MultiTenancy/TenantConfigurationCacheItem.cs

@ -1,20 +0,0 @@
using System.Collections.Generic;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.PlatformManagement.MultiTenancy;
[IgnoreMultiTenancy]
public class TenantConfigurationCacheItem
{
public List<TenantConfiguration> Tenants { get; set; }
public TenantConfigurationCacheItem()
{
Tenants = new List<TenantConfiguration>();
}
public TenantConfigurationCacheItem(List<TenantConfiguration> tenants)
{
Tenants = tenants;
}
}

13
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs

@ -1,7 +1,6 @@
using LINGYUN.Abp.Notifications;
using LINGYUN.Abp.Notifications.Templating;
using LY.MicroService.RealtimeMessage.BackgroundJobs;
using LY.MicroService.RealtimeMessage.MultiTenancy;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -57,9 +56,9 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary>
protected ICurrentTenant CurrentTenant { get; }
/// <summary>
/// Reference to <see cref="ITenantConfigurationCache"/>.
/// Reference to <see cref="ITenantStore"/>.
/// </summary>
protected ITenantConfigurationCache TenantConfigurationCache { get; }
protected ITenantStore TenantStore { get; }
/// <summary>
/// Reference to <see cref="IJsonSerializer"/>.
/// </summary>
@ -108,7 +107,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
IClock clock,
ICurrentTenant currentTenant,
ISettingProvider settingProvider,
ITenantConfigurationCache tenantConfigurationCache,
ITenantStore tenantStore,
IJsonSerializer jsonSerializer,
ITemplateRenderer templateRenderer,
IBackgroundJobManager backgroundJobManager,
@ -123,7 +122,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{
Clock = clock;
Options = options.Value;
TenantConfigurationCache = tenantConfigurationCache;
TenantStore = tenantStore;
CurrentTenant = currentTenant;
SettingProvider = settingProvider;
JsonSerializer = jsonSerializer;
@ -165,7 +164,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{
await SendToTenantAsync(null, notification, eventData, result);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{
@ -202,7 +201,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{
await SendToTenantAsync(null, notification, eventData);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
var allActiveTenants = (await TenantStore.GetListAsync()).Where(x => x.IsActive);
foreach (var activeTenant in allActiveTenants)
{

70
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/TenantSynchronizer.cs

@ -1,70 +0,0 @@
using LINGYUN.Abp.Data.DbMigrator;
using LINGYUN.Abp.Notifications;
using LINGYUN.Abp.Saas.Tenants;
using LY.MicroService.RealtimeMessage.MultiTenancy;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{
public class TenantSynchronizer :
IDistributedEventHandler<EntityCreatedEto<TenantEto>>,
IDistributedEventHandler<EntityUpdatedEto<TenantEto>>,
IDistributedEventHandler<EntityDeletedEto<TenantEto>>,
IDistributedEventHandler<TenantConnectionStringUpdatedEto>,
ITransientDependency
{
protected ILogger<TenantSynchronizer> Logger { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IDbSchemaMigrator DbSchemaMigrator { get; }
protected INotificationSender NotificationSender { get; }
protected ITenantConfigurationCache TenantConfigurationCache { get; }
protected INotificationSubscriptionManager NotificationSubscriptionManager { get; }
public TenantSynchronizer(
ICurrentTenant currentTenant,
IDbSchemaMigrator dbSchemaMigrator,
IUnitOfWorkManager unitOfWorkManager,
INotificationSender notificationSender,
ITenantConfigurationCache tenantConfigurationCache,
INotificationSubscriptionManager notificationSubscriptionManager,
ILogger<TenantSynchronizer> logger)
{
Logger = logger;
CurrentTenant = currentTenant;
DbSchemaMigrator = dbSchemaMigrator;
UnitOfWorkManager = unitOfWorkManager;
TenantConfigurationCache = tenantConfigurationCache;
NotificationSender = notificationSender;
NotificationSubscriptionManager = notificationSubscriptionManager;
}
public async virtual Task HandleEventAsync(EntityCreatedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(EntityUpdatedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(EntityDeletedEto<TenantEto> eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
public async virtual Task HandleEventAsync(TenantConnectionStringUpdatedEto eventData)
{
await TenantConfigurationCache.RefreshAsync();
}
}
}

12
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/ITenantConfigurationCache.cs

@ -1,12 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.RealtimeMessage.MultiTenancy;
public interface ITenantConfigurationCache
{
Task RefreshAsync();
Task<List<TenantConfiguration>> GetTenantsAsync();
}

62
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCache.cs

@ -1,62 +0,0 @@
using LINGYUN.Abp.Saas.Tenants;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.RealtimeMessage.MultiTenancy;
public class TenantConfigurationCache : ITenantConfigurationCache, ITransientDependency
{
protected ITenantRepository TenantRepository { get; }
protected IDistributedCache<TenantConfigurationCacheItem> TenantCache { get; }
public TenantConfigurationCache(
ITenantRepository tenantRepository,
IDistributedCache<TenantConfigurationCacheItem> tenantCache)
{
TenantRepository = tenantRepository;
TenantCache = tenantCache;
}
public async virtual Task RefreshAsync()
{
var cacheKey = GetCacheKey();
await TenantCache.RemoveAsync(cacheKey);
}
public async virtual Task<List<TenantConfiguration>> GetTenantsAsync()
{
return (await GetForCacheItemAsync()).Tenants;
}
protected async virtual Task<TenantConfigurationCacheItem> GetForCacheItemAsync()
{
var cacheKey = GetCacheKey();
var cacheItem = await TenantCache.GetAsync(cacheKey);
if (cacheItem == null)
{
var allActiveTenants = await TenantRepository.GetListAsync();
cacheItem = new TenantConfigurationCacheItem(
allActiveTenants
.Where(t => t.IsActive)
.Select(t => new TenantConfiguration(t.Id, t.Name)
{
IsActive = t.IsActive,
}).ToList());
await TenantCache.SetAsync(cacheKey, cacheItem);
}
return cacheItem;
}
protected virtual string GetCacheKey()
{
return "_Abp_Tenant_Configuration";
}
}

20
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/MultiTenancy/TenantConfigurationCacheItem.cs

@ -1,20 +0,0 @@
using System.Collections.Generic;
using Volo.Abp.MultiTenancy;
namespace LY.MicroService.RealtimeMessage.MultiTenancy;
[IgnoreMultiTenancy]
public class TenantConfigurationCacheItem
{
public List<TenantConfiguration> Tenants { get; set; }
public TenantConfigurationCacheItem()
{
Tenants = new List<TenantConfiguration>();
}
public TenantConfigurationCacheItem(List<TenantConfiguration> tenants)
{
Tenants = tenants;
}
}
Loading…
Cancel
Save