From 66dcaf0a5810f9ae65e3470ede16b571f1bf2b01 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Mon, 3 Dec 2018 09:22:06 +0100 Subject: [PATCH] Update massivo alal gestione CACHE in REDIS DA RIVEDERE nei vari programmi --- Jenkinsfile | 2 +- SteamWareLib/DataWrap.cs | 20 ++++++- SteamWareLib/memLayer.cs | 117 +++++++++++++++++++++++++++------------ 3 files changed, 101 insertions(+), 38 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4ccf97f..114209c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,7 +10,7 @@ pipeline { steps { /* calcolo numero versione... diverso x branch MASTER/DEVELOP */ script { - withEnv(['NEXT_BUILD_NUMBER=633']) { + withEnv(['NEXT_BUILD_NUMBER=639']) { // env.versionNumber = VersionNumber(versionNumberString : '3.2.${BUILD_DATE_FORMATTED, "yyMM"}.${BUILDS_ALL_TIME}', projectStartDate : '2006-01-01', skipFailedBuilds: true) env.versionNumber = VersionNumber(versionNumberString : '3.2.${BUILD_DATE_FORMATTED, "yyMM"}.${BUILDS_ALL_TIME}', projectStartDate : '2006-01-01', skipFailedBuilds: true, overrideBuildsAllTime: '${NEXT_BUILD_NUMBER}') env.APP_NAME = 'SteamWareLib' diff --git a/SteamWareLib/DataWrap.cs b/SteamWareLib/DataWrap.cs index 5e9b617..28f8d6f 100644 --- a/SteamWareLib/DataWrap.cs +++ b/SteamWareLib/DataWrap.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json; using System.Collections.Generic; namespace SteamWare @@ -85,12 +86,27 @@ namespace SteamWare { if (memLayer.ML.isInCacheObject("dictVocabolario")) { - dictVocabolario = (Dictionary)memLayer.ML.objCacheObj("dictVocabolario"); + if (memLayer.ML.cacheOnRedis) + { + dictVocabolario = JsonConvert.DeserializeObject>(memLayer.ML.objCacheObj("dictVocabolario").ToString()); + } + else + { + dictVocabolario = (Dictionary)memLayer.ML.objCacheObj("dictVocabolario"); + } } else { dictVocabolario = ricaricaDictVocabolario(); - memLayer.ML.setCacheVal("dictVocabolario", dictVocabolario, true); + if (memLayer.ML.cacheOnRedis) + { + string serVal = JsonConvert.SerializeObject(dictVocabolario); + memLayer.ML.setCacheVal("dictVocabolario", serVal, true); + } + else + { + memLayer.ML.setCacheVal("dictVocabolario", dictVocabolario, true); + } } // scrivo quanti lemmi ha caricato! logger.lg.scriviLog(string.Format("Caricati {0} lemmi!", dictVocabolario.Count)); diff --git a/SteamWareLib/memLayer.cs b/SteamWareLib/memLayer.cs index d6e73f3..1fe4c1a 100644 --- a/SteamWareLib/memLayer.cs +++ b/SteamWareLib/memLayer.cs @@ -945,7 +945,7 @@ namespace SteamWare /// /// Indica se usare la cache su REDIS (true) oppure cache applicativo IIS (false) /// - protected bool cacheOnRedis + public bool cacheOnRedis { get { @@ -964,11 +964,14 @@ namespace SteamWare // ...se uso redis... if (cacheOnRedis) { + answ = getRSV(redHash(nomeVar)); +#if false string valSer = getRSV(redHash(nomeVar)); if (valSer != "" && valSer != null) { answ = JsonConvert.DeserializeObject(valSer); - } + } +#endif } else { @@ -1046,9 +1049,12 @@ namespace SteamWare bool _done = false; if (cacheOnRedis) { +#if false // serializzo - string serVal = JsonConvert.SerializeObject(valore); + string serVal = JsonConvert.SerializeObject(valore); setRSV(redHash(nomeVar), serVal); +#endif + setRSV(redHash(nomeVar), valore.ToString()); _done = true; } else @@ -1115,7 +1121,14 @@ namespace SteamWare bool stringAnsw = false; if (cacheOnRedis) { + // cerco come hash... answ = redHashPresent(redHash(nomeVar)); + // cerco come variabile se NON trovata... + if (!answ) + { + var redVal = getRSV(redHash(nomeVar)); + answ = redVal != null && redVal != ""; + } } else { @@ -1143,24 +1156,31 @@ namespace SteamWare { get { + Dictionary answ = new Dictionary(); try { - Dictionary answ = new Dictionary(); var cacheVal = objCacheObj("tabelleInCache"); if (cacheVal != null) { +#if false answ = (Dictionary)cacheVal; +#endif + answ = JsonConvert.DeserializeObject>(cacheVal.ToString()); } - return answ; } - catch + catch (Exception exc) { - return new Dictionary(); + answ = new Dictionary(); } + return answ; } set { - setCacheVal("tabelleInCache", value); + string serVal = JsonConvert.SerializeObject(value); + setCacheVal("tabelleInCache", serVal); +#if false + setCacheVal("tabelleInCache", value); +#endif } } /// @@ -1263,6 +1283,38 @@ namespace SteamWare #region gestione valori in RedisCache + private IDatabase _currDB { get; set; } + /// + /// Oggetto DB REDIS corrente + /// + public IDatabase cache //currDB + { + get + { + IDatabase answ; + // se giĆ  valorizzato uso oggetto private... + if (_currDB != null) + { + answ = _currDB; + } + else + { + // init DB (sullo 0) + answ = connRedis.GetDatabase(); + // gestione override... + if (confReadInt("redisDb") >= 0) + { + // in questo caso uso il DB configurato in app.config... + answ = connRedis.GetDatabase(confReadInt("redisDb")); + } + _currDB = answ; + } + // restituisco oggetto DB + return answ; + } + } + + /// /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto... /// @@ -1308,24 +1360,39 @@ namespace SteamWare return answ; } + + /// /// Connessione lazy a redis... /// - private static Lazy lazyConnection = new Lazy(() => + private Lazy lazyConnection = new Lazy(() => { - return ConnectionMultiplexer.Connect("127.0.0.1,abortConnect=false,ssl=false"); + string RedisConn = memLayer.ML.confReadString("RedisConn"); + if (RedisConn == "") + { + RedisConn = "localhost,abortConnect=false,ssl=false"; + } + + return ConnectionMultiplexer.Connect(RedisConn); }); /// /// Connessione lazy a redis... /// - private static Lazy lazyConnectionAdmin = new Lazy(() => + private Lazy lazyConnectionAdmin = new Lazy(() => { - return ConnectionMultiplexer.Connect("127.0.0.1,abortConnect=false,ssl=false,allowAdmin=true"); + string RedisConnAdmin = memLayer.ML.confReadString("RedisConnAdmin"); + if (RedisConnAdmin == "") + { + RedisConnAdmin = "localhost,abortConnect=false,ssl=false,allowAdmin=true"; + } + + return ConnectionMultiplexer.Connect(RedisConnAdmin); }); + /// /// Oggetto statico connessione redis /// - public static ConnectionMultiplexer connRedis + public ConnectionMultiplexer connRedis { get { @@ -1335,7 +1402,7 @@ namespace SteamWare /// /// Oggetto statico connessione redis /// - public static ConnectionMultiplexer connRedisAdmin + public ConnectionMultiplexer connRedisAdmin { get { @@ -1377,7 +1444,6 @@ namespace SteamWare string answ = ""; try { - IDatabase cache = connRedis.GetDatabase(); answ = cache.StringGet(chiave); } catch (Exception exc) @@ -1397,7 +1463,6 @@ namespace SteamWare bool answ = false; try { - IDatabase cache = connRedis.GetDatabase(); cache.StringSet(chiave, valore); answ = true; } @@ -1419,7 +1484,6 @@ namespace SteamWare bool answ = false; try { - IDatabase cache = connRedis.GetDatabase(); TimeSpan expT = new TimeSpan(0, 0, TTL_sec); // salvo con expyry... cache.StringSet(chiave, valore, expT); @@ -1441,7 +1505,6 @@ namespace SteamWare long answ = 0; try { - IDatabase cache = connRedis.GetDatabase(); answ = cache.StringIncrement(chiave, 1); } catch (Exception exc) @@ -1460,7 +1523,6 @@ namespace SteamWare long answ = 0; try { - IDatabase cache = connRedis.GetDatabase(); answ = cache.StringDecrement(chiave, 1); } catch (Exception exc) @@ -1480,7 +1542,6 @@ namespace SteamWare string answ = ""; try { - IDatabase cache = connRedis.GetDatabase(); answ = cache.StringGet(chiave); answInt = Convert.ToInt32(answ); } @@ -1500,7 +1561,6 @@ namespace SteamWare bool answ = false; try { - IDatabase cache = connRedis.GetDatabase(); answ = cache.KeyDelete(chiave); } catch (Exception exc) @@ -1519,7 +1579,6 @@ namespace SteamWare RedisValue[] answ = null; try { - IDatabase cache = connRedis.GetDatabase(); answ = cache.StringGet(chiavi); } catch (Exception exc) @@ -1538,7 +1597,6 @@ namespace SteamWare bool answ = false; try { - IDatabase cache = connRedis.GetDatabase(); cache.StringSet(valori); answ = true; } @@ -1557,12 +1615,11 @@ namespace SteamWare { bool answ = false; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { answ = cache.HashGetAll(key).Length > 0; } - catch + catch (Exception exc) { } return answ; } @@ -1592,7 +1649,6 @@ namespace SteamWare { KeyValuePair[] answ = new KeyValuePair[1]; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = hashKey; @@ -1618,7 +1674,6 @@ namespace SteamWare { Dictionary answ = new Dictionary(); // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = hashKey; @@ -1642,7 +1697,6 @@ namespace SteamWare { string answ = ""; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = hashKey; @@ -1664,7 +1718,6 @@ namespace SteamWare { bool answ = false; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = hashKey; @@ -1692,7 +1745,6 @@ namespace SteamWare { bool answ = false; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = hashKey; @@ -1721,7 +1773,6 @@ namespace SteamWare { bool answ = false; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = hashKey; @@ -1747,7 +1798,6 @@ namespace SteamWare { bool answ = false; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = hashKey; @@ -1771,7 +1821,6 @@ namespace SteamWare { bool answ = false; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); try { RedisKey chiave = key; @@ -1791,7 +1840,6 @@ namespace SteamWare { bool answ = false; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); // se vuoto = ALL... keyPattern = keyPattern == "" ? "**" : keyPattern; try @@ -1821,7 +1869,6 @@ namespace SteamWare { int answ = 0; // cerco se ci sia valore in redis... - IDatabase cache = connRedis.GetDatabase(); // se vuoto = ALL... keyPattern = keyPattern == "" ? "**" : keyPattern; try