141 lines
4.5 KiB
C#
141 lines
4.5 KiB
C#
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<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";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<AnagEventiModel>? result = new List<AnagEventiModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:AnagEventi";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagEventiModel>>($"{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<AnagEventiModel>();
|
|
}
|
|
sw.Stop();
|
|
return result;
|
|
}
|
|
#region Protected Properties
|
|
|
|
/// <summary>
|
|
/// Durata cache breve (1 min circa + perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
protected TimeSpan FastCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
protected TimeSpan LongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache MOLTO lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
protected TimeSpan UltraLongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
#endregion Protected Properties
|
|
#region Private Fields
|
|
|
|
/// <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!;
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlLong = 60 * 5;
|
|
/// <summary>
|
|
/// Durata cache breve IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlShort = 60 * 1;
|
|
|
|
private string redisBaseKey = "MP:TAB:Cache";
|
|
private Random rnd = new Random();
|
|
#endregion Private Fields
|
|
}
|
|
} |