add in-memory caches to rules count endpoint

This commit is contained in:
Collin M. Barrett 2018-08-15 10:12:24 -05:00
parent db09845c8a
commit f720f83bdb
2 changed files with 14 additions and 2 deletions

View file

@ -18,6 +18,11 @@ public BaseController()
{
}
protected BaseController(IMemoryCache memoryCache)
{
MemoryCache = memoryCache;
}
protected BaseController(IMemoryCache memoryCache, SeedService seedService)
{
MemoryCache = memoryCache;

View file

@ -1,6 +1,7 @@
using System.Threading.Tasks;
using FilterLists.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Api.V1.Controllers
{
@ -8,9 +9,15 @@ public class RulesController : BaseController
{
private readonly RuleService ruleService;
public RulesController(RuleService ruleService) => this.ruleService = ruleService;
public RulesController(IMemoryCache memoryCache, RuleService ruleService) : base(memoryCache) =>
this.ruleService = ruleService;
[HttpGet]
public async Task<IActionResult> Index() => Json(await ruleService.GetCountAll());
public async Task<IActionResult> Index() =>
Json(await MemoryCache.GetOrCreate("RulesController_Index", entry =>
{
entry.AbsoluteExpirationRelativeToNow = FourHoursFromNow;
return ruleService.GetCountAll();
}));
}
}