/// <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); }
public async Task <ActionResult <WeatherMetric> > GetWeather(string id, string pwd) { Cache.CacheWeather cachedWeather = await new CacheWeatherLogic(_memoryCache, _context, _serviceFactory).GetPWSAsync(id, pwd); //no PWS foud by pwsData.PWSId if (cachedWeather == null) { return(NotFound(Constants.NoPWS)); } Weather lastWeather = cachedWeather.lastWeatherSet.LastOrDefault(); if (lastWeather == null) { lastWeather = await _context.Weather.Where(p => p.IdPws == cachedWeather.IdPws).OrderByDescending(p => p.Dateutc).FirstOrDefaultAsync <Weather>(); } if (lastWeather == null) { return(NotFound(Constants.NoPWSData)); } return(lastWeather.ToWeatherMetric()); }
public async Task <ActionResult <PWSDetail> > GetPws(string id, string pwd) { Cache.CacheWeather cachePwsWeather = await new CacheWeatherLogic(_memoryCache, _context, _serviceFactory).GetPWSAsync(id, pwd); if (cachePwsWeather == null) { return(NotFound(Constants.NoPWS)); } PWSDetail pws = await(from p in _context.Pws where p.IdPws == cachePwsWeather.IdPws select new PWSDetail { Alt = p.Alt, Desc = p.Desc, Id = p.Id, Lat = p.Lat, Lon = p.Lon, Name = p.Name, Pwd = p.Pwd }).FirstOrDefaultAsync(); if (pws == null) { return(NotFound(Constants.NoPWS)); } pws.WeatherRecordsCount = await _context.Weather.LongCountAsync(p => p.IdPws == cachePwsWeather.IdPws); pws.LastUpdateDateTime = await(from w in _context.Weather where w.IdPws == cachePwsWeather.IdPws orderby w.Id descending select w.Dateutc).FirstOrDefaultAsync(); // _context.Weather.Where(p => p.IdPws == cachePwsWeather.IdPws).OrderByDescending(p => p.Id).FirstOrDefaultAsync<Weather>(); return(pws); }