9690e26194
- fix auth annuale x home e footer
239 lines
8.9 KiB
C#
239 lines
8.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using NLog;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
using System.Diagnostics.Eventing.Reader;
|
|
|
|
namespace MP.Land.Data
|
|
{
|
|
public class AppAuthService : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
|
|
private static ILogger<AppAuthService> _logger;
|
|
|
|
private static List<MP.AppAuth.Models.Macchine> ElencoMacchine = new List<MP.AppAuth.Models.Macchine>();
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IDistributedCache distributedCache;
|
|
|
|
private readonly IMemoryCache memoryCache;
|
|
|
|
/// <summary>
|
|
/// Durata assoluta massima della cache
|
|
/// </summary>
|
|
private int chAbsExp = 15;
|
|
|
|
/// <summary>
|
|
/// Durata della cache in modalità inattiva (non acceduta) prima di venire rimossa
|
|
/// NON estende oltre il tempo massimo di validità della cache (chAbsExp)
|
|
/// </summary>
|
|
private int chSliExp = 5;
|
|
|
|
private Dictionary<string, string> Vocabolario = new Dictionary<string, string>();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Fields
|
|
|
|
public static AppAuth.Controllers.AppAuthController dbController;
|
|
public static AppAuth.Controllers.MPController MpDbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public AppAuthService(IConfiguration configuration, ILogger<AppAuthService> logger, IMemoryCache memoryCache, IDistributedCache distributedCache)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
// conf cache
|
|
this.memoryCache = memoryCache;
|
|
this.distributedCache = distributedCache;
|
|
// conf DB
|
|
string connStr = _configuration.GetConnectionString("MP.Land");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new AppAuth.Controllers.AppAuthController(configuration);
|
|
MpDbController = new AppAuth.Controllers.MPController(configuration);
|
|
_logger.LogInformation("DbController OK");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Private Properties
|
|
|
|
private DistributedCacheEntryOptions cacheOpt
|
|
{
|
|
get
|
|
{
|
|
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(chAbsExp)).SetSlidingExpiration(TimeSpan.FromMinutes(chSliExp));
|
|
}
|
|
}
|
|
|
|
private DistributedCacheEntryOptions cacheOptLong
|
|
{
|
|
get
|
|
{
|
|
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(chAbsExp * 10)).SetSlidingExpiration(TimeSpan.FromMinutes(chSliExp));
|
|
}
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected void CheckVoc()
|
|
{
|
|
string cacheKey = ":MP:VOCAB";
|
|
string rawData;
|
|
if (Vocabolario.Count == 0)
|
|
{
|
|
var redisDataList = distributedCache.Get(cacheKey);
|
|
if (redisDataList != null)
|
|
{
|
|
rawData = Encoding.UTF8.GetString(redisDataList);
|
|
Vocabolario = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
Vocabolario = dbController.VocabolarioGetAll().ToDictionary(x => $"{x.Lingua}#{x.Lemma}", x => x.Traduzione);
|
|
rawData = JsonConvert.SerializeObject(Vocabolario);
|
|
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
|
distributedCache.Set(cacheKey, redisDataList, cacheOptLong);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuato rilettura da DB + caching per Vocabolario: {ts.TotalMilliseconds} ms");
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Public Methods
|
|
|
|
public async Task<List<AppAuth.Models.AnagraficaGruppi>> AnagGruppiAll()
|
|
{
|
|
List<AppAuth.Models.AnagraficaGruppi> dbResult = new List<AppAuth.Models.AnagraficaGruppi>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.AnagGruppiGetAll();
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AnagGruppiAll: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<AppAuth.Models.AnagraficaGruppi>> AnagGruppiFilt(string codTipo)
|
|
{
|
|
List<AppAuth.Models.AnagraficaGruppi> dbResult = new List<AppAuth.Models.AnagraficaGruppi>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.AnagGruppiFilt(codTipo);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AnagGruppiFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
public async Task<List<AppAuth.Models.AnagraficaOperatori>> AnagOperByGroupList(string codGruppo, string searchVal)
|
|
{
|
|
List<AppAuth.Models.AnagraficaOperatori> dbResult = new List<AppAuth.Models.AnagraficaOperatori>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
var rawData = dbController
|
|
.AnagOpByGruppoGetFilt(codGruppo, searchVal);
|
|
dbResult = rawData
|
|
.GroupBy(user => user.MatrOpr)
|
|
.Select(grp => grp.First())
|
|
.ToList();
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AnagOperByGroupList: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<AppAuth.Models.AnagraficaOperatori>> AnagOperList(string searchVal)
|
|
{
|
|
List<AppAuth.Models.AnagraficaOperatori> dbResult = new List<AppAuth.Models.AnagraficaOperatori>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.AnagOpGetAll(searchVal);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AnagOperList: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
MpDbController.Dispose();
|
|
}
|
|
|
|
public string Traduci(string lemma, string lingua = "IT")
|
|
{
|
|
string answ = $"__{lemma}__";
|
|
string keyReq = $"{lingua}#{lemma}";
|
|
CheckVoc();
|
|
// cerco in vocabolario...
|
|
if (Vocabolario.ContainsKey(keyReq))
|
|
{
|
|
answ = Vocabolario[keyReq];
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<AppAuth.Models.UpdMan>> UpdManList()
|
|
{
|
|
List<AppAuth.Models.UpdMan> dbResult = new List<AppAuth.Models.UpdMan>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = dbController.UpdManGetAll();
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per UpdManList: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<AppAuth.Models.AnagKeyValueModel>> AnagKeyValList()
|
|
{
|
|
List<AppAuth.Models.AnagKeyValueModel> dbResult = new List<AppAuth.Models.AnagKeyValueModel>();
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
dbResult = MpDbController.AnagKeyValuesGetAll();
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB per AnagKeyValList: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task ResetCache()
|
|
{
|
|
string cacheKey = ":MP:VOCAB";
|
|
await distributedCache.RemoveAsync(cacheKey);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |