Aggiunta metodi gestione cache conf

This commit is contained in:
Samuele Locatelli
2023-10-03 12:17:14 +02:00
parent bc432f994f
commit 35767c2716
4 changed files with 106 additions and 49 deletions
+30
View File
@@ -21,6 +21,36 @@ namespace MP_TAB_SERV.Pages
protected override async Task OnInitializedAsync()
{
await ReloadData();
#if false
if (enableSchedaTecnica)
{
checkLottiOdl();
}
#endif
}
private void checkLottiOdl()
{
#if false
// controlla se abilitato check LOTTI MAG
if (enableMagLotti)
{
if (idxOdl > 0)
{
// controlla se ci sia lotto x ODL
if (DataLayerObj.taMagELotti.getByODL(idxOdl).Rows.Count == 0)
{
try
{
// se non c'è chiama stored x rigenerare
DataLayerObj.taMagELotti.UpsertByOdl(idxOdl, true);
}
catch
{ }
}
}
}
#endif
}
#endregion Protected Methods
+23 -6
View File
@@ -5,7 +5,6 @@ using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Data.Controllers
{
@@ -23,11 +22,6 @@ namespace MP.Data.Controllers
#region Public Methods
public void Dispose()
{
_configuration = null;
}
/// <summary>
/// Restituisce l'anagrafica eventi per intero
/// </summary>
@@ -45,6 +39,29 @@ namespace MP.Data.Controllers
return dbResult;
}
/// <summary>
/// Elenco da tabella Config
/// </summary>
/// <returns></returns>
public List<ConfigModel> ConfigGetAll()
{
List<ConfigModel> dbResult = new List<ConfigModel>();
using (var dbCtx = new MoonProContext(_configuration))
{
dbResult = dbCtx
.DbSetConfig
.AsNoTracking()
.OrderBy(x => x.Chiave)
.ToList();
}
return dbResult;
}
public void Dispose()
{
_configuration = null;
}
#endregion Public Methods
#region Private Fields
+2 -2
View File
@@ -181,7 +181,7 @@ namespace MP.Data.Services
/// </summary>
public async Task<string> IdxMaccGet()
{
return await localStorage.GetItemAsync<string>("CurrMach");
return await sessionStore.GetItemAsync<string>("CurrMach");
}
/// <summary>
@@ -189,7 +189,7 @@ namespace MP.Data.Services
/// </summary>
public async Task IdxMaccSet(string machSel)
{
await localStorage.SetItemAsync("CurrMach", machSel);
await sessionStore.SetItemAsync("CurrMach", machSel);
}
#if false
+51 -41
View File
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Configuration;
using MP.Data.DatabaseModels;
using Newtonsoft.Json;
using NLog.Fluent;
using NLog;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
@@ -11,7 +11,7 @@ using System.Threading.Tasks;
namespace MP.Data.Services
{
public class TabDataService
public class TabDataService : BaseServ, IDisposable
{
#region Public Constructors
@@ -33,23 +33,24 @@ namespace MP.Data.Services
{
dbController = new Controllers.MpTabController(configuration);
StringBuilder sb = new StringBuilder();
sb.AppendLine($"TabDataService | MpSpecController OK");
sb.AppendLine($"TabDataService | MpTabController OK");
Log.Info(sb.ToString());
}
}
#endregion Public Constructors
protected static IConfiguration _configuration = null!;
#region Public Properties
public static Controllers.MpTabController dbController { get; set; } = null!;
#endregion Public Properties
#region Public Methods
public async Task<List<AnagEventiModel>> AnagEventiGetAll()
{
// setup parametri costanti
bool inCorso = false;
string keyRichPart = "*";
string Reparto = "*";
DateTime startDate = new DateTime(2000, 1, 1);
DateTime endDate = DateTime.Today.AddDays(1);
string source = "DB";
@@ -76,44 +77,57 @@ namespace MP.Data.Services
result = new List<AnagEventiModel>();
}
sw.Stop();
Log.Debug($"AnagEventiGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
return result;
}
#region Protected Properties
/// <summary>
/// Durata cache breve (1 min circa + perturbazione percentuale +/-10%)
/// Config values attivi
/// </summary>
protected TimeSpan FastCache
/// <returns></returns>
public async Task<List<ConfigModel>> ConfigGetAll()
{
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
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}"))
{
result = JsonConvert.DeserializeObject<List<ConfigModel>>($"{rawData}");
source = "REDIS";
}
else
{
result = await Task.FromResult(dbController.ConfigGetAll());
// serializzp e salvo...
rawData = JsonConvert.SerializeObject(result);
await redisDb.StringSetAsync(currKey, rawData, LongCache);
}
if (result == null)
{
result = new List<ConfigModel>();
}
sw.Stop();
Log.Debug($"ConfigGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
return result;
//return Task.FromResult(dbController.ConfigGetAll().ToList());
}
/// <summary>
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
/// </summary>
protected TimeSpan LongCache
public void Dispose()
{
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
// Clear database controller
dbController.Dispose();
}
/// <summary>
/// Durata cache MOLTO breve (10 sec circa + perturbazione percentuale +/-10%)
/// </summary>
protected TimeSpan UltraFastCache
{
get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000);
}
#endregion Public Methods
/// <summary>
/// Durata cache MOLTO lunga (+ perturbazione percentuale +/-10%)
/// </summary>
protected TimeSpan UltraLongCache
{
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
}
#region Protected Fields
#endregion Protected Properties
#region Private Fields
protected static IConfiguration _configuration = null!;
/// <summary>
/// Oggetto per connessione a REDIS
@@ -125,17 +139,13 @@ namespace MP.Data.Services
/// </summary>
protected IDatabase redisDb = null!;
/// <summary>
/// Durata cache lunga IN SECONDI
/// </summary>
private int cacheTtlLong = 60 * 5;
/// <summary>
/// Durata cache breve IN SECONDI
/// </summary>
private int cacheTtlShort = 60 * 1;
#endregion Protected Fields
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private string redisBaseKey = "MP:TAB:Cache";
private Random rnd = new Random();
#endregion Private Fields
}
}