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. 60
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.DbFinder/LINGYUN/Abp/MultiTenancy/DbFinder/TenantStore.cs
  2. 151
      aspnet-core/modules/tenants/LINGYUN.Abp.MultiTenancy.RemoteService/LINGYUN/Abp/MultiTenancy/RemoteService/TenantStore.cs

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

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

@ -33,115 +33,116 @@ namespace LINGYUN.Abp.MultiTenancy.RemoteService
}
public virtual TenantConfiguration Find(string name)
{
using (_currentTenant.Change(null))
{
var tenantCacheItem = AsyncHelper.RunSync(async () => await
GetCacheItemByNameAsync(name));
var tenantCacheItem = AsyncHelper.RunSync(async () => await
GetCacheItemByNameAsync(name));
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
public virtual TenantConfiguration Find(Guid id)
{
using (_currentTenant.Change(null))
{
var tenantCacheItem = AsyncHelper.RunSync(async () => await
GetCacheItemByIdAsync(id));
var tenantCacheItem = AsyncHelper.RunSync(async () => await
GetCacheItemByIdAsync(id));
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
public virtual async Task<TenantConfiguration> FindAsync(string name)
{
using (_currentTenant.Change(null))
var tenantCacheItem = await GetCacheItemByNameAsync(name);
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
var tenantCacheItem = await GetCacheItemByNameAsync(name);
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
public virtual async Task<TenantConfiguration> FindAsync(Guid id)
{
using (_currentTenant.Change(null))
var tenantCacheItem = await GetCacheItemByIdAsync(id);
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
var tenantCacheItem = await GetCacheItemByIdAsync(id);
return new TenantConfiguration(tenantCacheItem.Id, tenantCacheItem.Name)
{
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
ConnectionStrings = tenantCacheItem.ConnectionStrings
};
}
protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByIdAsync(Guid id)
{
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString());
using (_currentTenant.Change(null))
{
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString());
Logger.LogDebug($"TenantStore.GetCacheItemByIdAsync: {cacheKey}");
Logger.LogDebug($"TenantStore.GetCacheItemByIdAsync: {cacheKey}");
var cacheItem = await _cache.GetAsync(cacheKey);
var cacheItem = await _cache.GetAsync(cacheKey);
if (cacheItem != null)
{
Logger.LogDebug($"Found in the cache: {cacheKey}");
return cacheItem;
}
Logger.LogDebug($"Not found in the cache, getting from the remote service: {cacheKey}");
if (cacheItem != null)
{
Logger.LogDebug($"Found in the cache: {cacheKey}");
return cacheItem;
}
Logger.LogDebug($"Not found in the cache, getting from the remote service: {cacheKey}");
var tenantDto = await _tenantAppService.GetAsync(id);
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(id);
var connectionStrings = new ConnectionStrings();
foreach (var tenantConnectionString in tenantConnectionStringsDto.Items)
{
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
}
cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings);
var tenantDto = await _tenantAppService.GetAsync(id);
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(id);
var connectionStrings = new ConnectionStrings();
foreach (var tenantConnectionString in tenantConnectionStringsDto.Items)
{
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
}
cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings);
Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem);
return cacheItem;
// 通过租户名称再次缓存,以便通过租户名称查询的api能命中缓存
await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Name), cacheItem);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
return cacheItem;
}
}
protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name)
{
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
using (_currentTenant.Change(null))
{
var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name);
Logger.LogDebug($"TenantStore.GetCacheItemByNameAsync: {cacheKey}");
Logger.LogDebug($"TenantStore.GetCacheItemByNameAsync: {cacheKey}");
var cacheItem = await _cache.GetAsync(cacheKey);
var cacheItem = await _cache.GetAsync(cacheKey);
if (cacheItem != null)
{
Logger.LogDebug($"Found in the cache: {cacheKey}");
return cacheItem;
}
Logger.LogDebug($"Not found in the cache, getting from the remote service: {cacheKey}");
if (cacheItem != null)
{
Logger.LogDebug($"Found in the cache: {cacheKey}");
return cacheItem;
}
Logger.LogDebug($"Not found in the cache, getting from the remote service: {cacheKey}");
var tenantDto = await _tenantAppService.GetAsync(new TenantGetByNameInputDto(name));
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(tenantDto.Id);
var connectionStrings = new ConnectionStrings();
foreach (var tenantConnectionString in tenantConnectionStringsDto.Items)
{
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
}
cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings);
var tenantDto = await _tenantAppService.GetAsync(new TenantGetByNameInputDto(name));
var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(tenantDto.Id);
var connectionStrings = new ConnectionStrings();
foreach(var tenantConnectionString in tenantConnectionStringsDto.Items)
{
connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
}
cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings);
Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem);
// 通过租户标识再次缓存,以便通过租户名称查询的api能命中缓存
await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Id.ToString()), cacheItem);
Logger.LogDebug($"Setting the cache item: {cacheKey}");
await _cache.SetAsync(cacheKey, cacheItem);
Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
Logger.LogDebug($"Finished setting the cache item: {cacheKey}");
return cacheItem;
return cacheItem;
}
}
}
}

Loading…
Cancel
Save