Browse Source

The tenant connection configuration information cannot be queried at the current tenant scope

pull/122/head
cKey 5 years ago
parent
commit
01410cf238
  1. 24
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantStore.cs
  2. 25
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantStore.cs

24
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantStore.cs

@ -3,7 +3,6 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -11,6 +10,11 @@ using Volo.Abp.MultiTenancy;
using Volo.Abp.TenantManagement; using Volo.Abp.TenantManagement;
using Volo.Abp.Threading; using Volo.Abp.Threading;
/*
* fix bug: ,
*
*/
namespace LINGYUN.Abp.MultiTenancy.DbFinder namespace LINGYUN.Abp.MultiTenancy.DbFinder
{ {
[Dependency(ServiceLifetime.Transient, ReplaceServices = true)] [Dependency(ServiceLifetime.Transient, ReplaceServices = true)]
@ -97,6 +101,8 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder
} }
protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByIdAsync(Guid id) protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByIdAsync(Guid id)
{
using (_currentTenant.Change(null))
{ {
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString()); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString());
@ -111,8 +117,6 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder
} }
Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}"); Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}");
using (_currentTenant.Change(null))
{
var tenant = await _tenantRepository.FindAsync(id, true); var tenant = await _tenantRepository.FindAsync(id, true);
if (tenant == null) if (tenant == null)
{ {
@ -129,12 +133,19 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder
Logger.LogDebug($"Setting the cache item: {cacheKey}"); Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem); await _cache.SetAsync(cacheKey, cacheItem);
// 用租户名称再次缓存,以便通过标识查询也能命中缓存
await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.Name), cacheItem);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
return cacheItem; return cacheItem;
} }
} }
protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name) protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name)
{
// 需要切换到最外层以解决查询无效的bug
using (_currentTenant.Change(null))
{ {
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
@ -149,8 +160,6 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder
} }
Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}"); Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}");
using (_currentTenant.Change(null))
{
var tenant = await _tenantRepository.FindByNameAsync(name); var tenant = await _tenantRepository.FindByNameAsync(name);
if (tenant == null) if (tenant == null)
{ {
@ -166,7 +175,12 @@ namespace LINGYUN.Abp.MultiTenancy.DbFinder
cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings);
Logger.LogDebug($"Setting the cache item: {cacheKey}"); Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem); await _cache.SetAsync(cacheKey, cacheItem);
// 用租户标识再次缓存,以便通过标识查询也能命中缓存
await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenant.Id.ToString()), cacheItem);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
return cacheItem; return cacheItem;

25
aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantStore.cs

@ -32,8 +32,6 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
Logger = NullLogger<TenantStore>.Instance; Logger = NullLogger<TenantStore>.Instance;
} }
public virtual TenantConfiguration Find(string name) public virtual TenantConfiguration Find(string name)
{
using (_currentTenant.Change(null))
{ {
var tenantCacheItem = AsyncHelper.RunSync(async () => await var tenantCacheItem = AsyncHelper.RunSync(async () => await
GetCacheItemByNameAsync(name)); GetCacheItemByNameAsync(name));
@ -43,11 +41,8 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
ConnectionStrings = tenantCacheItem.ConnectionStrings ConnectionStrings = tenantCacheItem.ConnectionStrings
}; };
} }
}
public virtual TenantConfiguration Find(Guid id) public virtual TenantConfiguration Find(Guid id)
{
using (_currentTenant.Change(null))
{ {
var tenantCacheItem = AsyncHelper.RunSync(async () => await var tenantCacheItem = AsyncHelper.RunSync(async () => await
GetCacheItemByIdAsync(id)); GetCacheItemByIdAsync(id));
@ -57,11 +52,8 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
ConnectionStrings = tenantCacheItem.ConnectionStrings ConnectionStrings = tenantCacheItem.ConnectionStrings
}; };
} }
}
public virtual async Task<TenantConfiguration> FindAsync(string name) public virtual async Task<TenantConfiguration> FindAsync(string name)
{
using (_currentTenant.Change(null))
{ {
var tenantCacheItem = await GetCacheItemByNameAsync(name); var tenantCacheItem = await GetCacheItemByNameAsync(name);
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name) return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
@ -69,11 +61,8 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
ConnectionStrings = tenantCacheItem.ConnectionStrings ConnectionStrings = tenantCacheItem.ConnectionStrings
}; };
} }
}
public virtual async Task<TenantConfiguration> FindAsync(Guid id) public virtual async Task<TenantConfiguration> FindAsync(Guid id)
{
using (_currentTenant.Change(null))
{ {
var tenantCacheItem = await GetCacheItemByIdAsync(id); var tenantCacheItem = await GetCacheItemByIdAsync(id);
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name) return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
@ -81,9 +70,10 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
ConnectionStrings = tenantCacheItem.ConnectionStrings ConnectionStrings = tenantCacheItem.ConnectionStrings
}; };
} }
}
protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByIdAsync(Guid id) protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByIdAsync(Guid id)
{
using (_currentTenant.Change(null))
{ {
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString()); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString());
@ -109,11 +99,17 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
Logger.LogDebug($"Setting the cache item: {cacheKey}"); Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem); await _cache.SetAsync(cacheKey, cacheItem);
// 通过租户名称再次缓存,以便通过租户名称查询的api能命中缓存
await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name), cacheItem);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
return cacheItem; return cacheItem;
} }
}
protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name) protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name)
{
using (_currentTenant.Change(null))
{ {
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name); var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
@ -139,9 +135,14 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
Logger.LogDebug($"Setting the cache item: {cacheKey}"); Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem); await _cache.SetAsync(cacheKey, cacheItem);
// 通过租户标识再次缓存,以便通过租户名称查询的api能命中缓存
await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Id.ToString()), cacheItem);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
return cacheItem; return cacheItem;
} }
} }
} }
}

Loading…
Cancel
Save