diff --git a/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs index c54efa9a63..9ce074271d 100644 --- a/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs +++ b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs @@ -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) { diff --git a/src/MusicStore/Components/AnnouncementComponent.cs b/src/MusicStore/Components/AnnouncementComponent.cs index 3f71aad489..01eea5ec24 100644 --- a/src/MusicStore/Components/AnnouncementComponent.cs +++ b/src/MusicStore/Components/AnnouncementComponent.cs @@ -34,11 +34,20 @@ namespace MusicStore.Components public async Task 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); } diff --git a/src/MusicStore/Controllers/HomeController.cs b/src/MusicStore/Controllers/HomeController.cs index c9dd44a3bd..87bf1110d5 100644 --- a/src/MusicStore/Controllers/HomeController.cs +++ b/src/MusicStore/Controllers/HomeController.cs @@ -22,13 +22,24 @@ namespace MusicStore.Controllers public async Task Index() { // Get most popular albums - var albums = await Cache.GetOrSet("topselling", async context => + var cacheKey = "topselling"; + List 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> GetTopSellingAlbums(int count) + private async Task> GetTopSellingAlbumsAsync(int count) { // Group the order details by album and return // the albums with the highest count diff --git a/src/MusicStore/Controllers/StoreController.cs b/src/MusicStore/Controllers/StoreController.cs index 056ab02367..79466d7d7b 100644 --- a/src/MusicStore/Controllers/StoreController.cs +++ b/src/MusicStore/Controllers/StoreController.cs @@ -45,17 +45,25 @@ namespace MusicStore.Controllers public async Task 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) {