From b42c8920b19400fee117fb488e94e11ca3ecc430 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Mon, 15 Sep 2025 10:45:07 +0200 Subject: [PATCH] Ancora update memLayer x evitare problemi data-flood --- SteamWare.IO/memLayer.cs | 56 ++++++++++------- SteamWare/memLayer.cs | 127 +++++++++++++++++++++++++++------------ TestBench/App.config | 6 +- 3 files changed, 126 insertions(+), 63 deletions(-) diff --git a/SteamWare.IO/memLayer.cs b/SteamWare.IO/memLayer.cs index 05a609b..dc09ea3 100644 --- a/SteamWare.IO/memLayer.cs +++ b/SteamWare.IO/memLayer.cs @@ -31,6 +31,11 @@ namespace SteamWare.IO /// public Dictionary AppConf; + /// + /// Definisce il TTL x appConf (secondi) + /// + public int maxAgeAppConf = 5; + /// /// Table adapter accesso conf parameters /// @@ -60,6 +65,7 @@ namespace SteamWare.IO initTA(); setupConnectionStringBase(); setupMongo(); + setupParams(); } #endregion Public Constructors @@ -196,27 +202,6 @@ namespace SteamWare.IO } } - /// - /// Recupera il TTL x appConf (secondi) - /// - public int maxAgeAppConf - { - get - { - // default 5 minuti x refresh... - int maxAge = 5; - try - { - maxAge = confReadInt("maxAgeAppConf_min"); - } - catch (Exception exc) - { - Log.Error(string.Format("Errore in lettura valore maxAgeAppConf_min{0}{1}", Environment.NewLine, exc)); - } - return maxAge * 60; - } - } - /// /// Numero record salvati in AppConf /// @@ -530,12 +515,14 @@ namespace SteamWare.IO public string configDbVal(string chiave) { string answ = ""; + bool cacheReloaded = false; if (confReadString("DbConfConnectionString") != "") { // verifico esista oggetto... if (AppConf == null) { resetAppConf(); + cacheReloaded = true; } if (AppConf != null) { @@ -543,9 +530,10 @@ namespace SteamWare.IO if (AppConf.Count == 0) { resetAppConf(); + cacheReloaded = true; } // controllo SE in redis ci sia ancora il valore, altrimenti rileggo... - if (!redKeyPresent(ACBH)) + if (!cacheReloaded && DateTime.Now > vetoCacheCheck && !redKeyPresent(ACBH)) { Log.Info($"Manca HASH in redis --> rileggo da DB --> REDIS --> Memoria | {ACBH} | {chiave}"); resetAppConf(); @@ -2400,6 +2388,8 @@ namespace SteamWare.IO } // salvo in redis valori (con TTL) redSaveHash(ACBH, valori, maxAgeAppConf); + // salvo info cache riletta 2 sec prima della scadenza + vetoCacheCheck = DateTime.Now.AddSeconds(maxAgeAppConf - 2); Log.Info("Completato procedura startupAppConf"); } else @@ -2449,6 +2439,8 @@ namespace SteamWare.IO return ConnectionMultiplexer.Connect(RedisConnAdmin); }); + private DateTime vetoCacheCheck = DateTime.Now; + #endregion Private Fields #region Private Properties @@ -2492,6 +2484,26 @@ namespace SteamWare.IO currMongoClient = new MongoClient(mongoConnString); } + private void setupParams() + { + // default 5 minuti x refresh... + int maxAge = 5; + try + { + maxAge = confReadInt("maxAgeAppConf_min"); + } + catch (Exception exc) + { + Log.Error($"Errore in lettura valore maxAgeAppConf_min{Environment.NewLine}{exc}"); + } + // controllo maxage valida... + if (maxAge < 1) + { + maxAge = 5; + } + maxAgeAppConf = maxAge * 60; + } + #endregion Private Methods } } \ No newline at end of file diff --git a/SteamWare/memLayer.cs b/SteamWare/memLayer.cs index 7d78feb..5d7d6b0 100644 --- a/SteamWare/memLayer.cs +++ b/SteamWare/memLayer.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Configuration; using System.Data; using System.Text; +using System.Threading; using System.Web; namespace SteamWare @@ -28,6 +29,11 @@ namespace SteamWare /// public Dictionary AppConf; + /// + /// Definisce il TTL x appConf (secondi) + /// + public int maxAgeAppConf = 5; + /// /// Table adapter accesso conf parameters /// @@ -53,6 +59,7 @@ namespace SteamWare initTA(); setupConnectionStringBase(); setupMongo(); + setupParams(); } #endregion Public Constructors @@ -189,27 +196,6 @@ namespace SteamWare } } - /// - /// Recupera il TTL x appConf (secondi) - /// - public int maxAgeAppConf - { - get - { - // default 5 minuti x refresh... - int maxAge = 5; - try - { - maxAge = confReadInt("maxAgeAppConf_min"); - } - catch (Exception exc) - { - Log.Error(string.Format("Errore in lettura valore maxAgeAppConf_min{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION); - } - return maxAge * 60; - } - } - /// /// Numero record salvati in AppConf /// @@ -523,12 +509,14 @@ namespace SteamWare public string configDbVal(string chiave) { string answ = ""; + bool cacheReloaded = false; if (confReadString("DbConfConnectionString") != "") { // verifico esista oggetto... if (AppConf == null) { resetAppConf(); + cacheReloaded = true; } if (AppConf != null) { @@ -536,9 +524,10 @@ namespace SteamWare if (AppConf.Count == 0) { resetAppConf(); + cacheReloaded = true; } // controllo SE in redis ci sia ancora il valore, altrimenti rileggo... - if (!redKeyPresent(ACBH)) + if (!cacheReloaded && DateTime.Now > vetoCacheCheck && !redKeyPresent(ACBH)) { Log.Info($"Manca HASH in redis --> rileggo da DB --> REDIS --> Memoria | {ACBH} | {chiave}"); resetAppConf(); @@ -1082,14 +1071,25 @@ namespace SteamWare public bool isInSessionObject(string nomeVar) { bool answ = false; + bool stringAnsw = false; // cerco se ci sia... try { - answ = (HttpContext.Current.Session[nomeVar] != null); + //stringAnsw = (string)HttpContext.Current.Session[nomeVar].ToString() != ""; + stringAnsw = HttpContext.Current.Session[nomeVar] != null; } catch (Exception exc) { - Log.Error($"Errore in isInSessionObject:{Environment.NewLine}{exc}"); + Log.Error($"Errore 01 | isInSessionObject:{Environment.NewLine}{exc}"); + } + // infine condizione doppia... + try + { + answ = (HttpContext.Current.Session[nomeVar] != null && stringAnsw); + } + catch (Exception exc) + { + Log.Error($"Errore 02 | isInSessionObject:{Environment.NewLine}{exc}"); } return answ; } @@ -2297,21 +2297,39 @@ namespace SteamWare Dictionary answ = new Dictionary(); // istanzio un NUOVO oggetto x evitare problemi init contestuali memLayer nML = new memLayer(); - var tabDati = nML.taConfig.GetData(); - // carico - foreach (DS_Utility.ConfigRow riga in tabDati) + DS_Utility.ConfigDataTable tabDati = null; + int waitMs = 100; + int numTry = 5; + do { try { - answ.Add(riga.chiave, riga.valore); + tabDati = nML.taConfig.GetData(); } catch (Exception exc) { - Log.Error($"Errore procedura ricaricaAppConf per kvp: {riga.chiave} / {riga.valore}{Environment.NewLine}{exc}"); + numTry--; + Log.Error($"Errore procedura nML.taConfig.GetData(), numTry = {numTry}, now {waitMs}ms wait{Environment.NewLine}{exc}"); + Thread.Sleep(waitMs); } + } while (numTry > 0 && tabDati == null); + if (tabDati != null) + { + // carico + foreach (DS_Utility.ConfigRow riga in tabDati) + { + try + { + answ.Add(riga.chiave, riga.valore); + } + catch (Exception exc) + { + Log.Error($"Errore procedura ricaricaAppConf per kvp: {riga.chiave} / {riga.valore}{Environment.NewLine}{exc}"); + } + } + // log ricarica + Log.Info($"Effettuata procedura ricaricaAppConf per {answ.Count} records"); } - // log ricarica - Log.Info($"Effettuata procedura ricaricaAppConf per {answ.Count} records"); return answ; } @@ -2353,16 +2371,25 @@ namespace SteamWare else { AppConf = ricaricaAppConf(); - KeyValuePair[] valori = new KeyValuePair[AppConf.Count]; - int i = 0; - foreach (var item in AppConf) + if (AppConf.Count > 0) { - valori[i] = new KeyValuePair(item.Key, item.Value); - i++; + KeyValuePair[] valori = new KeyValuePair[AppConf.Count]; + int i = 0; + foreach (var item in AppConf) + { + valori[i] = new KeyValuePair(item.Key, item.Value); + i++; + } + // salvo in redis valori (con TTL) + redSaveHash(ACBH, valori, maxAgeAppConf); + // salvo info cache riletta 2 sec prima della scadenza + vetoCacheCheck = DateTime.Now.AddSeconds(maxAgeAppConf - 2); + Log.Info("Completato procedura startupAppConf"); + } + else + { + Log.Error("Errore in procedura startupAppConf, ritornato insieme vuoto"); } - // salvo in redis valori (con TTL) - redSaveHash(ACBH, valori, maxAgeAppConf); - Log.Info("Completato procedura startupAppConf"); } } catch (Exception exc) @@ -2406,6 +2433,8 @@ namespace SteamWare return ConnectionMultiplexer.Connect(RedisConnAdmin); }); + private DateTime vetoCacheCheck = DateTime.Now; + #endregion Private Fields #region Private Properties @@ -2449,6 +2478,26 @@ namespace SteamWare currMongoClient = new MongoClient(mongoConnString); } + private void setupParams() + { + // default 5 minuti x refresh... + int maxAge = 5; + try + { + maxAge = confReadInt("maxAgeAppConf_min"); + } + catch (Exception exc) + { + Log.Error($"Errore in lettura valore maxAgeAppConf_min{Environment.NewLine}{exc}"); + } + // controllo maxage valida... + if (maxAge < 1) + { + maxAge = 5; + } + maxAgeAppConf = maxAge * 60; + } + #endregion Private Methods } } \ No newline at end of file diff --git a/TestBench/App.config b/TestBench/App.config index 124f2a1..93de4ec 100644 --- a/TestBench/App.config +++ b/TestBench/App.config @@ -5,8 +5,10 @@ - - + + +