5 changed files with 140 additions and 14 deletions
@ -0,0 +1,10 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace LY.MicroService.RealtimeMessage.MultiTenancy; |
|||
|
|||
public interface ITenantConfigurationCache |
|||
{ |
|||
Task<List<TenantConfiguration>> GetTenantsAsync(); |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
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<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.Select(t => |
|||
new TenantConfiguration(t.Id, t.Name) |
|||
{ |
|||
IsActive = t.IsActive, |
|||
}).ToList()); |
|||
|
|||
await TenantCache.SetAsync(cacheKey, cacheItem); |
|||
} |
|||
|
|||
return cacheItem; |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
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…
Reference in new issue