diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index b479d06..a9f7694 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -14,6 +14,7 @@ public class InventoryController : Controller { private readonly ILogger _logger; private readonly InventoryContext _context; private readonly DatabaseCacheService _cache; + private static object _lock = new object(); public InventoryController(ILogger logger, InventoryContext context, DatabaseCacheService cache) { _context = context; @@ -25,11 +26,13 @@ public class InventoryController : Controller { [HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { const int AMOUNT = 5; - + bool islock = false; IQueryable ret; - if (_cache.isOk()) - ret = _cache.GetCacheCopy().AsQueryable(); + if (_cache.isOk()) { + ret = _cache.queryCache(); + islock = true; + } else ret = _context.Products.AsQueryable(); switch (filterPrice) { @@ -93,20 +96,42 @@ public class InventoryController : Controller { bool yup = false; int add = 0; - if (lastId.HasValue) - foreach (Product prod in new List(ret.ToList())) { - if (yup && add < AMOUNT) { - lst.Add(prod); - add++; - } - if (prod.Id == lastId) - yup = true; - } - else if (all.HasValue && all == true) - lst = ret.ToList(); - else lst = ret.Take(AMOUNT).ToList(); + try { - return lst; + if (lastId.HasValue) { // Pour avoir les prochains peu importe l'ordre. + List prods; + if (islock) + lock (_lock) { + prods = ret.ToList(); + } + else prods = ret.ToList(); + foreach (Product prod in prods) { + if (yup && add < AMOUNT) { + lst.Add(prod); + add++; + } + if (prod.Id == lastId) + yup = true; + } + } else if (all.HasValue && all == true) { + if (islock) + lock (_lock) { + lst = ret.ToList(); + } + else lst = ret.ToList(); + } else if (islock) + lock (_lock) { + lst = ret.Take(AMOUNT).ToList(); + } + else lst = ret.Take(AMOUNT).ToList(); + + return lst; + } catch (Exception e) { + if (islock) + _logger.LogError(e, "Erreur d'appel de cache."); + else _logger.LogError(e, "Erreur d'appel d'API."); + return new List(); + } } // Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD. diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 0ff9a16..986e958 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -86,6 +86,9 @@ public class SearchController : Controller { query = query.Trim(); + if (_searchCache is null) + return products; + try { // Pour faire une liste priorisée. if (preview.HasValue && preview == true) products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); diff --git a/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs b/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs index f10c4b2..1326ac1 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs @@ -60,5 +60,15 @@ namespace GrossesMitainesAPI.Services { } return copy; } + public IQueryable queryCache() { + if (!_ok) return null; + + try { + return _cache.AsQueryable(); + } catch (Exception e) { + _logger.LogError(e, "Erreur de cache."); + return null; + } + } } }