diff --git a/MP.Data/Services/IOC/IocService.cs b/MP.Data/Services/IOC/IocService.cs index f8b03f89..711b3da9 100644 --- a/MP.Data/Services/IOC/IocService.cs +++ b/MP.Data/Services/IOC/IocService.cs @@ -181,9 +181,10 @@ namespace MP.Data.Services.IOC private readonly IIocRepository _repo; private readonly IServiceScopeFactory _scopeFactory; private readonly Microsoft.Extensions.Caching.Memory.IMemoryCache _cache; + private readonly System.Threading.SemaphoreSlim _semaphore = new System.Threading.SemaphoreSlim(1, 1); /// - /// Provider CultureInfo x parse valori (es dataora) + /// Provider CultureInfo x parse valori(es dataora) /// private CultureInfo ciProvider = CultureInfo.InvariantCulture; @@ -297,32 +298,34 @@ namespace MP.Data.Services.IOC /// /// Elenco completo config da DB /// - /// private async Task> ConfigGetAllAsync() { - List? result = new List(); - // cerco in redis... - RedisValue rawData = await _redisDb.StringGetAsync(MP.Data.Utils.redisConfKey); - string source = "DB"; - if (!string.IsNullOrEmpty($"{rawData}")) + return await GetOrFetchAsync("IOC_ConfigAll", async () => { - result = JsonConvert.DeserializeObject>($"{rawData}"); - source = "REDIS"; - } - else - { - result = await _repo.ConfigGetAllAsync(); - //result = await Task.FromResult(SpecDbController.ConfigGetAllAsync()); - // serializzo e salvo... - rawData = JsonConvert.SerializeObject(result); - await _redisDb.StringSetAsync(MP.Data.Utils.redisConfKey, rawData, getRandTOut(redisLongTimeCache)); - } - Log.Debug($"ConfigGetAllAsync Read from {source}"); - if (result == null) - { - result = new List(); - } - return result; + List? result = new List(); + // cerco in redis... + RedisValue rawData = await _redisDb.StringGetAsync(MP.Data.Utils.redisConfKey); + string source = "DB"; + if (!string.IsNullOrEmpty($"{rawData}")) + { + result = JsonConvert.DeserializeObject>($"{rawData}"); + source = "REDIS"; + } + else + { + result = await _repo.ConfigGetAllAsync(); + //result = await Task.FromResult(SpecDbController.ConfigGetAllAsync()); + // serializzo e salvo... + rawData = JsonConvert.SerializeObject(result); + await _redisDb.StringSetAsync(MP.Data.Utils.redisConfKey, rawData, getRandTOut(redisLongTimeCache)); + } + Log.Debug($"ConfigGetAllAsync Read from {source}"); + if (result == null) + { + result = new List(); + } + return result; + }, TimeSpan.FromMinutes(10)); } /// @@ -388,60 +391,81 @@ namespace MP.Data.Services.IOC return answ; } + /// + /// Implementa gestione recupero cache da memoria o da obj esterno + cache memoria + /// + /// + /// + /// + /// + /// + private async Task GetOrFetchAsync(string cacheKey, Func> fetchFunc, TimeSpan expiration) + { + if (_cache.TryGetValue(cacheKey, out T? cachedValue)) + { + return cachedValue!; + } + + await _semaphore.WaitAsync(); + try + { + if (_cache.TryGetValue(cacheKey, out cachedValue)) + { + return cachedValue!; + } + + T newValue = await fetchFunc(); + _cache.Set(cacheKey, newValue, expiration); + return newValue; + } + finally + { + _semaphore.Release(); + } + } + private async Task> ListMasterAsync() { - const string cacheKey = "IOC_ListMaster"; - if (_cache.TryGetValue(cacheKey, out HashSet? cachedList)) + return await GetOrFetchAsync("IOC_ListMaster", async () => { - return cachedList!; - } - - HashSet result = new(); - string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListMaster"; - RedisValue rawData = await _redisDb.StringGetAsync(currKey); - if (rawData.HasValue) - { - result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); - } - else - { - var fullList = await Macchine2SlaveGetAllAsync(); - result = fullList.Select(x => x.IdxMacchina).ToHashSet(); - // serializzo e salvo... - rawData = JsonConvert.SerializeObject(result); - await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10)); - } - - _cache.Set(cacheKey, result, TimeSpan.FromMinutes(5)); - return result; + HashSet result = new(); + string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListMaster"; + RedisValue rawData = await _redisDb.StringGetAsync(currKey); + if (rawData.HasValue) + { + result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); + } + else + { + var fullList = await Macchine2SlaveGetAllAsync(); + result = fullList.Select(x => x.IdxMacchina).ToHashSet(); + rawData = JsonConvert.SerializeObject(result); + await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10)); + } + return result; + }, TimeSpan.FromMinutes(5)); } private async Task> ListSlaveAsync() { - const string cacheKey = "IOC_ListSlave"; - if (_cache.TryGetValue(cacheKey, out HashSet? cachedList)) + return await GetOrFetchAsync("IOC_ListSlave", async () => { - return cachedList!; - } - - HashSet result = new(); - string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListSlave"; - RedisValue rawData = await _redisDb.StringGetAsync(currKey); - if (rawData.HasValue) - { - result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); - } - else - { - var fullList = await Macchine2SlaveGetAllAsync(); - result = fullList.Select(x => x.IdxMacchinaSlave).Distinct().ToHashSet(); - // serializzo e salvo... - rawData = JsonConvert.SerializeObject(result); - await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10)); - } - - _cache.Set(cacheKey, result, TimeSpan.FromMinutes(5)); - return result; + HashSet result = new(); + string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListSlave"; + RedisValue rawData = await _redisDb.StringGetAsync(currKey); + if (rawData.HasValue) + { + result = JsonConvert.DeserializeObject>($"{rawData}") ?? new(); + } + else + { + var fullList = await Macchine2SlaveGetAllAsync(); + result = fullList.Select(x => x.IdxMacchinaSlave).Distinct().ToHashSet(); + rawData = JsonConvert.SerializeObject(result); + await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10)); + } + return result; + }, TimeSpan.FromMinutes(5)); } /// diff --git a/MP.IOC/Controllers/IOBController.cs b/MP.IOC/Controllers/IOBController.cs index 7df179a4..b30d13ce 100644 --- a/MP.IOC/Controllers/IOBController.cs +++ b/MP.IOC/Controllers/IOBController.cs @@ -70,7 +70,7 @@ namespace MP.IOC.Controllers } catch (Exception exc) { - Log.Error($"Errore in GetTask2Exe{Environment.NewLine}{exc}"); + Log.Error($"Errore in AddTask2Exe{Environment.NewLine}{exc}"); return StatusCode(StatusCodes.Status500InternalServerError, "NO"); } return Ok(answ); @@ -100,7 +100,6 @@ namespace MP.IOC.Controllers { // Il metodo ora restituisce direttamente il booleano logico bool isEnabled = await IOCService.IobInsEnabAsync(id); - //bool isEnabled = await DService.IobInsEnabAsync(id); return isEnabled ? Ok("OK") diff --git a/MP.IOC/MP.IOC.csproj b/MP.IOC/MP.IOC.csproj index b1c6362a..f2baf173 100644 --- a/MP.IOC/MP.IOC.csproj +++ b/MP.IOC/MP.IOC.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 8.16.2605.607 + 8.16.2605.609 diff --git a/MP.IOC/Program.cs b/MP.IOC/Program.cs index 207578c7..c0ccbfda 100644 --- a/MP.IOC/Program.cs +++ b/MP.IOC/Program.cs @@ -67,6 +67,8 @@ logger.Info("RedisScript Provider configured"); var connStr = builder.Configuration.GetConnectionString("MP.Data") ?? throw new InvalidOperationException("ConnString 'MP.Data' mancante."); +builder.Services.AddMemoryCache(); + builder.Services.AddDbContextFactory(options => options.UseSqlServer(connStr) .EnableSensitiveDataLogging(false) // true solo in Sviluppo diff --git a/MP.IOC/Resources/ChangeLog.html b/MP.IOC/Resources/ChangeLog.html index 7c872463..8e8d8134 100644 --- a/MP.IOC/Resources/ChangeLog.html +++ b/MP.IOC/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo MP-IOC -

Versione: 8.16.2605.607

+

Versione: 8.16.2605.609


Note di rilascio:
  • diff --git a/MP.IOC/Resources/VersNum.txt b/MP.IOC/Resources/VersNum.txt index b78c4978..0a727783 100644 --- a/MP.IOC/Resources/VersNum.txt +++ b/MP.IOC/Resources/VersNum.txt @@ -1 +1 @@ -8.16.2605.607 +8.16.2605.609 diff --git a/MP.IOC/Resources/manifest.xml b/MP.IOC/Resources/manifest.xml index 97a76db6..0498bfc7 100644 --- a/MP.IOC/Resources/manifest.xml +++ b/MP.IOC/Resources/manifest.xml @@ -1,6 +1,6 @@ - 8.16.2605.607 + 8.16.2605.609 https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html false