220 lines
7.2 KiB
C#
220 lines
7.2 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, FilesClass> filesDict)
|
|
{
|
|
bool fatto = false;
|
|
string currKey = $"{Constants.FILES_TO_PROC}";
|
|
await ExecFlushRedisPattern(currKey);
|
|
|
|
foreach (var item in filesDict)
|
|
{
|
|
string valSer = JsonConvert.SerializeObject(item.Value, JSSettings);
|
|
|
|
await RedHashUpsert(currKey, item.Key, valSer);
|
|
}
|
|
|
|
fatto = true;
|
|
|
|
return fatto;
|
|
}
|
|
|
|
public async Task<bool> FileUpdate(string origFileName, FilesClass fileNewInfo)
|
|
{
|
|
bool fatto = false;
|
|
string currKey = $"{Constants.FILES_TO_PROC}";
|
|
//await ExecFlushRedisPattern(currKey);
|
|
string valSer = JsonConvert.SerializeObject(fileNewInfo, JSSettings);
|
|
|
|
await RedHashUpsert(currKey, origFileName, valSer);
|
|
|
|
fatto = true;
|
|
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = false;
|
|
var listEndpoints = redisConn.GetEndPoints();
|
|
foreach (var endPoint in listEndpoints)
|
|
{
|
|
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
|
|
var server = redisConn.GetServer(endPoint);
|
|
if (server != null)
|
|
{
|
|
var keyList = server.Keys(redisDb.Database, pattern);
|
|
foreach (var item in keyList)
|
|
{
|
|
await redisDb.KeyDeleteAsync(item);
|
|
}
|
|
answ = true;
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
public async Task<Dictionary<string, FilesClass>> FilesGetAll()
|
|
{
|
|
Dictionary<string, FilesClass> dbResult = new Dictionary<string, FilesClass>();
|
|
string currKey = $"{Constants.FILES_TO_PROC}";
|
|
var rawData = await redisDb.HashGetAllAsync(currKey);
|
|
foreach (var item in rawData)
|
|
{
|
|
var desVal = JsonConvert.DeserializeObject<FilesClass>(item.Value);
|
|
dbResult.Add($"{item.Name}", desVal);
|
|
}
|
|
|
|
return dbResult;
|
|
}
|
|
|
|
/// <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, string 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
|
|
}
|
|
} |