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; }
|
||||
|
|
@ -54,6 +58,8 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
.FirstOrDefaultAsync();
|
||||
|
||||
if (album != null)
|
||||
{
|
||||
if (_appSettings.CacheDbResults)
|
||||
{
|
||||
//Remove it from cache if not retrieved in last 10 minutes.
|
||||
cache.Set(
|
||||
|
|
@ -62,6 +68,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (album == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
@ -25,6 +32,8 @@ namespace MusicStore.Controllers
|
|||
albums = await GetTopSellingAlbumsAsync(dbContext, 6);
|
||||
|
||||
if (albums != null && albums.Count > 0)
|
||||
{
|
||||
if (_appSettings.CacheDbResults)
|
||||
{
|
||||
// Refresh it every 10 minutes.
|
||||
// Let this be the last item to be removed by cache if cache GC kicks in.
|
||||
|
|
@ -36,6 +45,7 @@ namespace MusicStore.Controllers
|
|||
.SetPriority(CacheItemPriority.High));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return View(albums);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
@ -59,6 +63,8 @@ namespace MusicStore.Controllers
|
|||
.FirstOrDefaultAsync();
|
||||
|
||||
if (album != null)
|
||||
{
|
||||
if (_appSettings.CacheDbResults)
|
||||
{
|
||||
//Remove it from cache if not retrieved in last 10 minutes
|
||||
cache.Set(
|
||||
|
|
@ -67,6 +73,7 @@ namespace MusicStore.Controllers
|
|||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (album == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,5 +3,7 @@
|
|||
public class AppSettings
|
||||
{
|
||||
public string SiteTitle { get; set; }
|
||||
|
||||
public bool CacheDbResults { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"AppSettings": {
|
||||
"SiteTitle": "ASP.NET MVC Music Store"
|
||||
"SiteTitle": "ASP.NET MVC Music Store",
|
||||
"CacheDbResults": true
|
||||
},
|
||||
"DefaultAdminUsername": "Administrator@test.com",
|
||||
"DefaultAdminPassword": "YouShouldChangeThisPassword1!",
|
||||
|
|
|
|||
|
|
@ -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