protected virtual async Task <TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name) { // 需要切换到最外层以解决查询无效的bug using (_currentTenant.Change(null)) { var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name); Logger.LogDebug($"TenantStore.GetCacheItemByNameAsync: {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}"); var tenant = await _tenantRepository.FindByNameAsync(name); if (tenant == null) { Logger.LogWarning($"Can not found tenant by name: {name}"); // throw new AbpException($"Can not found tenant by name: {name}"); return(null); } var connectionStrings = new ConnectionStrings(); foreach (var tenantConnectionString in tenant.ConnectionStrings) { connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; } 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); } }
protected virtual async Task <TenantConfigurationCacheItem> GetCacheItemByIdAsync(Guid id) { var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(id.ToString()); Logger.LogDebug($"TenantStore.GetCacheItemByIdAsync: {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}"); using (_currentTenant.Change(null)) { var tenant = await _tenantRepository.FindAsync(id, true); if (tenant == null) { Logger.LogWarning($"Can not found tenant by id: {id}"); // throw new AbpException($"Can not found tenant by id: {id}"); return(null); } var connectionStrings = new ConnectionStrings(); foreach (var tenantConnectionString in tenant.ConnectionStrings) { connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; } cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); Logger.LogDebug($"Setting the cache item: {cacheKey}"); await _cache.SetAsync(cacheKey, cacheItem); Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); return(cacheItem); } }
protected virtual async Task <TenantConfigurationCacheItem> GetCacheItemByNameAsync(string name) { var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(name); Logger.LogDebug($"TenantStore.GetCacheItemByNameAsync: {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}"); using (_dataFilter.Disable <IMultiTenant>()) { var tenant = await _tenantRepository.FindByNameAsync(name); if (tenant == null) { Logger.LogWarning($"Can not found tenant by name: {name}"); throw new AbpException($"Can not found tenant by name: {name}"); } var connectionStrings = new ConnectionStrings(); foreach (var tenantConnectionString in tenant.ConnectionStrings) { connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value; } cacheItem = new TenantConfigurationCacheItem(tenant.Id, tenant.Name, connectionStrings); Logger.LogDebug($"Setting the cache item: {cacheKey}"); await _cache.SetAsync(cacheKey, cacheItem); Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); return(cacheItem); } }