Files
Mapo-IOB/SMGen.Data/Services/SMGDataService.cs
T
2023-07-31 12:20:39 +02:00

174 lines
5.7 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using NLog;
using Org.BouncyCastle.Asn1.Pkcs;
using SMGen.Core;
using SMGen;
using SMGen.Data.Controllers;
using SMGen.Data.DbModels;
using StackExchange.Redis;
using System.Diagnostics;
using SMGen.Data;
using SMGen.Data.Data;
namespace SMGen.Data.Services
{
public class SMGDataService
{
public static SMGenController dbController = null!;
private static IConfiguration _configuration = null!;
private static JsonSerializerSettings? JSSettings;
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Durata cache lunga IN SECONDI
/// </summary>
private int cacheTtlLong = 60 * 5;
/// <summary>
/// Durata cache breve IN SECONDI
/// </summary>
private int cacheTtlShort = 60 * 1;
/// <summary>
/// Oggetto per connessione a REDIS
/// </summary>
private IConnectionMultiplexer redisConn;
/// <summary>
/// Oggetto DB redis da impiegare x chiamate R/W
/// </summary>
private IDatabase redisDb = null!;
private Random rnd = new Random();
public SMGDataService(IConfiguration configuration, IConnectionMultiplexer redisConnMult)
{
_configuration = configuration;
// Conf cache
redisConn = redisConnMult;
redisDb = this.redisConn.GetDatabase();
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
JSSettings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
// cod app
CodApp = _configuration["CodApp"];
// Conf DB
string connStr = _configuration.GetConnectionString("SMGen.DB");
if (string.IsNullOrEmpty(connStr))
{
Log.Info("ConnString empty!");
}
else
{
dbController = new SMGenController(configuration);
}
// chiudo log
Log.Info("Avviata classe WebDoorCreatorService");
}
public string CodApp { get; set; } = "";
public async Task<bool> TranInInsert(List<TransizioneIngressiModelTemp> newTIList, int currIdxFamiglia)
{
var dbResult = await dbController.TranInInsert(newTIList, currIdxFamiglia);
return dbResult;
}
public async Task<bool> AnagEventinInsert(List<AnagEventiModelTemp> newEvList)
{
var dbResult = await dbController.AnagEventinInsert(newEvList);
return dbResult;
}
public async Task<bool> FilesLoadRedis(Dictionary<string, bool> filesDict)
{
bool fatto = false;
string currKey = $"{Constants.FILES_TO_PROC}";
#if false
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string? rawData = await redisDb.StringGetAsync(currKey);
if (string.IsNullOrEmpty(rawData))
{
rawData = JsonConvert.SerializeObject(filesList, JSSettings);
fatto = await redisDb.StringSetAsync(currKey, rawData, LongCache);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"FilesLoad | REDIS in: {ts.TotalMilliseconds} ms");
#endif
foreach (var item in filesDict)
{
await RedHashUpsert(currKey, item.Key, item.Value);
}
fatto = true;
return fatto;
}
/// <summary>
/// Effettua upsert in HasList redis
/// </summary>
/// <param name="currKey">Chiave redis della Hashlist</param>
/// <param name="chiave">Chiave nella HashList</param>
/// <param name="valore">Valore da salvare</param>
/// <returns>Num record nella HashList</returns>
protected async Task<long> RedHashUpsert(RedisKey currKey, string chiave, bool valore)
{
long numReq = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await redisDb.HashSetAsync(currKey, chiave, valore);
numReq = await redisDb.HashLengthAsync(currKey);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"RedHashUpsert | {currKey} | in: {ts.TotalMilliseconds} ms");
return numReq;
}
#region Private Properties
/// <summary>
/// Durata cache breve (1 min circa + perturbazione percentuale +/-10%)
/// </summary>
private TimeSpan FastCache
{
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
}
/// <summary>
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
/// </summary>
private TimeSpan LongCache
{
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
}
/// <summary>
/// Durata cache molto breve (10 sec circa + perturbazione percentuale +/-10%)
/// </summary>
private TimeSpan UltraFastCache
{
get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000);
}
/// <summary>
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
/// </summary>
private TimeSpan UltraLongCache
{
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
}
#endregion Private Properties
}
}