diff --git a/SteamWare.IO/memLayer.cs b/SteamWare.IO/memLayer.cs
index ca87ffb..573688d 100644
--- a/SteamWare.IO/memLayer.cs
+++ b/SteamWare.IO/memLayer.cs
@@ -17,14 +17,63 @@ namespace SteamWare.IO
///
public class memLayer
{
- #region oggetti protected utilizzati
+ #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;
- #endregion
+ ///
+ /// Oggetto MongoDbCLient x accesso al motore
+ ///
+ protected MongoClient currMongoClient;
+
+ #endregion Protected Fields
+
+ #region Public Fields
+
+ ///
+ /// oggetto singleton x accesso al layer di memoria
+ ///
+ public static memLayer ML = new memLayer();
+
+ ///
+ /// oggetto dizionario con chiave / valore x configurazioni applicazione
+ ///
+ public Dictionary AppConf;
///
/// Table adapter accesso conf parameters
@@ -36,20 +85,9 @@ namespace SteamWare.IO
///
public DS_UtilityTableAdapters.ConfigTmpTableAdapter taConfigTmp;
- ///
- /// oggetto singleton x accesso al layer di memoria
- ///
- public static memLayer ML = new memLayer();
- ///
- /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per impiego di sessioni avanzate come Redis)
- ///
- public bool serializeSession
- {
- get
- {
- return CRB("serializeSession");
- }
- }
+ #endregion Public Fields
+
+ #region Protected Constructors
///
/// classe gestione accessi a Session, cache, viewstate, configuration...
@@ -64,14 +102,49 @@ namespace SteamWare.IO
setupMongo();
}
+ #endregion Protected Constructors
+
+ #region Public Enums
+
///
- /// init dei table adapters
+ /// Tipologia di ordinamento x liste KVP
///
- protected void initTA()
+ public enum kvpOrderBy
{
- taConfig = new DS_UtilityTableAdapters.ConfigTableAdapter();
- taConfigTmp = new DS_UtilityTableAdapters.ConfigTmpTableAdapter();
+ ///
+ /// Ordinamento ASCending per KEY
+ ///
+ KeyAsc,
+
+ ///
+ /// Ordinamento DESCending per KEY
+ ///
+ KeyDesc,
+
+ ///
+ /// Ordinamento ASCending per VAL
+ ///
+ ValAsc,
+
+ ///
+ /// Ordinamento DESCending per VAL
+ ///
+ ValDesc
}
+
+ #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
///
@@ -96,16 +169,25 @@ namespace SteamWare.IO
}
///
- /// effettua setup dei connection strings da web.config delal singola applicazione
+ /// Stringa di connessione mongoDb
///
- protected virtual void setupConnectionStringBase()
+ protected string mongoConnString
{
- // connections del db
- taConfig.Connection.ConnectionString = connStringDbConf;
- taConfigTmp.Connection.ConnectionString = connStringDbConf;
+ get
+ {
+ string answ = "";
+ answ = confReadString("mdbConnString");
+ if (string.IsNullOrEmpty(answ))
+ {
+ answ = "mongodb://W2019-MONGODB:27017";
+ }
+ return answ;
+ }
}
- #region area gestione config su DB
+ #endregion Protected Properties
+
+ #region Public Properties
///
/// Nome della variabile AppConf da utilizzare...
@@ -126,17 +208,318 @@ namespace SteamWare.IO
}
///
- /// oggetto dizionario con chiave / valore x configurazioni applicazione
+ /// Oggetto DB REDIS corrente
///
- public Dictionary AppConf;
- ///
- /// resetta AppConfig svuotando e rileggendo i dati...
- ///
- public void resetAppConf()
+ public IDatabase cache //currDB
{
- redDelKey(ACBH);
- startupAppConf();
+ 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;
+ }
}
+
+ ///
+ /// Indica se usare la cache su REDIS (true) oppure cache applicativo IIS (false)
+ ///
+ public bool cacheOnRedis
+ {
+ get
+ {
+ bool answ = confReadBool("cacheOnRedis");
+ return answ;
+ }
+ }
+
+ ///
+ /// Nome della variabile x indicare che si sta facendo refresh della appConf...
+ ///
+ public string CleaningKey
+ {
+ get
+ {
+ string answ = "RunningACCleaning";
+ try
+ {
+ answ = string.Format("{0}:{1}:{2}:RunningACCleaning", confReadString("CodModulo"), taConfig.Connection.DataSource, taConfig.Connection.Database).Replace("\\", "_");
+ }
+ catch
+ { }
+ return answ;
+ }
+ }
+
+ ///
+ /// Oggetto statico connessione redis
+ ///
+ public ConnectionMultiplexer connRedis
+ {
+ get
+ {
+ return lazyConnection.Value;
+ }
+ }
+
+ ///
+ /// Oggetto statico connessione redis
+ ///
+ public ConnectionMultiplexer connRedisAdmin
+ {
+ get
+ {
+ return lazyConnectionAdmin.Value;
+ }
+ }
+
+ ///
+ /// 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)
+ {
+ Logging.Instance.Error(string.Format("Errore in lettura valore maxAgeAppConf_min{0}{1}", Environment.NewLine, exc));
+ }
+ return maxAge * 60;
+ }
+ }
+
+ ///
+ /// Numero record salvati in AppConf
+ ///
+ public int numRecAppConf
+ {
+ get
+ {
+ int answ = 0;
+ try
+ {
+ answ = AppConf.Count;
+ }
+ catch
+ { }
+ return answ;
+ }
+ }
+
+ ///
+ /// Restituisce numero record in Redis DB
+ ///
+ public long numRecRedis
+ {
+ get
+ {
+ long answ = 0;
+ try
+ {
+ foreach (var ep in connRedis.GetEndPoints())
+ {
+ var server = connRedis.GetServer(ep);
+ answ += server.DatabaseSize();
+ }
+ }
+ catch
+ { }
+ return answ;
+ }
+ }
+
+ ///
+ /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per impiego di sessioni avanzate come Redis)
+ ///
+ public bool serializeSession
+ {
+ get
+ {
+ return CRB("serializeSession");
+ }
+ }
+
+ ///
+ /// elenco dictionary delle tab in cache da aggiornare con update svuotando da cache...
+ ///
+ public Dictionary tabelleInCache
+ {
+ get
+ {
+ Dictionary answ = new Dictionary();
+ try
+ {
+ var cacheVal = objCacheObj("tabelleInCache");
+ if (cacheVal != null)
+ {
+ answ = JsonConvert.DeserializeObject>(cacheVal.ToString());
+ }
+ }
+ catch (Exception exc)
+ {
+ answ = new Dictionary();
+ Logging.Instance.Error(string.Format("Eccezzione in tabelleInCache{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+ set
+ {
+ string serVal = JsonConvert.SerializeObject(value);
+ setCacheVal("tabelleInCache", serVal);
+ }
+ }
+
+ ///
+ /// elenco dictionary dei valori in session da NON aggiornare con update...
+ ///
+ public Dictionary valSess2SurvUpd
+ {
+ get
+ {
+ Dictionary answ = new Dictionary();
+ if (isInSessionObject("valoriInSession2Survive"))
+ {
+ try
+ {
+ answ = (Dictionary)objSessionObj("valoriInSession2Survive");
+ }
+ catch
+ {
+ answ = new Dictionary();
+ }
+ }
+ return answ;
+ }
+ set
+ {
+ setSessionVal("valoriInSession2Survive", value);
+ }
+ }
+
+ #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--;
+ Logging.Instance.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)
+ {
+ Logging.Instance.Error(string.Format("Errore procedura ricaricaAppConf per kvp: {0} / {1}{2}{3}", riga.chiave, riga.valore, Environment.NewLine, exc));
+ }
+ }
+ // log ricarica
+ Logging.Instance.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
///
@@ -190,86 +573,199 @@ namespace SteamWare.IO
}
}
}
+
+ #endregion Protected Methods
+
+ #region Public Methods
+
///
- /// Numero record salvati in AppConf
+ /// Formattazione stringa URL immagini con gestione "base url"
///
- public int numRecAppConf
+ ///
+ ///
+ public static string imgUrl(string urlRelPath)
{
- get
+ return string.Format("{0}/{1}", memLayer.ML.CRS("baseUrl"), urlRelPath);
+ }
+
+ ///
+ /// 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...
+ Dictionary _tabelleInCache = tabelleInCache;
+ try
{
- int answ = 0;
+ _tabelleInCache.Add(nuovaTab, nuovaTab);
+ tabelleInCache = _tabelleInCache;
+ }
+ catch
+ { }
+ }
+
+ ///
+ /// 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...
+ Dictionary _valoriInSession2Survive = valSess2SurvUpd;
+ // verifico se fare update o insert...
+ if (_valoriInSession2Survive.ContainsKey(nomePar))
+ {
+ // update, rimuovo vecchio valore...
try
{
- answ = AppConf.Count;
+ _valoriInSession2Survive.Remove(nomePar);
}
catch
{ }
- return answ;
}
+ // insert
+ try
+ {
+ _valoriInSession2Survive.Add(nomePar, valore);
+ valSess2SurvUpd = _valoriInSession2Survive;
+ }
+ catch
+ { }
}
+
///
- /// carica in ram oggetto AppConf
+ /// carica dalla Cachee un dato di tipo boolean (se vuoto false)
///
+ ///
///
- protected Dictionary ricaricaAppConf()
+ public bool BoolCacheObj(string nomeVar)
{
- 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
+ bool answ = false;
+ // ...se uso redis...
+ if (cacheOnRedis)
{
- try
- {
- tabDati = nML.taConfig.GetData();
- }
- catch (Exception exc)
- {
- numTry--;
- Logging.Instance.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)
+ string redVal = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
+ bool.TryParse(redVal, out answ);
+ }
+ else
{
- // carico
- foreach (DS_Utility.ConfigRow riga in tabDati)
+ if (HttpContext.Current.Cache[nomeVar] != null)
{
- try
- {
- answ.Add(riga.chiave, riga.valore);
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Errore procedura ricaricaAppConf per kvp: {0} / {1}{2}{3}", riga.chiave, riga.valore, Environment.NewLine, exc));
- }
+ answ = (bool)HttpContext.Current.Cache[nomeVar];
+ }
+ else
+ {
+ answ = false;
}
- // log ricarica
- Logging.Instance.Info(string.Format("Effettuata procedura ricaricaAppConf per {0} records", answ.Count));
}
return answ;
}
+
///
- /// Recupera il TTL x appConf (secondi)
+ /// carica dalla sessione un dato di tipo boolean (se vuoto false)
///
- public int maxAgeAppConf
+ ///
+ ///
+ public bool BoolSessionObj(string nomeVar)
{
- get
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ return (bool)HttpContext.Current.Session[nomeVar];
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ ///
+ /// Configurations da tabella DB Config (short form wrapper)
+ ///
+ /// Valore chiave
+ ///
+ public string cdv(string chiave)
+ {
+ return configDbVal(chiave);
+ }
+
+ ///
+ /// Configurations da tabella DB Config (short form wrapper) convertito a bOOL
+ ///
+ /// Valore chiave
+ ///
+ public bool cdvb(string chiave)
+ {
+ bool answ = false;
+ string sVal = configDbVal(chiave);
+ if (sVal != "")
{
- // default 5 minuti x refresh...
- int maxAge = 5;
try
{
- maxAge = confReadInt("maxAgeAppConf_min");
+ //answ = Convert.ToBoolean(configDbVal(chiave));
+ bool fatto = bool.TryParse(sVal, out answ);
+ if (!fatto)
+ {
+ Logging.Instance.Error($"Errore in lettura chiave [{chiave}] durante cdvb: ricevuto {sVal}");
+ }
}
catch (Exception exc)
{
- Logging.Instance.Error(string.Format("Errore in lettura valore maxAgeAppConf_min{0}{1}", Environment.NewLine, exc));
+ Logging.Instance.Error($"Eccezzione in lettura chiave [{chiave}] durante cdvb{Environment.NewLine}{exc}");
}
- return maxAge * 60;
}
+ return answ;
+ }
+
+ ///
+ /// Configurations da tabella DB Config (short form wrapper) convertito a INT
+ ///
+ /// Valore chiave
+ ///
+ public int cdvi(string chiave)
+ {
+ int answ = -1;
+ string sVal = configDbVal(chiave);
+ if (sVal != "")
+ {
+ try
+ {
+ //answ = Convert.ToInt32(configDbVal(chiave));
+ bool fatto = int.TryParse(sVal, out answ);
+ if (!fatto)
+ {
+ Logging.Instance.Error($"Errore in lettura chiave [{chiave}] durante cdvi: ricevuto {sVal}");
+ }
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error($"Eccezzione in lettura chiave [{chiave}] durante cdvi{Environment.NewLine}{exc}");
+ }
+ }
+ return answ;
+ }
+
+ ///
+ /// Effettua comaprazione x VALORE in KVP ASC
+ ///
+ ///
+ ///
+ ///
+ public int CompareVal(KeyValuePair x, KeyValuePair y)
+ {
+ return x.Value.CompareTo(y.Value);
+ }
+
+ ///
+ /// Effettua comaprazione x VALORE in KVP DESC
+ ///
+ ///
+ ///
+ ///
+ public int CompareValDesc(KeyValuePair x, KeyValuePair y)
+ {
+ return y.Value.CompareTo(x.Value);
}
///
@@ -316,73 +812,74 @@ namespace SteamWare.IO
}
return answ;
}
+
///
- /// Configurations da tabella DB Config (short form wrapper)
+ /// legge dalla config un valore bool
///
- /// Valore chiave
+ ///
///
- public string cdv(string chiave)
- {
- return configDbVal(chiave);
- }
- ///
- /// Configurations da tabella DB Config (short form wrapper) convertito a bOOL
- ///
- /// Valore chiave
- ///
- public bool cdvb(string chiave)
+ public bool confReadBool(string nomeParam)
{
bool answ = false;
- string sVal = configDbVal(chiave);
- if (sVal != "")
+ try
{
- try
- {
- //answ = Convert.ToBoolean(configDbVal(chiave));
- bool fatto = bool.TryParse(sVal, out answ);
- if (!fatto)
- {
- Logging.Instance.Error($"Errore in lettura chiave [{chiave}] durante cdvb: ricevuto {sVal}");
- }
- }
- catch (Exception exc)
- {
- Logging.Instance.Error($"Eccezzione in lettura chiave [{chiave}] durante cdvb{Environment.NewLine}{exc}");
- }
+ answ = (bool)configAppSetReader.GetValue(nomeParam, typeof(bool));
}
+ catch
+ { }
return answ;
}
+
///
- /// Configurations da tabella DB Config (short form wrapper) convertito a INT
+ /// legge dalla config un valore int
///
- /// Valore chiave
+ ///
///
- public int cdvi(string chiave)
+ public double confReadDouble(string nomeParam)
+ {
+ double answ = -1;
+ try
+ {
+ answ = Convert.ToDouble(configAppSetReader.GetValue(nomeParam, typeof(double)));
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// legge dalla config un valore int
+ ///
+ ///
+ ///
+ public int confReadInt(string nomeParam)
{
int answ = -1;
- string sVal = configDbVal(chiave);
- if (sVal != "")
+ try
{
- try
- {
- //answ = Convert.ToInt32(configDbVal(chiave));
- bool fatto = int.TryParse(sVal, out answ);
- if (!fatto)
- {
- Logging.Instance.Error($"Errore in lettura chiave [{chiave}] durante cdvi: ricevuto {sVal}");
- }
- }
- catch (Exception exc)
- {
- Logging.Instance.Error($"Eccezzione in lettura chiave [{chiave}] durante cdvi{Environment.NewLine}{exc}");
- }
+ answ = (int)configAppSetReader.GetValue(nomeParam, typeof(int));
}
+ catch
+ { }
return answ;
}
- #endregion
-
- #region utility gestione conf settings
+ ///
+ /// legge dalla config un valore string
+ ///
+ ///
+ ///
+ public string confReadString(string nomeParam)
+ {
+ string answ = "";
+ try
+ {
+ answ = (string)configAppSetReader.GetValue(nomeParam, typeof(string));
+ }
+ catch
+ { }
+ return answ;
+ }
///
/// shot-form di confReadBool: legge dalla config un valore bool
@@ -403,89 +900,7 @@ namespace SteamWare.IO
}
return answ;
}
- ///
- /// legge dalla config un valore bool
- ///
- ///
- ///
- public bool confReadBool(string nomeParam)
- {
- bool answ = false;
- try
- {
- answ = (bool)configAppSetReader.GetValue(nomeParam, typeof(bool));
- }
- catch
- { }
- return answ;
- }
- ///
- /// shot-form di confReadString: legge dalla config un valore string
- ///
- ///
- ///
- public string CRS(string nomeParam)
- {
- string answ = "";
- // PROVO IN PRIMIS a cercare su DB...
- answ = cdv(nomeParam);
- if (string.IsNullOrEmpty(answ))
- {
- answ = confReadString(nomeParam);
- }
- return answ;
- }
- ///
- /// legge dalla config un valore string
- ///
- ///
- ///
- public string confReadString(string nomeParam)
- {
- string answ = "";
- try
- {
- answ = (string)configAppSetReader.GetValue(nomeParam, typeof(string));
- }
- catch
- { }
- return answ;
- }
-
- ///
- /// shot-form di confReadInt: legge dalla config un valore int
- ///
- ///
- ///
- public int CRI(string nomeParam)
- {
- int answ = -1;
- // PROVO IN PRIMIS a cercare su DB...
- answ = cdvi(nomeParam);
- // se non trovato...
- if (answ < 0)
- {
- answ = confReadInt(nomeParam);
- }
- return answ;
- }
- ///
- /// legge dalla config un valore int
- ///
- ///
- ///
- public int confReadInt(string nomeParam)
- {
- int answ = -1;
- try
- {
- answ = (int)configAppSetReader.GetValue(nomeParam, typeof(int));
- }
- catch
- { }
- return answ;
- }
///
/// shot-form di confReadDouble: legge dalla config un valore double
///
@@ -508,174 +923,41 @@ namespace SteamWare.IO
}
return answ;
}
+
///
- /// legge dalla config un valore int
+ /// shot-form di confReadInt: legge dalla config un valore int
///
///
///
- public double confReadDouble(string nomeParam)
+ public int CRI(string nomeParam)
{
- double answ = -1;
- try
+ int answ = -1;
+ // PROVO IN PRIMIS a cercare su DB...
+ answ = cdvi(nomeParam);
+ // se non trovato...
+ if (answ < 0)
{
- answ = Convert.ToDouble(configAppSetReader.GetValue(nomeParam, typeof(double)));
+ answ = confReadInt(nomeParam);
}
- catch
- { }
return answ;
}
-
- #endregion
-
- #region utility gestione querystring, cookie, session e cache
-
- #region querystring
-
///
- /// recupera valore querystring STRING
+ /// shot-form di confReadString: legge dalla config un valore string
///
- ///
- /// valore string
- public string QSS(string nome)
+ ///
+ ///
+ public string CRS(string nomeParam)
{
string answ = "";
- if (HttpContext.Current.Request.QueryString[nome] != null)
+ // PROVO IN PRIMIS a cercare su DB...
+ answ = cdv(nomeParam);
+ if (string.IsNullOrEmpty(answ))
{
- try
- {
- answ = HttpContext.Current.Request.QueryString[nome].ToString().Trim();
- }
- catch
- { }
+ answ = confReadString(nomeParam);
}
return answ;
}
- ///
- /// recupera valore querystring INT
- ///
- ///
- /// valore INT
- public int QSI(string nome)
- {
- int answ = 0;
- try
- {
- answ = Convert.ToInt32(HttpContext.Current.Request.QueryString[nome]);
- }
- catch
- { }
- return answ;
- }
- ///
- /// recupera valore querystring BOOL
- ///
- ///
- /// valore string
- public bool QSB(string nome)
- {
- bool answ = false;
- try
- {
- answ = Convert.ToBoolean(HttpContext.Current.Request.QueryString[nome]);
- }
- catch
- { }
- return answ;
- }
- ///
- /// recupera valore querystring DATE
- ///
- ///
- /// valore DATE
- public DateTime QSD(string nome)
- {
- DateTime answ = DateTime.Now;
- try
- {
- answ = Convert.ToDateTime(HttpContext.Current.Request.QueryString[nome]);
- }
- catch
- { }
- return answ;
- }
-
- #endregion
-
- #region session
-
- ///
- /// carica dalla sessione un dato di tipo DataSet NON Tipizzato
- ///
- ///
- ///
- public DataSet dsSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- string valSer = ML.StringSessionObj(nomeVar);
- DataSet dataSet = JsonConvert.DeserializeObject(valSer);
- return dataSet;
- }
- else
- {
- return null;
- }
- }
-
- ///
- /// carica dalla sessione un dato di tipo object generico
- ///
- ///
- ///
- public object objSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- return HttpContext.Current.Session[nomeVar];
- }
- else
- {
- return "";
- }
- }
-
- ///
- /// carica dalla sessione un dato di tipo boolean (se vuoto false)
- ///
- ///
- ///
- public bool BoolSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- return (bool)HttpContext.Current.Session[nomeVar];
- }
- else
- {
- return false;
- }
- }
-
- ///
- /// carica dalla sessione un dato di tipo string
- ///
- ///
- ///
- public string StringSessionObj(string nomeVar)
- {
- string answ = "";
- try
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- answ = HttpContext.Current.Session[nomeVar].ToString();
- }
- }
- catch
- { }
- return answ;
- }
///
/// carica dalla sessione un dato di tipo DateTime
@@ -701,442 +983,40 @@ namespace SteamWare.IO
}
///
- /// carica dalla sessione un dato di tipo long
+ /// Deserializzazione di un valore string in oggetto generico
+ ///
+ ///
+ ///
+ public object deserializeVal(string serVal)
+ {
+ object answ = "";
+ try
+ {
+ answ = JsonConvert.DeserializeObject(serVal);
+ }
+ catch { }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo DataSet NON Tipizzato
///
///
///
- public long LongSessionObj(string nomeVar)
+ public DataSet dsSessionObj(string nomeVar)
{
if (HttpContext.Current.Session[nomeVar] != null)
{
- return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
+ string valSer = ML.StringSessionObj(nomeVar);
+ DataSet dataSet = JsonConvert.DeserializeObject(valSer);
+ return dataSet;
}
else
{
- return 0;
+ return null;
}
}
- ///
- /// carica dalla sessione un dato di tipo int
- ///
- ///
- ///
- public int IntSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
- }
- else
- {
- return -1;
- }
- }
-
- ///
- /// inserisce in session un valore
- ///
- ///
- ///
- public bool setSessionVal(string nome, object valore)
- {
- bool _done = false;
- try
- {
- HttpContext.Current.Session[nome] = valore;
- _done = true;
- }
- catch
- { }
- return _done;
- }
- ///
- /// inserisce in session un valore
- ///
- /// nome della variabile
- /// valore associato
- /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
- ///
- public bool setSessionVal(string nome, object valore, bool surviveUpdate)
- {
- bool _done = false;
- try
- {
- HttpContext.Current.Session[nome] = valore;
- if (surviveUpdate)
- {
- addValInSession(nome, valore.ToString());
- }
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataTable da salvare
- public bool setSessionDataTable(string nome, DataTable dTable)
- {
- bool _done = false;
- try
- {
- DataSet dataSet = new DataSet("dataSet");
- dataSet.Tables.Add(dTable);
- string dataSer = JsonConvert.SerializeObject(dataSet);
- _done = setSessionVal(nome, dataSer);
- }
- catch
- { }
- return _done;
- }
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataTable da salvare
- /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
- public bool setSessionDataTable(string nome, DataTable dTable, bool surviveUpdate)
- {
- bool _done = false;
- try
- {
- DataSet dataSet = new DataSet("dataSet");
- dataSet.Tables.Add(dTable);
- string dataSer = JsonConvert.SerializeObject(dataSet);
- _done = setSessionVal(nome, dataSer, surviveUpdate);
- }
- catch
- { }
- return _done;
- }
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataSet da salvare
- public bool setSessionDataSet(string nome, DataSet dSet)
- {
- bool _done = false;
- try
- {
- string dataSer = JsonConvert.SerializeObject(dSet);
- _done = setSessionVal(nome, dataSer);
- }
- catch
- { }
- return _done;
- }
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataSet da salvare
- /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
- ///
- public bool setSessionDataSet(string nome, DataSet dSet, bool surviveUpdate)
- {
- bool _done = false;
- try
- {
- string dataSer = JsonConvert.SerializeObject(dSet);
- _done = setSessionVal(nome, dataSer, surviveUpdate);
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// svuota una variabile dalla session
- ///
- ///
- public bool emptySessionVal(string nome)
- {
- bool _done = false;
- try
- {
- HttpContext.Current.Session.Remove(nome);
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// restituisce true se sia presente in session l'oggetto richiesto
- ///
- ///
- ///
- public bool isInSessionObject(string nomeVar)
- {
- bool answ = false;
- bool stringAnsw = false;
- // cerco se ci sia...
- try
- {
- //stringAnsw = (string)HttpContext.Current.Session[nomeVar].ToString() != "";
- stringAnsw = HttpContext.Current.Session[nomeVar] != null;
- }
- catch
- { }
- // infine condizione doppia...
- try
- {
- answ = (HttpContext.Current.Session[nomeVar] != null && stringAnsw);
- }
- catch
- { }
- return answ;
- }
-
- #endregion
-
- #region cookie
-
-
- ///
- /// restituisco se ci sia un dato cookie
- ///
- ///
- ///
- public bool hasCookieVal(string nome)
- {
- bool answ = false;
- if (HttpContext.Current.Request.Cookies[nome] != null)
- {
- try
- {
- answ = HttpContext.Current.Request.Cookies[nome].Value != "";
- }
- catch
- { }
- }
- return answ;
- }
-
- ///
- /// restituisco un valore da cookie
- ///
- ///
- ///
- public string getCookieVal(string nome)
- {
- string answ = "";
- if (hasCookieVal(nome))
- {
- try
- {
- answ = HttpContext.Current.Request.Cookies[nome].Value;
- }
- catch
- { }
- }
- return answ;
- }
- ///
- /// salvo un valore come cookie
- ///
- ///
- ///
- ///
- public bool setCookieVal(string nome, string valore)
- {
- bool _done = false;
- try
- {
- HttpCookie newCookie = new HttpCookie(nome, valore);
- HttpContext.Current.Response.AppendCookie(newCookie);
- _done = true;
- }
- catch
- { }
- return _done;
- }
- ///
- /// salvo un valore come cookie con expiry date esplicita
- ///
- ///
- ///
- ///
- ///
- public bool setCookieVal(string nome, string valore, DateTime expiryDate)
- {
- bool _done = false;
- try
- {
- // rimuovo vecchio cookie
- emptyCookieVal(nome);
- // creo nuovo cookie
- HttpCookie newCookie = new HttpCookie(nome, valore);
- newCookie.Expires = expiryDate;
- HttpContext.Current.Response.AppendCookie(newCookie);
- _done = true;
- }
- catch
- { }
- return _done;
- }
- ///
- /// elimina un cookie
- ///
- ///
- public bool emptyCookieVal(string nome)
- {
- bool _done = false;
- HttpCookie aCookie;
- try
- {
- aCookie = new HttpCookie(nome);
- aCookie.Expires = DateTime.Now.AddDays(-1);
- HttpContext.Current.Response.Cookies.Add(aCookie);
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- #endregion
-
- #region cache
-
- ///
- /// Indica se usare la cache su REDIS (true) oppure cache applicativo IIS (false)
- ///
- public bool cacheOnRedis
- {
- get
- {
- bool answ = confReadBool("cacheOnRedis");
- return answ;
- }
- }
- ///
- /// carica dalla Cache un dato di tipo object generico
- ///
- ///
- ///
- public object objCacheObj(string nomeVar)
- {
- object answ = null;
- // ...se uso redis...
- if (cacheOnRedis)
- {
- answ = getRSV(redHash(nomeVar));
- }
- else
- {
- 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)
- ///
- ///
- ///
- public bool BoolCacheObj(string nomeVar)
- {
- bool answ = false;
- // ...se uso redis...
- if (cacheOnRedis)
- {
- string redVal = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
- bool.TryParse(redVal, out answ);
- }
- else
- {
- 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
- ///
- ///
- ///
- public string StringCacheObj(string nomeVar)
- {
- string answ = "";
- // ...se uso redis...
- if (cacheOnRedis)
- {
- answ = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
- }
- else
- {
- if (HttpContext.Current.Cache[nomeVar] != null)
- {
- answ = HttpContext.Current.Cache[nomeVar].ToString();
- }
- else
- {
- answ = "";
- }
- }
- return answ;
- }
- ///
- /// inserisce in Cache un valore
- ///
- /// nome della variabile
- /// valore
- public bool setCacheVal(string nomeVar, object valore)
- {
- bool _done = false;
- if (cacheOnRedis)
- {
- setRSV(redHash(nomeVar), valore.ToString());
- _done = true;
- }
- else
- {
- try
- {
- HttpContext.Current.Cache[nomeVar] = valore;
- _done = true;
- }
- catch
- { }
- }
- return _done;
- }
- ///
- /// inserisce in Cache un valore e su richiesta regitra tra le tab in cache da svuotare on update..
- ///
- /// nome della variabile
- /// valore
- /// da registrare come tabella da svuotare on update
- ///
- public bool setCacheVal(string nome, object valore, bool setInTabInCache)
- {
- bool _done = setCacheVal(nome, valore);
- if (setInTabInCache)
- {
- addTabInCache(nome);
- }
- return _done;
- }
///
/// svuota una variabile dalla Cache
///
@@ -1161,6 +1041,219 @@ namespace SteamWare.IO
return _done;
}
+ ///
+ /// elimina un cookie
+ ///
+ ///
+ public bool emptyCookieVal(string nome)
+ {
+ bool _done = false;
+ HttpCookie aCookie;
+ try
+ {
+ aCookie = new HttpCookie(nome);
+ aCookie.Expires = DateTime.Now.AddDays(-1);
+ HttpContext.Current.Response.Cookies.Add(aCookie);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// svuota una variabile dalla session
+ ///
+ ///
+ public bool emptySessionVal(string nome)
+ {
+ bool _done = false;
+ try
+ {
+ HttpContext.Current.Session.Remove(nome);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// forza lo svuotamento delle tabelle indicate come in cache...
+ ///
+ public void flushRegisteredCache()
+ {
+ // elimino tutte le tab nella pos tabInCache...
+ foreach (KeyValuePair kvp in tabelleInCache)
+ {
+ if (cacheOnRedis)
+ {
+ redDelKey(redHash(kvp.Value));
+ }
+ else
+ {
+ HttpContext.Current.Cache.Remove(kvp.Value);
+ }
+ }
+ if (cacheOnRedis)
+ {
+ redDelKey(redHash("tabelleInCache"));
+ }
+ else
+ {
+ HttpContext.Current.Cache.Remove("tabelleInCache");
+ }
+ }
+
+ ///
+ /// restituisco un valore da cookie
+ ///
+ ///
+ ///
+ public string getCookieVal(string nome)
+ {
+ string answ = "";
+ if (hasCookieVal(nome))
+ {
+ try
+ {
+ answ = HttpContext.Current.Request.Cookies[nome].Value;
+ }
+ catch
+ { }
+ }
+ return answ;
+ }
+
+ ///
+ /// Restituisce oggetto DB richiesto
+ ///
+ ///
+ ///
+ public IMongoDatabase getMongoDatabase(string dbName)
+ {
+ IMongoDatabase answ = currMongoClient.GetDatabase(dbName);
+ return answ;
+ }
+
+ ///
+ /// Restituisce una chiave COUNTER in RedisCache
+ ///
+ ///
+ ///
+ public int getRCnt(string chiave)
+ {
+ int answInt = 0;
+ string answ = "";
+ try
+ {
+ answ = cache.StringGet(chiave);
+ answInt = Convert.ToInt32(answ);
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezione in getRSV:{0}{1}", Environment.NewLine, exc));
+ }
+ return answInt;
+ }
+
+ ///
+ /// Restituisce un pò di info sul server redis connesso
+ ///
+ ///
+ public string getRedisInfoData()
+ {
+ string answ = "";
+ StringBuilder sb = new StringBuilder();
+ try
+ {
+ sb.AppendLine($"Configuration: {connRedis.Configuration}");
+ sb.AppendLine($"Connected: {connRedis.IsConnected}");
+ sb.AppendLine($"ClientName: {connRedis.ClientName}");
+ sb.AppendLine($"Total Ops: {connRedis.OperationCount}");
+ sb.AppendLine($"Status: {connRedis.GetStatus()}");
+ answ = sb.ToString();
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Restituisce un set KVP (Key Value Pair) salvati in RedisCache
+ ///
+ ///
+ ///
+ public RedisValue[] getRKeys(RedisKey[] chiavi)
+ {
+ RedisValue[] answ = null;
+ try
+ {
+ answ = cache.StringGet(chiavi);
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezione in getRKeys:{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+
+ ///
+ /// Restituisce una chiave salvata in RedisCache
+ ///
+ ///
+ ///
+ public string getRSV(string chiave)
+ {
+ string answ = "";
+ try
+ {
+ answ = cache.StringGet(chiave);
+ }
+ catch (Exception exc)
+ {
+ //logger.lg.scriviLog(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
+ }
+ return answ;
+ }
+
+ ///
+ /// restituisco se ci sia un dato cookie
+ ///
+ ///
+ ///
+ public bool hasCookieVal(string nome)
+ {
+ bool answ = false;
+ if (HttpContext.Current.Request.Cookies[nome] != null)
+ {
+ try
+ {
+ answ = HttpContext.Current.Request.Cookies[nome].Value != "";
+ }
+ catch
+ { }
+ }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo int
+ ///
+ ///
+ ///
+ public int IntSessionObj(string nomeVar)
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
///
/// restituisce true se sia presente in cache l'oggetto richiesto
///
@@ -1174,7 +1267,7 @@ namespace SteamWare.IO
{
try
{
- // cerco come key...
+ // cerco come key...
answ = redKeyPresent(redHash(nomeVar));
if (!answ)
{
@@ -1214,811 +1307,166 @@ namespace SteamWare.IO
}
return answ;
}
+
///
- /// elenco dictionary delle tab in cache da aggiornare con update svuotando da cache...
+ /// restituisce true se sia presente in session l'oggetto richiesto
///
- public Dictionary tabelleInCache
+ ///
+ ///
+ public bool isInSessionObject(string nomeVar)
{
- get
- {
- Dictionary answ = new Dictionary();
- try
- {
- var cacheVal = objCacheObj("tabelleInCache");
- if (cacheVal != null)
- {
- answ = JsonConvert.DeserializeObject>(cacheVal.ToString());
- }
- }
- catch (Exception exc)
- {
- answ = new Dictionary();
- Logging.Instance.Error(string.Format("Eccezzione in tabelleInCache{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- set
- {
- string serVal = JsonConvert.SerializeObject(value);
- setCacheVal("tabelleInCache", serVal);
- }
- }
- ///
- /// 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...
- Dictionary _tabelleInCache = tabelleInCache;
+ bool answ = false;
+ bool stringAnsw = false;
+ // cerco se ci sia...
try
{
- _tabelleInCache.Add(nuovaTab, nuovaTab);
- tabelleInCache = _tabelleInCache;
+ //stringAnsw = (string)HttpContext.Current.Session[nomeVar].ToString() != "";
+ stringAnsw = HttpContext.Current.Session[nomeVar] != null;
}
catch
{ }
+ // infine condizione doppia...
+ try
+ {
+ answ = (HttpContext.Current.Session[nomeVar] != null && stringAnsw);
+ }
+ catch
+ { }
+ return answ;
}
///
- /// elenco dictionary dei valori in session da NON aggiornare con update...
+ /// carica dalla sessione un dato di tipo long
///
- public Dictionary valSess2SurvUpd
+ ///
+ ///
+ public long LongSessionObj(string nomeVar)
{
- get
+ if (HttpContext.Current.Session[nomeVar] != null)
{
- Dictionary answ = new Dictionary();
- if (isInSessionObject("valoriInSession2Survive"))
- {
- try
- {
- answ = (Dictionary)objSessionObj("valoriInSession2Survive");
- }
- catch
- {
- answ = new Dictionary();
- }
- }
- return answ;
- }
- set
- {
- setSessionVal("valoriInSession2Survive", value);
- }
- }
- ///
- /// 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...
- Dictionary _valoriInSession2Survive = valSess2SurvUpd;
- // verifico se fare update o insert...
- if (_valoriInSession2Survive.ContainsKey(nomePar))
- {
- // update, rimuovo vecchio valore...
- try
- {
- _valoriInSession2Survive.Remove(nomePar);
- }
- catch
- { }
- }
- // insert
- try
- {
- _valoriInSession2Survive.Add(nomePar, valore);
- valSess2SurvUpd = _valoriInSession2Survive;
- }
- catch
- { }
- }
- ///
- /// forza lo svuotamento delle tabelle indicate come in cache...
- ///
- public void flushRegisteredCache()
- {
- // elimino tutte le tab nella pos tabInCache...
- foreach (KeyValuePair kvp in tabelleInCache)
- {
- if (cacheOnRedis)
- {
- redDelKey(redHash(kvp.Value));
- }
- else
- {
- HttpContext.Current.Cache.Remove(kvp.Value);
- }
- }
- if (cacheOnRedis)
- {
- redDelKey(redHash("tabelleInCache"));
+ return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
}
else
{
- HttpContext.Current.Cache.Remove("tabelleInCache");
+ return 0;
}
}
- #endregion
-
- #endregion
-
- #region gestione valori in RedisCache
-
///
- /// Restituisce un pò di info sul server redis connesso
+ /// carica dalla Cache un dato di tipo object generico
///
+ ///
///
- public string getRedisInfoData()
+ public object objCacheObj(string nomeVar)
{
- string answ = "";
- StringBuilder sb = new StringBuilder();
- try
+ object answ = null;
+ // ...se uso redis...
+ if (cacheOnRedis)
{
- sb.AppendLine($"Configuration: {connRedis.Configuration}");
- sb.AppendLine($"Connected: {connRedis.IsConnected}");
- sb.AppendLine($"ClientName: {connRedis.ClientName}");
- sb.AppendLine($"Total Ops: {connRedis.OperationCount}");
- sb.AppendLine($"Status: {connRedis.GetStatus()}");
- answ = sb.ToString();
+ answ = getRSV(redHash(nomeVar));
}
- catch
- { }
- return answ;
- }
- ///
- /// Oggetto currentDb locale
- ///
- private IDatabase _currDB { get; set; }
- ///
- /// Oggetto DB REDIS corrente
- ///
- public IDatabase cache //currDB
- {
- get
+ else
{
- IDatabase answ;
- // se già valorizzato uso oggetto private...
- if (_currDB != null)
+ if (HttpContext.Current.Cache[nomeVar] != null)
{
- answ = _currDB;
+ answ = HttpContext.Current.Cache[nomeVar];
}
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;
+ answ = "";
}
- // restituisco oggetto DB
- return answ;
+ }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo object generico
+ ///
+ ///
+ ///
+ public object objSessionObj(string nomeVar)
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ return HttpContext.Current.Session[nomeVar];
+ }
+ else
+ {
+ return "";
}
}
-
///
- /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto...
+ /// recupera valore querystring BOOL
///
- public string redHash(string keyName)
+ ///
+ /// valore string
+ public bool QSB(string nome)
{
- string answ = keyName;
+ bool answ = false;
try
{
- answ = string.Format("{0}:{1}:{2}:{3}", confReadString("CodModulo"), taConfig.Connection.DataSource, taConfig.Connection.Database, keyName).Replace("\\", "_");
+ answ = Convert.ToBoolean(HttpContext.Current.Request.QueryString[nome]);
}
catch
{ }
return answ;
}
+
///
- /// Serializzazione di un oggetto generico
+ /// recupera valore querystring DATE
///
- ///
- ///
- public string serializeVal(object origVal)
+ ///
+ /// valore DATE
+ public DateTime QSD(string nome)
+ {
+ DateTime answ = DateTime.Now;
+ try
+ {
+ answ = Convert.ToDateTime(HttpContext.Current.Request.QueryString[nome]);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// recupera valore querystring INT
+ ///
+ ///
+ /// valore INT
+ public int QSI(string nome)
+ {
+ int answ = 0;
+ try
+ {
+ answ = Convert.ToInt32(HttpContext.Current.Request.QueryString[nome]);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// recupera valore querystring STRING
+ ///
+ ///
+ /// valore string
+ public string QSS(string nome)
{
string answ = "";
- try
+ if (HttpContext.Current.Request.QueryString[nome] != null)
{
- answ = JsonConvert.SerializeObject(origVal);
- }
- catch { }
- return answ;
- }
- ///
- /// Deserializzazione di un valore string in oggetto generico
- ///
- ///
- ///
- public object deserializeVal(string serVal)
- {
- object answ = "";
- try
- {
- answ = JsonConvert.DeserializeObject(serVal);
- }
- catch { }
- return answ;
- }
-
-
-
- ///
- /// 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);
- });
-
- ///
- /// Oggetto statico connessione redis
- ///
- public ConnectionMultiplexer connRedis
- {
- get
- {
- return lazyConnection.Value;
- }
- }
- ///
- /// Oggetto statico connessione redis
- ///
- public ConnectionMultiplexer connRedisAdmin
- {
- get
- {
- return lazyConnectionAdmin.Value;
- }
- }
- ///
- /// Restituisce info dei server connessi...
- ///
- ///
- public IServer[] redServInfo()
- {
- IServer[] answ = new IServer[1];
- try
- {
- answ = new IServer[connRedisAdmin.GetEndPoints().Length];
- int i = 0;
- foreach (var ep in connRedisAdmin.GetEndPoints())
- {
- var server = connRedisAdmin.GetServer(ep);
- answ[i] = server;
- i++;
- }
- }
- catch (Exception exc)
- {
- Logging.Instance.Error($"redServInfo:{Environment.NewLine}{exc}");
- }
- return answ;
- }
- ///
- /// Restituisce una chiave salvata in RedisCache
- ///
- ///
- ///
- public string getRSV(string chiave)
- {
- string answ = "";
- try
- {
- answ = cache.StringGet(chiave);
- }
- catch (Exception exc)
- {
- //logger.lg.scriviLog(string.Format("Errore in getRSV:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
- }
- return answ;
- }
- ///
- /// Salva una chiave in RedisCache
- ///
- ///
- ///
- ///
- public bool setRSV(string chiave, string valore)
- {
- bool answ = false;
- try
- {
- cache.StringSet(chiave, valore);
- answ = true;
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezzione in setRSV:{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- ///
- /// Salva una chiave in RedisCache
- ///
- ///
- ///
- /// in secondi
- ///
- public bool setRSV(string chiave, string valore, int TTL_sec)
- {
- bool answ = false;
- try
- {
- TimeSpan expT = new TimeSpan(0, 0, TTL_sec);
- // salvo con expyry...
- cache.StringSet(chiave, valore, expT);
- answ = true;
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezzione in setRSV:{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- ///
- /// Incrementa un contatore in Redis
- ///
- ///
- ///
- public long setRCntI(string chiave)
- {
- long answ = 0;
- try
- {
- answ = cache.StringIncrement(chiave, 1);
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezzione in setRCI:{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- ///
- /// Decrementa un contatore in Redis
- ///
- ///
- ///
- public long setRCntD(string chiave)
- {
- long answ = 0;
- try
- {
- answ = cache.StringDecrement(chiave, 1);
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezzione in setRCD:{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- ///
- /// Restituisce una chiave COUNTER in RedisCache
- ///
- ///
- ///
- public int getRCnt(string chiave)
- {
- int answInt = 0;
- string answ = "";
- try
- {
- answ = cache.StringGet(chiave);
- answInt = Convert.ToInt32(answ);
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezione in getRSV:{0}{1}", Environment.NewLine, exc));
- }
- return answInt;
- }
- ///
- /// Resetta (elimina) un contatore in Redis
- ///
- ///
- ///
- public bool resetRCnt(string chiave)
- {
- bool answ = false;
- try
- {
- answ = cache.KeyDelete(chiave);
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezione in resetRCnt:{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- ///
- /// Restituisce un set KVP (Key Value Pair) salvati in RedisCache
- ///
- ///
- ///
- public RedisValue[] getRKeys(RedisKey[] chiavi)
- {
- RedisValue[] answ = null;
- try
- {
- answ = cache.StringGet(chiavi);
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezione in getRKeys:{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- ///
- /// Salva un set KVP (Key Value Pair) in RedisCache
- ///
- /// Set KVP chiave-valore da salvare
- ///
- public bool setRKeys(KeyValuePair[] valori)
- {
- bool answ = false;
- try
- {
- cache.StringSet(valori);
- answ = true;
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezione in setRKeys:{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- ///
- /// Verifica se ci siano valori nella KEY indicata...
- ///
- ///
- ///
- public bool redKeyPresent(RedisKey key)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- try
- {
- answ = cache.KeyExists(key);
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezione in redKeyPresent per la key {2}:{0}{1}", Environment.NewLine, exc, key));
- }
- return answ;
- }
- ///
- /// Verifica se ci siano valori nella KEY indicata (string)
- ///
- ///
- ///
- public bool redKeyPresentSz(string key)
- {
- bool answ = false;
- try
- {
- RedisKey chiave = key;
- answ = redKeyPresent(chiave);
- }
- catch
- { }
- return answ;
- }
- ///
- /// Verifica se ci siano valori nella hash indicata...
- ///
- ///
- ///
- public bool redHashPresent(RedisKey key)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- try
- {
- answ = cache.HashGetAll(key).Length > 0;
- }
- catch (Exception exc)
- {
- Logging.Instance.Error(string.Format("Eccezione in redHashPresent per la key {2}{0}{1}", Environment.NewLine, exc, key));
- }
- return answ;
- }
- ///
- /// Verifica se ci siano valori nella hash indicata (string)
- ///
- ///
- ///
- public bool redHashPresentSz(string key)
- {
- bool answ = false;
- try
- {
- RedisKey chiave = key;
- answ = redHashPresent(chiave);
- }
- catch
- { }
- return answ;
- }
- ///
- /// Recupera tutti i valori dalla hash
- ///
- ///
- ///
- public KeyValuePair[] redGetHash(string hashKey)
- {
- KeyValuePair[] answ = new KeyValuePair[1];
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = hashKey;
- HashEntry[] valori = cache.HashGetAll(chiave);
- answ = new KeyValuePair[valori.Length];
- int i = 0;
- foreach (HashEntry item in valori)
- {
- answ[i] = new KeyValuePair(item.Name, item.Value);
- i++;
- }
- }
- catch
- { }
- return answ;
- }
- ///
- /// Recupera tutti i valori dalla hash in formato Dictionary
- ///
- ///
- ///
- public Dictionary redGetHashDict(string hashKey)
- {
- Dictionary answ = new Dictionary();
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = hashKey;
- HashEntry[] valori = cache.HashGetAll(chiave);
- foreach (HashEntry item in valori)
- {
- answ.Add(item.Name, item.Value);
- }
- }
- catch
- { }
- return answ;
- }
- ///
- /// Recupera UN SINGOLO VALORE dalla hash per un dato field
- ///
- ///
- ///
- ///
- public string redGetHashField(string hashKey, string hashField)
- {
- string answ = "";
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = hashKey;
- RedisValue campo = hashField;
- RedisValue valOut = cache.HashGet(chiave, campo);
- answ = valOut.ToString();
- }
- catch
- { }
- return answ;
- }
- ///
- /// Salvataggio di una hash di valori
- ///
- /// chiave
- /// valori
- ///
- public bool redSaveHash(string hashKey, KeyValuePair[] hashFields)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = hashKey;
- HashEntry[] valori = new HashEntry[hashFields.Length];
- int i = 0;
- foreach (KeyValuePair kvp in hashFields)
- {
- valori[i] = new HashEntry(kvp.Key, kvp.Value);
- i++;
- }
- cache.HashSet(chiave, valori);
- answ = true;
- }
- catch
- { }
- return answ;
- }
- ///
- /// Salvataggio di una hash di valori in formato Dictionary
- ///
- /// chiave
- /// valori
- ///
- public bool redSaveHashDict(string hashKey, Dictionary hashFields)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = hashKey;
- HashEntry[] valori = new HashEntry[hashFields.Count];
- int i = 0;
- foreach (KeyValuePair kvp in hashFields)
- {
- valori[i] = new HashEntry(kvp.Key, kvp.Value);
- i++;
- }
- cache.HashSet(chiave, valori);
- answ = true;
- }
- catch
- { }
- return answ;
- }
-
- ///
- /// Salvataggio di una hash di valori
- ///
- /// chiave
- /// valori come lista KVP
- ///
- public bool redSaveHashList(string hashKey, List> hashListKVP)
- {
- bool answ = false;
- if (connRedis.IsConnected)
- {
- // cerco se ci sia valore in redis...
- IDatabase cache = connRedis.GetDatabase();
try
{
- 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;
+ answ = HttpContext.Current.Request.QueryString[nome].ToString().Trim();
}
catch
{ }
}
return answ;
}
- ///
- /// Salvataggio di una hash di valori
- ///
- /// chiave
- /// valori
- /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade)
- ///
- public bool redSaveHash(string hashKey, KeyValuePair[] hashFields, double expireSeconds = -1)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = hashKey;
- answ = redSaveHash(hashKey, hashFields);
- if (expireSeconds > 0)
- {
- cache.KeyExpire(chiave, DateTime.Now.AddSeconds(expireSeconds));
- }
- //answ = true;
- }
- catch
- { }
- return answ;
- }
- ///
- /// Salvataggio di una hash di valori in formato Dictionary
- ///
- /// chiave
- /// valori
- /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade)
- ///
- public bool redSaveHashDict(string hashKey, Dictionary hashFields, double expireSeconds = -1)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = hashKey;
- answ = redSaveHashDict(hashKey, hashFields);
- if (expireSeconds > 0)
- {
- cache.KeyExpire(chiave, DateTime.Now.AddSeconds(expireSeconds));
- }
- //answ = true;
- }
- catch
- { }
- return answ;
- }
- ///
- /// Elimina una key (hash, string)
- ///
- ///
- ///
- public bool redDelKey(string key)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- try
- {
- RedisKey chiave = key;
- cache.KeyDelete(chiave);
- answ = true;
- }
- catch
- { }
- return answ;
- }
- ///
- /// Flush completo cache redis
- ///
- /// ** = tutti
- ///
- public bool redFlushKey(string keyPattern)
- {
- bool answ = false;
- // cerco se ci sia valore in redis...
- // se vuoto = ALL...
- keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern;
- try
- {
- foreach (var ep in connRedis.GetEndPoints())
- {
- var server = connRedis.GetServer(ep);
- var keys = server.Keys(database: confReadInt("redisDb"), pattern: $"{keyPattern}*");
- foreach (var key in keys)
- {
- cache.KeyDelete(key);
- }
- }
- answ = true;
- }
- catch (Exception exc)
- {
- Logging.Instance.Error($"Eccezione in redFlushKey{Environment.NewLine}{exc}");
- }
- return answ;
- }
+
///
/// Conta num oggetti cache redis che rispondono a pattern
///
@@ -2049,26 +1497,56 @@ namespace SteamWare.IO
}
///
- /// Restituisce numero record in Redis DB
+ /// Elimina una key (hash, string)
///
- public long numRecRedis
+ ///
+ ///
+ public bool redDelKey(string key)
{
- get
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ try
{
- long answ = 0;
- try
+ RedisKey chiave = key;
+ cache.KeyDelete(chiave);
+ answ = true;
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Flush completo cache redis
+ ///
+ /// ** = tutti
+ ///
+ public bool redFlushKey(string keyPattern)
+ {
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ // se vuoto = ALL...
+ keyPattern = string.IsNullOrEmpty(keyPattern) ? "**" : keyPattern;
+ try
+ {
+ foreach (var ep in connRedis.GetEndPoints())
{
- foreach (var ep in connRedis.GetEndPoints())
+ var server = connRedis.GetServer(ep);
+ var keys = server.Keys(database: confReadInt("redisDb"), pattern: $"{keyPattern}*");
+ foreach (var key in keys)
{
- var server = connRedis.GetServer(ep);
- answ += server.DatabaseSize();
+ cache.KeyDelete(key);
}
}
- catch
- { }
- return answ;
+ answ = true;
}
+ catch (Exception exc)
+ {
+ Logging.Instance.Error($"Eccezione in redFlushKey{Environment.NewLine}{exc}");
+ }
+ return answ;
}
+
///
/// Restituisce oggetti cache redis che rispondono a pattern
///
@@ -2121,143 +1599,763 @@ namespace SteamWare.IO
case kvpOrderBy.KeyAsc:
answ.Sort(CompareKey);
break;
+
case kvpOrderBy.KeyDesc:
answ.Sort(CompareKeyDesc);
break;
+
case kvpOrderBy.ValAsc:
answ.Sort(CompareVal);
break;
+
case kvpOrderBy.ValDesc:
answ.Sort(CompareValDesc);
break;
+
default:
break;
}
return answ;
}
+
///
- /// Effettua comaprazione x CHIAVE in KVP ASC
+ /// Recupera tutti i valori dalla hash
///
- ///
- ///
+ ///
///
- private int CompareKey(KeyValuePair x, KeyValuePair y)
+ public KeyValuePair[] redGetHash(string hashKey)
{
- return x.Key.CompareTo(y.Key);
- }
- ///
- /// Effettua comaprazione x VALORE in KVP ASC
- ///
- ///
- ///
- ///
- public int CompareVal(KeyValuePair x, KeyValuePair y)
- {
- return x.Value.CompareTo(y.Value);
- }
- ///
- /// Effettua comaprazione x CHIAVE in KVP DESC
- ///
- ///
- ///
- ///
- private int CompareKeyDesc(KeyValuePair x, KeyValuePair y)
- {
- return y.Key.CompareTo(x.Key);
- }
- ///
- /// Effettua comaprazione x VALORE in KVP DESC
- ///
- ///
- ///
- ///
- public int CompareValDesc(KeyValuePair x, KeyValuePair y)
- {
- return y.Value.CompareTo(x.Value);
- }
-
- ///
- /// Tipologia di ordinamento x liste KVP
- ///
- public enum kvpOrderBy
- {
- ///
- /// Ordinamento ASCending per KEY
- ///
- KeyAsc,
- ///
- /// Ordinamento DESCending per KEY
- ///
- KeyDesc,
- ///
- /// Ordinamento ASCending per VAL
- ///
- ValAsc,
- ///
- /// Ordinamento DESCending per VAL
- ///
- ValDesc
- }
-
- #endregion
-
- #region URL corretti immagini
-
- ///
- /// Formattazione stringa URL immagini con gestione "base url"
- ///
- ///
- ///
- public static string imgUrl(string urlRelPath)
- {
- return string.Format("{0}/{1}", memLayer.ML.CRS("baseUrl"), urlRelPath);
- }
-
- #endregion
-
- #region gestione mongoDb
-
- ///
- /// Stringa di connessione mongoDb
- ///
- protected string mongoConnString
- {
- get
+ KeyValuePair[] answ = new KeyValuePair[1];
+ // cerco se ci sia valore in redis...
+ try
{
- string answ = "";
- answ = confReadString("mdbConnString");
- if (string.IsNullOrEmpty(answ))
+ RedisKey chiave = hashKey;
+ HashEntry[] valori = cache.HashGetAll(chiave);
+ answ = new KeyValuePair[valori.Length];
+ int i = 0;
+ foreach (HashEntry item in valori)
{
- answ = "mongodb://W2019-MONGODB:27017";
+ answ[i] = new KeyValuePair(item.Name, item.Value);
+ i++;
}
- return answ;
}
- }
- ///
- /// Oggetto MongoDbCLient x accesso al motore
- ///
- protected MongoClient currMongoClient;
- ///
- /// Init accesso MongoDb
- ///
- private void setupMongo()
- {
- currMongoClient = new MongoClient(mongoConnString);
- }
- ///
- /// Restituisce oggetto DB richiesto
- ///
- ///
- ///
- public IMongoDatabase getMongoDatabase(string dbName)
- {
- IMongoDatabase answ = currMongoClient.GetDatabase(dbName);
+ catch
+ { }
return answ;
}
+ ///
+ /// Recupera tutti i valori dalla hash in formato Dictionary
+ ///
+ ///
+ ///
+ public Dictionary redGetHashDict(string hashKey)
+ {
+ Dictionary answ = new Dictionary();
+ // cerco se ci sia valore in redis...
+ try
+ {
+ RedisKey chiave = hashKey;
+ HashEntry[] valori = cache.HashGetAll(chiave);
+ foreach (HashEntry item in valori)
+ {
+ answ.Add(item.Name, item.Value);
+ }
+ }
+ catch
+ { }
+ return answ;
+ }
+ ///
+ /// Recupera UN SINGOLO VALORE dalla hash per un dato field
+ ///
+ ///
+ ///
+ ///
+ public string redGetHashField(string hashKey, string hashField)
+ {
+ string answ = "";
+ // cerco se ci sia valore in redis...
+ try
+ {
+ RedisKey chiave = hashKey;
+ RedisValue campo = hashField;
+ RedisValue valOut = cache.HashGet(chiave, campo);
+ answ = valOut.ToString();
+ }
+ catch
+ { }
+ return answ;
+ }
- #endregion
+ ///
+ /// Nome della variabile HASH da utilizzare (dato CodModulo / Server / DB impiegato da funzionalita' DbConfig) + keyName richiesto...
+ ///
+ public string redHash(string keyName)
+ {
+ string answ = keyName;
+ try
+ {
+ answ = string.Format("{0}:{1}:{2}:{3}", confReadString("CodModulo"), taConfig.Connection.DataSource, taConfig.Connection.Database, keyName).Replace("\\", "_");
+ }
+ catch
+ { }
+ return answ;
+ }
+ ///
+ /// Verifica se ci siano valori nella hash indicata...
+ ///
+ ///
+ ///
+ public bool redHashPresent(RedisKey key)
+ {
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ try
+ {
+ answ = cache.HashGetAll(key).Length > 0;
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezione in redHashPresent per la key {2}{0}{1}", Environment.NewLine, exc, key));
+ }
+ return answ;
+ }
+
+ ///
+ /// Verifica se ci siano valori nella hash indicata (string)
+ ///
+ ///
+ ///
+ public bool redHashPresentSz(string key)
+ {
+ bool answ = false;
+ try
+ {
+ RedisKey chiave = key;
+ answ = redHashPresent(chiave);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Verifica se ci siano valori nella KEY indicata...
+ ///
+ ///
+ ///
+ public bool redKeyPresent(RedisKey key)
+ {
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ try
+ {
+ answ = cache.KeyExists(key);
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezione in redKeyPresent per la key {2}:{0}{1}", Environment.NewLine, exc, key));
+ }
+ return answ;
+ }
+
+ ///
+ /// Verifica se ci siano valori nella KEY indicata (string)
+ ///
+ ///
+ ///
+ public bool redKeyPresentSz(string key)
+ {
+ bool answ = false;
+ try
+ {
+ RedisKey chiave = key;
+ answ = redKeyPresent(chiave);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Salvataggio di una hash di valori
+ ///
+ /// chiave
+ /// valori
+ ///
+ public bool redSaveHash(string hashKey, KeyValuePair[] hashFields)
+ {
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ try
+ {
+ RedisKey chiave = hashKey;
+ HashEntry[] valori = new HashEntry[hashFields.Length];
+ int i = 0;
+ foreach (KeyValuePair kvp in hashFields)
+ {
+ valori[i] = new HashEntry(kvp.Key, kvp.Value);
+ i++;
+ }
+ cache.HashSet(chiave, valori);
+ answ = true;
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Salvataggio di una hash di valori
+ ///
+ /// chiave
+ /// valori
+ /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade)
+ ///
+ public bool redSaveHash(string hashKey, KeyValuePair[] hashFields, double expireSeconds = -1)
+ {
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ try
+ {
+ RedisKey chiave = hashKey;
+ answ = redSaveHash(hashKey, hashFields);
+ if (expireSeconds > 0)
+ {
+ cache.KeyExpire(chiave, DateTime.Now.AddSeconds(expireSeconds));
+ }
+ //answ = true;
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Salvataggio di una hash di valori in formato Dictionary
+ ///
+ /// chiave
+ /// valori
+ ///
+ public bool redSaveHashDict(string hashKey, Dictionary hashFields)
+ {
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ try
+ {
+ RedisKey chiave = hashKey;
+ HashEntry[] valori = new HashEntry[hashFields.Count];
+ int i = 0;
+ foreach (KeyValuePair kvp in hashFields)
+ {
+ valori[i] = new HashEntry(kvp.Key, kvp.Value);
+ i++;
+ }
+ cache.HashSet(chiave, valori);
+ answ = true;
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Salvataggio di una hash di valori in formato Dictionary
+ ///
+ /// chiave
+ /// valori
+ /// scadenza preimpostata hash (secondi) | defaoult = -1 (non scade)
+ ///
+ public bool redSaveHashDict(string hashKey, Dictionary hashFields, double expireSeconds = -1)
+ {
+ bool answ = false;
+ // cerco se ci sia valore in redis...
+ try
+ {
+ RedisKey chiave = hashKey;
+ answ = redSaveHashDict(hashKey, hashFields);
+ if (expireSeconds > 0)
+ {
+ cache.KeyExpire(chiave, DateTime.Now.AddSeconds(expireSeconds));
+ }
+ //answ = true;
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// Salvataggio di una hash di valori
+ ///
+ /// chiave
+ /// valori come lista KVP
+ ///
+ public bool redSaveHashList(string hashKey, List> hashListKVP)
+ {
+ bool answ = false;
+ if (connRedis.IsConnected)
+ {
+ // cerco se ci sia valore in redis...
+ IDatabase cache = connRedis.GetDatabase();
+ try
+ {
+ 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
+ { }
+ }
+ return answ;
+ }
+
+ ///
+ /// Restituisce info dei server connessi...
+ ///
+ ///
+ public IServer[] redServInfo()
+ {
+ IServer[] answ = new IServer[1];
+ try
+ {
+ answ = new IServer[connRedisAdmin.GetEndPoints().Length];
+ int i = 0;
+ foreach (var ep in connRedisAdmin.GetEndPoints())
+ {
+ var server = connRedisAdmin.GetServer(ep);
+ answ[i] = server;
+ i++;
+ }
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error($"redServInfo:{Environment.NewLine}{exc}");
+ }
+ return answ;
+ }
+
+ ///
+ /// resetta AppConfig svuotando e rileggendo i dati...
+ ///
+ public void resetAppConf()
+ {
+ // controlo chiave x evitare doppio reset
+ if (!string.IsNullOrEmpty(getRSV(CleaningKey)))
+ {
+ //imposto veto 10 sec
+ setRSV(CleaningKey, DateTime.Now.ToString(), 10);
+ redDelKey(ACBH);
+ startupAppConf();
+ // veto 1 sec --> se ne va subito
+ setRSV(CleaningKey, "", 1);
+ }
+ }
+
+ ///
+ /// Resetta (elimina) un contatore in Redis
+ ///
+ ///
+ ///
+ public bool resetRCnt(string chiave)
+ {
+ bool answ = false;
+ try
+ {
+ answ = cache.KeyDelete(chiave);
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezione in resetRCnt:{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+
+ ///
+ /// Serializzazione di un oggetto generico
+ ///
+ ///
+ ///
+ public string serializeVal(object origVal)
+ {
+ string answ = "";
+ try
+ {
+ answ = JsonConvert.SerializeObject(origVal);
+ }
+ catch { }
+ return answ;
+ }
+
+ ///
+ /// inserisce in Cache un valore
+ ///
+ /// nome della variabile
+ /// valore
+ public bool setCacheVal(string nomeVar, object valore)
+ {
+ bool _done = false;
+ if (cacheOnRedis)
+ {
+ setRSV(redHash(nomeVar), valore.ToString());
+ _done = true;
+ }
+ else
+ {
+ try
+ {
+ HttpContext.Current.Cache[nomeVar] = valore;
+ _done = true;
+ }
+ catch
+ { }
+ }
+ return _done;
+ }
+
+ ///
+ /// inserisce in Cache un valore e su richiesta regitra tra le tab in cache da svuotare on update..
+ ///
+ /// nome della variabile
+ /// valore
+ /// da registrare come tabella da svuotare on update
+ ///
+ public bool setCacheVal(string nome, object valore, bool setInTabInCache)
+ {
+ bool _done = setCacheVal(nome, valore);
+ if (setInTabInCache)
+ {
+ addTabInCache(nome);
+ }
+ return _done;
+ }
+
+ ///
+ /// salvo un valore come cookie
+ ///
+ ///
+ ///
+ ///
+ public bool setCookieVal(string nome, string valore)
+ {
+ bool _done = false;
+ try
+ {
+ HttpCookie newCookie = new HttpCookie(nome, valore);
+ HttpContext.Current.Response.AppendCookie(newCookie);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// salvo un valore come cookie con expiry date esplicita
+ ///
+ ///
+ ///
+ ///
+ ///
+ public bool setCookieVal(string nome, string valore, DateTime expiryDate)
+ {
+ bool _done = false;
+ try
+ {
+ // rimuovo vecchio cookie
+ emptyCookieVal(nome);
+ // creo nuovo cookie
+ HttpCookie newCookie = new HttpCookie(nome, valore);
+ newCookie.Expires = expiryDate;
+ HttpContext.Current.Response.AppendCookie(newCookie);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// Decrementa un contatore in Redis
+ ///
+ ///
+ ///
+ public long setRCntD(string chiave)
+ {
+ long answ = 0;
+ try
+ {
+ answ = cache.StringDecrement(chiave, 1);
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezzione in setRCD:{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+
+ ///
+ /// Incrementa un contatore in Redis
+ ///
+ ///
+ ///
+ public long setRCntI(string chiave)
+ {
+ long answ = 0;
+ try
+ {
+ answ = cache.StringIncrement(chiave, 1);
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezzione in setRCI:{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+
+ ///
+ /// Salva un set KVP (Key Value Pair) in RedisCache
+ ///
+ /// Set KVP chiave-valore da salvare
+ ///
+ public bool setRKeys(KeyValuePair[] valori)
+ {
+ bool answ = false;
+ try
+ {
+ cache.StringSet(valori);
+ answ = true;
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezione in setRKeys:{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+
+ ///
+ /// Salva una chiave in RedisCache
+ ///
+ ///
+ ///
+ ///
+ public bool setRSV(string chiave, string valore)
+ {
+ bool answ = false;
+ try
+ {
+ cache.StringSet(chiave, valore);
+ answ = true;
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezzione in setRSV:{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+
+ ///
+ /// Salva una chiave in RedisCache
+ ///
+ ///
+ ///
+ /// in secondi
+ ///
+ public bool setRSV(string chiave, string valore, int TTL_sec)
+ {
+ bool answ = false;
+ try
+ {
+ TimeSpan expT = new TimeSpan(0, 0, TTL_sec);
+ // salvo con expyry...
+ cache.StringSet(chiave, valore, expT);
+ answ = true;
+ }
+ catch (Exception exc)
+ {
+ Logging.Instance.Error(string.Format("Eccezzione in setRSV:{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+
+ ///
+ /// inserisce in session un DataSet (serializzandolo)
+ ///
+ /// nome della variabile
+ /// DataSet da salvare
+ public bool setSessionDataSet(string nome, DataSet dSet)
+ {
+ bool _done = false;
+ try
+ {
+ string dataSer = JsonConvert.SerializeObject(dSet);
+ _done = setSessionVal(nome, dataSer);
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un DataSet (serializzandolo)
+ ///
+ /// nome della variabile
+ /// DataSet da salvare
+ /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
+ ///
+ public bool setSessionDataSet(string nome, DataSet dSet, bool surviveUpdate)
+ {
+ bool _done = false;
+ try
+ {
+ string dataSer = JsonConvert.SerializeObject(dSet);
+ _done = setSessionVal(nome, dataSer, surviveUpdate);
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un DataSet (serializzandolo)
+ ///
+ /// nome della variabile
+ /// DataTable da salvare
+ public bool setSessionDataTable(string nome, DataTable dTable)
+ {
+ bool _done = false;
+ try
+ {
+ DataSet dataSet = new DataSet("dataSet");
+ dataSet.Tables.Add(dTable);
+ string dataSer = JsonConvert.SerializeObject(dataSet);
+ _done = setSessionVal(nome, dataSer);
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un DataSet (serializzandolo)
+ ///
+ /// nome della variabile
+ /// DataTable da salvare
+ /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
+ public bool setSessionDataTable(string nome, DataTable dTable, bool surviveUpdate)
+ {
+ bool _done = false;
+ try
+ {
+ DataSet dataSet = new DataSet("dataSet");
+ dataSet.Tables.Add(dTable);
+ string dataSer = JsonConvert.SerializeObject(dataSet);
+ _done = setSessionVal(nome, dataSer, surviveUpdate);
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un valore
+ ///
+ ///
+ ///
+ public bool setSessionVal(string nome, object valore)
+ {
+ bool _done = false;
+ try
+ {
+ HttpContext.Current.Session[nome] = valore;
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un valore
+ ///
+ /// nome della variabile
+ /// valore associato
+ /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
+ ///
+ public bool setSessionVal(string nome, object valore, bool surviveUpdate)
+ {
+ bool _done = false;
+ try
+ {
+ HttpContext.Current.Session[nome] = valore;
+ if (surviveUpdate)
+ {
+ addValInSession(nome, valore.ToString());
+ }
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// carica dalla Cachee un dato di tipo string
+ ///
+ ///
+ ///
+ public string StringCacheObj(string nomeVar)
+ {
+ string answ = "";
+ // ...se uso redis...
+ if (cacheOnRedis)
+ {
+ answ = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
+ }
+ else
+ {
+ if (HttpContext.Current.Cache[nomeVar] != null)
+ {
+ answ = HttpContext.Current.Cache[nomeVar].ToString();
+ }
+ else
+ {
+ answ = "";
+ }
+ }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo string
+ ///
+ ///
+ ///
+ public string StringSessionObj(string nomeVar)
+ {
+ string answ = "";
+ try
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ answ = HttpContext.Current.Session[nomeVar].ToString();
+ }
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ #endregion Public Methods
}
-}
+}
\ No newline at end of file
diff --git a/SteamWareLib/memLayer.cs b/SteamWareLib/memLayer.cs
index 84437ef..04ed6b9 100644
--- a/SteamWareLib/memLayer.cs
+++ b/SteamWareLib/memLayer.cs
@@ -15,14 +15,51 @@ namespace SteamWare
///
public class memLayer
{
- #region oggetti protected utilizzati
+ #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;
- #endregion oggetti protected utilizzati
+ ///
+ /// Oggetto MongoDbCLient x accesso al motore
+ ///
+ protected MongoClient currMongoClient;
+
+ #endregion Protected Fields
#region Public Fields
@@ -31,6 +68,11 @@ namespace SteamWare
///
public static memLayer ML = new memLayer();
+ ///
+ /// oggetto dizionario con chiave / valore x configurazioni applicazione
+ ///
+ public Dictionary AppConf;
+
///
/// Table adapter accesso conf parameters
///
@@ -60,6 +102,45 @@ namespace SteamWare
#endregion Protected Constructors
+ #region Public Enums
+
+ ///
+ /// Tipologia di ordinamento x liste KVP
+ ///
+ public enum kvpOrderBy
+ {
+ ///
+ /// Ordinamento ASCending per KEY
+ ///
+ KeyAsc,
+
+ ///
+ /// Ordinamento DESCending per KEY
+ ///
+ KeyDesc,
+
+ ///
+ /// Ordinamento ASCending per VAL
+ ///
+ ValAsc,
+
+ ///
+ /// Ordinamento DESCending per VAL
+ ///
+ ValDesc
+ }
+
+ #endregion Public Enums
+
+ #region Private Properties
+
+ ///
+ /// Oggetto currentDb locale
+ ///
+ private IDatabase _currDB { get; set; }
+
+ #endregion Private Properties
+
#region Protected Properties
///
@@ -85,44 +166,26 @@ namespace SteamWare
}
}
- #endregion Protected Properties
-
///
- /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per impiego di sessioni avanzate come Redis)
+ /// Stringa di connessione mongoDb
///
- public bool serializeSession
+ protected string mongoConnString
{
get
{
- return CRB("serializeSession");
+ string answ = "";
+ answ = confReadString("mdbConnString");
+ if (string.IsNullOrEmpty(answ))
+ {
+ answ = "mongodb://W2019-MONGODB:27017";
+ }
+ return answ;
}
}
- ///
- /// init dei table adapters
- ///
- protected void initTA()
- {
- taConfig = new DS_UtilityTableAdapters.ConfigTableAdapter();
- taConfigTmp = new DS_UtilityTableAdapters.ConfigTmpTableAdapter();
- }
+ #endregion Protected Properties
- ///
- /// 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;
- }
-
- #region area gestione config su DB
-
- ///
- /// oggetto dizionario con chiave / valore x configurazioni applicazione
- ///
- public Dictionary AppConf;
+ #region Public Properties
///
/// Nome della variabile AppConf da utilizzare...
@@ -142,6 +205,88 @@ namespace SteamWare
}
}
+ ///
+ /// 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;
+ }
+ }
+
+ ///
+ /// Indica se usare la cache su REDIS (true) oppure cache applicativo IIS (false)
+ ///
+ public bool cacheOnRedis
+ {
+ get
+ {
+ bool answ = confReadBool("cacheOnRedis");
+ return answ;
+ }
+ }
+
+ ///
+ /// Nome della variabile x indicare che si sta facendo refresh della appConf...
+ ///
+ public string CleaningKey
+ {
+ get
+ {
+ string answ = "RunningACCleaning";
+ try
+ {
+ answ = string.Format("{0}:{1}:{2}:RunningACCleaning", confReadString("CodModulo"), taConfig.Connection.DataSource, taConfig.Connection.Database).Replace("\\", "_");
+ }
+ catch
+ { }
+ return answ;
+ }
+ }
+
+ ///
+ /// Oggetto statico connessione redis
+ ///
+ public ConnectionMultiplexer connRedis
+ {
+ get
+ {
+ return lazyConnection.Value;
+ }
+ }
+
+ ///
+ /// Oggetto statico connessione redis
+ ///
+ public ConnectionMultiplexer connRedisAdmin
+ {
+ get
+ {
+ return lazyConnectionAdmin.Value;
+ }
+ }
+
///
/// Recupera il TTL x appConf (secondi)
///
@@ -181,6 +326,333 @@ namespace SteamWare
}
}
+ ///
+ /// Restituisce numero record in Redis DB
+ ///
+ public long numRecRedis
+ {
+ get
+ {
+ long answ = 0;
+ try
+ {
+ foreach (var ep in connRedis.GetEndPoints())
+ {
+ var server = connRedis.GetServer(ep);
+ answ += server.DatabaseSize();
+ }
+ }
+ catch
+ { }
+ return answ;
+ }
+ }
+
+ ///
+ /// Verifica se si debba serializzare ogni valore complesso (tabelle/righe) in sessione (per impiego di sessioni avanzate come Redis)
+ ///
+ public bool serializeSession
+ {
+ get
+ {
+ return CRB("serializeSession");
+ }
+ }
+
+ ///
+ /// elenco dictionary delle tab in cache da aggiornare con update svuotando da cache...
+ ///
+ public Dictionary tabelleInCache
+ {
+ get
+ {
+ Dictionary answ = new Dictionary();
+ try
+ {
+ var cacheVal = objCacheObj("tabelleInCache");
+ if (cacheVal != null)
+ {
+ answ = JsonConvert.DeserializeObject>(cacheVal.ToString());
+ }
+ }
+ catch (Exception exc)
+ {
+ answ = new Dictionary();
+ logger.lg.scriviLog(string.Format("Errore in tabelleInCache{0}{1}", Environment.NewLine, exc));
+ }
+ return answ;
+ }
+ set
+ {
+ string serVal = JsonConvert.SerializeObject(value);
+ setCacheVal("tabelleInCache", serVal);
+ }
+ }
+
+ ///
+ /// elenco dictionary dei valori in session da NON aggiornare con update...
+ ///
+ public Dictionary valSess2SurvUpd
+ {
+ get
+ {
+ Dictionary answ = new Dictionary();
+ if (isInSessionObject("valoriInSession2Survive"))
+ {
+ try
+ {
+ answ = (Dictionary)objSessionObj("valoriInSession2Survive");
+ }
+ catch
+ {
+ answ = new Dictionary();
+ }
+ }
+ return answ;
+ }
+ set
+ {
+ setSessionVal("valoriInSession2Survive", value);
+ }
+ }
+
+ #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();
+ var tabDati = nML.taConfig.GetData();
+ // carico
+ foreach (DS_Utility.ConfigRow riga in tabDati)
+ {
+ try
+ {
+ answ.Add(riga.chiave, riga.valore);
+ }
+ catch (Exception exc)
+ {
+ logger.lg.scriviLog(string.Format("Errore procedura ricaricaAppConf per kvp: {0} / {1}{2}{3}", riga.chiave, riga.valore, Environment.NewLine, exc), tipoLog.EXCEPTION);
+ }
+ }
+ // log ricarica
+ Logger.Logging.Instance.Info($"Effettuata procedura ricaricaAppConf per {answ.Count} records");
+ 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();
+ 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);
+ Logger.Logging.Instance.Info("Completato procedura startupAppConf");
+ }
+ }
+ catch (Exception exc)
+ {
+ Logger.Logging.Instance.Error($"Errore in startupAppConf:{Environment.NewLine}{exc}");
+ }
+ }
+ }
+
+ #endregion Protected Methods
+
+ #region Public Methods
+
+ ///
+ /// Formattazione stringa URL immagini con gestione "base url"
+ ///
+ ///
+ ///
+ public static string imgUrl(string urlRelPath)
+ {
+ return string.Format("{0}/{1}", SteamWare.memLayer.ML.CRS("baseUrl"), urlRelPath);
+ }
+
+ ///
+ /// 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...
+ Dictionary _tabelleInCache = tabelleInCache;
+ try
+ {
+ _tabelleInCache.Add(nuovaTab, nuovaTab);
+ tabelleInCache = _tabelleInCache;
+ }
+ catch
+ { }
+ }
+
+ ///
+ /// 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...
+ Dictionary _valoriInSession2Survive = valSess2SurvUpd;
+ // verifico se fare update o insert...
+ if (_valoriInSession2Survive.ContainsKey(nomePar))
+ {
+ // update, rimuovo vecchio valore...
+ try
+ {
+ _valoriInSession2Survive.Remove(nomePar);
+ }
+ catch
+ { }
+ }
+ // insert
+ try
+ {
+ _valoriInSession2Survive.Add(nomePar, valore);
+ valSess2SurvUpd = _valoriInSession2Survive;
+ }
+ catch
+ { }
+ }
+
+ ///
+ /// carica dalla Cachee un dato di tipo boolean (se vuoto false)
+ ///
+ ///
+ ///
+ public bool BoolCacheObj(string nomeVar)
+ {
+ bool answ = false;
+ // ...se uso redis...
+ if (cacheOnRedis)
+ {
+ string redVal = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
+ bool.TryParse(redVal, out answ);
+ }
+ else
+ {
+ if (HttpContext.Current.Cache[nomeVar] != null)
+ {
+ answ = (bool)HttpContext.Current.Cache[nomeVar];
+ }
+ else
+ {
+ answ = false;
+ }
+ }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo boolean (se vuoto false)
+ ///
+ ///
+ ///
+ public bool BoolSessionObj(string nomeVar)
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ return (bool)HttpContext.Current.Session[nomeVar];
+ }
+ else
+ {
+ return false;
+ }
+ }
+
///
/// Configurations da tabella DB Config (short form wrapper)
///
@@ -247,6 +719,28 @@ namespace SteamWare
return answ;
}
+ ///
+ /// Effettua comaprazione x VALORE in KVP ASC
+ ///
+ ///
+ ///
+ ///
+ public int CompareVal(KeyValuePair x, KeyValuePair y)
+ {
+ return x.Value.CompareTo(y.Value);
+ }
+
+ ///
+ /// Effettua comaprazione x VALORE in KVP DESC
+ ///
+ ///
+ ///
+ ///
+ public int CompareValDesc(KeyValuePair x, KeyValuePair y)
+ {
+ return y.Value.CompareTo(x.Value);
+ }
+
///
/// Configurations da tabella DB Config
///
@@ -292,93 +786,6 @@ namespace SteamWare
return answ;
}
- ///
- /// resetta AppConfig svuotando e rileggendo i dati...
- ///
- public void resetAppConf()
- {
- redDelKey(ACBH);
- startupAppConf();
- }
-
- ///
- /// 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();
- var tabDati = nML.taConfig.GetData();
- // carico
- foreach (DS_Utility.ConfigRow riga in tabDati)
- {
- try
- {
- answ.Add(riga.chiave, riga.valore);
- }
- catch (Exception exc)
- {
- logger.lg.scriviLog(string.Format("Errore procedura ricaricaAppConf per kvp: {0} / {1}{2}{3}", riga.chiave, riga.valore, Environment.NewLine, exc), tipoLog.EXCEPTION);
- }
- }
- // log ricarica
- Logger.Logging.Instance.Info($"Effettuata procedura ricaricaAppConf per {answ.Count} records");
- return answ;
- }
-
- ///
- /// 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();
- 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);
- Logger.Logging.Instance.Info("Completato procedura startupAppConf");
- }
- }
- catch (Exception exc)
- {
- Logger.Logging.Instance.Error($"Errore in startupAppConf:{Environment.NewLine}{exc}");
- }
- }
- }
-
- #endregion area gestione config su DB
-
- #region utility gestione conf settings
-
///
/// legge dalla config un valore bool
///
@@ -525,104 +932,6 @@ namespace SteamWare
return answ;
}
- #endregion utility gestione conf settings
-
- #region utility gestione querystring, cookie, session e cache
-
- #region querystring
-
- ///
- /// recupera valore querystring BOOL
- ///
- ///
- /// valore string
- public bool QSB(string nome)
- {
- bool answ = false;
- try
- {
- answ = Convert.ToBoolean(HttpContext.Current.Request.QueryString[nome]);
- }
- catch
- { }
- return answ;
- }
-
- ///
- /// recupera valore querystring DATE
- ///
- ///
- /// valore DATE
- public DateTime QSD(string nome)
- {
- DateTime answ = DateTime.Now;
- try
- {
- answ = Convert.ToDateTime(HttpContext.Current.Request.QueryString[nome]);
- }
- catch
- { }
- return answ;
- }
-
- ///
- /// recupera valore querystring INT
- ///
- ///
- /// valore INT
- public int QSI(string nome)
- {
- int answ = 0;
- try
- {
- answ = Convert.ToInt32(HttpContext.Current.Request.QueryString[nome]);
- }
- catch
- { }
- return answ;
- }
-
- ///
- /// recupera valore querystring STRING
- ///
- ///
- /// valore string
- public string QSS(string nome)
- {
- string answ = "";
- if (HttpContext.Current.Request.QueryString[nome] != null)
- {
- try
- {
- answ = HttpContext.Current.Request.QueryString[nome].ToString().Trim();
- }
- catch
- { }
- }
- return answ;
- }
-
- #endregion querystring
-
- #region session
-
- ///
- /// carica dalla sessione un dato di tipo boolean (se vuoto false)
- ///
- ///
- ///
- public bool BoolSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- return (bool)HttpContext.Current.Session[nomeVar];
- }
- else
- {
- return false;
- }
- }
-
///
/// carica dalla sessione un dato di tipo DateTime
///
@@ -646,6 +955,22 @@ namespace SteamWare
return answ;
}
+ ///
+ /// Deserializzazione di un valore string in oggetto generico
+ ///
+ ///
+ ///
+ public object deserializeVal(string serVal)
+ {
+ object answ = "";
+ try
+ {
+ answ = JsonConvert.DeserializeObject(serVal);
+ }
+ catch { }
+ return answ;
+ }
+
///
/// carica dalla sessione un dato di tipo DataSet NON Tipizzato
///
@@ -665,499 +990,6 @@ namespace SteamWare
}
}
- ///
- /// svuota una variabile dalla session
- ///
- ///
- public bool emptySessionVal(string nome)
- {
- bool _done = false;
- try
- {
- HttpContext.Current.Session.Remove(nome);
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// carica dalla sessione un dato di tipo int
- ///
- ///
- ///
- public int IntSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
- }
- else
- {
- return -1;
- }
- }
-
- ///
- /// restituisce true se sia presente in session l'oggetto richiesto
- ///
- ///
- ///
- public bool isInSessionObject(string nomeVar)
- {
- bool answ = false;
- bool stringAnsw = false;
- // cerco se ci sia...
- try
- {
- //stringAnsw = (string)HttpContext.Current.Session[nomeVar].ToString() != "";
- stringAnsw = HttpContext.Current.Session[nomeVar] != null;
- }
- catch
- { }
- // infine condizione doppia...
- try
- {
- answ = (HttpContext.Current.Session[nomeVar] != null && stringAnsw);
- }
- catch
- { }
- return answ;
- }
-
- ///
- /// carica dalla sessione un dato di tipo long
- ///
- ///
- ///
- public long LongSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
- }
- else
- {
- return 0;
- }
- }
-
- ///
- /// carica dalla sessione un dato di tipo object generico
- ///
- ///
- ///
- public object objSessionObj(string nomeVar)
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- return HttpContext.Current.Session[nomeVar];
- }
- else
- {
- return "";
- }
- }
-
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataSet da salvare
- public bool setSessionDataSet(string nome, DataSet dSet)
- {
- bool _done = false;
- try
- {
- string dataSer = JsonConvert.SerializeObject(dSet);
- _done = setSessionVal(nome, dataSer);
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataSet da salvare
- /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
- ///
- public bool setSessionDataSet(string nome, DataSet dSet, bool surviveUpdate)
- {
- bool _done = false;
- try
- {
- string dataSer = JsonConvert.SerializeObject(dSet);
- _done = setSessionVal(nome, dataSer, surviveUpdate);
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataTable da salvare
- public bool setSessionDataTable(string nome, DataTable dTable)
- {
- bool _done = false;
- try
- {
- DataSet dataSet = new DataSet("dataSet");
- dataSet.Tables.Add(dTable);
- string dataSer = JsonConvert.SerializeObject(dataSet);
- _done = setSessionVal(nome, dataSer);
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// inserisce in session un DataSet (serializzandolo)
- ///
- /// nome della variabile
- /// DataTable da salvare
- /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
- public bool setSessionDataTable(string nome, DataTable dTable, bool surviveUpdate)
- {
- bool _done = false;
- try
- {
- DataSet dataSet = new DataSet("dataSet");
- dataSet.Tables.Add(dTable);
- string dataSer = JsonConvert.SerializeObject(dataSet);
- _done = setSessionVal(nome, dataSer, surviveUpdate);
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// inserisce in session un valore
- ///
- ///
- ///
- public bool setSessionVal(string nome, object valore)
- {
- bool _done = false;
- try
- {
- HttpContext.Current.Session[nome] = valore;
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// inserisce in session un valore
- ///
- /// nome della variabile
- /// valore associato
- /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
- ///
- public bool setSessionVal(string nome, object valore, bool surviveUpdate)
- {
- bool _done = false;
- try
- {
- HttpContext.Current.Session[nome] = valore;
- if (surviveUpdate)
- {
- addValInSession(nome, valore.ToString());
- }
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// carica dalla sessione un dato di tipo string
- ///
- ///
- ///
- public string StringSessionObj(string nomeVar)
- {
- string answ = "";
- try
- {
- if (HttpContext.Current.Session[nomeVar] != null)
- {
- answ = HttpContext.Current.Session[nomeVar].ToString();
- }
- }
- catch
- { }
- return answ;
- }
-
- #endregion session
-
- #region cookie
-
- ///
- /// elimina un cookie
- ///
- ///
- public bool emptyCookieVal(string nome)
- {
- bool _done = false;
- HttpCookie aCookie;
- try
- {
- aCookie = new HttpCookie(nome);
- aCookie.Expires = DateTime.Now.AddDays(-1);
- HttpContext.Current.Response.Cookies.Add(aCookie);
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// restituisco un valore da cookie
- ///
- ///
- ///
- public string getCookieVal(string nome)
- {
- string answ = "";
- if (hasCookieVal(nome))
- {
- try
- {
- answ = HttpContext.Current.Request.Cookies[nome].Value;
- }
- catch
- { }
- }
- return answ;
- }
-
- ///
- /// restituisco se ci sia un dato cookie
- ///
- ///
- ///
- public bool hasCookieVal(string nome)
- {
- bool answ = false;
- if (HttpContext.Current.Request.Cookies[nome] != null)
- {
- try
- {
- answ = HttpContext.Current.Request.Cookies[nome].Value != "";
- }
- catch
- { }
- }
- return answ;
- }
-
- ///
- /// salvo un valore come cookie
- ///
- ///
- ///
- ///
- public bool setCookieVal(string nome, string valore)
- {
- bool _done = false;
- try
- {
- HttpCookie newCookie = new HttpCookie(nome, valore);
- HttpContext.Current.Response.AppendCookie(newCookie);
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- ///
- /// salvo un valore come cookie con expiry date esplicita
- ///
- ///
- ///
- ///
- ///
- public bool setCookieVal(string nome, string valore, DateTime expiryDate)
- {
- bool _done = false;
- try
- {
- // rimuovo vecchio cookie
- emptyCookieVal(nome);
- // creo nuovo cookie
- HttpCookie newCookie = new HttpCookie(nome, valore);
- newCookie.Expires = expiryDate;
- HttpContext.Current.Response.AppendCookie(newCookie);
- _done = true;
- }
- catch
- { }
- return _done;
- }
-
- #endregion cookie
-
- #region cache
-
- ///
- /// Indica se usare la cache su REDIS (true) oppure cache applicativo IIS (false)
- ///
- public bool cacheOnRedis
- {
- get
- {
- bool answ = confReadBool("cacheOnRedis");
- return answ;
- }
- }
-
- ///
- /// elenco dictionary delle tab in cache da aggiornare con update svuotando da cache...
- ///
- public Dictionary tabelleInCache
- {
- get
- {
- Dictionary answ = new Dictionary();
- try
- {
- var cacheVal = objCacheObj("tabelleInCache");
- if (cacheVal != null)
- {
- answ = JsonConvert.DeserializeObject>(cacheVal.ToString());
- }
- }
- catch (Exception exc)
- {
- answ = new Dictionary();
- logger.lg.scriviLog(string.Format("Errore in tabelleInCache{0}{1}", Environment.NewLine, exc));
- }
- return answ;
- }
- set
- {
- string serVal = JsonConvert.SerializeObject(value);
- setCacheVal("tabelleInCache", serVal);
- }
- }
-
- ///
- /// elenco dictionary dei valori in session da NON aggiornare con update...
- ///
- public Dictionary valSess2SurvUpd
- {
- get
- {
- Dictionary answ = new Dictionary();
- if (isInSessionObject("valoriInSession2Survive"))
- {
- try
- {
- answ = (Dictionary)objSessionObj("valoriInSession2Survive");
- }
- catch
- {
- answ = new Dictionary();
- }
- }
- return answ;
- }
- set
- {
- setSessionVal("valoriInSession2Survive", value);
- }
- }
-
- ///
- /// 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...
- Dictionary _tabelleInCache = tabelleInCache;
- try
- {
- _tabelleInCache.Add(nuovaTab, nuovaTab);
- tabelleInCache = _tabelleInCache;
- }
- catch
- { }
- }
-
- ///
- /// 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...
- Dictionary _valoriInSession2Survive = valSess2SurvUpd;
- // verifico se fare update o insert...
- if (_valoriInSession2Survive.ContainsKey(nomePar))
- {
- // update, rimuovo vecchio valore...
- try
- {
- _valoriInSession2Survive.Remove(nomePar);
- }
- catch
- { }
- }
- // insert
- try
- {
- _valoriInSession2Survive.Add(nomePar, valore);
- valSess2SurvUpd = _valoriInSession2Survive;
- }
- catch
- { }
- }
-
- ///
- /// carica dalla Cachee un dato di tipo boolean (se vuoto false)
- ///
- ///
- ///
- public bool BoolCacheObj(string nomeVar)
- {
- bool answ = false;
- // ...se uso redis...
- if (cacheOnRedis)
- {
- string redVal = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
- bool.TryParse(redVal, out answ);
- }
- else
- {
- if (HttpContext.Current.Cache[nomeVar] != null)
- {
- answ = (bool)HttpContext.Current.Cache[nomeVar];
- }
- else
- {
- answ = false;
- }
- }
- return answ;
- }
-
///
/// svuota una variabile dalla Cache
///
@@ -1182,6 +1014,43 @@ namespace SteamWare
return _done;
}
+ ///
+ /// elimina un cookie
+ ///
+ ///
+ public bool emptyCookieVal(string nome)
+ {
+ bool _done = false;
+ HttpCookie aCookie;
+ try
+ {
+ aCookie = new HttpCookie(nome);
+ aCookie.Expires = DateTime.Now.AddDays(-1);
+ HttpContext.Current.Response.Cookies.Add(aCookie);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// svuota una variabile dalla session
+ ///
+ ///
+ public bool emptySessionVal(string nome)
+ {
+ bool _done = false;
+ try
+ {
+ HttpContext.Current.Session.Remove(nome);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
///
/// forza lo svuotamento delle tabelle indicate come in cache...
///
@@ -1210,345 +1079,33 @@ namespace SteamWare
}
///
- /// restituisce true se sia presente in cache l'oggetto richiesto
+ /// restituisco un valore da cookie
///
- ///
+ ///
///
- public bool isInCacheObject(string nomeVar)
- {
- bool answ = false;
- bool stringAnsw = false;
- if (cacheOnRedis)
- {
- try
- {
- // cerco come key...
- var redVal = getRSV(redHash(nomeVar));
- answ = !string.IsNullOrEmpty(redVal);
- if (!answ)
- {
- answ = redKeyPresent(redHash(nomeVar));
- // cerco come variabile se NON trovata...
- if (!answ)
- {
- // cerco come hash...
- answ = redHashPresent(redHash(nomeVar));
- }
- }
-#if false
- // cerco come key...
- answ = redKeyPresent(redHash(nomeVar));
- if (!answ)
- {
- // cerco come hash...
- answ = redHashPresent(redHash(nomeVar));
- }
- // cerco come variabile se NON trovata...
- if (!answ)
- {
- var redVal = getRSV(redHash(nomeVar));
- answ = !string.IsNullOrEmpty(redVal);
- }
-#endif
- }
- catch (Exception exc)
- {
- logger.lg.scriviLog(string.Format("Errore in verifica isInCacheObject REDIS per chiave{0}{1}{2}", nomeVar, Environment.NewLine, exc));
- }
- }
- else
- {
- // 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 (Exception exc)
- {
- logger.lg.scriviLog(string.Format("Errore in verifica isInCacheObject per chiave{0}{1}{2}", nomeVar, Environment.NewLine, exc));
- }
- }
- return answ;
- }
-
- ///
- /// carica dalla Cache un dato di tipo object generico
- ///
- ///
- ///
- public object objCacheObj(string nomeVar)
- {
- object answ = null;
- // ...se uso redis...
- if (cacheOnRedis)
- {
- answ = getRSV(redHash(nomeVar));
- }
- else
- {
- if (HttpContext.Current.Cache[nomeVar] != null)
- {
- answ = HttpContext.Current.Cache[nomeVar];
- }
- else
- {
- answ = "";
- }
- }
- return answ;
- }
-
- ///
- /// inserisce in Cache un valore
- ///
- /// nome della variabile
- /// valore
- public bool setCacheVal(string nomeVar, object valore)
- {
- bool _done = false;
- if (cacheOnRedis)
- {
- setRSV(redHash(nomeVar), valore.ToString());
- _done = true;
- }
- else
- {
- try
- {
- HttpContext.Current.Cache[nomeVar] = valore;
- _done = true;
- }
- catch
- { }
- }
- return _done;
- }
-
- ///
- /// inserisce in Cache un valore e su richiesta regitra tra le tab in cache da svuotare on update..
- ///
- /// nome della variabile
- /// valore
- /// da registrare come tabella da svuotare on update
- ///
- public bool setCacheVal(string nome, object valore, bool setInTabInCache)
- {
- bool _done = setCacheVal(nome, valore);
- if (setInTabInCache)
- {
- addTabInCache(nome);
- }
- return _done;
- }
-
- ///
- /// carica dalla Cachee un dato di tipo string
- ///
- ///
- ///
- public string StringCacheObj(string nomeVar)
+ public string getCookieVal(string nome)
{
string answ = "";
- // ...se uso redis...
- if (cacheOnRedis)
+ if (hasCookieVal(nome))
{
- answ = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
- }
- else
- {
- if (HttpContext.Current.Cache[nomeVar] != null)
+ try
{
- answ = HttpContext.Current.Cache[nomeVar].ToString();
- }
- else
- {
- answ = "";
+ answ = HttpContext.Current.Request.Cookies[nome].Value;
}
+ catch
+ { }
}
return answ;
}
- #endregion cache
-
- #endregion utility gestione querystring, cookie, session e cache
-
- #region gestione valori in RedisCache
-
///
- /// Connessione lazy a redis...
+ /// Restituisce oggetto DB richiesto
///
- 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);
- });
-
- ///
- /// Tipologia di ordinamento x liste KVP
- ///
- public enum kvpOrderBy
- {
- ///
- /// Ordinamento ASCending per KEY
- ///
- KeyAsc,
-
- ///
- /// Ordinamento DESCending per KEY
- ///
- KeyDesc,
-
- ///
- /// Ordinamento ASCending per VAL
- ///
- ValAsc,
-
- ///
- /// Ordinamento DESCending per VAL
- ///
- ValDesc
- }
-
- ///
- /// 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;
- }
- }
-
- ///
- /// Oggetto statico connessione redis
- ///
- public ConnectionMultiplexer connRedis
- {
- get
- {
- return lazyConnection.Value;
- }
- }
-
- ///
- /// Oggetto statico connessione redis
- ///
- public ConnectionMultiplexer connRedisAdmin
- {
- get
- {
- return lazyConnectionAdmin.Value;
- }
- }
-
- ///
- /// Restituisce numero record in Redis DB
- ///
- public long numRecRedis
- {
- get
- {
- long answ = 0;
- try
- {
- foreach (var ep in connRedis.GetEndPoints())
- {
- var server = connRedis.GetServer(ep);
- answ += server.DatabaseSize();
- }
- }
- catch
- { }
- return answ;
- }
- }
-
- ///
- /// Oggetto currentDb locale
- ///
- private IDatabase _currDB { get; set; }
-
- ///
- /// Effettua comaprazione x VALORE in KVP ASC
- ///
- ///
- ///
+ ///
///
- public int CompareVal(KeyValuePair x, KeyValuePair y)
+ public IMongoDatabase getMongoDatabase(string dbName)
{
- return x.Value.CompareTo(y.Value);
- }
-
- ///
- /// Effettua comaprazione x VALORE in KVP DESC
- ///
- ///
- ///
- ///
- public int CompareValDesc(KeyValuePair x, KeyValuePair y)
- {
- return y.Value.CompareTo(x.Value);
- }
-
- ///
- /// Deserializzazione di un valore string in oggetto generico
- ///
- ///
- ///
- public object deserializeVal(string serVal)
- {
- object answ = "";
- try
- {
- answ = JsonConvert.DeserializeObject(serVal);
- }
- catch { }
+ IMongoDatabase answ = currMongoClient.GetDatabase(dbName);
return answ;
}
@@ -1633,6 +1190,271 @@ namespace SteamWare
return answ;
}
+ ///
+ /// restituisco se ci sia un dato cookie
+ ///
+ ///
+ ///
+ public bool hasCookieVal(string nome)
+ {
+ bool answ = false;
+ if (HttpContext.Current.Request.Cookies[nome] != null)
+ {
+ try
+ {
+ answ = HttpContext.Current.Request.Cookies[nome].Value != "";
+ }
+ catch
+ { }
+ }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo int
+ ///
+ ///
+ ///
+ public int IntSessionObj(string nomeVar)
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ ///
+ /// restituisce true se sia presente in cache l'oggetto richiesto
+ ///
+ ///
+ ///
+ public bool isInCacheObject(string nomeVar)
+ {
+ bool answ = false;
+ bool stringAnsw = false;
+ if (cacheOnRedis)
+ {
+ try
+ {
+ // cerco come key...
+ var redVal = getRSV(redHash(nomeVar));
+ answ = !string.IsNullOrEmpty(redVal);
+ if (!answ)
+ {
+ answ = redKeyPresent(redHash(nomeVar));
+ // cerco come variabile se NON trovata...
+ if (!answ)
+ {
+ // cerco come hash...
+ answ = redHashPresent(redHash(nomeVar));
+ }
+ }
+#if false
+ // cerco come key...
+ answ = redKeyPresent(redHash(nomeVar));
+ if (!answ)
+ {
+ // cerco come hash...
+ answ = redHashPresent(redHash(nomeVar));
+ }
+ // cerco come variabile se NON trovata...
+ if (!answ)
+ {
+ var redVal = getRSV(redHash(nomeVar));
+ answ = !string.IsNullOrEmpty(redVal);
+ }
+#endif
+ }
+ catch (Exception exc)
+ {
+ logger.lg.scriviLog(string.Format("Errore in verifica isInCacheObject REDIS per chiave{0}{1}{2}", nomeVar, Environment.NewLine, exc));
+ }
+ }
+ else
+ {
+ // 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 (Exception exc)
+ {
+ logger.lg.scriviLog(string.Format("Errore in verifica isInCacheObject per chiave{0}{1}{2}", nomeVar, Environment.NewLine, exc));
+ }
+ }
+ return answ;
+ }
+
+ ///
+ /// restituisce true se sia presente in session l'oggetto richiesto
+ ///
+ ///
+ ///
+ public bool isInSessionObject(string nomeVar)
+ {
+ bool answ = false;
+ bool stringAnsw = false;
+ // cerco se ci sia...
+ try
+ {
+ //stringAnsw = (string)HttpContext.Current.Session[nomeVar].ToString() != "";
+ stringAnsw = HttpContext.Current.Session[nomeVar] != null;
+ }
+ catch
+ { }
+ // infine condizione doppia...
+ try
+ {
+ answ = (HttpContext.Current.Session[nomeVar] != null && stringAnsw);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo long
+ ///
+ ///
+ ///
+ public long LongSessionObj(string nomeVar)
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ return Convert.ToInt32(HttpContext.Current.Session[nomeVar].ToString());
+ }
+ else
+ {
+ return 0;
+ }
+ }
+
+ ///
+ /// carica dalla Cache un dato di tipo object generico
+ ///
+ ///
+ ///
+ public object objCacheObj(string nomeVar)
+ {
+ object answ = null;
+ // ...se uso redis...
+ if (cacheOnRedis)
+ {
+ answ = getRSV(redHash(nomeVar));
+ }
+ else
+ {
+ if (HttpContext.Current.Cache[nomeVar] != null)
+ {
+ answ = HttpContext.Current.Cache[nomeVar];
+ }
+ else
+ {
+ answ = "";
+ }
+ }
+ return answ;
+ }
+
+ ///
+ /// carica dalla sessione un dato di tipo object generico
+ ///
+ ///
+ ///
+ public object objSessionObj(string nomeVar)
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ return HttpContext.Current.Session[nomeVar];
+ }
+ else
+ {
+ return "";
+ }
+ }
+
+ ///
+ /// recupera valore querystring BOOL
+ ///
+ ///
+ /// valore string
+ public bool QSB(string nome)
+ {
+ bool answ = false;
+ try
+ {
+ answ = Convert.ToBoolean(HttpContext.Current.Request.QueryString[nome]);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// recupera valore querystring DATE
+ ///
+ ///
+ /// valore DATE
+ public DateTime QSD(string nome)
+ {
+ DateTime answ = DateTime.Now;
+ try
+ {
+ answ = Convert.ToDateTime(HttpContext.Current.Request.QueryString[nome]);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// recupera valore querystring INT
+ ///
+ ///
+ /// valore INT
+ public int QSI(string nome)
+ {
+ int answ = 0;
+ try
+ {
+ answ = Convert.ToInt32(HttpContext.Current.Request.QueryString[nome]);
+ }
+ catch
+ { }
+ return answ;
+ }
+
+ ///
+ /// recupera valore querystring STRING
+ ///
+ ///
+ /// valore string
+ public string QSS(string nome)
+ {
+ string answ = "";
+ if (HttpContext.Current.Request.QueryString[nome] != null)
+ {
+ try
+ {
+ answ = HttpContext.Current.Request.QueryString[nome].ToString().Trim();
+ }
+ catch
+ { }
+ }
+ return answ;
+ }
+
///
/// Conta num oggetti cache redis che rispondono a pattern
///
@@ -2140,6 +1962,23 @@ namespace SteamWare
return answ;
}
+ ///
+ /// resetta AppConfig svuotando e rileggendo i dati...
+ ///
+ public void resetAppConf()
+ {
+ // controlo chiave x evitare doppio reset
+ if (!string.IsNullOrEmpty(getRSV(CleaningKey)))
+ {
+ //imposto veto 10 sec
+ setRSV(CleaningKey, DateTime.Now.ToString(), 10);
+ redDelKey(ACBH);
+ startupAppConf();
+ // veto 1 sec --> se ne va subito
+ setRSV(CleaningKey, "", 1);
+ }
+ }
+
///
/// Resetta (elimina) un contatore in Redis
///
@@ -2175,6 +2014,94 @@ namespace SteamWare
return answ;
}
+ ///
+ /// inserisce in Cache un valore
+ ///
+ /// nome della variabile
+ /// valore
+ public bool setCacheVal(string nomeVar, object valore)
+ {
+ bool _done = false;
+ if (cacheOnRedis)
+ {
+ setRSV(redHash(nomeVar), valore.ToString());
+ _done = true;
+ }
+ else
+ {
+ try
+ {
+ HttpContext.Current.Cache[nomeVar] = valore;
+ _done = true;
+ }
+ catch
+ { }
+ }
+ return _done;
+ }
+
+ ///
+ /// inserisce in Cache un valore e su richiesta regitra tra le tab in cache da svuotare on update..
+ ///
+ /// nome della variabile
+ /// valore
+ /// da registrare come tabella da svuotare on update
+ ///
+ public bool setCacheVal(string nome, object valore, bool setInTabInCache)
+ {
+ bool _done = setCacheVal(nome, valore);
+ if (setInTabInCache)
+ {
+ addTabInCache(nome);
+ }
+ return _done;
+ }
+
+ ///
+ /// salvo un valore come cookie
+ ///
+ ///
+ ///
+ ///
+ public bool setCookieVal(string nome, string valore)
+ {
+ bool _done = false;
+ try
+ {
+ HttpCookie newCookie = new HttpCookie(nome, valore);
+ HttpContext.Current.Response.AppendCookie(newCookie);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// salvo un valore come cookie con expiry date esplicita
+ ///
+ ///
+ ///
+ ///
+ ///
+ public bool setCookieVal(string nome, string valore, DateTime expiryDate)
+ {
+ bool _done = false;
+ try
+ {
+ // rimuovo vecchio cookie
+ emptyCookieVal(nome);
+ // creo nuovo cookie
+ HttpCookie newCookie = new HttpCookie(nome, valore);
+ newCookie.Expires = expiryDate;
+ HttpContext.Current.Response.AppendCookie(newCookie);
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
///
/// Decrementa un contatore in Redis
///
@@ -2279,86 +2206,173 @@ namespace SteamWare
}
///
- /// Effettua comaprazione x CHIAVE in KVP ASC
+ /// inserisce in session un DataSet (serializzandolo)
///
- ///
- ///
- ///
- private int CompareKey(KeyValuePair x, KeyValuePair y)
+ /// nome della variabile
+ /// DataSet da salvare
+ public bool setSessionDataSet(string nome, DataSet dSet)
{
- 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);
- }
-
- #endregion gestione valori in RedisCache
-
- #region URL corretti immagini
-
- ///
- /// Formattazione stringa URL immagini con gestione "base url"
- ///
- ///
- ///
- public static string imgUrl(string urlRelPath)
- {
- return string.Format("{0}/{1}", SteamWare.memLayer.ML.CRS("baseUrl"), urlRelPath);
- }
-
- #endregion URL corretti immagini
-
- #region gestione mongoDb
-
- ///
- /// Oggetto MongoDbCLient x accesso al motore
- ///
- protected MongoClient currMongoClient;
-
- ///
- /// Stringa di connessione mongoDb
- ///
- protected string mongoConnString
- {
- get
+ bool _done = false;
+ try
{
- string answ = "";
- answ = confReadString("mdbConnString");
- if (string.IsNullOrEmpty(answ))
- {
- answ = "mongodb://W2019-MONGODB:27017";
- }
- return answ;
+ string dataSer = JsonConvert.SerializeObject(dSet);
+ _done = setSessionVal(nome, dataSer);
}
+ catch
+ { }
+ return _done;
}
///
- /// Restituisce oggetto DB richiesto
+ /// inserisce in session un DataSet (serializzandolo)
///
- ///
+ /// nome della variabile
+ /// DataSet da salvare
+ /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
///
- public IMongoDatabase getMongoDatabase(string dbName)
+ public bool setSessionDataSet(string nome, DataSet dSet, bool surviveUpdate)
{
- IMongoDatabase answ = currMongoClient.GetDatabase(dbName);
+ bool _done = false;
+ try
+ {
+ string dataSer = JsonConvert.SerializeObject(dSet);
+ _done = setSessionVal(nome, dataSer, surviveUpdate);
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un DataSet (serializzandolo)
+ ///
+ /// nome della variabile
+ /// DataTable da salvare
+ public bool setSessionDataTable(string nome, DataTable dTable)
+ {
+ bool _done = false;
+ try
+ {
+ DataSet dataSet = new DataSet("dataSet");
+ dataSet.Tables.Add(dTable);
+ string dataSer = JsonConvert.SerializeObject(dataSet);
+ _done = setSessionVal(nome, dataSer);
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un DataSet (serializzandolo)
+ ///
+ /// nome della variabile
+ /// DataTable da salvare
+ /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
+ public bool setSessionDataTable(string nome, DataTable dTable, bool surviveUpdate)
+ {
+ bool _done = false;
+ try
+ {
+ DataSet dataSet = new DataSet("dataSet");
+ dataSet.Tables.Add(dTable);
+ string dataSer = JsonConvert.SerializeObject(dataSet);
+ _done = setSessionVal(nome, dataSer, surviveUpdate);
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un valore
+ ///
+ ///
+ ///
+ public bool setSessionVal(string nome, object valore)
+ {
+ bool _done = false;
+ try
+ {
+ HttpContext.Current.Session[nome] = valore;
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// inserisce in session un valore
+ ///
+ /// nome della variabile
+ /// valore associato
+ /// indica se debba sopravvivere ad update (inserita in elenco valSess2SurvUpd)
+ ///
+ public bool setSessionVal(string nome, object valore, bool surviveUpdate)
+ {
+ bool _done = false;
+ try
+ {
+ HttpContext.Current.Session[nome] = valore;
+ if (surviveUpdate)
+ {
+ addValInSession(nome, valore.ToString());
+ }
+ _done = true;
+ }
+ catch
+ { }
+ return _done;
+ }
+
+ ///
+ /// carica dalla Cachee un dato di tipo string
+ ///
+ ///
+ ///
+ public string StringCacheObj(string nomeVar)
+ {
+ string answ = "";
+ // ...se uso redis...
+ if (cacheOnRedis)
+ {
+ answ = JsonConvert.DeserializeObject(getRSV(redHash(nomeVar))).ToString();
+ }
+ else
+ {
+ if (HttpContext.Current.Cache[nomeVar] != null)
+ {
+ answ = HttpContext.Current.Cache[nomeVar].ToString();
+ }
+ else
+ {
+ answ = "";
+ }
+ }
return answ;
}
///
- /// Init accesso MongoDb
+ /// carica dalla sessione un dato di tipo string
///
- private void setupMongo()
+ ///
+ ///
+ public string StringSessionObj(string nomeVar)
{
- currMongoClient = new MongoClient(mongoConnString);
+ string answ = "";
+ try
+ {
+ if (HttpContext.Current.Session[nomeVar] != null)
+ {
+ answ = HttpContext.Current.Session[nomeVar].ToString();
+ }
+ }
+ catch
+ { }
+ return answ;
}
- #endregion gestione mongoDb
+ #endregion Public Methods
}
}
\ No newline at end of file