Update massivo alal gestione CACHE in REDIS

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