diff --git a/SteamWare.IO/Redis.cs b/SteamWare.IO/Redis.cs index 9b70774..19a29f3 100644 --- a/SteamWare.IO/Redis.cs +++ b/SteamWare.IO/Redis.cs @@ -323,6 +323,29 @@ namespace SteamWare.IO return answ; } + /// + /// ELIMINA un SINGOLO VALORE dalla hash (dato field = chiave) + /// + /// + /// + /// + public bool redDelHashField(string hashKey, string hashField) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + RedisValue campo = hashField; + answ = cache.HashDelete(chiave, campo); + } + catch (Exception exc) + { + Log.Error($"Errore in redDelHashField{Environment.NewLine}{exc}"); + } + return answ; + } + /// /// Elimina una key (hash, string) /// diff --git a/SteamWare.IO/memLayer.cs b/SteamWare.IO/memLayer.cs index 3a4b5df..f1b426c 100644 --- a/SteamWare.IO/memLayer.cs +++ b/SteamWare.IO/memLayer.cs @@ -19,52 +19,6 @@ namespace SteamWare.IO /// public class memLayer { - #region Private Fields - - /// - /// Connessione lazy a redis... - /// - private Lazy lazyConnection = new Lazy(() => - { - string RedisConn = memLayer.ML.confReadString("RedisConn"); - if (string.IsNullOrEmpty(RedisConn)) - { - RedisConn = "localhost,abortConnect=false,ssl=false"; - } - - return ConnectionMultiplexer.Connect(RedisConn); - }); - - /// - /// Connessione lazy a redis... - /// - private Lazy lazyConnectionAdmin = new Lazy(() => - { - string RedisConnAdmin = memLayer.ML.confReadString("RedisConnAdmin"); - if (string.IsNullOrEmpty(RedisConnAdmin)) - { - RedisConnAdmin = "localhost,abortConnect=false,ssl=false,allowAdmin=true"; - } - - return ConnectionMultiplexer.Connect(RedisConnAdmin); - }); - - #endregion Private Fields - - #region Protected Fields - - /// - /// lettore file configurazione - /// - protected AppSettingsReader configAppSetReader; - - /// - /// Oggetto MongoDbCLient x accesso al motore - /// - protected MongoClient currMongoClient; - - #endregion Protected Fields - #region Public Fields /// @@ -89,28 +43,6 @@ namespace SteamWare.IO #endregion Public Fields - #region Protected Constructors - - private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); - /// - /// classe gestione accessi a Session, cache, viewstate, configuration... - /// - protected memLayer() - { - if (Log == null) - { - Log = LogManager.GetCurrentClassLogger(); - } - // istanzia il conf setting reader... - configAppSetReader = new AppSettingsReader(); - // avvio e configuro TA - initTA(); - setupConnectionStringBase(); - setupMongo(); - } - - #endregion Protected Constructors - #region Public Enums /// @@ -141,59 +73,6 @@ namespace SteamWare.IO #endregion Public Enums - #region Private Properties - - /// - /// Oggetto currentDb locale - /// - private IDatabase _currDB { get; set; } - - #endregion Private Properties - - #region Protected Properties - - /// - /// stringa conn x DB CONF - /// - protected string connStringDbConf - { - get - { - string answ = ""; - answ = confReadString("DbConfConnectionString"); - // se fosse vuota fallback a path dei permessi... - if (string.IsNullOrEmpty(answ)) - { - answ = confReadString("PermessiConnectionString"); - } - // se ancora vuota --> devAuth - if (string.IsNullOrEmpty(answ)) - { - answ = confReadString("DevicesAuthConnectionString"); - } - return answ; - } - } - - /// - /// Stringa di connessione mongoDb - /// - protected string mongoConnString - { - get - { - string answ = ""; - answ = confReadString("mdbConnString"); - if (string.IsNullOrEmpty(answ)) - { - answ = "mongodb://W2019-MONGODB:27017"; - } - return answ; - } - } - - #endregion Protected Properties - #region Public Properties /// @@ -358,7 +237,8 @@ namespace SteamWare.IO } /// - /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per impiego di sessioni avanzate come Redis) + /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per + /// impiego di sessioni avanzate come Redis) /// public bool serializeSession { @@ -427,162 +307,6 @@ namespace SteamWare.IO #endregion Public Properties - #region Private Methods - - /// - /// Effettua comaprazione x CHIAVE in KVP ASC - /// - /// - /// - /// - private int CompareKey(KeyValuePair x, KeyValuePair y) - { - return x.Key.CompareTo(y.Key); - } - - /// - /// Effettua comaprazione x CHIAVE in KVP DESC - /// - /// - /// - /// - private int CompareKeyDesc(KeyValuePair x, KeyValuePair y) - { - return y.Key.CompareTo(x.Key); - } - - /// - /// Init accesso MongoDb - /// - private void setupMongo() - { - currMongoClient = new MongoClient(mongoConnString); - } - - #endregion Private Methods - - #region Protected Methods - - /// - /// init dei table adapters - /// - protected void initTA() - { - taConfig = new DS_UtilityTableAdapters.ConfigTableAdapter(); - taConfigTmp = new DS_UtilityTableAdapters.ConfigTmpTableAdapter(); - } - - /// - /// carica in ram oggetto AppConf - /// - /// - protected Dictionary ricaricaAppConf() - { - Dictionary answ = new Dictionary(); - // istanzio un NUOVO oggetto x evitare problemi init contestuali - memLayer nML = new memLayer(); - DS_Utility.ConfigDataTable tabDati = null; - int waitMs = 100; - int numTry = 5; - do - { - try - { - tabDati = nML.taConfig.GetData(); - } - catch (Exception 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(string.Format("Errore procedura ricaricaAppConf per kvp: {0} / {1}{2}{3}", riga.chiave, riga.valore, Environment.NewLine, exc)); - } - } - // log ricarica - Log.Info(string.Format("Effettuata procedura ricaricaAppConf per {0} records", answ.Count)); - } - return answ; - } - - /// - /// effettua setup dei connection strings da web.config delal singola applicazione - /// - protected virtual void setupConnectionStringBase() - { - // connections del db - taConfig.Connection.ConnectionString = connStringDbConf; - taConfigTmp.Connection.ConnectionString = connStringDbConf; - } - - /// - /// avvio oggetto AppConf in ram - /// - protected void startupAppConf() - { - // SOLO SE ho la chiave x abilitare config su DB... - if (confReadString("DbConfConnectionString") != "") - { - try - { - if (redKeyPresent(ACBH)) - { - AppConf = new Dictionary(); - foreach (var item in redGetHash(ACBH)) - { - if (AppConf.ContainsKey(item.Key)) - { - AppConf[item.Key] = item.Value; - } - else - { - AppConf.Add(item.Key, item.Value); - } - } - } - else - { - AppConf = ricaricaAppConf(); - if (AppConf.Count > 0) - { - 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); - Log.Info("Completato procedura startupAppConf"); - } - else - { - Log.Error("Errore in procedura startupAppConf, ritornato insieme vuoto"); - } - } - } - catch (Exception exc) - { - Log.Error(string.Format("Errore in startupAppConf:{0}{1}", Environment.NewLine, exc)); - } - } - } - - #endregion Protected Methods - #region Public Methods /// @@ -596,12 +320,13 @@ namespace SteamWare.IO } /// - /// aggiunge la stringa corrente nel dictionary delle tabelle messe in cache e da aggiornare su comando update + /// aggiunge la stringa corrente nel dictionary delle tabelle messe in cache e da aggiornare + /// su comando update /// /// public void addTabInCache(string nuovaTab) { - // provo ad aggiungere nuova tab in elenco... + // provo ad aggiungere nuova tab in elenco... Dictionary _tabelleInCache = tabelleInCache; try { @@ -613,13 +338,14 @@ namespace SteamWare.IO } /// - /// aggiunge la stringa corrente nel dictionary delle tabelle messe in session che vanno preservate da comando update (es: oggetto selezionato...) + /// aggiunge la stringa corrente nel dictionary delle tabelle messe in session che vanno + /// preservate da comando update (es: oggetto selezionato...) /// /// /// public void addValInSession(string nomePar, string valore) { - // provo ad aggiungere nuova tab in elenco... + // provo ad aggiungere nuova tab in elenco... Dictionary _valoriInSession2Survive = valSess2SurvUpd; // verifico se fare update o insert... if (_valoriInSession2Survive.ContainsKey(nomePar)) @@ -1482,8 +1208,7 @@ namespace SteamWare.IO public int redCountKey(string keyPattern) { int answ = 0; - // cerco se ci sia valore in redis... - // se vuoto = ALL... + // cerco se ci sia valore in redis... se vuoto = ALL... keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; try { @@ -1503,6 +1228,29 @@ namespace SteamWare.IO return answ; } + /// + /// ELIMINA un SINGOLO VALORE dalla hash (dato field = chiave) + /// + /// + /// + /// + public bool redDelHashField(string hashKey, string hashField) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + RedisValue campo = hashField; + answ = cache.HashDelete(chiave, campo); + } + catch (Exception exc) + { + Log.Error($"Errore in redDelHashField{Environment.NewLine}{exc}"); + } + return answ; + } + /// /// Elimina una key (hash, string) /// @@ -1531,8 +1279,7 @@ namespace SteamWare.IO public bool redFlushKey(string keyPattern) { bool answ = false; - // cerco se ci sia valore in redis... - // se vuoto = ALL... + // cerco se ci sia valore in redis... se vuoto = ALL... keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern; try { @@ -1697,7 +1444,8 @@ namespace SteamWare.IO } /// - /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto... + /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da + /// funzionalita' DbConfig) + keyName richiesto... /// public string redHash(string keyName) { @@ -1820,7 +1568,9 @@ namespace SteamWare.IO /// /// chiave /// valori - /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// + /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// /// public bool redSaveHash(string hashKey, KeyValuePair[] hashFields, double expireSeconds = -1) { @@ -1874,7 +1624,9 @@ namespace SteamWare.IO /// /// chiave /// valori - /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// + /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade) + /// /// public bool redSaveHashDict(string hashKey, Dictionary hashFields, double expireSeconds = -1) { @@ -2123,7 +1875,7 @@ namespace SteamWare.IO } /// - /// Salva un set KVP (Key Value Pair) in RedisCache + /// Salva un set KVP (Key Value Pair) in RedisCache /// /// Set KVP chiave-valore da salvare /// @@ -2217,7 +1969,9 @@ namespace SteamWare.IO /// /// nome della variabile /// DataSet da salvare - /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// + /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// /// public bool setSessionDataSet(string nome, DataSet dSet, bool surviveUpdate) { @@ -2257,7 +2011,9 @@ namespace SteamWare.IO /// /// nome della variabile /// DataTable da salvare - /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// + /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// public bool setSessionDataTable(string nome, DataTable dTable, bool surviveUpdate) { bool _done = false; @@ -2296,7 +2052,9 @@ namespace SteamWare.IO /// /// nome della variabile /// valore associato - /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// + /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd) + /// /// public bool setSessionVal(string nome, object valore, bool surviveUpdate) { @@ -2363,5 +2121,283 @@ namespace SteamWare.IO } #endregion Public Methods + + #region Protected Fields + + /// + /// lettore file configurazione + /// + protected AppSettingsReader configAppSetReader; + + /// + /// Oggetto MongoDbCLient x accesso al motore + /// + protected MongoClient currMongoClient; + + #endregion Protected Fields + + #region Protected Constructors + + /// + /// classe gestione accessi a Session, cache, viewstate, configuration... + /// + protected memLayer() + { + if (Log == null) + { + Log = LogManager.GetCurrentClassLogger(); + } + // istanzia il conf setting reader... + configAppSetReader = new AppSettingsReader(); + // avvio e configuro TA + initTA(); + setupConnectionStringBase(); + setupMongo(); + } + + #endregion Protected Constructors + + #region Protected Properties + + /// + /// stringa conn x DB CONF + /// + protected string connStringDbConf + { + get + { + string answ = ""; + answ = confReadString("DbConfConnectionString"); + // se fosse vuota fallback a path dei permessi... + if (string.IsNullOrEmpty(answ)) + { + answ = confReadString("PermessiConnectionString"); + } + // se ancora vuota --> devAuth + if (string.IsNullOrEmpty(answ)) + { + answ = confReadString("DevicesAuthConnectionString"); + } + return answ; + } + } + + /// + /// Stringa di connessione mongoDb + /// + protected string mongoConnString + { + get + { + string answ = ""; + answ = confReadString("mdbConnString"); + if (string.IsNullOrEmpty(answ)) + { + answ = "mongodb://W2019-MONGODB:27017"; + } + return answ; + } + } + + #endregion Protected Properties + + #region Protected Methods + + /// + /// init dei table adapters + /// + protected void initTA() + { + taConfig = new DS_UtilityTableAdapters.ConfigTableAdapter(); + taConfigTmp = new DS_UtilityTableAdapters.ConfigTmpTableAdapter(); + } + + /// + /// carica in ram oggetto AppConf + /// + /// + protected Dictionary ricaricaAppConf() + { + Dictionary answ = new Dictionary(); + // istanzio un NUOVO oggetto x evitare problemi init contestuali + memLayer nML = new memLayer(); + DS_Utility.ConfigDataTable tabDati = null; + int waitMs = 100; + int numTry = 5; + do + { + try + { + tabDati = nML.taConfig.GetData(); + } + catch (Exception 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(string.Format("Errore procedura ricaricaAppConf per kvp: {0} / {1}{2}{3}", riga.chiave, riga.valore, Environment.NewLine, exc)); + } + } + // log ricarica + Log.Info(string.Format("Effettuata procedura ricaricaAppConf per {0} records", answ.Count)); + } + return answ; + } + + /// + /// effettua setup dei connection strings da web.config delal singola applicazione + /// + protected virtual void setupConnectionStringBase() + { + // connections del db + taConfig.Connection.ConnectionString = connStringDbConf; + taConfigTmp.Connection.ConnectionString = connStringDbConf; + } + + /// + /// avvio oggetto AppConf in ram + /// + protected void startupAppConf() + { + // SOLO SE ho la chiave x abilitare config su DB... + if (confReadString("DbConfConnectionString") != "") + { + try + { + if (redKeyPresent(ACBH)) + { + AppConf = new Dictionary(); + foreach (var item in redGetHash(ACBH)) + { + if (AppConf.ContainsKey(item.Key)) + { + AppConf[item.Key] = item.Value; + } + else + { + AppConf.Add(item.Key, item.Value); + } + } + } + else + { + AppConf = ricaricaAppConf(); + if (AppConf.Count > 0) + { + 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); + Log.Info("Completato procedura startupAppConf"); + } + else + { + Log.Error("Errore in procedura startupAppConf, ritornato insieme vuoto"); + } + } + } + catch (Exception exc) + { + Log.Error(string.Format("Errore in startupAppConf:{0}{1}", Environment.NewLine, exc)); + } + } + } + + #endregion Protected Methods + + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + /// + /// Connessione lazy a redis... + /// + private Lazy lazyConnection = new Lazy(() => + { + string RedisConn = memLayer.ML.confReadString("RedisConn"); + if (string.IsNullOrEmpty(RedisConn)) + { + RedisConn = "localhost,abortConnect=false,ssl=false"; + } + + return ConnectionMultiplexer.Connect(RedisConn); + }); + + /// + /// Connessione lazy a redis... + /// + private Lazy lazyConnectionAdmin = new Lazy(() => + { + string RedisConnAdmin = memLayer.ML.confReadString("RedisConnAdmin"); + if (string.IsNullOrEmpty(RedisConnAdmin)) + { + RedisConnAdmin = "localhost,abortConnect=false,ssl=false,allowAdmin=true"; + } + + return ConnectionMultiplexer.Connect(RedisConnAdmin); + }); + + #endregion Private Fields + + #region Private Properties + + /// + /// Oggetto currentDb locale + /// + private IDatabase _currDB { get; set; } + + #endregion Private Properties + + #region Private Methods + + /// + /// Effettua comaprazione x CHIAVE in KVP ASC + /// + /// + /// + /// + private int CompareKey(KeyValuePair x, KeyValuePair y) + { + return x.Key.CompareTo(y.Key); + } + + /// + /// Effettua comaprazione x CHIAVE in KVP DESC + /// + /// + /// + /// + private int CompareKeyDesc(KeyValuePair x, KeyValuePair y) + { + return y.Key.CompareTo(x.Key); + } + + /// + /// Init accesso MongoDb + /// + private void setupMongo() + { + currMongoClient = new MongoClient(mongoConnString); + } + + #endregion Private Methods } } \ No newline at end of file diff --git a/SteamWare/memLayer.cs b/SteamWare/memLayer.cs index 8519eab..8b0433a 100644 --- a/SteamWare/memLayer.cs +++ b/SteamWare/memLayer.cs @@ -1245,6 +1245,29 @@ namespace SteamWare return answ; } + /// + /// ELIMINA un SINGOLO VALORE dalla hash (dato field = chiave) + /// + /// + /// + /// + public bool redDelHashField(string hashKey, string hashField) + { + bool answ = false; + // cerco se ci sia valore in redis... + try + { + RedisKey chiave = hashKey; + RedisValue campo = hashField; + answ = cache.HashDelete(chiave, campo); + } + catch (Exception exc) + { + Log.Error($"Errore in redDelHashField{Environment.NewLine}{exc}"); + } + return answ; + } + /// /// Elimina una key (hash, string) ///