From 3935bfcd8f140a1a37ed405be5485022fb1885ec Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Thu, 24 Jul 2025 18:59:00 +0200 Subject: [PATCH] Update vari x gestione redis ottimizzata --- IOB-MAN.Core/Data/RedisIobCache.cs | 127 +++++++----------- IOB-MAN.Core/IobAdapt.cs | 23 ++-- IOB-MAN.Core/Services/AppControlService.cs | 4 +- IOB-MAN.Core/Services/FluxLogManService.cs | 5 +- .../Components/Compo/ApplicationCheck.razor | 11 +- .../Compo/ApplicationCheck.razor.cs | 4 +- .../Components/Compo/DataParetoTable.razor.cs | 22 +-- IOB-MAN/Components/Pages/FlogDetail.razor.cs | 25 ++-- IOB-MAN/IOB-MAN.csproj | 2 +- 9 files changed, 102 insertions(+), 121 deletions(-) diff --git a/IOB-MAN.Core/Data/RedisIobCache.cs b/IOB-MAN.Core/Data/RedisIobCache.cs index 4f315a7..bfce41b 100644 --- a/IOB-MAN.Core/Data/RedisIobCache.cs +++ b/IOB-MAN.Core/Data/RedisIobCache.cs @@ -1,18 +1,10 @@ -using EgwCoreLib.Razor; -using IOB_MAN.Core; -using IOB_MAN.Core.DTO; -using Microsoft.Extensions.Configuration; +using IOB_MAN.Core.DTO; using Newtonsoft.Json; using NLog; using StackExchange.Redis; -using System; -using System.Collections.Generic; -using System.Diagnostics; using System.Globalization; -using System.Linq; -using System.Runtime; using System.Text; -using System.Threading.Tasks; +using System.Threading.Channels; namespace IOB_MAN.Core.Data { @@ -44,14 +36,15 @@ namespace IOB_MAN.Core.Data { // init REDIS obj RedisConnMPlex = redisMultiplex; - RedisDb = this.RedisConnMPlex.GetDatabase(); + RedisDb = RedisConnMPlex.GetDatabase(); + RedisSubs = RedisConnMPlex.GetSubscriber(); // json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/ JSSettings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; // init classe sottoscrizione PubSub CHannel messages REDIS - messageDisp.Subscribe(ChannelName(codIob), (channel, message) => + RedisSubs.Subscribe(ChannelName(codIob), (channel, message) => { SaveIobStatus(channel, message); }); @@ -486,8 +479,10 @@ namespace IOB_MAN.Core.Data { for (int i = 0; i < numAnsw; i++) { - Int32.TryParse(valori[i], out currVal); - answ.Add(new KeyValuePair(chiavi[i], currVal)); + if (int.TryParse(valori[i], out currVal)) + { + answ.Add(new KeyValuePair($"{chiavi[i]}", currVal)); + } } } catch (Exception exc) @@ -536,8 +531,11 @@ namespace IOB_MAN.Core.Data int i = 0; foreach (HashEntry item in valori) { - answ[i] = new KeyValuePair(item.Name, item.Value); - i++; + if (item.Name.HasValue) + { + answ[i] = new KeyValuePair($"{item.Name}", $"{item.Value}"); + i++; + } } } catch (Exception exc) @@ -562,7 +560,10 @@ namespace IOB_MAN.Core.Data HashEntry[] valori = RedisDb.HashGetAll(chiave); foreach (HashEntry item in valori) { - answ.Add(item.Name, item.Value); + if (item.Name.HasValue) + { + answ.Add($"{item.Name}", $"{item.Value}"); + } } } catch (Exception exc) @@ -693,7 +694,7 @@ namespace IOB_MAN.Core.Data int i = 0; foreach (KeyValuePair kvp in hashFields) { - valori[i] = new HashEntry(kvp.Key, kvp.Value); + valori[i] = new HashEntry($"{kvp.Key}", $"{kvp.Value}"); i++; } RedisDb.HashSet(chiave, valori); @@ -753,7 +754,7 @@ namespace IOB_MAN.Core.Data int i = 0; foreach (KeyValuePair kvp in hashFields) { - valori[i] = new HashEntry(kvp.Key, kvp.Value); + valori[i] = new HashEntry($"{kvp.Key}", $"{kvp.Value}"); i++; } RedisDb.HashSet(chiave, valori); @@ -805,27 +806,22 @@ namespace IOB_MAN.Core.Data public bool redSaveHashList(string hashKey, List> hashListKVP) { bool answ = false; - if (RedisConnMPlex.IsConnected) + try { - // cerco se ci sia valore in redis... - IDatabase cache = RedisConnMPlex.GetDatabase(); - try + RedisKey chiave = hashKey; + HashEntry[] valori = new HashEntry[hashListKVP.Count]; + int i = 0; + foreach (KeyValuePair kvp in hashListKVP) { - RedisKey chiave = hashKey; - HashEntry[] valori = new HashEntry[hashListKVP.Count]; - int i = 0; - foreach (KeyValuePair kvp in hashListKVP) - { - valori[i] = new HashEntry(kvp.Key, kvp.Value); - i++; - } - cache.HashSet(chiave, valori); - answ = true; - } - catch (Exception exc) - { - Log.Error($"redSaveHashList:{Environment.NewLine}{exc}"); + valori[i] = new HashEntry(kvp.Key, kvp.Value); + i++; } + RedisDb.HashSet(chiave, valori); + answ = true; + } + catch (Exception exc) + { + Log.Error($"redSaveHashList:{Environment.NewLine}{exc}"); } return answ; } @@ -970,34 +966,6 @@ namespace IOB_MAN.Core.Data #endregion Public Methods - #region Protected Properties - - /// - /// Message Dispatcher: oggetto comunicazione pub/sub via REDIS channels corrente - /// - protected ISubscriber messageDisp - { - get - { - ISubscriber answ; - // se già valorizzato uso oggetto private... - if (_currSub != null) - { - answ = _currSub; - } - else - { - // sottoscrizione al dispatcher messaggi - answ = RedisConnMPlex.GetSubscriber(); - _currSub = answ; - } - // restituisco oggetto DB - return answ; - } - } - - #endregion Protected Properties - #region Protected Methods protected void CleanExpiredEvents(string rkeyTS, string rkeyHash, int minutesBack, int batchSize = 1000) @@ -1224,12 +1192,13 @@ namespace IOB_MAN.Core.Data private double lastFLRatio = 0; -#if DEBUG private long NumChannelCall = 0; + private long NumReadCache = 0; + private long NumReadLast = 0; + private long NumReadPubSub = 0; -#endif /// /// Random genertor @@ -1252,24 +1221,27 @@ namespace IOB_MAN.Core.Data private IDatabase RedisDb = null!; /// - /// Hash redis x dati server + /// Message Dispatcher: oggetto comunicazione pub/sub via REDIS channels corrente /// - private string RedServKey = ""; + private ISubscriber RedisSubs; + +#if DEBUG +#endif /// /// Hash redis x dati MAN /// private string RedManKey = ""; + /// + /// Hash redis x dati server + /// + private string RedServKey = ""; + #endregion Private Fields #region Private Properties - /// - /// Oggetto subscriber x pubblicazione/sottoscrizione canali REDIS - /// - private ISubscriber _currSub { get; set; } = null!; - /// /// Verifica validità dati channel (quando refresh ricevuto entro periodo validità) /// @@ -1290,9 +1262,11 @@ namespace IOB_MAN.Core.Data #region Private Methods - private string ChannelName(string codIob) + private RedisChannel ChannelName(string codIob) { - return $"IobChannel_{codIob}"; + string chName = $"IobChannel_{codIob}"; + RedisChannel rChannel = new RedisChannel($"IobChannel_{codIob}", RedisChannel.PatternMode.Literal); + return rChannel; } /// @@ -1588,6 +1562,5 @@ namespace IOB_MAN.Core.Data } #endregion Private Methods - } } \ No newline at end of file diff --git a/IOB-MAN.Core/IobAdapt.cs b/IOB-MAN.Core/IobAdapt.cs index e0b132a..f973d1d 100644 --- a/IOB-MAN.Core/IobAdapt.cs +++ b/IOB-MAN.Core/IobAdapt.cs @@ -9,18 +9,6 @@ namespace IOB_MAN.Core { public class IobAdapt : IDisposable { - public void Dispose() - { - CodIOB = ""; - pID = 0; - ExeName = ""; - TgtName = ""; - isRunning = false; - // initi redis - redisMan = null; - GC.Collect(); - } - #region Public Constructors /// @@ -228,6 +216,17 @@ namespace IOB_MAN.Core #endregion Public Properties + #region Public Methods + + public void Dispose() + { + isRunning = false; + redisMan = null; + GC.Collect(); + } + + #endregion Public Methods + #region Protected Fields protected int maxVeto = 3000; diff --git a/IOB-MAN.Core/Services/AppControlService.cs b/IOB-MAN.Core/Services/AppControlService.cs index 9fe9bf8..0a57a4b 100644 --- a/IOB-MAN.Core/Services/AppControlService.cs +++ b/IOB-MAN.Core/Services/AppControlService.cs @@ -1080,8 +1080,8 @@ namespace IOB_MAN.Core.Services if (!File.Exists(item.Value.NLogPath)) { // verifico folder parent... - string parentDir = Path.GetDirectoryName(item.Value.NLogPath); - if (!Directory.Exists(parentDir)) + string parentDir = Path.GetDirectoryName(item.Value.NLogPath) ?? ""; + if (!string.IsNullOrEmpty(parentDir) && !Directory.Exists(parentDir)) { Directory.CreateDirectory(parentDir); } diff --git a/IOB-MAN.Core/Services/FluxLogManService.cs b/IOB-MAN.Core/Services/FluxLogManService.cs index 8714c62..f0115d5 100644 --- a/IOB-MAN.Core/Services/FluxLogManService.cs +++ b/IOB-MAN.Core/Services/FluxLogManService.cs @@ -333,7 +333,10 @@ namespace IOB_MAN.Core.Services HashEntry[] valori = RedisDb.HashGetAll(chiave); foreach (HashEntry item in valori) { - answ.Add(item.Name, item.Value); + if (item.Name.HasValue && item.Value.HasValue) + { + answ.Add($"{item.Name}", $"{item.Value}"); + } } } catch (Exception exc) diff --git a/IOB-MAN/Components/Compo/ApplicationCheck.razor b/IOB-MAN/Components/Compo/ApplicationCheck.razor index 1e161e3..4520aba 100644 --- a/IOB-MAN/Components/Compo/ApplicationCheck.razor +++ b/IOB-MAN/Components/Compo/ApplicationCheck.razor @@ -72,7 +72,6 @@
@if (dxMenuVisible) { - @*