diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentTenantAccessor.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyCurrentTenantAccessor.cs similarity index 80% rename from framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentTenantAccessor.cs rename to framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyCurrentTenantAccessor.cs index eb0ee781ad..3857db2b36 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentTenantAccessor.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyCurrentTenantAccessor.cs @@ -1,7 +1,7 @@ using Volo.Abp.DependencyInjection; using Volo.Abp.MultiTenancy; -namespace Volo.Abp.AspNetCore.Components.WebAssembly +namespace Volo.Abp.AspNetCore.Components.WebAssembly.MultiTenancy { [Dependency(ReplaceServices = true)] public class WebAssemblyCurrentTenantAccessor : ICurrentTenantAccessor, ISingletonDependency diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyRemoteTenantStore.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStore.cs similarity index 54% rename from framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyRemoteTenantStore.cs rename to framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStore.cs index ebe170cfa8..7e132db9c9 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyRemoteTenantStore.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStore.cs @@ -6,44 +6,55 @@ using Volo.Abp.Http.Client.DynamicProxying; using Volo.Abp.MultiTenancy; using Volo.Abp.Threading; -namespace Volo.Abp.AspNetCore.Components.WebAssembly +namespace Volo.Abp.AspNetCore.Components.WebAssembly.MultiTenancy { public class WebAssemblyRemoteTenantStore : ITenantStore, ITransientDependency { protected IHttpClientProxy Proxy { get; } + protected WebAssemblyRemoteTenantStoreCache Cache { get; } public WebAssemblyRemoteTenantStore( - IHttpClientProxy proxy) + IHttpClientProxy proxy, + WebAssemblyRemoteTenantStoreCache cache) { Proxy = proxy; + Cache = cache; } public async Task FindAsync(string name) { - //TODO: Cache + var cached = Cache.Find(name); + if (cached != null) + { + return cached; + } - return CreateTenantConfiguration(await Proxy.Service.FindTenantByNameAsync(name)); + var tenant = CreateTenantConfiguration(await Proxy.Service.FindTenantByNameAsync(name)); + Cache.Set(tenant); + return tenant; } public async Task FindAsync(Guid id) { - //TODO: Cache + var cached = Cache.Find(id); + if (cached != null) + { + return cached; + } - return CreateTenantConfiguration(await Proxy.Service.FindTenantByIdAsync(id)); + var tenant = CreateTenantConfiguration(await Proxy.Service.FindTenantByIdAsync(id)); + Cache.Set(tenant); + return tenant; } public TenantConfiguration Find(string name) { - //TODO: Cache - - return AsyncHelper.RunSync(async () => CreateTenantConfiguration(await Proxy.Service.FindTenantByNameAsync(name))); + return AsyncHelper.RunSync(() => FindAsync(name)); } public TenantConfiguration Find(Guid id) { - //TODO: Cache - - return AsyncHelper.RunSync(async () => CreateTenantConfiguration(await Proxy.Service.FindTenantByIdAsync(id))); + return AsyncHelper.RunSync(() => FindAsync(id)); } protected virtual TenantConfiguration CreateTenantConfiguration(FindTenantResultDto tenantResultDto) @@ -55,15 +66,5 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly 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}"; - } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStoreCache.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStoreCache.cs new file mode 100644 index 0000000000..e1c5112e02 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStoreCache.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.MultiTenancy +{ + public class WebAssemblyRemoteTenantStoreCache : IScopedDependency + { + protected readonly List CachedTenants = new List(); + + public virtual TenantConfiguration Find(string name) + { + return CachedTenants.FirstOrDefault(t => t.Name == name); + } + + public virtual TenantConfiguration Find(Guid id) + { + return CachedTenants.FirstOrDefault(t => t.Id == id); + } + + public virtual void Set(TenantConfiguration tenant) + { + var existingTenant = Find(tenant.Id); + if (existingTenant != null) + { + existingTenant.Name = tenant.Name; + return; + } + + CachedTenants.Add(tenant); + } + } +}