Ejemplo n.º 1
0
        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 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);

            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)
        {
            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 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);

                Logger.LogDebug($"Setting the cache item: {cacheKey}");
                await _cache.SetAsync(cacheKey, cacheItem);

                // 通过租户标识再次缓存,以便通过租户名称查询的api能命中缓存
                await _cache.SetAsync(TenantConfigurationCacheItem.CalculateCacheKey(tenantDto.Id.ToString()), cacheItem);

                Logger.LogDebug($"Finished setting the cache item: {cacheKey}");

                return(cacheItem);
            }
        }