Files
webdoorcreator/WebDoorCreator.UI/Data/WDCVocabularyService.cs
T
2023-04-04 11:07:50 +02:00

157 lines
5.1 KiB
C#

using Microsoft.AspNetCore.Identity.UI.Services;
using Newtonsoft.Json;
using NLog;
using Org.BouncyCastle.Asn1.X500;
using StackExchange.Redis;
using System.Diagnostics;
using WebDoorCreator.Data.Controllers;
namespace WebDoorCreator.UI.Data
{
public class WDCVocabularyService
{
public static WebDoorCreatorController dbController = null!;
public WDCVocabularyService(IConfiguration configuration, IConnectionMultiplexer redisConnMult, IEmailSender emailSender)
{
_configuration = configuration;
_emailSender = emailSender;
// 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("WDC.DB");
if (string.IsNullOrEmpty(connStr))
{
Log.Info("ConnString empty!");
}
else
{
dbController = new WebDoorCreatorController(configuration);
}
if(VocabularyLemmas == null)
{
VocabularyLemmas = Task.Run(async () => await VocLemmaGetAll()).Result;
}
Log.Info("Avviata classe WDCVocabularyService");
}
public Dictionary<string, Dictionary<string, string>>? VocabularyLemmas
{
get => _vocabularyLemmas;
set
{
if (_vocabularyLemmas != value)
{
_vocabularyLemmas = value;
}
}
}
private Dictionary<string, Dictionary<string, string>>? _vocabularyLemmas { get; set; } = null;
private TimeSpan UltraLongCache
{
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
}
public string traduci(string lingua, string lemma)
{
string answ = $"[{lemma}]";
if (VocabularyLemmas != null && VocabularyLemmas.ContainsKey(lingua))
{
if (VocabularyLemmas[lingua].ContainsKey(lemma))
{
answ = VocabularyLemmas[lingua][lemma];
}
}
return answ;
}
public async Task<Dictionary<string, Dictionary<string, string>>?> VocLemmaGetAll()
{
string source = "DB";
Dictionary<string, Dictionary<string, string>>? dbResult = new Dictionary<string, Dictionary<string, string>>();
// cerco da cache
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string currKey = $"{rKeyVocLemma}";
string? rawData = await redisDb.StringGetAsync(currKey);
if (!string.IsNullOrEmpty(rawData))
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(rawData);
if (tempResult == null)
{
dbResult = new Dictionary<string, Dictionary<string, string>>();
}
else
{
dbResult = tempResult;
}
}
else
{
dbResult = dbController.VocLemmaGetAll();
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
}
if (dbResult == null)
{
dbResult = new Dictionary<string, Dictionary<string, string>>();
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Debug($"VocLemmaGetAll | {source} in: {ts.TotalMilliseconds} ms");
return dbResult;
}
/// <summary>
/// Durata cache lunga IN SECONDI
/// </summary>
private int cacheTtlLong = 60 * 5;
/// <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!;
protected const string redisBaseAddr = "WDC";
protected const string rKeyVocLemma = $"{redisBaseAddr}:Cache:VocLemma";
protected Random rnd = new Random();
private static IConfiguration _configuration = null!;
private static JsonSerializerSettings? JSSettings;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private readonly IEmailSender _emailSender;
public string CodApp { get; set; } = "";
}
}