// // GET: /Home/ public async Task <IActionResult> Index( [FromServices] IMusicStore musicStore, [FromServices] IMemoryCache cache) { // Get most popular albums var cacheKey = "topselling"; List <Album> albums; if (!cache.TryGetValue(cacheKey, out albums)) { albums = await musicStore.GetTopSellingAlbumsAsync(6); if (albums != null && albums.Count > 0) { if (_appSettings.CacheDbResults) { // Refresh it every 10 minutes. // Let this be the last item to be removed by cache if cache GC kicks in. cache.Set( cacheKey, albums, new MemoryCacheEntryOptions() .SetAbsoluteExpiration(TimeSpan.FromMinutes(10)) .SetPriority(CacheItemPriority.High)); } } } return(View(albums)); }
protected override async Task <List <Model.Album> > RunAsync() { var top = CheckCache(); if (top != null) { _logger.LogInformation("TopAlbum returning from cache!"); return(top); } top = await _storeService.GetTopSellingAlbumsAsync(_count); CacheResults(top); _logger.LogInformation("TopAlbum returned from store!"); return(top); }