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 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.
context.SetSlidingExpiration(TimeSpan.FromMinutes(10));
album = await DbContext.Albums
.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.
return await DbContext.Albums
.Where(a => a.AlbumId == id)
.Include(a => a.Artist)
.Include(a => a.Genre)
.FirstOrDefaultAsync();
});
if (album != null)
{
//Remove it from cache if not retrieved in last 10 minutes.
Cache.Set(
cacheKey,
album,
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
}
}
if (album == null)
{

View File

@ -34,11 +34,20 @@ namespace MusicStore.Components
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));
return await GetLatestAlbum();
});
latestAlbum = await GetLatestAlbum();
if (latestAlbum != null)
{
Cache.Set(
cacheKey,
latestAlbum,
new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(10)));
}
}
return View(latestAlbum);
}

View File

@ -22,13 +22,24 @@ namespace MusicStore.Controllers
public async Task<IActionResult> Index()
{
// 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.
context.SetAbsoluteExpiration(TimeSpan.FromMinutes(10));
context.SetPriority(CachePreservationPriority.High);
return await GetTopSellingAlbums(6);
});
albums = await GetTopSellingAlbumsAsync(6);
if (albums != null && albums.Count > 0)
{
// 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);
}
@ -43,7 +54,7 @@ namespace MusicStore.Controllers
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
// the albums with the highest count

View File

@ -45,17 +45,25 @@ namespace MusicStore.Controllers
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
context.SetSlidingExpiration(TimeSpan.FromMinutes(10));
album = await DbContext.Albums
.Where(a => a.AlbumId == id)
.Include(a => a.Artist)
.Include(a => a.Genre)
.FirstOrDefaultAsync();
return await DbContext.Albums
.Where(a => a.AlbumId == id)
.Include(a => a.Artist)
.Include(a => a.Genre)
.FirstOrDefaultAsync();
});
if (album != null)
{
//Remove it from cache if not retrieved in last 10 minutes
Cache.Set(
cacheKey,
album,
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
}
}
if (album == null)
{