Service de cache de database fonctionnel

This commit is contained in:
MarcEricMartel
2022-10-21 14:52:25 -07:00
parent bd1b2edfea
commit 09786b0c50
5 changed files with 145 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
namespace GrossesMitainesAPI.Controllers;
@@ -12,10 +13,12 @@ namespace GrossesMitainesAPI.Controllers;
public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
public InventoryController(ILogger<InventoryController> logger, InventoryContext context) {
public InventoryController(ILogger<InventoryController> logger, InventoryContext context, DatabaseCacheService cache) {
_context = context;
_logger = logger;
_cache = cache;
}
[EnableCors("_myAllowSpecificOrigins")]
@@ -23,7 +26,11 @@ public class InventoryController : Controller {
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
const int AMOUNT = 5;
var ret = _context.Products.AsQueryable();
IQueryable<Product> ret;
if (_cache.isOk())
ret = _cache.GetCacheCopy().AsQueryable();
else ret = _context.Products.AsQueryable();
switch (filterPrice) {
case "PriceUnder20":