From f720f83bdb662ec62fc58b0613fe9b4fa0791d29 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Wed, 15 Aug 2018 10:12:24 -0500 Subject: [PATCH] add in-memory caches to rules count endpoint --- src/FilterLists.Api/V1/Controllers/BaseController.cs | 5 +++++ src/FilterLists.Api/V1/Controllers/RulesController.cs | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/FilterLists.Api/V1/Controllers/BaseController.cs b/src/FilterLists.Api/V1/Controllers/BaseController.cs index 37942f9a5..e77e4d6fb 100644 --- a/src/FilterLists.Api/V1/Controllers/BaseController.cs +++ b/src/FilterLists.Api/V1/Controllers/BaseController.cs @@ -18,6 +18,11 @@ public BaseController() { } + protected BaseController(IMemoryCache memoryCache) + { + MemoryCache = memoryCache; + } + protected BaseController(IMemoryCache memoryCache, SeedService seedService) { MemoryCache = memoryCache; diff --git a/src/FilterLists.Api/V1/Controllers/RulesController.cs b/src/FilterLists.Api/V1/Controllers/RulesController.cs index 9c37fbafa..111980852 100644 --- a/src/FilterLists.Api/V1/Controllers/RulesController.cs +++ b/src/FilterLists.Api/V1/Controllers/RulesController.cs @@ -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 Index() => Json(await ruleService.GetCountAll()); + public async Task Index() => + Json(await MemoryCache.GetOrCreate("RulesController_Index", entry => + { + entry.AbsoluteExpirationRelativeToNow = FourHoursFromNow; + return ruleService.GetCountAll(); + })); } } \ No newline at end of file