Created a configurable memory cache
This commit is contained in:
parent
73e341e543
commit
6435591d1c
|
|
@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using MusicStore.ViewModels;
|
using MusicStore.ViewModels;
|
||||||
|
|
||||||
|
|
@ -17,9 +18,12 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
[Authorize("ManageStore")]
|
[Authorize("ManageStore")]
|
||||||
public class StoreManagerController : Controller
|
public class StoreManagerController : Controller
|
||||||
{
|
{
|
||||||
public StoreManagerController(MusicStoreContext dbContext)
|
private readonly AppSettings _appSettings;
|
||||||
|
|
||||||
|
public StoreManagerController(MusicStoreContext dbContext, IOptions<AppSettings> options)
|
||||||
{
|
{
|
||||||
DbContext = dbContext;
|
DbContext = dbContext;
|
||||||
|
_appSettings = options.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MusicStoreContext DbContext { get; }
|
public MusicStoreContext DbContext { get; }
|
||||||
|
|
@ -55,11 +59,14 @@ namespace MusicStore.Areas.Admin.Controllers
|
||||||
|
|
||||||
if (album != null)
|
if (album != null)
|
||||||
{
|
{
|
||||||
//Remove it from cache if not retrieved in last 10 minutes.
|
if (_appSettings.CacheDbResults)
|
||||||
cache.Set(
|
{
|
||||||
cacheKey,
|
//Remove it from cache if not retrieved in last 10 minutes.
|
||||||
album,
|
cache.Set(
|
||||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
cacheKey,
|
||||||
|
album,
|
||||||
|
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,19 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
|
private readonly AppSettings _appSettings;
|
||||||
|
|
||||||
|
public HomeController(IOptions<AppSettings> options)
|
||||||
|
{
|
||||||
|
_appSettings = options.Value;
|
||||||
|
}
|
||||||
//
|
//
|
||||||
// GET: /Home/
|
// GET: /Home/
|
||||||
public async Task<IActionResult> Index(
|
public async Task<IActionResult> Index(
|
||||||
|
|
@ -26,14 +33,17 @@ namespace MusicStore.Controllers
|
||||||
|
|
||||||
if (albums != null && albums.Count > 0)
|
if (albums != null && albums.Count > 0)
|
||||||
{
|
{
|
||||||
// Refresh it every 10 minutes.
|
if (_appSettings.CacheDbResults)
|
||||||
// Let this be the last item to be removed by cache if cache GC kicks in.
|
{
|
||||||
cache.Set(
|
// Refresh it every 10 minutes.
|
||||||
cacheKey,
|
// Let this be the last item to be removed by cache if cache GC kicks in.
|
||||||
albums,
|
cache.Set(
|
||||||
new MemoryCacheEntryOptions()
|
cacheKey,
|
||||||
|
albums,
|
||||||
|
new MemoryCacheEntryOptions()
|
||||||
.SetAbsoluteExpiration(TimeSpan.FromMinutes(10))
|
.SetAbsoluteExpiration(TimeSpan.FromMinutes(10))
|
||||||
.SetPriority(CacheItemPriority.High));
|
.SetPriority(CacheItemPriority.High));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,19 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
public class StoreController : Controller
|
public class StoreController : Controller
|
||||||
{
|
{
|
||||||
public StoreController(MusicStoreContext dbContext)
|
private readonly AppSettings _appSettings;
|
||||||
|
|
||||||
|
public StoreController(MusicStoreContext dbContext, IOptions<AppSettings> options)
|
||||||
{
|
{
|
||||||
DbContext = dbContext;
|
DbContext = dbContext;
|
||||||
|
_appSettings = options.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MusicStoreContext DbContext { get; }
|
public MusicStoreContext DbContext { get; }
|
||||||
|
|
@ -60,11 +64,14 @@ namespace MusicStore.Controllers
|
||||||
|
|
||||||
if (album != null)
|
if (album != null)
|
||||||
{
|
{
|
||||||
//Remove it from cache if not retrieved in last 10 minutes
|
if (_appSettings.CacheDbResults)
|
||||||
cache.Set(
|
{
|
||||||
cacheKey,
|
//Remove it from cache if not retrieved in last 10 minutes
|
||||||
album,
|
cache.Set(
|
||||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
cacheKey,
|
||||||
|
album,
|
||||||
|
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,7 @@
|
||||||
public class AppSettings
|
public class AppSettings
|
||||||
{
|
{
|
||||||
public string SiteTitle { get; set; }
|
public string SiteTitle { get; set; }
|
||||||
|
|
||||||
|
public bool CacheDbResults { get; set; } = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,14 +1,15 @@
|
||||||
{
|
{
|
||||||
"AppSettings": {
|
"AppSettings": {
|
||||||
"SiteTitle": "ASP.NET MVC Music Store"
|
"SiteTitle": "ASP.NET MVC Music Store",
|
||||||
},
|
"CacheDbResults": true
|
||||||
"DefaultAdminUsername": "Administrator@test.com",
|
},
|
||||||
"DefaultAdminPassword": "YouShouldChangeThisPassword1!",
|
"DefaultAdminUsername": "Administrator@test.com",
|
||||||
"Data": {
|
"DefaultAdminPassword": "YouShouldChangeThisPassword1!",
|
||||||
"DefaultConnection": {
|
"Data": {
|
||||||
// Use a shared (and running) LocalDB database when executing in IIS e.g.
|
"DefaultConnection": {
|
||||||
// "Server=(localdb)\\.\\IIS_DB;Database=MusicStore;Trusted_Connection=False;MultipleActiveResultSets=true;User ID=iis_login;Password=********"
|
// Use a shared (and running) LocalDB database when executing in IIS e.g.
|
||||||
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStore;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"
|
// "Server=(localdb)\\.\\IIS_DB;Database=MusicStore;Trusted_Connection=False;MultipleActiveResultSets=true;User ID=iis_login;Password=********"
|
||||||
}
|
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStore;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
|
using MusicStore.Test;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
|
|
@ -30,7 +31,7 @@ namespace MusicStore.Controllers
|
||||||
public void Error_ReturnsErrorView()
|
public void Error_ReturnsErrorView()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var controller = new HomeController();
|
var controller = new HomeController(new TestAppSettings());
|
||||||
var errorView = "~/Views/Shared/Error.cshtml";
|
var errorView = "~/Views/Shared/Error.cshtml";
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -48,7 +49,7 @@ namespace MusicStore.Controllers
|
||||||
// Arrange
|
// Arrange
|
||||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||||
var cache = _serviceProvider.GetRequiredService<IMemoryCache>();
|
var cache = _serviceProvider.GetRequiredService<IMemoryCache>();
|
||||||
var controller = new HomeController();
|
var controller = new HomeController(new TestAppSettings());
|
||||||
PopulateData(dbContext);
|
PopulateData(dbContext);
|
||||||
|
|
||||||
// Action
|
// Action
|
||||||
|
|
@ -69,7 +70,7 @@ namespace MusicStore.Controllers
|
||||||
public void StatusCodePage_ReturnsStatusCodePage()
|
public void StatusCodePage_ReturnsStatusCodePage()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var controller = new HomeController();
|
var controller = new HomeController(new TestAppSettings());
|
||||||
var statusCodeView = "~/Views/Shared/StatusCodePage.cshtml";
|
var statusCodeView = "~/Views/Shared/StatusCodePage.cshtml";
|
||||||
|
|
||||||
// Action
|
// Action
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
|
using MusicStore.Test;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
|
|
@ -33,7 +34,7 @@ namespace MusicStore.Controllers
|
||||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||||
CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: dbContext);
|
CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: dbContext);
|
||||||
|
|
||||||
var controller = new StoreController(dbContext);
|
var controller = new StoreController(dbContext, new TestAppSettings());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await controller.Index();
|
var result = await controller.Index();
|
||||||
|
|
@ -51,7 +52,9 @@ namespace MusicStore.Controllers
|
||||||
public async Task Browse_ReturnsHttpNotFoundWhenNoGenreData()
|
public async Task Browse_ReturnsHttpNotFoundWhenNoGenreData()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var controller = new StoreController(_serviceProvider.GetRequiredService<MusicStoreContext>());
|
var controller = new StoreController(
|
||||||
|
_serviceProvider.GetRequiredService<MusicStoreContext>(),
|
||||||
|
new TestAppSettings());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await controller.Browse(string.Empty);
|
var result = await controller.Browse(string.Empty);
|
||||||
|
|
@ -69,7 +72,7 @@ namespace MusicStore.Controllers
|
||||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||||
CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);
|
CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);
|
||||||
|
|
||||||
var controller = new StoreController(dbContext);
|
var controller = new StoreController(dbContext, new TestAppSettings());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await controller.Browse(genreName);
|
var result = await controller.Browse(genreName);
|
||||||
|
|
@ -90,7 +93,9 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var albumId = int.MinValue;
|
var albumId = int.MinValue;
|
||||||
var controller = new StoreController(_serviceProvider.GetRequiredService<MusicStoreContext>());
|
var controller = new StoreController(
|
||||||
|
_serviceProvider.GetRequiredService<MusicStoreContext>(),
|
||||||
|
new TestAppSettings());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await controller.Details(_serviceProvider.GetRequiredService<IMemoryCache>(), albumId);
|
var result = await controller.Details(_serviceProvider.GetRequiredService<IMemoryCache>(), albumId);
|
||||||
|
|
@ -110,7 +115,7 @@ namespace MusicStore.Controllers
|
||||||
|
|
||||||
var cache = _serviceProvider.GetRequiredService<IMemoryCache>();
|
var cache = _serviceProvider.GetRequiredService<IMemoryCache>();
|
||||||
|
|
||||||
var controller = new StoreController(dbContext);
|
var controller = new StoreController(dbContext, new TestAppSettings());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await controller.Details(cache, albumId);
|
var result = await controller.Details(cache, albumId);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace MusicStore.Test
|
||||||
|
{
|
||||||
|
public class TestAppSettings : IOptions<AppSettings>
|
||||||
|
{
|
||||||
|
private readonly AppSettings _appSettings;
|
||||||
|
|
||||||
|
public TestAppSettings(bool storeInCache = true)
|
||||||
|
{
|
||||||
|
_appSettings = new AppSettings()
|
||||||
|
{
|
||||||
|
SiteTitle = "ASP.NET MVC Music Store",
|
||||||
|
CacheDbResults = storeInCache
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public AppSettings Value
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _appSettings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue