Adding in memory caching to the sample
This commit is contained in:
parent
694bfe91c7
commit
7e77078160
|
|
@ -20,8 +20,8 @@ IF EXIST packages\KoreBuild goto run
|
||||||
.nuget\NuGet.exe install Sake -version 0.2 -o packages -ExcludeVersion
|
.nuget\NuGet.exe install Sake -version 0.2 -o packages -ExcludeVersion
|
||||||
|
|
||||||
IF "%SKIP_KRE_INSTALL%"=="1" goto run
|
IF "%SKIP_KRE_INSTALL%"=="1" goto run
|
||||||
CALL packages\KoreBuild\build\kvm upgrade -x64
|
CALL packages\KoreBuild\build\kvm upgrade -amd64
|
||||||
CALL packages\KoreBuild\build\kvm install default -runtime CoreCLR -x64
|
CALL packages\KoreBuild\build\kvm install default -runtime CoreCLR -amd64
|
||||||
CALL packages\KoreBuild\build\kvm install default -x86
|
CALL packages\KoreBuild\build\kvm install default -x86
|
||||||
CALL packages\KoreBuild\build\kvm install default -runtime CoreCLR -x86
|
CALL packages\KoreBuild\build\kvm install default -runtime CoreCLR -x86
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using MusicStore.Models;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using MusicStore.Hubs;
|
using MusicStore.Hubs;
|
||||||
using MusicStore.ViewModels;
|
using MusicStore.ViewModels;
|
||||||
|
using Microsoft.AspNet.MemoryCache;
|
||||||
|
|
||||||
namespace MusicStore.Areas.Admin.Controllers
|
namespace MusicStore.Areas.Admin.Controllers
|
||||||
{
|
{
|
||||||
|
|
@ -16,11 +17,13 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
{
|
{
|
||||||
private readonly MusicStoreContext db;
|
private readonly MusicStoreContext db;
|
||||||
private IHubContext annoucementHub;
|
private IHubContext annoucementHub;
|
||||||
|
private readonly IMemoryCache cache;
|
||||||
|
|
||||||
public StoreManagerController(MusicStoreContext context, IConnectionManager connectionManager)
|
public StoreManagerController(MusicStoreContext context, IConnectionManager connectionManager, IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
db = context;
|
db = context;
|
||||||
annoucementHub = connectionManager.GetHubContext<AnnouncementHub>();
|
annoucementHub = connectionManager.GetHubContext<AnnouncementHub>();
|
||||||
|
cache = memoryCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
@ -58,12 +61,18 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
//
|
//
|
||||||
// GET: /StoreManager/Details/5
|
// GET: /StoreManager/Details/5
|
||||||
|
|
||||||
public IActionResult Details(int id = 0)
|
public IActionResult Details(int id)
|
||||||
{
|
{
|
||||||
Album album = db.Albums.Single(a => a.AlbumId == id);
|
string cacheId = string.Format("album_{0}", id);
|
||||||
|
var album = cache.GetOrAdd(cacheId, context =>
|
||||||
|
{
|
||||||
|
//If this returns null how do we prevent the cache to store this.
|
||||||
|
return db.Albums.Single(a => a.AlbumId == id);
|
||||||
|
});
|
||||||
|
|
||||||
if (album == null)
|
if (album == null)
|
||||||
{
|
{
|
||||||
|
cache.Remove(cacheId);
|
||||||
return HttpNotFound();
|
return HttpNotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,7 +111,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /StoreManager/Edit/5
|
// GET: /StoreManager/Edit/5
|
||||||
public IActionResult Edit(int id = 0)
|
public IActionResult Edit(int id)
|
||||||
{
|
{
|
||||||
Album album = db.Albums.Single(a => a.AlbumId == id);
|
Album album = db.Albums.Single(a => a.AlbumId == id);
|
||||||
|
|
||||||
|
|
@ -126,6 +135,8 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
{
|
{
|
||||||
db.ChangeTracker.Entry(album).State = EntityState.Modified;
|
db.ChangeTracker.Entry(album).State = EntityState.Modified;
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
//Invalidate the cache entry as it is modified
|
||||||
|
cache.Remove(string.Format("album_{0}", album.AlbumId));
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,7 +147,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
|
|
||||||
//
|
//
|
||||||
// GET: /StoreManager/RemoveAlbum/5
|
// GET: /StoreManager/RemoveAlbum/5
|
||||||
public IActionResult RemoveAlbum(int id = 0)
|
public IActionResult RemoveAlbum(int id)
|
||||||
{
|
{
|
||||||
Album album = db.Albums.Single(a => a.AlbumId == id);
|
Album album = db.Albums.Single(a => a.AlbumId == id);
|
||||||
if (album == null)
|
if (album == null)
|
||||||
|
|
@ -154,6 +165,8 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
Album album = db.Albums.Single(a => a.AlbumId == id);
|
Album album = db.Albums.Single(a => a.AlbumId == id);
|
||||||
db.Albums.Remove(album);
|
db.Albums.Remove(album);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
//Remove the cache entry as it is removed
|
||||||
|
cache.Remove(string.Format("album_{0}", id));
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
|
using Microsoft.AspNet.MemoryCache;
|
||||||
|
|
||||||
namespace MusicStore.Components
|
namespace MusicStore.Components
|
||||||
{
|
{
|
||||||
|
|
@ -10,15 +11,21 @@ namespace MusicStore.Components
|
||||||
public class AnnouncementComponent : ViewComponent
|
public class AnnouncementComponent : ViewComponent
|
||||||
{
|
{
|
||||||
private readonly MusicStoreContext db;
|
private readonly MusicStoreContext db;
|
||||||
|
private readonly IMemoryCache cache;
|
||||||
|
|
||||||
public AnnouncementComponent(MusicStoreContext context)
|
public AnnouncementComponent(MusicStoreContext context, IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
db = context;
|
db = context;
|
||||||
|
cache = memoryCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IViewComponentResult> InvokeAsync()
|
public async Task<IViewComponentResult> InvokeAsync()
|
||||||
{
|
{
|
||||||
var latestAlbum = await GetLatestAlbum();
|
var latestAlbum = await cache.GetOrAdd("latestAlbum", async context =>
|
||||||
|
{
|
||||||
|
return await GetLatestAlbum();
|
||||||
|
});
|
||||||
|
|
||||||
return View(latestAlbum);
|
return View(latestAlbum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ namespace MusicStore.Components
|
||||||
|
|
||||||
private Task<IOrderedEnumerable<string>> GetCartItems()
|
private Task<IOrderedEnumerable<string>> GetCartItems()
|
||||||
{
|
{
|
||||||
var cart = ShoppingCart.GetCart(db, this.Context);
|
var cart = ShoppingCart.GetCart(db, Context);
|
||||||
|
|
||||||
var cartItems = cart.GetCartItems()
|
var cartItems = cart.GetCartItems()
|
||||||
.Select(a => a.Album.Title)
|
.Select(a => a.Album.Title)
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ namespace MusicStore.Controllers
|
||||||
db.Orders.Add(order);
|
db.Orders.Add(order);
|
||||||
|
|
||||||
//Process the order
|
//Process the order
|
||||||
var cart = ShoppingCart.GetCart(db, this.Context);
|
var cart = ShoppingCart.GetCart(db, Context);
|
||||||
cart.CreateOrder(order);
|
cart.CreateOrder(order);
|
||||||
|
|
||||||
// Save all changes
|
// Save all changes
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.MemoryCache;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -8,10 +9,12 @@ namespace MusicStore.Controllers
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
private readonly MusicStoreContext db;
|
private readonly MusicStoreContext db;
|
||||||
|
private readonly IMemoryCache cache;
|
||||||
|
|
||||||
public HomeController(MusicStoreContext context)
|
public HomeController(MusicStoreContext context, IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
db = context;
|
db = context;
|
||||||
|
cache = memoryCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
@ -19,7 +22,7 @@ namespace MusicStore.Controllers
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
// Get most popular albums
|
// Get most popular albums
|
||||||
var albums = GetTopSellingAlbums(6);
|
var albums = cache.GetOrAdd("topselling", (context => GetTopSellingAlbums(6)));
|
||||||
|
|
||||||
return View(albums);
|
return View(albums);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.MemoryCache;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
|
@ -7,10 +8,12 @@ namespace MusicStore.Controllers
|
||||||
public class StoreController : Controller
|
public class StoreController : Controller
|
||||||
{
|
{
|
||||||
private readonly MusicStoreContext db;
|
private readonly MusicStoreContext db;
|
||||||
|
private readonly IMemoryCache cache;
|
||||||
|
|
||||||
public StoreController(MusicStoreContext context)
|
public StoreController(MusicStoreContext context, IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
db = context;
|
db = context;
|
||||||
|
cache = memoryCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
@ -39,11 +42,15 @@ namespace MusicStore.Controllers
|
||||||
|
|
||||||
public IActionResult Details(int id)
|
public IActionResult Details(int id)
|
||||||
{
|
{
|
||||||
var album = db.Albums.Single(a => a.AlbumId == id);
|
var album = cache.GetOrAdd(string.Format("album_{0}", id), context =>
|
||||||
|
{
|
||||||
|
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.
|
// TODO [EF] We don't query related data as yet. We have to populate this until we do automatically.
|
||||||
album.Genre = db.Genres.Single(g => g.GenreId == album.GenreId);
|
albumData.Genre = db.Genres.Single(g => g.GenreId == albumData.GenreId);
|
||||||
album.Artist = db.Artists.Single(a => a.ArtistId == album.ArtistId);
|
albumData.Artist = db.Artists.Single(a => a.ArtistId == albumData.ArtistId);
|
||||||
|
return albumData;
|
||||||
|
});
|
||||||
|
|
||||||
return View(album);
|
return View(album);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,8 @@ namespace MusicStore.Models
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Album()
|
public Album()
|
||||||
{
|
{
|
||||||
this.OrderDetails = new List<OrderDetail>();
|
OrderDetails = new List<OrderDetail>();
|
||||||
this.Created = DateTime.UtcNow;
|
Created = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -14,6 +14,7 @@ using Microsoft.AspNet.Security.Google;
|
||||||
using Microsoft.AspNet.Security.Twitter;
|
using Microsoft.AspNet.Security.Twitter;
|
||||||
using Microsoft.AspNet.Security.MicrosoftAccount;
|
using Microsoft.AspNet.Security.MicrosoftAccount;
|
||||||
using Microsoft.AspNet.Security;
|
using Microsoft.AspNet.Security;
|
||||||
|
using Microsoft.AspNet.MemoryCache;
|
||||||
|
|
||||||
namespace MusicStore
|
namespace MusicStore
|
||||||
{
|
{
|
||||||
|
|
@ -76,6 +77,10 @@ namespace MusicStore
|
||||||
|
|
||||||
//Add all SignalR related services to IoC.
|
//Add all SignalR related services to IoC.
|
||||||
services.AddSignalR();
|
services.AddSignalR();
|
||||||
|
|
||||||
|
//Add InMemoryCache
|
||||||
|
//Currently not able to AddSingleTon
|
||||||
|
services.AddInstance<IMemoryCache>(new MemoryCache());
|
||||||
});
|
});
|
||||||
|
|
||||||
//Configure SignalR
|
//Configure SignalR
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,8 @@
|
||||||
"EntityFramework.InMemory": "7.0.0-*",
|
"EntityFramework.InMemory": "7.0.0-*",
|
||||||
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
|
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
|
||||||
"Microsoft.Framework.OptionsModel": "1.0.0-*",
|
"Microsoft.Framework.OptionsModel": "1.0.0-*",
|
||||||
"Microsoft.AspNet.SignalR.Server": "3.0.0-*"
|
"Microsoft.AspNet.SignalR.Server": "3.0.0-*",
|
||||||
|
"Microsoft.AspNet.MemoryCache": "1.0.0-*"
|
||||||
},
|
},
|
||||||
"commands": {
|
"commands": {
|
||||||
"WebListener": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5002",
|
"WebListener": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5002",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue