From d1d111ef186fb1cd0a7b30a3a946956c0e793297 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 2 Dec 2022 17:39:36 +0800 Subject: [PATCH] Add MauiBlazorRemoteTenantStore --- .../MauiBlazor/MauiBlazorRemoteTenantStore.cs | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.MauiBlazor/Volo/Abp/AspNetCore/Components/MauiBlazor/MauiBlazorRemoteTenantStore.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Components.MauiBlazor/Volo/Abp/AspNetCore/Components/MauiBlazor/MauiBlazorRemoteTenantStore.cs b/framework/src/Volo.Abp.AspNetCore.Components.MauiBlazor/Volo/Abp/AspNetCore/Components/MauiBlazor/MauiBlazorRemoteTenantStore.cs new file mode 100644 index 0000000000..3cace3f113 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.MauiBlazor/Volo/Abp/AspNetCore/Components/MauiBlazor/MauiBlazorRemoteTenantStore.cs @@ -0,0 +1,113 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; +using Pages.Abp.MultiTenancy.ClientProxies; +using Volo.Abp.AspNetCore.Mvc.MultiTenancy; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Threading; +using DependencyAttribute = Volo.Abp.DependencyInjection.DependencyAttribute; + +namespace Volo.Abp.AspNetCore.Components.MauiBlazor; + +[Dependency(ReplaceServices = true)] +public class MauiBlazorRemoteTenantStore : ITenantStore, ITransientDependency +{ + protected AbpTenantClientProxy TenantAppService { get; } + protected IDistributedCache Cache { get; } + + public MauiBlazorRemoteTenantStore(AbpTenantClientProxy tenantAppService, IDistributedCache cache) + { + TenantAppService = tenantAppService; + Cache = cache; + } + + public async Task FindAsync(string name) + { + var cacheKey = CreateCacheKey(name); + + var tenantConfiguration = await Cache.GetOrAddAsync( + cacheKey, + async () => CreateTenantConfiguration(await TenantAppService.FindTenantByNameAsync(name)), + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = + TimeSpan.FromMinutes(5) //TODO: Should be configurable. + } + ); + + return tenantConfiguration; + } + + public async Task FindAsync(Guid id) + { + var cacheKey = CreateCacheKey(id); + + var tenantConfiguration = await Cache.GetOrAddAsync( + cacheKey, + async () => CreateTenantConfiguration(await TenantAppService.FindTenantByIdAsync(id)), + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = + TimeSpan.FromMinutes(5) //TODO: Should be configurable. + } + ); + + return tenantConfiguration; + } + + public TenantConfiguration Find(string name) + { + var cacheKey = CreateCacheKey(name); + + var tenantConfiguration = Cache.GetOrAdd( + cacheKey, + () => AsyncHelper.RunSync(async () => CreateTenantConfiguration(await TenantAppService.FindTenantByNameAsync(name))), + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = + TimeSpan.FromMinutes(5) //TODO: Should be configurable. + } + ); + + return tenantConfiguration; + } + + public TenantConfiguration Find(Guid id) + { + var cacheKey = CreateCacheKey(id); + + var tenantConfiguration = Cache.GetOrAdd( + cacheKey, + () => AsyncHelper.RunSync(async () => CreateTenantConfiguration(await TenantAppService.FindTenantByIdAsync(id))), + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = + TimeSpan.FromMinutes(5) //TODO: Should be configurable. + } + ); + + return tenantConfiguration; + } + + protected virtual TenantConfiguration CreateTenantConfiguration(FindTenantResultDto tenantResultDto) + { + if (!tenantResultDto.Success || tenantResultDto.TenantId == null) + { + return null; + } + + return new TenantConfiguration(tenantResultDto.TenantId.Value, tenantResultDto.Name); + } + + protected virtual string CreateCacheKey(string tenantName) + { + return $"RemoteTenantStore_Name_{tenantName}"; + } + + protected virtual string CreateCacheKey(Guid tenantId) + { + return $"RemoteTenantStore_Id_{tenantId:N}"; + } +} \ No newline at end of file