예제 #1
0
        public async Task <IEnumerable <Data> > GetQuickDataForLocation(int month, string locationId, [FromQuery] bool ignoreCache = false)
        {
            if (!ignoreCache && _memCache.TryGetValue(month + "MonthlyData" + locationId, out IEnumerable <Data> data))
            {
                return(data);
            }

            var dataApi  = new DataApi(_ncdcApiManager);
            var taskList = new List <Task <IEnumerable <Data> > >();

            for (int i = 0; i <= 5; i++)
            {
                var endDateString = DateTime.Now.Year - i + "-" + month.ToString().PadLeft(2, '0') + "-" + DateTime.DaysInMonth(DateTime.Now.Year, month).ToString().PadLeft(2, '0');
                var endDate       = DateTime.Parse(endDateString);
                var startDate     = DateTime.Parse(DateTime.Now.Year - i + "-" + month.ToString().PadLeft(2, '0') + "-01");

                taskList.Add(dataApi.GetDataAsync(new List <DataType> {
                    DataType.PRCP, DataType.DP01, DataType.TMAX, DataType.TMIN
                }, DataSet.GSOM, locationId, startDate, endDate));
            }

            var results = await Task.WhenAll(taskList);

            var result = results.SelectMany(r => r).ToList();

            var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    .SetAbsoluteExpiration(TimeSpan.FromDays(1));

            _memCache.Set(month + "MonthlyData" + locationId, result, cacheEntryOptions);
            return(result);
        }
예제 #2
0
        public async Task <IEnumerable <Data> > GetAllDataForLocation(string locationId)
        {
            if (_memCache.TryGetValue("AllData" + locationId, out IEnumerable <Data> data))
            {
                return(data);
            }

            var dataApi = new DataApi(_ncdcApiManager);

            var dataTask = await dataApi.GetDataAsync(new List <DataType> {
                DataType.PRCP, DataType.TMAX, DataType.TMIN, DataType.TAVG, DataType.DP01
            },
                                                      DataSet.GSOM, locationId, _historicalStartDate, DateTime.Now);

            var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    .SetAbsoluteExpiration(TimeSpan.FromDays(1));

            _memCache.Set("AllData" + locationId, dataTask, cacheEntryOptions);
            return(dataTask);
        }
예제 #3
0
        public IEnumerable <Data> GetPrecipitationForLocation(string locationId)
        {
            if (_memCache.TryGetValue("PrcpData" + locationId, out IEnumerable <Data> data))
            {
                return(data);
            }

            var dataApi = new DataApi(_ncdcApiManager);

            var dataTask = dataApi.GetDataAsync(new List <DataType> {
                DataType.PRCP, DataType.DP01
            }, DataSet.GSOM, locationId, _historicalStartDate, DateTime.Now);

            dataTask.Wait();

            var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    .SetAbsoluteExpiration(TimeSpan.FromDays(1));

            _memCache.Set("PrcpData" + locationId, dataTask.Result, cacheEntryOptions);
            return(dataTask.Result);
        }