Browse Source

Resolved #5401: Implement caching for WebAssemblyRemoteTenantStore

pull/5800/head
Halil İbrahim Kalkan 6 years ago
parent
commit
29d583743c
  1. 2
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyCurrentTenantAccessor.cs
  2. 45
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStore.cs
  3. 35
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenancy/WebAssemblyRemoteTenantStoreCache.cs

2
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCurrentTenantAccessor.cs → 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

45
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyRemoteTenantStore.cs → 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<IAbpTenantAppService> Proxy { get; }
protected WebAssemblyRemoteTenantStoreCache Cache { get; }
public WebAssemblyRemoteTenantStore(
IHttpClientProxy<IAbpTenantAppService> proxy)
IHttpClientProxy<IAbpTenantAppService> proxy,
WebAssemblyRemoteTenantStoreCache cache)
{
Proxy = proxy;
Cache = cache;
}
public async Task<TenantConfiguration> 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<TenantConfiguration> 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}";
}
}
}

35
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<TenantConfiguration> CachedTenants = new List<TenantConfiguration>();
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);
}
}
}
Loading…
Cancel
Save