2022-10-21 17:52:25 -04:00
|
|
|
|
using GrossesMitainesAPI.Data;
|
|
|
|
|
using GrossesMitainesAPI.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace GrossesMitainesAPI.Services {
|
|
|
|
|
public class DatabaseCacheService {
|
|
|
|
|
private readonly IServiceScopeFactory _contextFactory; // https://entityframeworkcore.com/knowledge-base/51939451/how-to-use-a-database-context-in-a-singleton-service-
|
|
|
|
|
private readonly ILogger<DatabaseCacheService> _logger;
|
2022-10-21 18:07:11 -04:00
|
|
|
|
|
2022-10-21 17:52:25 -04:00
|
|
|
|
private Product[] _cache = new Product[1];
|
2022-10-27 12:37:13 -04:00
|
|
|
|
private Dictionary<uint, uint> _hits = new();
|
2022-10-21 17:52:25 -04:00
|
|
|
|
private bool _ok = false, _needUpd = true;
|
|
|
|
|
private PeriodicTimer _timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
|
|
|
|
|
|
|
|
|
|
public DatabaseCacheService(ILogger<DatabaseCacheService> logger, IServiceScopeFactory scopeFactory) {
|
|
|
|
|
_contextFactory = scopeFactory;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_ok = UpdateCache();
|
|
|
|
|
_needUpd = !_ok;
|
|
|
|
|
UpdateJob();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void UpdateJob() {
|
2022-10-27 12:37:13 -04:00
|
|
|
|
while (await _timer.WaitForNextTickAsync()) {
|
|
|
|
|
if (_needUpd) {
|
2022-10-21 17:52:25 -04:00
|
|
|
|
_ok = UpdateCache();
|
|
|
|
|
_needUpd = !_ok;
|
2022-10-27 12:37:13 -04:00
|
|
|
|
}
|
|
|
|
|
if (_hits.Count > 0 && _ok)
|
|
|
|
|
UpdateMetrics();
|
|
|
|
|
}
|
2022-10-21 17:52:25 -04:00
|
|
|
|
}
|
|
|
|
|
private bool UpdateCache() {
|
|
|
|
|
try {
|
|
|
|
|
Product[] prods;
|
|
|
|
|
using (var scope = _contextFactory.CreateScope()) {
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
|
|
|
|
|
prods = db.Products.ToArray();
|
|
|
|
|
}
|
|
|
|
|
lock (_cache) {
|
|
|
|
|
_cache = prods;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de mise à jour de cache.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-10-27 12:37:13 -04:00
|
|
|
|
|
|
|
|
|
private bool UpdateMetrics() {
|
|
|
|
|
try {
|
|
|
|
|
Dictionary<uint, uint> hits;
|
|
|
|
|
lock (_hits) {
|
|
|
|
|
hits = new(_hits);
|
|
|
|
|
_hits.Clear();
|
|
|
|
|
}
|
|
|
|
|
List<uint> ids = hits.Keys.ToList();
|
|
|
|
|
using (var scope = _contextFactory.CreateScope()) {
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
|
|
|
|
|
List<Product> lst = db.Products.Where(x => ids.Contains((uint)x.Id)).ToList();
|
|
|
|
|
foreach (var x in hits) {
|
|
|
|
|
//Product prod = lst.First(x => x.Id == x.Id);
|
|
|
|
|
lst.First(x => x.Id == x.Id).Hits += x.Value;
|
|
|
|
|
// prod.Hits = prod.Hits + x.Value;
|
|
|
|
|
// db.Products.Update(prod);
|
|
|
|
|
}
|
|
|
|
|
db.UpdateRange(lst);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de mise à jour de cache.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 17:52:25 -04:00
|
|
|
|
public bool isOk() { return _ok; }
|
|
|
|
|
public void askForRefresh() { _needUpd = true; }
|
2022-10-27 12:37:13 -04:00
|
|
|
|
public void addHit(uint id) {
|
|
|
|
|
lock (_hits) {
|
|
|
|
|
if (_hits.ContainsKey(id))
|
|
|
|
|
_hits[id] = _hits[id] + 1;
|
|
|
|
|
else _hits[id] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 17:52:25 -04:00
|
|
|
|
public Product[]? GetCacheCopy() {
|
2022-10-27 12:37:13 -04:00
|
|
|
|
if (!_ok)
|
|
|
|
|
return null;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
|
|
|
|
|
Product[] copy;
|
|
|
|
|
try {
|
|
|
|
|
lock (_cache) {
|
|
|
|
|
copy = new Product[_cache.Length];
|
|
|
|
|
_cache.CopyTo(copy, 0);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de copie de cache.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
2022-10-25 10:54:11 -04:00
|
|
|
|
|
2022-10-27 12:37:13 -04:00
|
|
|
|
public IQueryable<Product> queryCache() {
|
|
|
|
|
if (!_ok)
|
|
|
|
|
return null;
|
2022-10-25 10:54:11 -04:00
|
|
|
|
try {
|
2022-10-25 12:11:04 -04:00
|
|
|
|
return _cache.AsQueryable();
|
2022-10-25 10:54:11 -04:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de cache.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-21 17:52:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|