Create a gist now

Instantly share code, notes, and snippets.

public async Task<IActionResult> Index([FromServices] MusicStoreContext dbContext, [FromServices] IMemoryCache cache)
{
var cacheKey = "topselling";
List<Album> albums;
if (!cache.TryGetValue(cacheKey, out albums))
{
albums = await GetTopSellingAlbumsAsync(dbContext, 6);
if (albums != null && albums.Count > 0)
{
if (_appSettings.CacheDbResults)
{
cache.Set(cacheKey, albums,
new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(10))
.SetPriority(CacheItemPriority.High));
}
}
}
return View(albums);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment