diff --git a/src/FilterLists.Api/V1/Controllers/BaseController.cs b/src/FilterLists.Api/V1/Controllers/BaseController.cs index 41b47f4e6..8c924da3d 100644 --- a/src/FilterLists.Api/V1/Controllers/BaseController.cs +++ b/src/FilterLists.Api/V1/Controllers/BaseController.cs @@ -1,4 +1,6 @@ using System; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; using FilterLists.Services.Seed; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; @@ -11,23 +13,35 @@ namespace FilterLists.Api.V1.Controllers [ResponseCache(Duration = 14400)] public class BaseController : Controller { - protected static readonly TimeSpan MemoryCacheExpirationDefault = TimeSpan.FromHours(4); - protected readonly IMemoryCache MemoryCache; + private static readonly TimeSpan MemoryCacheExpirationDefault = TimeSpan.FromHours(4); protected readonly SeedService SeedService; + private readonly IMemoryCache memoryCache; public BaseController() { } - protected BaseController(IMemoryCache memoryCache) => MemoryCache = memoryCache; + protected BaseController(IMemoryCache memoryCache) => this.memoryCache = memoryCache; protected BaseController(IMemoryCache memoryCache, SeedService seedService) { - MemoryCache = memoryCache; + this.memoryCache = memoryCache; SeedService = seedService; } - protected IActionResult CoalesceNotFound(JsonResult result) => + protected async Task Get(Func> createAction, int? actionParam = null, + [CallerMemberName] string actionName = null) + { + var cacheKey = GetType().Name + "_" + actionName + "_" + actionParam; + var result = await memoryCache.GetOrCreateAsync(cacheKey, entry => + { + entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; + return createAction(); + }); + return CoalesceNotFound(Json(result)); + } + + private IActionResult CoalesceNotFound(JsonResult result) => result.Value is null ? NotFound() : (IActionResult)result; } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/DependentsController.cs b/src/FilterLists.Api/V1/Controllers/DependentsController.cs index 12a2730d5..a11dc6958 100644 --- a/src/FilterLists.Api/V1/Controllers/DependentsController.cs +++ b/src/FilterLists.Api/V1/Controllers/DependentsController.cs @@ -15,11 +15,8 @@ public DependentsController(IMemoryCache memoryCache, SeedService seedService) : [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("DependentsController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(typeof(Dependent).GetProperty("DependentFilterListId"), - typeof(Dependent).GetProperty("DependencyFilterListId")); - }))); + await Get(() => SeedService.GetAllAsync( + typeof(Dependent).GetProperty("DependentFilterListId"), + typeof(Dependent).GetProperty("DependencyFilterListId"))); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/ForksController.cs b/src/FilterLists.Api/V1/Controllers/ForksController.cs index 5353e5149..1615b34ef 100644 --- a/src/FilterLists.Api/V1/Controllers/ForksController.cs +++ b/src/FilterLists.Api/V1/Controllers/ForksController.cs @@ -15,11 +15,8 @@ public ForksController(IMemoryCache memoryCache, SeedService seedService) : base [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ForksController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(typeof(Fork).GetProperty("UpstreamFilterListId"), - typeof(Fork).GetProperty("ForkFilterListId")); - }))); + await Get(() => SeedService.GetAllAsync( + typeof(Fork).GetProperty("UpstreamFilterListId"), + typeof(Fork).GetProperty("ForkFilterListId"))); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/LanguagesController.cs b/src/FilterLists.Api/V1/Controllers/LanguagesController.cs index 18fd80220..b7f0e4769 100644 --- a/src/FilterLists.Api/V1/Controllers/LanguagesController.cs +++ b/src/FilterLists.Api/V1/Controllers/LanguagesController.cs @@ -17,27 +17,15 @@ public LanguagesController(IMemoryCache memoryCache, SeedService seedService, La [HttpGet] public async Task Index() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LanguagesController_Index", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return languageService.GetAllTargetedAsync(); - }))); + await Get(() => languageService.GetAllTargetedAsync()); [HttpGet] [Route("{id}")] public async Task GetById(int id) => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LanguagesController_GetById_" + id, entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return languageService.GetTargetedByIdAsync(id); - }))); + await Get(() => languageService.GetTargetedByIdAsync(id), id); [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LanguagesController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(); - }))); + await Get(() => SeedService.GetAllAsync()); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/LicensesController.cs b/src/FilterLists.Api/V1/Controllers/LicensesController.cs index 9a8c57323..a205af9cb 100644 --- a/src/FilterLists.Api/V1/Controllers/LicensesController.cs +++ b/src/FilterLists.Api/V1/Controllers/LicensesController.cs @@ -15,10 +15,6 @@ public LicensesController(IMemoryCache memoryCache, SeedService seedService) : b [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LicensesController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(); - }))); + await Get(() => SeedService.GetAllAsync()); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/ListsController.cs b/src/FilterLists.Api/V1/Controllers/ListsController.cs index ef5706bbd..f2726996f 100644 --- a/src/FilterLists.Api/V1/Controllers/ListsController.cs +++ b/src/FilterLists.Api/V1/Controllers/ListsController.cs @@ -17,27 +17,15 @@ public ListsController(IMemoryCache memoryCache, SeedService seedService, Filter [HttpGet] public async Task Index() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsController_Index", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return filterListService.GetAllAsync(); - }))); + await Get(() => filterListService.GetAllAsync()); [HttpGet] [Route("{id}")] public async Task GetById(int id) => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsController_GetById_" + id, entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return filterListService.GetDetailsAsync((uint)id); - }))); + await Get(() => filterListService.GetDetailsAsync(id), id); [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(); - }))); + await Get(() => SeedService.GetAllAsync()); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/ListsLanguagesController.cs b/src/FilterLists.Api/V1/Controllers/ListsLanguagesController.cs index 7e47a9439..454d1e1e8 100644 --- a/src/FilterLists.Api/V1/Controllers/ListsLanguagesController.cs +++ b/src/FilterLists.Api/V1/Controllers/ListsLanguagesController.cs @@ -16,12 +16,8 @@ public ListsLanguagesController(IMemoryCache memoryCache, SeedService seedServic [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsLanguagesController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync( - typeof(FilterListLanguage).GetProperty("FilterListId"), - typeof(FilterListLanguage).GetProperty("LanguageId")); - }))); + await Get(() => SeedService.GetAllAsync( + typeof(FilterListLanguage).GetProperty("FilterListId"), + typeof(FilterListLanguage).GetProperty("LanguageId"))); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/ListsMaintainersController.cs b/src/FilterLists.Api/V1/Controllers/ListsMaintainersController.cs index cfadfba4c..335c858e9 100644 --- a/src/FilterLists.Api/V1/Controllers/ListsMaintainersController.cs +++ b/src/FilterLists.Api/V1/Controllers/ListsMaintainersController.cs @@ -16,12 +16,8 @@ public ListsMaintainersController(IMemoryCache memoryCache, SeedService seedServ [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsMaintainersController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync( - typeof(FilterListMaintainer).GetProperty("MaintainerId"), - typeof(FilterListMaintainer).GetProperty("FilterListId")); - }))); + await Get(() => SeedService.GetAllAsync( + typeof(FilterListMaintainer).GetProperty("MaintainerId"), + typeof(FilterListMaintainer).GetProperty("FilterListId"))); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/ListsTagsController.cs b/src/FilterLists.Api/V1/Controllers/ListsTagsController.cs index 5d41fc4ce..0ceecaa7b 100644 --- a/src/FilterLists.Api/V1/Controllers/ListsTagsController.cs +++ b/src/FilterLists.Api/V1/Controllers/ListsTagsController.cs @@ -16,12 +16,8 @@ public ListsTagsController(IMemoryCache memoryCache, SeedService seedService) [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsTagsController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync( - typeof(FilterListTag).GetProperty("FilterListId"), - typeof(FilterListTag).GetProperty("TagId")); - }))); + await Get(() => SeedService.GetAllAsync( + typeof(FilterListTag).GetProperty("FilterListId"), + typeof(FilterListTag).GetProperty("TagId"))); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/MaintainersController.cs b/src/FilterLists.Api/V1/Controllers/MaintainersController.cs index 10848ff54..4034d4dfe 100644 --- a/src/FilterLists.Api/V1/Controllers/MaintainersController.cs +++ b/src/FilterLists.Api/V1/Controllers/MaintainersController.cs @@ -15,10 +15,6 @@ public MaintainersController(IMemoryCache memoryCache, SeedService seedService) [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("MaintainersController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(); - }))); + await Get(() => SeedService.GetAllAsync()); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/MergesController.cs b/src/FilterLists.Api/V1/Controllers/MergesController.cs index 81dd1c437..a3931c94d 100644 --- a/src/FilterLists.Api/V1/Controllers/MergesController.cs +++ b/src/FilterLists.Api/V1/Controllers/MergesController.cs @@ -15,11 +15,8 @@ public MergesController(IMemoryCache memoryCache, SeedService seedService) : bas [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("MergesController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(typeof(Merge).GetProperty("MergeFilterListId"), - typeof(Merge).GetProperty("UpstreamFilterListId")); - }))); + await Get(() => SeedService.GetAllAsync( + typeof(Merge).GetProperty("MergeFilterListId"), + typeof(Merge).GetProperty("UpstreamFilterListId"))); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/RulesController.cs b/src/FilterLists.Api/V1/Controllers/RulesController.cs index 3cdcb2a68..e3690f236 100644 --- a/src/FilterLists.Api/V1/Controllers/RulesController.cs +++ b/src/FilterLists.Api/V1/Controllers/RulesController.cs @@ -14,10 +14,6 @@ public RulesController(IMemoryCache memoryCache, RuleService ruleService) : base [HttpGet] public async Task Index() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("RulesController_Index", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return ruleService.GetCountAll(); - }))); + await Get(() => ruleService.GetCountAll()); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/SoftwareController.cs b/src/FilterLists.Api/V1/Controllers/SoftwareController.cs index fc89d79de..22803bb91 100644 --- a/src/FilterLists.Api/V1/Controllers/SoftwareController.cs +++ b/src/FilterLists.Api/V1/Controllers/SoftwareController.cs @@ -17,18 +17,10 @@ public SoftwareController(IMemoryCache memoryCache, SeedService seedService, Sof [HttpGet] public async Task Index() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SoftwareController_Index", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return softwareService.GetAll(); - }))); + await Get(() => softwareService.GetAll()); [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SoftwareController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(); - }))); + await Get(() => SeedService.GetAllAsync()); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/SoftwareSyntaxesController.cs b/src/FilterLists.Api/V1/Controllers/SoftwareSyntaxesController.cs index 18bd2aed2..3131afacf 100644 --- a/src/FilterLists.Api/V1/Controllers/SoftwareSyntaxesController.cs +++ b/src/FilterLists.Api/V1/Controllers/SoftwareSyntaxesController.cs @@ -16,11 +16,8 @@ public SoftwareSyntaxesController(IMemoryCache memoryCache, SeedService seedServ [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SoftwareSyntaxesController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync( - typeof(SoftwareSyntax).GetProperty("SyntaxId"), typeof(SoftwareSyntax).GetProperty("SoftwareId")); - }))); + await Get(() => SeedService.GetAllAsync( + typeof(SoftwareSyntax).GetProperty("SyntaxId"), + typeof(SoftwareSyntax).GetProperty("SoftwareId"))); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/SyntaxesController.cs b/src/FilterLists.Api/V1/Controllers/SyntaxesController.cs index c1d3109cc..e94e9624e 100644 --- a/src/FilterLists.Api/V1/Controllers/SyntaxesController.cs +++ b/src/FilterLists.Api/V1/Controllers/SyntaxesController.cs @@ -15,10 +15,6 @@ public SyntaxesController(IMemoryCache memoryCache, SeedService seedService) : b [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SyntaxesController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(); - }))); + await Get(() => SeedService.GetAllAsync()); } } \ No newline at end of file diff --git a/src/FilterLists.Api/V1/Controllers/TagsController.cs b/src/FilterLists.Api/V1/Controllers/TagsController.cs index 7e39b2ddb..3538f29e6 100644 --- a/src/FilterLists.Api/V1/Controllers/TagsController.cs +++ b/src/FilterLists.Api/V1/Controllers/TagsController.cs @@ -17,18 +17,10 @@ public TagsController(IMemoryCache memoryCache, SeedService seedService, TagServ [HttpGet] public async Task Index() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("TagsController_Index", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return tagService.GetAll(); - }))); + await Get(() => tagService.GetAll()); [HttpGet("seed")] public async Task Seed() => - CoalesceNotFound(Json(await MemoryCache.GetOrCreate("TagsController_Seed", entry => - { - entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault; - return SeedService.GetAllAsync(); - }))); + await Get(() => SeedService.GetAllAsync()); } } \ No newline at end of file diff --git a/src/FilterLists.Services/FilterList/FilterListService.cs b/src/FilterLists.Services/FilterList/FilterListService.cs index e3f450860..2c1b8d6c1 100644 --- a/src/FilterLists.Services/FilterList/FilterListService.cs +++ b/src/FilterLists.Services/FilterList/FilterListService.cs @@ -22,7 +22,7 @@ await DbContext.FilterLists .ProjectTo(MapConfig) .ToListAsync(); - public async Task GetDetailsAsync(uint id) => + public async Task GetDetailsAsync(int id) => await DbContext.FilterLists .ProjectTo(MapConfig) .FirstOrDefaultAsync(x => x.Id == id)