From 944d470c1e1a2fa74e03ebd24aa8f3d9a35837d8 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Mon, 22 Oct 2018 15:23:45 +0200 Subject: [PATCH] Aggiunti metodi override x cache HTTP/Redis --- SteamWareLib/DS_Utility.xsd | 8 +- SteamWareLib/DS_Utility.xss | 4 +- SteamWareLib/app.config | 2 +- SteamWareLib/memLayer.cs | 151 +++++++++++++++++++++++++++--------- 4 files changed, 120 insertions(+), 45 deletions(-) diff --git a/SteamWareLib/DS_Utility.xsd b/SteamWareLib/DS_Utility.xsd index 77d3cfe..5c3314e 100644 --- a/SteamWareLib/DS_Utility.xsd +++ b/SteamWareLib/DS_Utility.xsd @@ -358,7 +358,7 @@ SELECT sorgente, chiave, valore, note FROM ConfigTmp WHERE (chiave = @chiave) AN - + @@ -393,7 +393,7 @@ SELECT sorgente, chiave, valore, note FROM ConfigTmp WHERE (chiave = @chiave) AN - + @@ -413,7 +413,7 @@ SELECT sorgente, chiave, valore, note FROM ConfigTmp WHERE (chiave = @chiave) AN - + @@ -440,7 +440,7 @@ SELECT sorgente, chiave, valore, note FROM ConfigTmp WHERE (chiave = @chiave) AN - + diff --git a/SteamWareLib/DS_Utility.xss b/SteamWareLib/DS_Utility.xss index 67cf2e8..c076772 100644 --- a/SteamWareLib/DS_Utility.xss +++ b/SteamWareLib/DS_Utility.xss @@ -4,10 +4,10 @@ Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. --> - + - + diff --git a/SteamWareLib/app.config b/SteamWareLib/app.config index c7a45c3..e62373e 100644 --- a/SteamWareLib/app.config +++ b/SteamWareLib/app.config @@ -55,7 +55,7 @@ connectionString="Data Source=SQL2012DEV;Initial Catalog=Equa_Anagrafica;Persist Security Info=True;User ID=sa;Password=keyhammer16;" providerName="System.Data.SqlClient" /> + /// Indica se usare la cache su REDIS (true) oppure cache applicativo IIS (false) + /// + protected bool cacheOnRedis + { + get + { + return CRB("cacheOnRedis"); + } + } /// /// carica dalla Cache un dato di tipo object generico /// @@ -935,16 +943,25 @@ namespace SteamWare /// public object objCacheObj(string nomeVar) { - if (HttpContext.Current.Cache[nomeVar] != null) + object answ = null; + // ...se uso redis... + if (cacheOnRedis) { - return HttpContext.Current.Cache[nomeVar]; + answ = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))); } else { - return ""; + if (HttpContext.Current.Cache[nomeVar] != null) + { + answ = HttpContext.Current.Cache[nomeVar]; + } + else + { + answ = ""; + } } + return answ; } - /// /// carica dalla Cachee un dato di tipo boolean (se vuoto false) /// @@ -952,16 +969,26 @@ namespace SteamWare /// public bool BoolCacheObj(string nomeVar) { - if (HttpContext.Current.Cache[nomeVar] != null) + bool answ = false; + // ...se uso redis... + if (cacheOnRedis) { - return (bool)HttpContext.Current.Cache[nomeVar]; + string redVal = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString(); + bool.TryParse(redVal, out answ); } else { - return false; + if (HttpContext.Current.Cache[nomeVar] != null) + { + answ = (bool)HttpContext.Current.Cache[nomeVar]; + } + else + { + answ = false; + } } + return answ; } - /// /// carica dalla Cachee un dato di tipo string /// @@ -969,31 +996,50 @@ namespace SteamWare /// public string StringCacheObj(string nomeVar) { - if (HttpContext.Current.Cache[nomeVar] != null) + string answ = ""; + // ...se uso redis... + if (cacheOnRedis) { - return HttpContext.Current.Cache[nomeVar].ToString(); + answ = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString(); } else { - return ""; + if (HttpContext.Current.Cache[nomeVar] != null) + { + answ = HttpContext.Current.Cache[nomeVar].ToString(); + } + else + { + answ = ""; + } } + return answ; } - /// /// inserisce in Cache un valore /// - /// nome della variabile + /// nome della variabile /// valore - public bool setCacheVal(string nome, object valore) + public bool setCacheVal(string nomeVar, object valore) { bool _done = false; - try + if (cacheOnRedis) { - HttpContext.Current.Cache[nome] = valore; + // serializzo + string serVal = JsonConvert.SerializeObject(valore); + setRSV(redHash(nomeVar), serVal); _done = true; } - catch - { } + else + { + try + { + HttpContext.Current.Cache[nomeVar] = valore; + _done = true; + } + catch + { } + } return _done; } /// @@ -1015,17 +1061,25 @@ namespace SteamWare /// /// svuota una variabile dalla Cache /// + /// /// - public bool emptyCacheVal(string nome) + public bool emptyCacheVal(string nomeVar) { bool _done = false; - try + if (cacheOnRedis) { - HttpContext.Current.Cache.Remove(nome); - _done = true; + redDelKey(redHash(nomeVar)); + } + else + { + try + { + HttpContext.Current.Cache.Remove(nomeVar); + _done = true; + } + catch + { } } - catch - { } return _done; } @@ -1038,20 +1092,27 @@ namespace SteamWare { bool answ = false; bool stringAnsw = false; - // cerco di fare cast a stringa... - try + if (cacheOnRedis) { - stringAnsw = HttpContext.Current.Cache[nomeVar].ToString() != ""; + answ = redHashPresent(redHash(nomeVar)); } - catch - { } - // infine condizione doppia... - try + else { - answ = (HttpContext.Current.Cache[nomeVar] != null && stringAnsw); + // cerco di fare cast a stringa... + try + { + stringAnsw = HttpContext.Current.Cache[nomeVar].ToString() != ""; + } + catch + { } + // infine condizione doppia... + try + { + answ = (HttpContext.Current.Cache[nomeVar] != null && stringAnsw); + } + catch + { } } - catch - { } return answ; } /// @@ -1150,9 +1211,23 @@ namespace SteamWare // elimino tutte le tab nella pos tabInCache... foreach (KeyValuePair kvp in tabelleInCache) { - HttpContext.Current.Cache.Remove(kvp.Value); + if (cacheOnRedis) + { + redDelKey(redHash(kvp.Value)); + } + else + { + HttpContext.Current.Cache.Remove(kvp.Value); + } + } + if (cacheOnRedis) + { + redDelKey(redHash("tabelleInCache")); + } + else + { + HttpContext.Current.Cache.Remove("tabelleInCache"); } - HttpContext.Current.Cache.Remove("tabelleInCache"); } #endregion