diff --git a/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs index be77e9c2de..284da30797 100644 --- a/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs +++ b/src/MusicStore/Areas/Admin/Controllers/StoreManagerController.cs @@ -7,7 +7,8 @@ using MusicStore.Models; using System.Linq; using MusicStore.Hubs; using MusicStore.ViewModels; -using Microsoft.AspNet.MemoryCache; +using Microsoft.Framework.Cache.Memory; +using System; namespace MusicStore.Areas.Admin.Controllers { @@ -66,14 +67,16 @@ namespace MusicStore.Areas.Admin.Controllers string cacheId = string.Format("album_{0}", id); var album = cache.GetOrAdd(cacheId, context => { + //Remove it from cache if not retrieved in last 10 minutes + context.SetSlidingExpiraiton(TimeSpan.FromMinutes(10)); //If this returns null how do we prevent the cache to store this. - return db.Albums.Single(a => a.AlbumId == id); + return db.Albums.Where(a => a.AlbumId == id).FirstOrDefault(); }); if (album == null) { cache.Remove(cacheId); - return HttpNotFound(); + return View(album); } // TODO [EF] We don't query related data as yet. We have to populate this until we do automatically. @@ -101,6 +104,7 @@ namespace MusicStore.Areas.Admin.Controllers db.Albums.Add(album); db.SaveChanges(); annoucementHub.Clients.All.announcement(new AlbumData() { Title = album.Title, Url = Url.Action("Details", "Store", new { id = album.AlbumId }) }); + cache.Remove("latestAlbum"); return RedirectToAction("Index"); } @@ -113,11 +117,11 @@ namespace MusicStore.Areas.Admin.Controllers // GET: /StoreManager/Edit/5 public IActionResult Edit(int id) { - Album album = db.Albums.Single(a => a.AlbumId == id); + Album album = db.Albums.Where(a => a.AlbumId == id).FirstOrDefault(); if (album == null) { - return HttpNotFound(); + return View(album); } ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); @@ -149,11 +153,7 @@ namespace MusicStore.Areas.Admin.Controllers // GET: /StoreManager/RemoveAlbum/5 public IActionResult RemoveAlbum(int id) { - Album album = db.Albums.Single(a => a.AlbumId == id); - if (album == null) - { - return HttpNotFound(); - } + Album album = db.Albums.Where(a => a.AlbumId == id).FirstOrDefault(); return View(album); } @@ -162,11 +162,16 @@ namespace MusicStore.Areas.Admin.Controllers [HttpPost, ActionName("RemoveAlbum")] public IActionResult RemoveAlbumConfirmed(int id) { - Album album = db.Albums.Single(a => a.AlbumId == id); - db.Albums.Remove(album); - db.SaveChanges(); - //Remove the cache entry as it is removed - cache.Remove(string.Format("album_{0}", id)); + Album album = db.Albums.Where(a => a.AlbumId == id).FirstOrDefault(); + + if (album != null) + { + db.Albums.Remove(album); + db.SaveChanges(); + //Remove the cache entry as it is removed + cache.Remove(string.Format("album_{0}", id)); + } + return RedirectToAction("Index"); } diff --git a/src/MusicStore/Areas/Admin/Views/StoreManager/Details.cshtml b/src/MusicStore/Areas/Admin/Views/StoreManager/Details.cshtml index 3e65c71cc6..bd342e5327 100644 --- a/src/MusicStore/Areas/Admin/Views/StoreManager/Details.cshtml +++ b/src/MusicStore/Areas/Admin/Views/StoreManager/Details.cshtml @@ -4,54 +4,61 @@ ViewBag.Title = "Details"; } -
@Html.ActionLink("Edit", "Edit", new { id = Model.AlbumId }) | @Html.ActionLink("Back to List", "Index") diff --git a/src/MusicStore/Areas/Admin/Views/StoreManager/Edit.cshtml b/src/MusicStore/Areas/Admin/Views/StoreManager/Edit.cshtml index 3e9def0bfb..63d6b61430 100644 --- a/src/MusicStore/Areas/Admin/Views/StoreManager/Edit.cshtml +++ b/src/MusicStore/Areas/Admin/Views/StoreManager/Edit.cshtml @@ -6,62 +6,69 @@
- Are you sure you want to delete the album titled - @Model.Title? -
- -@using (Html.BeginForm()) +@if (Model != null) { +- -
-- @Html.ActionLink("Back to List", "Index") + Are you sure you want to delete the album titled + @Model.Title?
+ @using (Html.BeginForm()) + { ++ +
++ @Html.ActionLink("Back to List", "Index") +
+ } +} +else +{ + @Html.Label(null, "Unable to locate the album") } \ No newline at end of file diff --git a/src/MusicStore/Components/AnnouncementComponent.cs b/src/MusicStore/Components/AnnouncementComponent.cs index 17bef7f3c9..17ce94f357 100644 --- a/src/MusicStore/Components/AnnouncementComponent.cs +++ b/src/MusicStore/Components/AnnouncementComponent.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Mvc; using MusicStore.Models; -using Microsoft.AspNet.MemoryCache; +using Microsoft.Framework.Cache.Memory; namespace MusicStore.Components { @@ -23,6 +23,7 @@ namespace MusicStore.Components { var latestAlbum = await cache.GetOrAdd("latestAlbum", async context => { + context.SetAbsoluteExpiration(TimeSpan.FromMinutes(10)); return await GetLatestAlbum(); }); diff --git a/src/MusicStore/Controllers/HomeController.cs b/src/MusicStore/Controllers/HomeController.cs index 0740ef5793..e66e9a7add 100644 --- a/src/MusicStore/Controllers/HomeController.cs +++ b/src/MusicStore/Controllers/HomeController.cs @@ -1,6 +1,7 @@ -using Microsoft.AspNet.MemoryCache; +using Microsoft.Framework.Cache.Memory; using Microsoft.AspNet.Mvc; using MusicStore.Models; +using System; using System.Collections.Generic; using System.Linq; @@ -22,7 +23,13 @@ namespace MusicStore.Controllers public IActionResult Index() { // Get most popular albums - var albums = cache.GetOrAdd("topselling", (context => GetTopSellingAlbums(6))); + var albums = cache.GetOrAdd("topselling", context => + { + //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 GetTopSellingAlbums(6); + }); return View(albums); } diff --git a/src/MusicStore/Controllers/StoreController.cs b/src/MusicStore/Controllers/StoreController.cs index db7fc0c108..282c0fd4c6 100644 --- a/src/MusicStore/Controllers/StoreController.cs +++ b/src/MusicStore/Controllers/StoreController.cs @@ -1,6 +1,7 @@ -using Microsoft.AspNet.MemoryCache; +using Microsoft.Framework.Cache.Memory; using Microsoft.AspNet.Mvc; using MusicStore.Models; +using System; using System.Linq; namespace MusicStore.Controllers @@ -44,6 +45,9 @@ namespace MusicStore.Controllers { var album = cache.GetOrAdd(string.Format("album_{0}", id), context => { + //Remove it from cache if not retrieved in last 10 minutes + context.SetSlidingExpiraiton(TimeSpan.FromMinutes(10)); + var albumData = db.Albums.Single(a => a.AlbumId == id); // TODO [EF] We don't query related data as yet. We have to populate this until we do automatically. diff --git a/src/MusicStore/Mocks/StartupSocialTesting.cs b/src/MusicStore/Mocks/StartupSocialTesting.cs index 20974508ad..01763a0d18 100644 --- a/src/MusicStore/Mocks/StartupSocialTesting.cs +++ b/src/MusicStore/Mocks/StartupSocialTesting.cs @@ -14,7 +14,7 @@ using Microsoft.AspNet.Security.Google; using Microsoft.AspNet.Security.Twitter; using Microsoft.AspNet.Security.MicrosoftAccount; using Microsoft.AspNet.Security; -using Microsoft.AspNet.MemoryCache; +using Microsoft.Framework.Cache.Memory; using MusicStore.Mocks.Common; using MusicStore.Mocks.Facebook; using MusicStore.Mocks.Twitter; diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index e12ca0b5ca..27c688b0dc 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -14,7 +14,7 @@ using Microsoft.AspNet.Security.Google; using Microsoft.AspNet.Security.Twitter; using Microsoft.AspNet.Security.MicrosoftAccount; using Microsoft.AspNet.Security; -using Microsoft.AspNet.MemoryCache; +using Microsoft.Framework.Cache.Memory; namespace MusicStore { diff --git a/src/MusicStore/StartupNtlmAuthentication.cs b/src/MusicStore/StartupNtlmAuthentication.cs index cc44a7ef2d..5fa10aaa86 100644 --- a/src/MusicStore/StartupNtlmAuthentication.cs +++ b/src/MusicStore/StartupNtlmAuthentication.cs @@ -10,22 +10,22 @@ using Microsoft.Net.Http.Server; using Microsoft.AspNet.Server.WebListener; using System.Security.Claims; using System.Security.Principal; -using Microsoft.AspNet.MemoryCache; +using Microsoft.Framework.Cache.Memory; namespace MusicStore { ///