Service de cache de database fonctionnel
This commit is contained in:
		| @@ -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": | ||||
|   | ||||
| @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.AspNetCore.Cors; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using GrossesMitainesAPI.Services; | ||||
|  | ||||
| namespace GrossesMitainesAPI.Controllers; | ||||
|  | ||||
| @@ -14,10 +15,12 @@ namespace GrossesMitainesAPI.Controllers; | ||||
| public class ProductController : ControllerBase { | ||||
|     private readonly ILogger<ProductController> _logger; | ||||
|     private readonly InventoryContext _context; | ||||
|     private readonly DatabaseCacheService _cache; | ||||
|  | ||||
|     public ProductController(ILogger<ProductController> logger, InventoryContext context) { | ||||
|     public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) { | ||||
|         _logger = logger; | ||||
|         _context = context; | ||||
|         _cache = cache; | ||||
|     } | ||||
|  | ||||
|     [EnableCors("_myAllowSpecificOrigins")] | ||||
| @@ -44,6 +47,7 @@ public class ProductController : ControllerBase { | ||||
|         try { | ||||
|             _context.Products.Add(prod); | ||||
|             _context.SaveChanges(); | ||||
|             _cache.askForRefresh(); | ||||
|         } | ||||
|         catch (Exception e) { | ||||
|             _logger.LogError(8, e.Message); | ||||
| @@ -59,6 +63,7 @@ public class ProductController : ControllerBase { | ||||
|         try { | ||||
|             _context.Products.Update(prod); | ||||
|             _context.SaveChanges(); | ||||
|             _cache.askForRefresh(); | ||||
|         } | ||||
|         catch (Exception e) { | ||||
|             _logger.LogError(8, e.Message); | ||||
| @@ -74,6 +79,7 @@ public class ProductController : ControllerBase { | ||||
|         try { | ||||
|             _context.Products.Remove(_context.Products.Where(x => x.Id == id).First()); | ||||
|             _context.SaveChanges(); | ||||
|             _cache.askForRefresh(); | ||||
|         } | ||||
|         catch (Exception e) { | ||||
|             _logger.LogError(8, e.Message); | ||||
|   | ||||
| @@ -5,6 +5,7 @@ using GrossesMitainesAPI.Data; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Cors; | ||||
| using GrossesMitainesAPI.Services; | ||||
|  | ||||
| namespace GrossesMitainesAPI.Controllers; | ||||
|  | ||||
| @@ -13,22 +14,74 @@ namespace GrossesMitainesAPI.Controllers; | ||||
| public class SearchController : Controller { | ||||
|     private readonly ILogger<SearchController> _logger; | ||||
|     private readonly InventoryContext _context; | ||||
|     private static List<Product> _searchCache = new(); | ||||
|     private static DateTime? _cacheDate;  | ||||
|     private readonly DatabaseCacheService _cache; | ||||
|     private Product[]? _searchCache = null; | ||||
|     private const int PREVIEW = 4; | ||||
|  | ||||
|     public SearchController(ILogger<SearchController> logger, InventoryContext context) { | ||||
|  | ||||
|     public SearchController(ILogger<SearchController> logger, InventoryContext context, DatabaseCacheService cache) { | ||||
|         _logger = logger; | ||||
|         _context = context; | ||||
|         if (_searchCache.Count == 0 || _cacheDate.HasValue && _cacheDate.Value.AddMinutes(5) < DateTime.Now) {  | ||||
|             _searchCache = _context.Products.ToList(); | ||||
|             _cacheDate = DateTime.Now; | ||||
|         } | ||||
|         _cache = cache; | ||||
|  | ||||
|         if (_cache.isOk()) | ||||
|             _searchCache = _cache.GetCacheCopy(); | ||||
|     } | ||||
|  | ||||
|     [EnableCors("_myAllowSpecificOrigins")] | ||||
|     [HttpPost(Name = "Search")] | ||||
|     public IEnumerable<Product> Post(string query, bool? preview, bool? deep) { | ||||
|         const int PREVIEW = 4; | ||||
|         if (_searchCache is not null) | ||||
|             return SearchCached(query, preview, deep); | ||||
|         else return Search(query, preview, deep); | ||||
|  | ||||
|     } | ||||
|     private List<Product> Search(string query, bool? preview, bool? deep) { | ||||
|         List<Product> products = new(); | ||||
|  | ||||
|         query = query.Trim(); | ||||
|  | ||||
|         try { // Pour faire une liste priorisée. | ||||
|             if (preview.HasValue && preview == true) | ||||
|                 products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); | ||||
|             else { | ||||
|                 if (deep.HasValue && deep == true) { | ||||
|                     List<Product> title = new(), desc = new(), cat = new(); | ||||
|                     query = query.ToLower(); | ||||
|  | ||||
|                     foreach (Product prod in _context.Products.ToList()) { | ||||
|                         string sTitle = prod.Title.Replace(",", " ").ToLower(), | ||||
|                                sCat = prod.Category.ToLower(), | ||||
|                                sDesc = prod.Description.Replace(".", " ").Replace(",", " ").ToLower(); | ||||
|                         if (sTitle.StartsWith(query)) | ||||
|                             products.Add(prod); | ||||
|                         else if (sTitle.Contains(" " + query + " ")) | ||||
|                             title.Add(prod); | ||||
|                         else if (sDesc.StartsWith(query) || sDesc.Contains(" " + query + " ")) | ||||
|                             desc.Add(prod); | ||||
|                         else if (sCat.Contains(query)) | ||||
|                             cat.Add(prod); | ||||
|                     } | ||||
|                     products.AddRange(title); | ||||
|                     products.AddRange(desc); | ||||
|                     products.AddRange(cat); | ||||
|                 } else { | ||||
|                     products = _context.Products.Where(x => x.Title.Contains(query)).ToList(); | ||||
|                     foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList()) | ||||
|                         if (!products.Contains(prod)) | ||||
|                             products.Add(prod); | ||||
|                     foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) | ||||
|                         if (!products.Contains(prod)) | ||||
|                             products.Add(prod); | ||||
|                 } | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             _logger.LogError(8, e.Message); | ||||
|         } | ||||
|         return products; | ||||
|     } | ||||
|  | ||||
|     private List<Product> SearchCached(string query, bool? preview, bool? deep) { | ||||
|         List<Product> products = new(); | ||||
|  | ||||
|         query = query.Trim(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user