public async Task <IEnumerable <SearchResponse> > Get(string keywords, string tagurl) { _logger.LogInformation(1, $"Getting update Statistic"); var _response = new List <SearchResponse>(); var isUpdate = true; if (_memoryCache.TryGetValue(_configuration["AppSettings:api:cachKey"], out Dictionary <DateTime, List <SearchResponse> > _cacheEntry)) { var temp = _cacheEntry.FirstOrDefault(d => d.Value.All(s => s.Keywords.Equals(keywords))); if (temp.Value != null) { _response = temp.Value; isUpdate = temp.Key.AddMinutes(_configuration.GetValue <int>("AppSettings:api:timeout", 60)) < DateTime.Now; } } _cacheEntry ??= new Dictionary <DateTime, List <SearchResponse> >(); if (isUpdate) { var newResult = new SearchResponse { Keywords = keywords, Date = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"), Google = (await _googleSearchService.Search(keywords, tagurl)).Results, Bing = (await _bingSearchService.Search(keywords, tagurl)).Results }; _response.Add(newResult); _cacheEntry.Add(DateTime.Now, _response); _memoryCache.Set(_configuration["AppSettings:api:cachKey"], _cacheEntry); } return(_response); }
public async Task <IActionResult> Get(string keywords, string tagurl) { _logger.LogInformation(1, $"Getting Statistic"); var response = new List <SearchResult>(); try { if (_memoryCache.TryGetValue(SearchResult, out List <SearchResult> cacheEntry)) { response = cacheEntry; } else { response.Add(await _googleSearchService.Search(keywords, tagurl)); response.Add(await _bingSearchService.Search(keywords, tagurl)); var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(30)); _memoryCache.Set(SearchResult, response, cacheEntryOptions); } } catch (Exception ex) { BadRequest(ex.Message); } return(Ok(response)); }