Ancora ottimizzazione async/await verso redis

This commit is contained in:
Samuele Locatelli
2026-04-18 15:24:53 +02:00
parent 4c117d274a
commit 7e0f73d9d2
5 changed files with 53 additions and 144 deletions
+49 -140
View File
@@ -1748,6 +1748,38 @@ namespace MP.IOC.Data
return val != null && (val == "1" || val.ToLower() == "true");
}
/// <summary>
/// Restituisce il valore booleano se la macchina sia abilitata all'inserimento COMPLETO nel
/// Signal Log
/// </summary>
/// <param name="idxMacchina"></param>
/// <returns></returns>
public async Task<bool> IobSLogEnabAsync(string idxMacchina)
{
bool answ = false;
// ORA recupero da memoria redis...
try
{
var currHash = Utils.RedKeyDatiMacc(idxMacchina, MpIoNS);
RedisValue rawData = await redisDb.HashGetAsync(currHash, (RedisValue)"sLogEnabled");
// se è vuoto... leggo da DB e popolo!
if (!rawData.HasValue)
{
await ResetDatiMacchinaAsync(idxMacchina);
// riprovo
rawData = await redisDb.HashGetAsync(currHash, (RedisValue)"sLogEnabled");
}
// provo conversione
bool.TryParse($"{rawData}", out answ);
}
catch (Exception exc)
{
Log.Error($"Errore IobSLogEnabAsync | idxMacchina {idxMacchina}:{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// </summary>
/// <param name="IdxOdl">idxMacc odl da cercare</param>
@@ -3114,7 +3146,7 @@ namespace MP.IOC.Data
if (idxMacchina != "" && valore != "")
{
// se abilitato registro evento sul DB
if (!string.IsNullOrEmpty(idxMacchina) && await sLogEnabAsync(idxMacchina))
if (!string.IsNullOrEmpty(idxMacchina) && await IobSLogEnabAsync(idxMacchina))
{
int cntVal = 0;
int.TryParse(contatore, out cntVal);
@@ -3400,30 +3432,6 @@ namespace MP.IOC.Data
return answ;
}
/// <summary>
/// Esegue eliminazione memoria redis keyVal
/// </summary>
/// <param name="keyVal"></param>
/// <returns></returns>
public async Task<bool> RedisDelKeyAsync(string keyVal)
{
return await RedisDelKeyAsync((RedisKey)keyVal);
#if false
bool answ = false;
var listEndpoints = redisConnAdmin.GetEndPoints();
foreach (var endPoint in listEndpoints)
{
var server = redisConnAdmin.GetServer(endPoint);
if (server != null)
{
await redisDb.KeyDeleteAsync((RedisKey)keyVal);
answ = true;
}
}
return answ;
#endif
}
/// <summary>
/// Esegue eliminazione memoria redis keyVal
/// </summary>
@@ -3530,7 +3538,6 @@ namespace MP.IOC.Data
return await redisDb.KeyExistsAsync(key);
}
public void RedisSetHash(RedisKey redKey, KeyValuePair<string, string>[] valori, double expireSeconds = -1.0)
{
HashEntry[] redHash = valori.Select(x => new HashEntry(x.Key, x.Value)).ToArray();
@@ -3704,7 +3711,7 @@ namespace MP.IOC.Data
public KeyValuePair<string, string>[] resetMSMI(string idxMacchina)
{
var currHash = Utils.RedKeyMsmi(idxMacchina);
//currTask = RedisGetHash(currHash);
//currTask = RedisGetHash(redKey);
// recupero records
var tabMSMI = IocDbController.VMSFDGetMultiByMacc(idxMacchina);
@@ -4131,38 +4138,6 @@ namespace MP.IOC.Data
return answ;
}
/// <summary>
/// Restituisce il valore booleano se la macchina sia abilitata all'inserimento COMPLETO nel
/// Signal Log
/// </summary>
/// <param name="idxMacchina"></param>
/// <returns></returns>
public async Task<bool> sLogEnabAsync(string idxMacchina)
{
bool answ = false;
// ORA recupero da memoria redis...
try
{
var currHash = Utils.RedKeyDatiMacc(idxMacchina, MpIoNS);
RedisValue rawData = await redisDb.HashGetAsync(currHash, (RedisValue)"sLogEnabled");
// se è vuoto... leggo da DB e popolo!
if (!rawData.HasValue)
{
await ResetDatiMacchinaAsync(idxMacchina);
// riprovo
rawData = await redisDb.HashGetAsync(currHash, (RedisValue)"sLogEnabled");
}
// provo conversione
bool.TryParse($"{rawData}", out answ);
}
catch (Exception exc)
{
Log.Error($"Errore sLogEnabAsync | idxMacchina {idxMacchina}:{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// Restitusice elenco KVP dei campi della State Machine ingressi nel formato
/// currKey: cState_nVal (current MICRO-STATE + "_" + new Value)
@@ -4193,6 +4168,7 @@ namespace MP.IOC.Data
}
return answ;
}
/// <summary>
/// Restitusice elenco KVP dei campi della State Machine ingressi nel formato
/// currKey: cState_nVal (current MICRO-STATE + "_" + new Value)
@@ -4794,76 +4770,6 @@ namespace MP.IOC.Data
await RedisFlushPatternAsync(pattern);
}
/// <summary>
/// Restitusice elenco KVP dei campi DatiMacchine + StatoMacchine per l'impianto indicato
/// </summary>
/// <param name="idxMacc"></param>
/// <returns></returns>
private Dictionary<string, string> ResetDatiMacchina(string idxMacc)
{
var currHash = Utils.RedKeyDatiMacc(idxMacc, MpIoNS);
// inizio con un bel reset...
RedisFlushPattern($"{currHash}");
Dictionary<string, string>? result = new Dictionary<string, string>();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string readType = "DB";
var dbResults = IocDbController.VMSFDGetByMacc(idxMacc);
double numSecCache = redisLongTimeCache;
// converto in formato dizionario...
if (dbResults != null && dbResults.Count > 0)
{
var rowResult = dbResults[0];
// salvo 1:1 i valori... STATO
result.Add("IdxMicroStato", $"{rowResult.IdxMicroStato}");
result.Add("IdxStato", $"{rowResult.IdxStato}");
result.Add("CodArticolo", $"{rowResult.CodArticolo}");
result.Add("insEnabled", $"{rowResult.InsEnabled}");
result.Add("sLogEnabled", $"{rowResult.SLogEnabled}");
result.Add("pallet", $"{rowResult.Pallet}");
result.Add("CodArticolo_A", $"{rowResult.CodArticoloA}");
result.Add("CodArticolo_B", $"{rowResult.CodArticoloB}");
result.Add("TempoCicloBase", $"{rowResult.TempoCicloBase}");
result.Add("PzPalletProd", $"{rowResult.PzPalletProd}");
result.Add("MatrOpr", $"{rowResult.MatrOpr}");
result.Add("lastVal", $"{rowResult.LastVal}");
result.Add("TCBase", $"{rowResult.TempoCicloBase}");
//...e SETUP
result.Add("CodMacc", $"{rowResult.Codmacchina}");
result.Add("IdxFamIn", $"{rowResult.IdxFamigliaIngresso}");
result.Add("Multi", $"{rowResult.Multi}");
result.Add("BitFilt", $"{rowResult.BitFilt}");
result.Add("MaxVal", $"{rowResult.MaxVal}");
result.Add("BSR", $"{rowResult.Bsr}");
result.Add("ExplodeBit", $"{rowResult.ExplodeBit}");
result.Add("NumBit", $"{rowResult.NumBit}");
result.Add("IdxFamMacc", $"{rowResult.IdxFamiglia}");
result.Add("simplePallet", $"{rowResult.SimplePallet}");
result.Add("palletChange", $"{rowResult.PalletChange}");
//double numSecCache = ((result["insEnabled"].ToLower() == "true") ? redisShortTimeCache : redisLongTimeCache);
numSecCache = rowResult.InsEnabled ? redisShortTimeCache : redisLongTimeCache;
}
// cerco info Master/slave...
var m2sTab = Macchine2SlaveGetAll();
string isMaster = m2sTab.Where(x => x.IdxMacchina == idxMacc).Count() > 0 ? "1" : "0";
string isSlave = m2sTab.Where(x => x.IdxMacchinaSlave == idxMacc).Count() > 0 ? "1" : "0";
result.Add("Master", isMaster);
result.Add("Slave", isSlave);
// ...e salvo...
RedisSetHashDict(currHash, result, numSecCache);
if (result == null)
{
result = new Dictionary<string, string>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"GetCurrMSFDMacc | Read from {readType}: {ts.TotalMilliseconds}ms");
return result;
}
/// <summary>
/// Restitusice elenco KVP dei campi DatiMacchine + StatoMacchine per l'impianto indicato
/// </summary>
@@ -4871,11 +4777,7 @@ namespace MP.IOC.Data
/// <returns></returns>
private async Task<Dictionary<string, string>> ResetDatiMacchinaAsync(string idxMacc)
{
var currHash = Utils.RedKeyDatiMacc(idxMacc, MpIoNS);
RedisValue pattern = (RedisValue)$"{currHash}";
// inizio con un bel reset...
await RedisFlushPatternAsync(pattern);
Dictionary<string, string>? result = new Dictionary<string, string>();
Dictionary<string, string> result = new Dictionary<string, string>();
var dbResult = await IocDbController.VMSFDGetByMaccAsync(idxMacc);
double numSecCache = redisLongTimeCache;
@@ -4919,13 +4821,19 @@ namespace MP.IOC.Data
result.Add("Master", isMaster);
result.Add("Slave", isSlave);
// elimino valore precedente...
var redKey = Utils.RedKeyDatiMacc(idxMacc, MpIoNS);
#if false
await RedisDelKeyAsync(redKey);
// ...e salvo...
await RedisSetHashDictAsync(currHash, result, numSecCache);
await RedisSetHashDictAsync(redKey, result, numSecCache);
#endif
// Creiamo i task senza attenderli immediatamente
var delTask = RedisDelKeyAsync(redKey);
var setTask = RedisSetHashDictAsync(redKey, result, numSecCache);
// Attendiamo entrambi i task contemporaneamente (1 solo RTT percepito)
await Task.WhenAll(delTask, setTask);
if (result == null)
{
result = new Dictionary<string, string>();
}
return result;
}
@@ -4965,6 +4873,7 @@ namespace MP.IOC.Data
RedisSetHash(currHash, answ, tOut);
return answ;
}
/// <summary>
/// Resetta (rileggendo) i dati della State Machine ingressi nel formato
/// currKey: cState_nVal (current MICRO-STATE + "_" + new Value)
@@ -5027,7 +4936,7 @@ namespace MP.IOC.Data
answ.outValue = inserito.ToString();
answ.needStatusRefresh = true;
return answ;
}
}
#endif
/// <summary>
+1 -1
View File
@@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>6.16.2604.1814</Version>
<Version>6.16.2604.1815</Version>
</PropertyGroup>
<ItemGroup>
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>Modulo MP-IOC </i>
<h4>Versione: 6.16.2604.1814</h4>
<h4>Versione: 6.16.2604.1815</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
6.16.2604.1814
6.16.2604.1815
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>6.16.2604.1814</version>
<version>6.16.2604.1815</version>
<url>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>