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);
        }
        public virtual async Task HandleEventAsync(EntityCreatedEto <TenantEto> eventData)
        {
            var tenantDto = await _tenantAppService.GetAsync(eventData.Entity.Id);

            var tenantConnectionStringsDto = await _tenantAppService.GetConnectionStringAsync(eventData.Entity.Id);

            var connectionStrings = new ConnectionStrings();

            foreach (var tenantConnectionString in tenantConnectionStringsDto.Items)
            {
                connectionStrings[tenantConnectionString.Name] = tenantConnectionString.Value;
            }
            var cacheItem = new TenantConfigurationCacheItem(tenantDto.Id, tenantDto.Name, connectionStrings);

            var cacheKey = TenantConfigurationCacheItem.CalculateCacheKey(eventData.Entity.Id.ToString());
            await _cache.SetAsync(cacheKey, cacheItem);
        }