43aae08d1e
- Update visualizzazione ODL ed Energy
285 lines
9.1 KiB
C#
285 lines
9.1 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using MP.Data.Conf;
|
|
using MP.Data.DatabaseModels;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Data.Services
|
|
{
|
|
/// <summary>
|
|
/// Classe accesso processi traduzione
|
|
/// </summary>
|
|
public class TranslateSrv : BaseServ, IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public TranslateSrv(IConfiguration configuration)
|
|
{
|
|
Stopwatch sw = Stopwatch.StartNew();
|
|
sw.Start();
|
|
_configuration = configuration;
|
|
// setup compoenti REDIS
|
|
redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
|
redisDb = redisConn.GetDatabase();
|
|
|
|
// conf DB
|
|
string connStr = _configuration.GetConnectionString("MP.Voc");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
Log.Error("MP.Voc: ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new Controllers.MpVocController(configuration);
|
|
InitDict();
|
|
sw.Stop();
|
|
Log.Info($"TranslateSrv | MpVocController OK | {sw.Elapsed.TotalMilliseconds} ms");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public static Controllers.MpVocController dbController { get; set; } = null!;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Recupero elenco config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ConfigModel>? result = new List<ConfigModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:Conf";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ConfigModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ConfigGetAll();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSet(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ConfigModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ConfigGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
// redis dispose
|
|
redisConn = null;
|
|
redisDb = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis (tutta)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache()
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
// rileggo vocabolario!
|
|
var rawData = await VocabolarioGetAll();
|
|
DictVocab = rawData.ToDictionary(kvp => $"{kvp.Lingua}_{kvp.Lemma}".ToUpper(), kvp => kvp.Traduzione);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis per chiave specifica (da redisBaseKey...)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache(string KeyReq)
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:{KeyReq}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero elenco config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<LingueModel>> LingueGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<LingueModel>? result = new List<LingueModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:Lang";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<LingueModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.LingueGetAll();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSet(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<LingueModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"LingueGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Traduzione termine
|
|
/// </summary>
|
|
/// <param name="lemma"></param>
|
|
/// <param name="lingua"></param>
|
|
/// <returns></returns>
|
|
public string Traduci(string lemma, string lingua = "IT")
|
|
{
|
|
string answ = $"[{lemma}.{lingua}]";
|
|
string key = $"{lingua}_{lemma}".ToUpper();
|
|
if (DictVocab.ContainsKey(key))
|
|
{
|
|
answ = DictVocab[key];
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero elenco config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<VocabolarioModel>> VocabolarioGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<VocabolarioModel>? result = new List<VocabolarioModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:Voc";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<VocabolarioModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.VocabolarioGetAll();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
redisDb.StringSet(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<VocabolarioModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"VocabolarioGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// Vocabolario x recupero rapido traduzioni
|
|
/// </summary>
|
|
protected static Dictionary<string, string> DictVocab = new Dictionary<string, string>();
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
protected ConnectionMultiplexer redisConn = null!;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
protected IDatabase redisDb = null!;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private string redisBaseKey = "MP:Voc:Cache";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Inizializzazione dict vari
|
|
/// </summary>
|
|
private static void InitDict()
|
|
{
|
|
// inizializzo dizionario vocabolario
|
|
var rawData = dbController.VocabolarioGetAll();
|
|
DictVocab = rawData.ToDictionary(kvp => $"{kvp.Lingua}_{kvp.Lemma}".ToUpper(), kvp => kvp.Traduzione);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria redis dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private 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;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |