// [ResponseCache(Location = ResponseCacheLocation.Any, Duration = 10)]
        public async Task <IEnumerable <WeatherForecast> > Get()
        {
            var weatherForecast = await _weatherApiClient.GetWeatherForecast();

            _logger.LogInformation("Loaded {Count} items", weatherForecast.Count);
            return(weatherForecast);
        }
Beispiel #2
0
        public async Task <IEnumerable <WeatherForecast> > Get()
        {
            const string cacheKey = "getWeatherForecast";

            var weatherForecastBytes = await _cache.GetAsync(cacheKey);

            if (weatherForecastBytes != null)
            {
                _logger.LogInformation("Return result from cache");
                return(JsonSerializer.Deserialize <List <WeatherForecast> >(weatherForecastBytes));
            }

            var weatherForecast = await _weatherApiClient.GetWeatherForecast();

            var cacheExpirationOptions = new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = DateTime.Now.AddSeconds(10),
            };

            weatherForecastBytes = JsonSerializer.SerializeToUtf8Bytes(weatherForecast);
            await _cache.SetAsync(cacheKey, weatherForecastBytes, cacheExpirationOptions);

            _logger.LogInformation("Loaded {Count} items", weatherForecast.Count);

            return(weatherForecast);
        }
Beispiel #3
0
        private async Task <List <WeatherForecast> > PopulateCache()
        {
            var weatherForecast = await _weatherApiClient.GetWeatherForecast();

            _logger.LogInformation("Loaded {Count} items", weatherForecast.Count);

            return(weatherForecast);
        }
Beispiel #4
0
        public void UpdateWeather(string city)
        {
            string            url = String.Format("http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&cnt={1}&units=metric&mode=json&APPID={2}", city, CNT, WeatherApiClient.GetAPIKey());
            Task <RootObject> t   = Task.Run(() => WeatherApiClient.GetWeatherForecast(url));

            t.Wait();
            RootObject r = t.Result;

            if (r != null)
            {
                WeatherList = new ObservableCollection <DayElem>(r.list);
                SelectedDay = WeatherList[0];
                Root        = r;
            }
        }
Beispiel #5
0
        public async Task <IEnumerable <WeatherForecast> > Get()
        {
            // Используем Microsoft.Extensions.Caching.Memory

            const string cacheKey = "getWeatherForecast";

            if (_cache.TryGetValue(cacheKey, out List <WeatherForecast> weatherForecast))
            {
                _logger.LogInformation("Return result from cache");
                return(weatherForecast);
            }

            await _semaphore.WaitAsync();

            try
            {
                // double check locking
                if (_cache.TryGetValue(cacheKey, out weatherForecast))
                {
                    _logger.LogInformation("Return result from cache (inside the lock)");
                    return(weatherForecast);
                }

                weatherForecast = await _weatherApiClient.GetWeatherForecast();

                var cacheEntryOptions = new MemoryCacheEntryOptions
                {
                    AbsoluteExpiration = DateTime.Now.AddSeconds(100),
                    Priority           = CacheItemPriority.Normal,
                };

                _cache.Set(cacheKey, weatherForecast, cacheEntryOptions);

                _logger.LogInformation("Loaded {Count} items", weatherForecast.Count);
                return(weatherForecast);
            }
            finally
            {
                _semaphore.Release();
            }
        }