in UnicornStore/Controllers/StoreController.cs [51:84]
public async Task<IActionResult> Details(
[FromServices] IMemoryCache cache,
int id)
{
var cacheKey = string.Format("blessing_{0}", id);
Blessing blessing;
if (!cache.TryGetValue(cacheKey, out blessing))
{
blessing = await DbContext.Blessings
.Where(a => a.BlessingId == id)
.Include(a => a.Unicorn)
.Include(a => a.Genre)
.FirstOrDefaultAsync();
if (blessing != null)
{
if (_appSettings.CacheDbResults)
{
//Remove it from cache if not retrieved in last 10 minutes
cache.Set(
cacheKey,
blessing,
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
}
}
}
if (blessing == null)
{
return NotFound();
}
return View(blessing);
}