diff --git a/MP-TAB/MP-TAB/Program.cs b/MP-TAB/MP-TAB/Program.cs index 1e2f3a4e..5009a01f 100644 --- a/MP-TAB/MP-TAB/Program.cs +++ b/MP-TAB/MP-TAB/Program.cs @@ -11,9 +11,10 @@ string connStringRedis = cString ?? "localhost:6379, DefaultDatabase=1, connectT // avvio oggetto shared x redis... var redisMultiplexer = ConnectionMultiplexer.Connect(connStringRedis); -// Add services to the container. +// Add services x accesso dati builder.Services.AddSingleton(redisMultiplexer); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); // nuova versione contenuti... builder.Services.AddRazorComponents() diff --git a/MP-TAB/MP-TAB/appsettings.json b/MP-TAB/MP-TAB/appsettings.json index b6522a98..de143ab9 100644 --- a/MP-TAB/MP-TAB/appsettings.json +++ b/MP-TAB/MP-TAB/appsettings.json @@ -9,6 +9,7 @@ "CodApp": "MP.TAB", "ConnectionStrings": { "Redis": "localhost:6379,DefaultDatabase=1,connectTimeout=5000,syncTimeout=5000,asyncTimeout=5000,abortConnect=false,ssl=false", + "MP.All": "Server=SQL2016DEV;Database=MoonPro; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=Blazor.ServerApp;", "MP.Mon": "Server=SQL2016DEV;Database=MoonPro; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=Blazor.ServerApp;", "MP.Tab": "Server=SQL2016DEV;Database=MoonPro; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=Blazor.ServerApp;" }, diff --git a/MP.Data/Controllers/MpSpecController.cs b/MP.Data/Controllers/MpSpecController.cs index a05153ba..c0bcee56 100644 --- a/MP.Data/Controllers/MpSpecController.cs +++ b/MP.Data/Controllers/MpSpecController.cs @@ -570,6 +570,11 @@ namespace MP.Data.Controllers return dbResult; } + /// + /// Elenco link JQM dato filtro tipo + /// + /// + /// public List ListLinkFilt(string tipoLink) { List dbResult = new List(); diff --git a/MP.Data/Services/BaseServ.cs b/MP.Data/Services/BaseServ.cs new file mode 100644 index 00000000..4faf5346 --- /dev/null +++ b/MP.Data/Services/BaseServ.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MP.Data.Services +{ + /// + /// Classe di aprtenza x costruzione servizi di accesso dati + cache + /// + public class BaseServ + { + #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 lunga (+ perturbazione percentuale +/-10%) + /// + protected TimeSpan UltraLongCache + { + get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000); + } + + #endregion Protected Properties + + #region Private Fields + + /// + /// Durata cache lunga IN SECONDI + /// + private int cacheTtlLong = 60 * 5; + + /// + /// Durata cache breve IN SECONDI + /// + private int cacheTtlShort = 60 * 1; + + private Random rnd = new Random(); + + #endregion Private Fields + } +} \ No newline at end of file diff --git a/MP.Data/Services/MenuData.cs b/MP.Data/Services/MenuData.cs new file mode 100644 index 00000000..66e1c6fe --- /dev/null +++ b/MP.Data/Services/MenuData.cs @@ -0,0 +1,126 @@ +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 +{ + public class MenuData : BaseServ, IDisposable + { + #region Public Constructors + + public MenuData(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.MpSpecController(configuration); + StringBuilder sb = new StringBuilder(); + sb.AppendLine($"MenuData | MpSpecController OK"); + Log.Info(sb.ToString()); + } + } + + #endregion Public Constructors + + #region Public Properties + + public static Controllers.MpSpecController dbController { get; set; } = null!; + + #endregion Public Properties + + #region Public Methods + + public Task> ConfigGetAll() + { + return Task.FromResult(dbController.ConfigGetAll().ToList()); + } + + public void Dispose() + { + // Clear database controller + dbController.Dispose(); + } + + /// + /// Elenco link JQM dato filtro tipo + /// + /// + /// + public async Task> ListLinkFilt(string tipoLink) + { + string source = "DB"; + Stopwatch sw = new Stopwatch(); + sw.Start(); + List? result = new List(); + // cerco in redis... + string currKey = $"{redisMenuDataKey}:{tipoLink}"; + RedisValue rawData = await redisDb.StringGetAsync(currKey); + if (!string.IsNullOrEmpty($"{rawData}")) + { + result = JsonConvert.DeserializeObject>($"{rawData}"); + source = "REDIS"; + } + else + { + result = await Task.FromResult(dbController.ListLinkFilt(tipoLink)); + // serializzp e salvo... + rawData = JsonConvert.SerializeObject(result); + await redisDb.StringSetAsync(currKey, rawData, UltraLongCache); + } + if (result == null) + { + result = new List(); + } + sw.Stop(); + Log.Debug($"ListLinkFilt | tipoLink: {tipoLink} | {source} | {sw.Elapsed.TotalMilliseconds}ms"); + return result; + } + + #endregion Public Methods + + #region Protected Fields + + /// + /// Oggetto per connessione a REDIS + /// + protected ConnectionMultiplexer redisConn = null!; + + /// + /// Oggetto DB redis da impiegare x chiamate R/W + /// + protected IDatabase redisDb = null!; + + #endregion Protected Fields + + #region Private Fields + + private static IConfiguration _configuration = null!; + + private static Logger Log = LogManager.GetCurrentClassLogger(); + private string redisMenuDataKey = "MP:ALL:Cache:Menu"; + + #endregion Private Fields + } +} \ No newline at end of file diff --git a/MP.Data/Services/StatusData.cs b/MP.Data/Services/StatusData.cs index 74a1bc39..4ae03574 100644 --- a/MP.Data/Services/StatusData.cs +++ b/MP.Data/Services/StatusData.cs @@ -37,7 +37,7 @@ namespace MP.Data.Services { dbController = new MP.Data.Controllers.MpMonController(configuration); StringBuilder sb = new StringBuilder(); - sb.AppendLine($"DbController OK"); + sb.AppendLine($"StatusData | MpMonController OK"); Log.Info(sb.ToString()); }