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.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MusicStore.Models;
|
||||
using MusicStore.ViewModels;
|
||||
|
||||
|
|
@ -17,9 +18,12 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
[Authorize("ManageStore")]
|
||||
public class StoreManagerController : Controller
|
||||
{
|
||||
public StoreManagerController(MusicStoreContext dbContext)
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public StoreManagerController(MusicStoreContext dbContext, IOptions<AppSettings> options)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
_appSettings = options.Value;
|
||||
}
|
||||
|
||||
public MusicStoreContext DbContext { get; }
|
||||
|
|
@ -55,11 +59,14 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
|
||||
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 (_appSettings.CacheDbResults)
|
||||
{
|
||||
//Remove it from cache if not retrieved in last 10 minutes.
|
||||
cache.Set(
|
||||
cacheKey,
|
||||
album,
|
||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,12 +5,19 @@ using System.Threading.Tasks;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MusicStore.Models;
|
||||
|
||||
namespace MusicStore.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public HomeController(IOptions<AppSettings> options)
|
||||
{
|
||||
_appSettings = options.Value;
|
||||
}
|
||||
//
|
||||
// GET: /Home/
|
||||
public async Task<IActionResult> Index(
|
||||
|
|
@ -26,14 +33,17 @@ namespace MusicStore.Controllers
|
|||
|
||||
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()
|
||||
if (_appSettings.CacheDbResults)
|
||||
{
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,15 +4,19 @@ using System.Threading.Tasks;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MusicStore.Models;
|
||||
|
||||
namespace MusicStore.Controllers
|
||||
{
|
||||
public class StoreController : Controller
|
||||
{
|
||||
public StoreController(MusicStoreContext dbContext)
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public StoreController(MusicStoreContext dbContext, IOptions<AppSettings> options)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
_appSettings = options.Value;
|
||||
}
|
||||
|
||||
public MusicStoreContext DbContext { get; }
|
||||
|
|
@ -60,11 +64,14 @@ namespace MusicStore.Controllers
|
|||
|
||||
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 (_appSettings.CacheDbResults)
|
||||
{
|
||||
//Remove it from cache if not retrieved in last 10 minutes
|
||||
cache.Set(
|
||||
cacheKey,
|
||||
album,
|
||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,5 +3,7 @@
|
|||
public class AppSettings
|
||||
{
|
||||
public string SiteTitle { get; set; }
|
||||
|
||||
public bool CacheDbResults { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,15 @@
|
|||
{
|
||||
"AppSettings": {
|
||||
"SiteTitle": "ASP.NET MVC Music Store"
|
||||
},
|
||||
"DefaultAdminUsername": "Administrator@test.com",
|
||||
"DefaultAdminPassword": "YouShouldChangeThisPassword1!",
|
||||
"Data": {
|
||||
"DefaultConnection": {
|
||||
// Use a shared (and running) LocalDB database when executing in IIS e.g.
|
||||
// "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;"
|
||||
}
|
||||
"AppSettings": {
|
||||
"SiteTitle": "ASP.NET MVC Music Store",
|
||||
"CacheDbResults": true
|
||||
},
|
||||
"DefaultAdminUsername": "Administrator@test.com",
|
||||
"DefaultAdminPassword": "YouShouldChangeThisPassword1!",
|
||||
"Data": {
|
||||
"DefaultConnection": {
|
||||
// Use a shared (and running) LocalDB database when executing in IIS e.g.
|
||||
// "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.DependencyInjection;
|
||||
using MusicStore.Models;
|
||||
using MusicStore.Test;
|
||||
using Xunit;
|
||||
|
||||
namespace MusicStore.Controllers
|
||||
|
|
@ -30,7 +31,7 @@ namespace MusicStore.Controllers
|
|||
public void Error_ReturnsErrorView()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new HomeController();
|
||||
var controller = new HomeController(new TestAppSettings());
|
||||
var errorView = "~/Views/Shared/Error.cshtml";
|
||||
|
||||
// Act
|
||||
|
|
@ -48,7 +49,7 @@ namespace MusicStore.Controllers
|
|||
// Arrange
|
||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
var cache = _serviceProvider.GetRequiredService<IMemoryCache>();
|
||||
var controller = new HomeController();
|
||||
var controller = new HomeController(new TestAppSettings());
|
||||
PopulateData(dbContext);
|
||||
|
||||
// Action
|
||||
|
|
@ -69,7 +70,7 @@ namespace MusicStore.Controllers
|
|||
public void StatusCodePage_ReturnsStatusCodePage()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new HomeController();
|
||||
var controller = new HomeController(new TestAppSettings());
|
||||
var statusCodeView = "~/Views/Shared/StatusCodePage.cshtml";
|
||||
|
||||
// Action
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MusicStore.Models;
|
||||
using MusicStore.Test;
|
||||
using Xunit;
|
||||
|
||||
namespace MusicStore.Controllers
|
||||
|
|
@ -33,7 +34,7 @@ namespace MusicStore.Controllers
|
|||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: dbContext);
|
||||
|
||||
var controller = new StoreController(dbContext);
|
||||
var controller = new StoreController(dbContext, new TestAppSettings());
|
||||
|
||||
// Act
|
||||
var result = await controller.Index();
|
||||
|
|
@ -51,7 +52,9 @@ namespace MusicStore.Controllers
|
|||
public async Task Browse_ReturnsHttpNotFoundWhenNoGenreData()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new StoreController(_serviceProvider.GetRequiredService<MusicStoreContext>());
|
||||
var controller = new StoreController(
|
||||
_serviceProvider.GetRequiredService<MusicStoreContext>(),
|
||||
new TestAppSettings());
|
||||
|
||||
// Act
|
||||
var result = await controller.Browse(string.Empty);
|
||||
|
|
@ -69,7 +72,7 @@ namespace MusicStore.Controllers
|
|||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);
|
||||
|
||||
var controller = new StoreController(dbContext);
|
||||
var controller = new StoreController(dbContext, new TestAppSettings());
|
||||
|
||||
// Act
|
||||
var result = await controller.Browse(genreName);
|
||||
|
|
@ -90,7 +93,9 @@ namespace MusicStore.Controllers
|
|||
{
|
||||
// Arrange
|
||||
var albumId = int.MinValue;
|
||||
var controller = new StoreController(_serviceProvider.GetRequiredService<MusicStoreContext>());
|
||||
var controller = new StoreController(
|
||||
_serviceProvider.GetRequiredService<MusicStoreContext>(),
|
||||
new TestAppSettings());
|
||||
|
||||
// Act
|
||||
var result = await controller.Details(_serviceProvider.GetRequiredService<IMemoryCache>(), albumId);
|
||||
|
|
@ -110,7 +115,7 @@ namespace MusicStore.Controllers
|
|||
|
||||
var cache = _serviceProvider.GetRequiredService<IMemoryCache>();
|
||||
|
||||
var controller = new StoreController(dbContext);
|
||||
var controller = new StoreController(dbContext, new TestAppSettings());
|
||||
|
||||
// Act
|
||||
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