Ancora ottimizzazioni minori IOC x metodi + frequenti
This commit is contained in:
@@ -117,7 +117,7 @@ namespace MP.Data.Services.IOC
|
||||
result = await GetCurrOdlByProdAsync(idxMacchina);
|
||||
_redisDb.StringSet(currKey, result, GetRandTOut(redisShortTimeCache));
|
||||
}
|
||||
return result;
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -138,16 +138,37 @@ namespace MP.Data.Services.IOC
|
||||
fetchFunc: async () =>
|
||||
{
|
||||
var rKey = MP.Data.Utils.RedKeyDatiMacc(idxMacchina, MpIoNS);
|
||||
var val = await _redisDb.HashGetAsync(rKey, "insEnabled");
|
||||
|
||||
string? val = await _redisDb.HashGetAsync(rKey, "insEnabled");
|
||||
|
||||
if (val == null)
|
||||
if (!val.HasValue)
|
||||
{
|
||||
var data = await ResetDatiMacchinaAsync(idxMacchina);
|
||||
data.TryGetValue("insEnabled", out val);
|
||||
// 2. Uso del pattern matching per evitare allocazioni e passaggi intermedi
|
||||
return data != null
|
||||
&& data.TryGetValue("insEnabled", out string? sVal)
|
||||
&& IsStringTrue(sVal);
|
||||
}
|
||||
|
||||
return val != null && (val == "1" || val.ToLower() == "true");
|
||||
// 3. Conversione efficiente da RedisValue a string (evita l'interpolazione $"{val}")
|
||||
string? sRedisVal = val;
|
||||
return IsStringTrue(sRedisVal);
|
||||
|
||||
#if false
|
||||
var rKey = MP.Data.Utils.RedKeyDatiMacc(idxMacchina, MpIoNS);
|
||||
var val = await _redisDb.HashGetAsync(rKey, "insEnabled");
|
||||
string sVal = "";
|
||||
if (!val.HasValue)
|
||||
{
|
||||
var data = await ResetDatiMacchinaAsync(idxMacchina);
|
||||
data.TryGetValue("insEnabled", out sVal);
|
||||
}
|
||||
else
|
||||
{
|
||||
sVal = $"{val}";
|
||||
}
|
||||
|
||||
return !string.IsNullOrEmpty(sVal) && (sVal == "1" || sVal.ToLower() == "true");
|
||||
#endif
|
||||
},
|
||||
expiration: GetRandTOut(30),
|
||||
tagList: ["IOC_IobInsEnab", cKey, idxMacchina]
|
||||
@@ -303,30 +324,14 @@ namespace MP.Data.Services.IOC
|
||||
|
||||
#endregion Protected Fields
|
||||
|
||||
#if false
|
||||
/// <summary>
|
||||
/// Restituisce un timeout dai minuti richiesti + tempo random 1..60 sec
|
||||
/// </summary>
|
||||
/// <param name="stdMinutes"></param>
|
||||
/// <returns></returns>
|
||||
protected TimeSpan GetRandTOut(double stdMinutes)
|
||||
{
|
||||
double rndValue = stdMinutes + (double)rand.Next(1, 60) / 60;
|
||||
return TimeSpan.FromMinutes(rndValue);
|
||||
}
|
||||
#endif
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
#if false
|
||||
private readonly IFusionCache _cache;
|
||||
#endif
|
||||
|
||||
private readonly string _className;
|
||||
|
||||
private readonly IIocRepository _repo;
|
||||
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
/// <summary>
|
||||
@@ -341,14 +346,38 @@ namespace MP.Data.Services.IOC
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static bool IsStringTrue(string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) return false;
|
||||
|
||||
// Evita ToLower() che alloca una nuova stringa in memoria ad ogni chiamata
|
||||
return value == "1"
|
||||
|| value.Equals("true", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
#if false
|
||||
/// <summary>
|
||||
/// Restituisce un timeout dai minuti richiesti + tempo random 1..60 sec
|
||||
/// </summary>
|
||||
/// <param name="stdMinutes"></param>
|
||||
/// <returns></returns>
|
||||
protected TimeSpan GetRandTOut(double stdMinutes)
|
||||
{
|
||||
double rndValue = stdMinutes + (double)rand.Next(1, 60) / 60;
|
||||
return TimeSpan.FromMinutes(rndValue);
|
||||
}
|
||||
#endif
|
||||
#if false
|
||||
private readonly IFusionCache _cache;
|
||||
#endif
|
||||
#if false
|
||||
private int redisLongTimeCache = 5;
|
||||
|
||||
private int redisShortTimeCache = 2;
|
||||
#endif
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// controlla se da il segnale di "microstato" deriva un evento da generare - modalità OFFLINE
|
||||
/// </summary>
|
||||
@@ -1070,7 +1099,7 @@ namespace MP.Data.Services.IOC
|
||||
{
|
||||
result = new StatoProdModel();
|
||||
}
|
||||
return result;
|
||||
return result;
|
||||
#endif
|
||||
return await _repo.StatoProdMacchinaAsync(idxMacchina, dtReq);
|
||||
},
|
||||
|
||||
@@ -75,39 +75,42 @@ namespace MP.IOC.Controllers
|
||||
return Ok(answ);
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// GET: IOB/
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//[HttpGet]
|
||||
//public string Alive()
|
||||
//{
|
||||
// return "OK"; // Restituisce Status 200
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// GET: IOB/enabled/SIMUL_03
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("enabled/{id}")]
|
||||
public async Task<IActionResult> Enabled(string id)
|
||||
public async Task<string> Enabled(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id)) return BadRequest("Missing ID");
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
return "Missing ID";
|
||||
//return BadRequest("Missing ID");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Il metodo ora restituisce direttamente il booleano logico
|
||||
bool isEnabled = await IOCService.IobInsEnabAsync(id);
|
||||
|
||||
return isEnabled
|
||||
? Ok("OK")
|
||||
: UnprocessableEntity("NO");
|
||||
// Eliminazione delle allocazioni di stringhe e oggetti inutili
|
||||
if (!isEnabled)
|
||||
{
|
||||
Response.StatusCode = StatusCodes.Status422UnprocessableEntity;
|
||||
return "NO";
|
||||
}
|
||||
|
||||
// Status 200 di default, scrive direttamente sul body della response
|
||||
return "OK";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Errore durante la verifica abilitazione per {Id}", id);
|
||||
return StatusCode(500, "Errore interno del server");
|
||||
Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
return "Errore interno del server";
|
||||
//return StatusCode(500, "Errore interno del server");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Infrastructure": "Warning",
|
||||
"Microsoft.WebTools.BrowserLink.Net.BrowserLinkMiddleware": "None",
|
||||
"Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware": "None"
|
||||
"Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware": "None",
|
||||
"ZiggyCreatures.Caching.Fusion": "Warning"
|
||||
}
|
||||
},
|
||||
"NLog": {
|
||||
|
||||
Reference in New Issue
Block a user