Reacting to Caching api review changes

This commit is contained in:
Kiran Challa 2015-05-18 11:54:51 -07:00
parent 3bdaec5b7e
commit 903885c20d
4 changed files with 64 additions and 30 deletions

View File

@ -60,18 +60,24 @@ namespace MusicStore.Areas.Admin.Controllers
{ {
var cacheKey = GetCacheKey(id); var cacheKey = GetCacheKey(id);
var album = await Cache.GetOrSet(cacheKey, async context => Album album;
if(!Cache.TryGetValue(cacheKey, out album))
{ {
//Remove it from cache if not retrieved in last 10 minutes. album = await DbContext.Albums
context.SetSlidingExpiration(TimeSpan.FromMinutes(10)); .Where(a => a.AlbumId == id)
.Include(a => a.Artist)
.Include(a => a.Genre)
.FirstOrDefaultAsync();
//If this returns null how do we prevent the cache to store this. if (album != null)
return await DbContext.Albums {
.Where(a => a.AlbumId == id) //Remove it from cache if not retrieved in last 10 minutes.
.Include(a => a.Artist) Cache.Set(
.Include(a => a.Genre) cacheKey,
.FirstOrDefaultAsync(); album,
}); new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
}
}
if (album == null) if (album == null)
{ {

View File

@ -34,11 +34,20 @@ namespace MusicStore.Components
public async Task<IViewComponentResult> InvokeAsync() public async Task<IViewComponentResult> InvokeAsync()
{ {
var latestAlbum = await Cache.GetOrSet("latestAlbum", async context => var cacheKey = "latestAlbum";
Album latestAlbum;
if(!Cache.TryGetValue(cacheKey, out latestAlbum))
{ {
context.SetAbsoluteExpiration(TimeSpan.FromMinutes(10)); latestAlbum = await GetLatestAlbum();
return await GetLatestAlbum();
}); if (latestAlbum != null)
{
Cache.Set(
cacheKey,
latestAlbum,
new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10)));
}
}
return View(latestAlbum); return View(latestAlbum);
} }

View File

@ -22,13 +22,24 @@ namespace MusicStore.Controllers
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
// Get most popular albums // Get most popular albums
var albums = await Cache.GetOrSet("topselling", async context => var cacheKey = "topselling";
List<Album> albums;
if(!Cache.TryGetValue(cacheKey, out albums))
{ {
//Refresh it every 10 minutes. Let this be the last item to be removed by cache if cache GC kicks in. albums = await GetTopSellingAlbumsAsync(6);
context.SetAbsoluteExpiration(TimeSpan.FromMinutes(10));
context.SetPriority(CachePreservationPriority.High); if (albums != null && albums.Count > 0)
return await GetTopSellingAlbums(6); {
}); // 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); return View(albums);
} }
@ -43,7 +54,7 @@ namespace MusicStore.Controllers
return View("~/Views/Shared/StatusCodePage.cshtml"); return View("~/Views/Shared/StatusCodePage.cshtml");
} }
private async Task<List<Album>> GetTopSellingAlbums(int count) private async Task<List<Album>> GetTopSellingAlbumsAsync(int count)
{ {
// Group the order details by album and return // Group the order details by album and return
// the albums with the highest count // the albums with the highest count

View File

@ -45,17 +45,25 @@ namespace MusicStore.Controllers
public async Task<IActionResult> Details(int id) public async Task<IActionResult> Details(int id)
{ {
var album = await Cache.GetOrSet(string.Format("album_{0}", id), async context => var cacheKey = string.Format("album_{0}", id);
Album album;
if(!Cache.TryGetValue(cacheKey, out album))
{ {
//Remove it from cache if not retrieved in last 10 minutes album = await DbContext.Albums
context.SetSlidingExpiration(TimeSpan.FromMinutes(10)); .Where(a => a.AlbumId == id)
.Include(a => a.Artist)
.Include(a => a.Genre)
.FirstOrDefaultAsync();
return await DbContext.Albums if (album != null)
.Where(a => a.AlbumId == id) {
.Include(a => a.Artist) //Remove it from cache if not retrieved in last 10 minutes
.Include(a => a.Genre) Cache.Set(
.FirstOrDefaultAsync(); cacheKey,
}); album,
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
}
}
if (album == null) if (album == null)
{ {