public async Task <IActionResult> PostWeather(string id, string pwd, [FromBody] WeatherImperial weatherImperial) { DateTime now = DateTime.UtcNow; //get pws and they last requet from cache - chache timeout in Constants.PWSTimeout, after this period is chache refreshed, and same request can by writed CacheWeather pwsCache = await new CacheWeatherLogic(_memoryCache, _context, _serviceFactory).GetPWSAsync(id, pwd); //no PWS foud by pwsData.PWSId if (pwsCache == null) { return(Unauthorized(Constants.NoPWS)); } string checkRangeError = string.Empty; if (!weatherImperial.CheckRange(ref checkRangeError)) { return(ValidationProblem(checkRangeError)); } Weather weather = weatherImperial.ToWeather(); weather.IdPws = pwsCache.IdPws; weather.Dateutc = now; pwsCache.lastWeatherSet.Enqueue(weather); return(Ok()); }
/// <summary> /// chache station DB key and last upload request /// </summary> /// <returns></returns> public async Task <Cache.CacheWeather> GetPWSAsync(string id, string pwd) { string key = CacheKeys.PWS + id; Cache.CacheWeather cacheWeather = null; if (!_memCache.TryGetValue(key, out cacheWeather)) { //not in cache //TODO on change password cache must by reseted int?IdPws = await _context.Pws.Where(pws => pws.Id == id && pws.Pwd == pwd).Select(pws => pws.IdPws).FirstOrDefaultAsync(); if (IdPws.HasValue && IdPws > 0) { _memCache.Set(key, cacheWeather = new CacheWeather() { IdPws = IdPws.Value, lastWeatherSet = new ConcurrentQueue <Weather>() }, new MemoryCacheEntryOptions().RegisterPostEvictionCallback(async(object key, object value, EvictionReason reason, object state) => { CacheWeather cacheWeather = (CacheWeather)value; await Task.Run(async() => { await storeCachedWeatherData(cacheWeather.IdPws, cacheWeather.lastWeatherSet); }); cacheWeather.lastWeatherSet.Clear(); }, this).SetAbsoluteExpiration(Constants.weatherPostCacheTimeout)); } } return(cacheWeather); }