Aggiunta InMemoryCache x servizi legati a INPUT (es list master/slave)

This commit is contained in:
Samuele Locatelli
2026-05-06 07:15:57 +02:00
parent 5e258917c4
commit 7ca5637fe4
5 changed files with 26 additions and 7 deletions
+22 -3
View File
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MP.Core.Objects;
using MP.Data.DbModels;
@@ -23,13 +24,15 @@ namespace MP.Data.Services.IOC
IConfiguration config,
IConnectionMultiplexer redis,
IIocRepository repo,
IServiceScopeFactory scopeFactory) : base(config, redis)
IServiceScopeFactory scopeFactory,
Microsoft.Extensions.Caching.Memory.IMemoryCache cache) : base(config, redis)
{
_className = "IocServ";
int.TryParse(config.GetValue<string>("ServerConf:redisLongTimeCache"), out redisLongTimeCache);
int.TryParse(config.GetValue<string>("ServerConf:redisShortTimeCache"), out redisShortTimeCache);
_repo = repo;
_scopeFactory = scopeFactory;
_cache = cache;
}
#endregion Public Constructors
@@ -176,8 +179,8 @@ namespace MP.Data.Services.IOC
private readonly string _className;
private readonly IIocRepository _repo;
private readonly IServiceScopeFactory _scopeFactory;
private readonly Microsoft.Extensions.Caching.Memory.IMemoryCache _cache;
/// <summary>
/// Provider CultureInfo x parse valori (es dataora)
@@ -387,6 +390,12 @@ namespace MP.Data.Services.IOC
private async Task<HashSet<string>> ListMasterAsync()
{
const string cacheKey = "IOC_ListMaster";
if (_cache.TryGetValue(cacheKey, out HashSet<string>? cachedList))
{
return cachedList!;
}
HashSet<string> result = new();
string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListMaster";
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
@@ -402,11 +411,19 @@ namespace MP.Data.Services.IOC
rawData = JsonConvert.SerializeObject(result);
await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10));
}
_cache.Set(cacheKey, result, TimeSpan.FromMinutes(5));
return result;
}
private async Task<HashSet<string>> ListSlaveAsync()
{
const string cacheKey = "IOC_ListSlave";
if (_cache.TryGetValue(cacheKey, out HashSet<string>? cachedList))
{
return cachedList!;
}
HashSet<string> result = new();
string currKey = $"{MP.Data.Utils.redisBaseAddr}:ListSlave";
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
@@ -422,6 +439,8 @@ namespace MP.Data.Services.IOC
rawData = JsonConvert.SerializeObject(result);
await _redisDb.StringSetAsync(currKey, rawData, getRandTOut(redisLongTimeCache * 10));
}
_cache.Set(cacheKey, result, TimeSpan.FromMinutes(5));
return result;
}