Пример #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    var forecast = await _weatherApiClient.GetWeatherForecastAsync(stoppingToken);

                    if (forecast is object)
                    {
                        var currentWeather = new CurrentWeatherResult {
                            Description = forecast.Weather.Description
                        };

                        var cacheKey = $"current_weather_{DateTime.UtcNow:yyyy_MM_dd}";

                        _logger.LogInformation("Updating weather in cache.");

                        await _cache.SetAsync(cacheKey, currentWeather, _minutesToCache);
                    }

                    await Task.Delay(TimeSpan.FromSeconds(_refreshIntervalInSeconds), stoppingToken);
                }
            }
            catch (OperationCanceledException)
            {
                // swallow
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An exception occurred when populating the cache");
            }
        }
Пример #2
0
        public async Task <CurrentWeatherResult> GetCurrentWeatherAsync()
        {
            var currentWeather = await _weatherApiClient.GetWeatherForecastAsync();

            var result = new CurrentWeatherResult
            {
                Description = currentWeather.Weather.Description
            };

            return(result);
        }