From 35767c2716e5e3fcba999654ea736cea5c0a2bbd Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 3 Oct 2023 12:17:14 +0200 Subject: [PATCH] Aggiunta metodi gestione cache conf --- MP-TAB-SERV/Pages/MachineDetail.razor.cs | 30 ++++++++ MP.Data/Controllers/MpTabController.cs | 29 ++++++-- MP.Data/Services/MessageService.cs | 4 +- MP.Data/Services/TabDataService.cs | 92 +++++++++++++----------- 4 files changed, 106 insertions(+), 49 deletions(-) diff --git a/MP-TAB-SERV/Pages/MachineDetail.razor.cs b/MP-TAB-SERV/Pages/MachineDetail.razor.cs index 38b75727..bd9e2fba 100644 --- a/MP-TAB-SERV/Pages/MachineDetail.razor.cs +++ b/MP-TAB-SERV/Pages/MachineDetail.razor.cs @@ -21,6 +21,36 @@ namespace MP_TAB_SERV.Pages protected override async Task OnInitializedAsync() { await ReloadData(); +#if false + if (enableSchedaTecnica) + { + checkLottiOdl(); + } +#endif + } + + private void checkLottiOdl() + { +#if false + // controlla se abilitato check LOTTI MAG + if (enableMagLotti) + { + if (idxOdl > 0) + { + // controlla se ci sia lotto x ODL + if (DataLayerObj.taMagELotti.getByODL(idxOdl).Rows.Count == 0) + { + try + { + // se non c'è chiama stored x rigenerare + DataLayerObj.taMagELotti.UpsertByOdl(idxOdl, true); + } + catch + { } + } + } + } +#endif } #endregion Protected Methods diff --git a/MP.Data/Controllers/MpTabController.cs b/MP.Data/Controllers/MpTabController.cs index 1f6690fd..df67fb6b 100644 --- a/MP.Data/Controllers/MpTabController.cs +++ b/MP.Data/Controllers/MpTabController.cs @@ -5,7 +5,6 @@ using NLog; using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; namespace MP.Data.Controllers { @@ -23,11 +22,6 @@ namespace MP.Data.Controllers #region Public Methods - public void Dispose() - { - _configuration = null; - } - /// /// Restituisce l'anagrafica eventi per intero /// @@ -45,6 +39,29 @@ namespace MP.Data.Controllers return dbResult; } + /// + /// Elenco da tabella Config + /// + /// + public List ConfigGetAll() + { + List dbResult = new List(); + using (var dbCtx = new MoonProContext(_configuration)) + { + dbResult = dbCtx + .DbSetConfig + .AsNoTracking() + .OrderBy(x => x.Chiave) + .ToList(); + } + return dbResult; + } + + public void Dispose() + { + _configuration = null; + } + #endregion Public Methods #region Private Fields diff --git a/MP.Data/Services/MessageService.cs b/MP.Data/Services/MessageService.cs index 13fbc465..7dc7c77c 100644 --- a/MP.Data/Services/MessageService.cs +++ b/MP.Data/Services/MessageService.cs @@ -181,7 +181,7 @@ namespace MP.Data.Services /// public async Task IdxMaccGet() { - return await localStorage.GetItemAsync("CurrMach"); + return await sessionStore.GetItemAsync("CurrMach"); } /// @@ -189,7 +189,7 @@ namespace MP.Data.Services /// public async Task IdxMaccSet(string machSel) { - await localStorage.SetItemAsync("CurrMach", machSel); + await sessionStore.SetItemAsync("CurrMach", machSel); } #if false diff --git a/MP.Data/Services/TabDataService.cs b/MP.Data/Services/TabDataService.cs index f4f4b950..94b5a472 100644 --- a/MP.Data/Services/TabDataService.cs +++ b/MP.Data/Services/TabDataService.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.Configuration; using MP.Data.DatabaseModels; using Newtonsoft.Json; -using NLog.Fluent; +using NLog; using StackExchange.Redis; using System; using System.Collections.Generic; @@ -11,7 +11,7 @@ using System.Threading.Tasks; namespace MP.Data.Services { - public class TabDataService + public class TabDataService : BaseServ, IDisposable { #region Public Constructors @@ -33,23 +33,24 @@ namespace MP.Data.Services { dbController = new Controllers.MpTabController(configuration); StringBuilder sb = new StringBuilder(); - sb.AppendLine($"TabDataService | MpSpecController OK"); + sb.AppendLine($"TabDataService | MpTabController OK"); Log.Info(sb.ToString()); } } #endregion Public Constructors - protected static IConfiguration _configuration = null!; + #region Public Properties public static Controllers.MpTabController dbController { get; set; } = null!; + #endregion Public Properties + + #region Public Methods + public async Task> AnagEventiGetAll() { // setup parametri costanti - bool inCorso = false; - string keyRichPart = "*"; - string Reparto = "*"; DateTime startDate = new DateTime(2000, 1, 1); DateTime endDate = DateTime.Today.AddDays(1); string source = "DB"; @@ -76,44 +77,57 @@ namespace MP.Data.Services result = new List(); } sw.Stop(); + Log.Debug($"AnagEventiGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms"); return result; } - #region Protected Properties /// - /// Durata cache breve (1 min circa + perturbazione percentuale +/-10%) + /// Config values attivi /// - protected TimeSpan FastCache + /// + public async Task> ConfigGetAll() { - get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000); + string source = "DB"; + Stopwatch sw = new Stopwatch(); + sw.Start(); + List? result = new List(); + // cerco in redis... + string currKey = $"{redisBaseKey}:Conf"; + RedisValue rawData = await redisDb.StringGetAsync(currKey); + if (!string.IsNullOrEmpty($"{rawData}")) + { + result = JsonConvert.DeserializeObject>($"{rawData}"); + source = "REDIS"; + } + else + { + result = await Task.FromResult(dbController.ConfigGetAll()); + // serializzp e salvo... + rawData = JsonConvert.SerializeObject(result); + await redisDb.StringSetAsync(currKey, rawData, LongCache); + } + if (result == null) + { + result = new List(); + } + sw.Stop(); + Log.Debug($"ConfigGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms"); + return result; + + //return Task.FromResult(dbController.ConfigGetAll().ToList()); } - /// - /// Durata cache lunga (+ perturbazione percentuale +/-10%) - /// - protected TimeSpan LongCache + public void Dispose() { - get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000); + // Clear database controller + dbController.Dispose(); } - /// - /// Durata cache MOLTO breve (10 sec circa + perturbazione percentuale +/-10%) - /// - protected TimeSpan UltraFastCache - { - get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000); - } + #endregion Public Methods - /// - /// Durata cache MOLTO lunga (+ perturbazione percentuale +/-10%) - /// - protected TimeSpan UltraLongCache - { - get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000); - } + #region Protected Fields - #endregion Protected Properties - #region Private Fields + protected static IConfiguration _configuration = null!; /// /// Oggetto per connessione a REDIS @@ -125,17 +139,13 @@ namespace MP.Data.Services /// protected IDatabase redisDb = null!; - /// - /// Durata cache lunga IN SECONDI - /// - private int cacheTtlLong = 60 * 5; - /// - /// Durata cache breve IN SECONDI - /// - private int cacheTtlShort = 60 * 1; + #endregion Protected Fields + #region Private Fields + + private static Logger Log = LogManager.GetCurrentClassLogger(); private string redisBaseKey = "MP:TAB:Cache"; - private Random rnd = new Random(); + #endregion Private Fields } } \ No newline at end of file