using Microsoft.Extensions.Configuration; using MP.Data.DatabaseModels; using Newtonsoft.Json; using NLog.Fluent; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Threading.Tasks; namespace MP.Data.Services { public class TabDataService { #region Public Constructors public TabDataService(IConfiguration configuration) { _configuration = configuration; // setup compoenti REDIS redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis")); redisDb = redisConn.GetDatabase(); // conf DB string connStr = _configuration.GetConnectionString("Mp.All"); if (string.IsNullOrEmpty(connStr)) { Log.Error("ConnString empty!"); } else { dbController = new Controllers.MpTabController(configuration); StringBuilder sb = new StringBuilder(); sb.AppendLine($"TabDataService | MpSpecController OK"); Log.Info(sb.ToString()); } } #endregion Public Constructors protected static IConfiguration _configuration = null!; public static Controllers.MpTabController dbController { get; set; } = null!; public async Task> 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"; Stopwatch sw = new Stopwatch(); sw.Start(); List? result = new List(); // cerco in redis... string currKey = $"{redisBaseKey}:AnagEventi"; RedisValue rawData = await redisDb.StringGetAsync(currKey); if (!string.IsNullOrEmpty($"{rawData}")) { result = JsonConvert.DeserializeObject>($"{rawData}"); source = "REDIS"; } else { result = dbController.AnagEventiGetAll(); // serializzp e salvo... rawData = JsonConvert.SerializeObject(result); await redisDb.StringSetAsync(currKey, rawData, LongCache); } if (result == null) { result = new List(); } sw.Stop(); return result; } #region Protected Properties /// /// Durata cache breve (1 min circa + perturbazione percentuale +/-10%) /// protected TimeSpan FastCache { get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000); } /// /// Durata cache lunga (+ perturbazione percentuale +/-10%) /// protected TimeSpan LongCache { get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000); } /// /// Durata cache MOLTO breve (10 sec circa + perturbazione percentuale +/-10%) /// protected TimeSpan UltraFastCache { get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000); } /// /// Durata cache MOLTO lunga (+ perturbazione percentuale +/-10%) /// protected TimeSpan UltraLongCache { get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000); } #endregion Protected Properties #region Private Fields /// /// Oggetto per connessione a REDIS /// protected ConnectionMultiplexer redisConn = null!; /// /// Oggetto DB redis da impiegare x chiamate R/W /// protected IDatabase redisDb = null!; /// /// Durata cache lunga IN SECONDI /// private int cacheTtlLong = 60 * 5; /// /// Durata cache breve IN SECONDI /// private int cacheTtlShort = 60 * 1; private string redisBaseKey = "MP:TAB:Cache"; private Random rnd = new Random(); #endregion Private Fields } }