2022-10-30 12:17:10 -04:00
|
|
|
|
namespace GrossesMitainesAPI.Services;
|
|
|
|
|
|
|
|
|
|
#region Dependencies
|
|
|
|
|
using GrossesMitainesAPI.Data;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
using GrossesMitainesAPI.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
2022-10-21 18:07:11 -04:00
|
|
|
|
|
2022-10-30 16:58:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Service pour copier la BD localement au
|
|
|
|
|
/// démarrage de l'API.
|
|
|
|
|
/// </summary>
|
2022-10-30 12:17:10 -04:00
|
|
|
|
public class DatabaseCacheService {
|
|
|
|
|
#region DI
|
|
|
|
|
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 17:52:25 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Fields
|
2022-11-01 13:33:08 -04:00
|
|
|
|
private ProductModel[] _cache = new ProductModel[1];
|
2022-10-30 12:17:10 -04:00
|
|
|
|
private Dictionary<uint, uint> _hits = new();
|
|
|
|
|
private bool _ok = false, _needUpd = true;
|
|
|
|
|
private PeriodicTimer _timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
|
|
|
|
public DatabaseCacheService(ILogger<DatabaseCacheService> logger, IServiceScopeFactory scopeFactory) {
|
|
|
|
|
_contextFactory = scopeFactory;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_ok = UpdateCache();
|
|
|
|
|
_needUpd = !_ok;
|
|
|
|
|
UpdateJob();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-10-21 17:52:25 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#region Internal Methods
|
|
|
|
|
private async void UpdateJob() {
|
|
|
|
|
while (await _timer.WaitForNextTickAsync()) {
|
|
|
|
|
if (_needUpd) {
|
|
|
|
|
_ok = UpdateCache();
|
|
|
|
|
_needUpd = !_ok;
|
2022-10-27 12:37:13 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
if (_hits.Count > 0 && _ok)
|
|
|
|
|
UpdateMetrics(); // les updates de metrics ne déclencheront pas d'update de cache
|
|
|
|
|
// puisque les clients ne voient pas les métriques.
|
2022-10-21 17:52:25 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
}
|
|
|
|
|
private bool UpdateCache() {
|
|
|
|
|
try {
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ProductModel[] prods;
|
2022-10-30 12:17:10 -04:00
|
|
|
|
using (var scope = _contextFactory.CreateScope()) {
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<InventoryContext>();
|
|
|
|
|
prods = db.Products.ToArray();
|
2022-10-21 17:52:25 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
lock (_cache) {
|
|
|
|
|
_cache = prods;
|
2022-10-27 12:37:13 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de mise à jour de cache.");
|
|
|
|
|
return false;
|
2022-10-27 12:37:13 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
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>();
|
2022-11-01 13:33:08 -04:00
|
|
|
|
List<ProductModel> lst = db.Products.Where(x => ids.Contains((uint)x.Id)).ToList();
|
2022-10-30 12:17:10 -04:00
|
|
|
|
foreach (var x in hits)
|
|
|
|
|
lst.First(x => x.Id == x.Id).Hits += x.Value;
|
|
|
|
|
db.UpdateRange(lst);
|
|
|
|
|
db.SaveChanges();
|
2022-10-27 12:37:13 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de mise à jour de cache.");
|
|
|
|
|
return false;
|
2022-10-27 12:37:13 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-10-21 17:52:25 -04:00
|
|
|
|
|
2022-10-30 12:17:10 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
2022-11-03 12:45:59 -04:00
|
|
|
|
#region Service Methods
|
2022-10-30 12:17:10 -04:00
|
|
|
|
public bool isOk() { return _ok; }
|
2022-10-31 10:12:25 -04:00
|
|
|
|
public void askForRefresh() => _needUpd = true;
|
2022-10-30 12:17:10 -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
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
}
|
2022-10-25 10:54:11 -04:00
|
|
|
|
|
2022-11-01 13:33:08 -04:00
|
|
|
|
public ProductModel[]? GetCacheCopy() {
|
2022-10-30 12:17:10 -04:00
|
|
|
|
if (!_ok)
|
|
|
|
|
return null;
|
2022-11-01 13:33:08 -04:00
|
|
|
|
ProductModel[] copy;
|
2022-10-30 12:17:10 -04:00
|
|
|
|
try {
|
|
|
|
|
lock (_cache) {
|
2022-11-01 13:33:08 -04:00
|
|
|
|
copy = new ProductModel[_cache.Length];
|
2022-10-30 12:17:10 -04:00
|
|
|
|
_cache.CopyTo(copy, 0);
|
2022-10-25 10:54:11 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de copie de cache.");
|
|
|
|
|
return null;
|
2022-10-25 10:54:11 -04:00
|
|
|
|
}
|
2022-10-30 12:17:10 -04:00
|
|
|
|
return copy;
|
2022-10-21 17:52:25 -04:00
|
|
|
|
}
|
2022-11-02 12:51:32 -04:00
|
|
|
|
public IQueryable<ProductModel>? queryCache() {
|
2022-10-30 12:17:10 -04:00
|
|
|
|
if (!_ok)
|
|
|
|
|
return null;
|
|
|
|
|
try {
|
|
|
|
|
return _cache.AsQueryable();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(e, "Erreur de cache.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|