inizio inserimento InMemoryCache x metodi vari IOC
This commit is contained in:
@@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// Provider CultureInfo x parse valori (es dataora)
|
||||
/// Provider CultureInfo x parse valori(es dataora)
|
||||
/// </summary>
|
||||
private CultureInfo ciProvider = CultureInfo.InvariantCulture;
|
||||
|
||||
@@ -297,32 +298,34 @@ namespace MP.Data.Services.IOC
|
||||
/// <summary>
|
||||
/// Elenco completo config da DB
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task<List<ConfigModel>> ConfigGetAllAsync()
|
||||
{
|
||||
List<ConfigModel>? result = new List<ConfigModel>();
|
||||
// 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<List<ConfigModel>>($"{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<ConfigModel>();
|
||||
}
|
||||
return result;
|
||||
List<ConfigModel>? result = new List<ConfigModel>();
|
||||
// cerco in redis...
|
||||
RedisValue rawData = await _redisDb.StringGetAsync(MP.Data.Utils.redisConfKey);
|
||||
string source = "DB";
|
||||
if (!string.IsNullOrEmpty($"{rawData}"))
|
||||
{
|
||||
result = JsonConvert.DeserializeObject<List<ConfigModel>>($"{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<ConfigModel>();
|
||||
}
|
||||
return result;
|
||||
}, TimeSpan.FromMinutes(10));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -388,60 +391,81 @@ namespace MP.Data.Services.IOC
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementa gestione recupero cache da memoria o da obj esterno + cache memoria
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="cacheKey"></param>
|
||||
/// <param name="fetchFunc"></param>
|
||||
/// <param name="expiration"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<T> GetOrFetchAsync<T>(string cacheKey, Func<Task<T>> 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<HashSet<string>> ListMasterAsync()
|
||||
{
|
||||
const string cacheKey = "IOC_ListMaster";
|
||||
if (_cache.TryGetValue(cacheKey, out HashSet<string>? cachedList))
|
||||
return await GetOrFetchAsync("IOC_ListMaster", async () =>
|
||||
{
|
||||
return cachedList!;
|
||||
}
|
||||
|
||||
HashSet<string> result = new();
|
||||
string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListMaster";
|
||||
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
||||
if (rawData.HasValue)
|
||||
{
|
||||
result = JsonConvert.DeserializeObject<HashSet<string>>($"{rawData}") ?? new();
|
||||
}
|
||||
else
|
||||
{
|
||||
var fullList = await Macchine2SlaveGetAllAsync();
|
||||
result = fullList.Select(x => x.IdxMacchina).ToHashSet<string>();
|
||||
// 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<string> result = new();
|
||||
string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListMaster";
|
||||
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
||||
if (rawData.HasValue)
|
||||
{
|
||||
result = JsonConvert.DeserializeObject<HashSet<string>>($"{rawData}") ?? new();
|
||||
}
|
||||
else
|
||||
{
|
||||
var fullList = await Macchine2SlaveGetAllAsync();
|
||||
result = fullList.Select(x => x.IdxMacchina).ToHashSet<string>();
|
||||
rawData = JsonConvert.SerializeObject(result);
|
||||
await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10));
|
||||
}
|
||||
return result;
|
||||
}, TimeSpan.FromMinutes(5));
|
||||
}
|
||||
|
||||
private async Task<HashSet<string>> ListSlaveAsync()
|
||||
{
|
||||
const string cacheKey = "IOC_ListSlave";
|
||||
if (_cache.TryGetValue(cacheKey, out HashSet<string>? cachedList))
|
||||
return await GetOrFetchAsync("IOC_ListSlave", async () =>
|
||||
{
|
||||
return cachedList!;
|
||||
}
|
||||
|
||||
HashSet<string> result = new();
|
||||
string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListSlave";
|
||||
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
||||
if (rawData.HasValue)
|
||||
{
|
||||
result = JsonConvert.DeserializeObject<HashSet<string>>($"{rawData}") ?? new();
|
||||
}
|
||||
else
|
||||
{
|
||||
var fullList = await Macchine2SlaveGetAllAsync();
|
||||
result = fullList.Select(x => x.IdxMacchinaSlave).Distinct().ToHashSet<string>();
|
||||
// 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<string> result = new();
|
||||
string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListSlave";
|
||||
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
||||
if (rawData.HasValue)
|
||||
{
|
||||
result = JsonConvert.DeserializeObject<HashSet<string>>($"{rawData}") ?? new();
|
||||
}
|
||||
else
|
||||
{
|
||||
var fullList = await Macchine2SlaveGetAllAsync();
|
||||
result = fullList.Select(x => x.IdxMacchinaSlave).Distinct().ToHashSet<string>();
|
||||
rawData = JsonConvert.SerializeObject(result);
|
||||
await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10));
|
||||
}
|
||||
return result;
|
||||
}, TimeSpan.FromMinutes(5));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Version>8.16.2605.607</Version>
|
||||
<Version>8.16.2605.609</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -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<MoonProContext>(options =>
|
||||
options.UseSqlServer(connStr)
|
||||
.EnableSensitiveDataLogging(false) // true solo in Sviluppo
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<body>
|
||||
<i>Modulo MP-IOC </i>
|
||||
<h4>Versione: 8.16.2605.607</h4>
|
||||
<h4>Versione: 8.16.2605.609</h4>
|
||||
<br /> Note di rilascio:
|
||||
<ul>
|
||||
<li>
|
||||
|
||||
@@ -1 +1 @@
|
||||
8.16.2605.607
|
||||
8.16.2605.609
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<item>
|
||||
<version>8.16.2605.607</version>
|
||||
<version>8.16.2605.609</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>
|
||||
|
||||
Reference in New Issue
Block a user