400 lines
14 KiB
C#
400 lines
14 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using MP.Data.DbModels;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.Data.Services
|
|
{
|
|
/// <summary>
|
|
/// Classe accesso dati filtrati per operatore
|
|
/// </summary>
|
|
public class ListSelectDataSrv : BaseServ
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ListSelectDataSrv(IConfiguration configuration, IConnectionMultiplexer redConn) : base(configuration, redConn)
|
|
{
|
|
// conf DB
|
|
string connStr = _configuration.GetConnectionString("MP.All");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
Log.Error("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
dbController = new Controllers.MpSpecController(configuration);
|
|
sb.AppendLine($"ListSelectDataSrv | 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
|
|
|
|
/// <summary>
|
|
/// Elenco tabella Articoli da filtro
|
|
/// </summary>
|
|
/// <param name="numRecord">Numero record max da recuperare</param>
|
|
/// <param name="azienda">cod azienda, * = tutte</param>
|
|
/// <param name="searchVal">Ricerca testuale</param>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagArticoliModel>> ArticoliGetSearch(int numRecord, string azienda, string searchVal)
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<AnagArticoliModel>? result = new List<AnagArticoliModel>();
|
|
// cerco in _redisConn...
|
|
string currKey = $"{redisBaseKey}:Art:{azienda}:{searchVal}:{numRecord}";
|
|
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagArticoliModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ArticoliGetSearch(numRecord, azienda, searchVal);
|
|
#if false
|
|
result = await Task.FromResult(dbController.ArticoliGetSearch(numRecord, azienda, searchVal));
|
|
#endif
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await _redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagArticoliModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ArticoliGetSearch | azienda: {azienda} | searchVal: {searchVal} | numRecord: {numRecord} | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero elenco config
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ConfigModel>? result = new List<ConfigModel>();
|
|
// cerco in _redisConn...
|
|
string currKey = $"{redisBaseKey}:Conf";
|
|
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ConfigModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbController.ConfigGetAll();
|
|
// serializzo e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
_redisDb.StringSet(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ConfigModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ConfigGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis (tutta)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache()
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis per chiave specifica (da redisBaseKey...)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache(string KeyReq)
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:{KeyReq}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco link JQM (totale)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<LinkMenuModel>> ListLinkAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<LinkMenuModel>? result = new List<LinkMenuModel>();
|
|
// cerco in _redisConn...
|
|
string currKey = $"{redisBaseKey}:Menu:ALL";
|
|
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<LinkMenuModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = await Task.FromResult(dbController.ListLinkAll());
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await _redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<LinkMenuModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ListLinkAll | tipoLink: * | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco link JQM dato filtro tipo
|
|
/// </summary>
|
|
/// <param name="tipoLink"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<LinkMenuModel>> ListLinkFilt(string tipoLink)
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<LinkMenuModel>? result = new List<LinkMenuModel>();
|
|
// cerco in _redisConn...
|
|
string currKey = $"{redisBaseKey}:Menu:{tipoLink}";
|
|
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<LinkMenuModel>>($"{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<LinkMenuModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ListLinkFilt | tipoLink: {tipoLink} | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Macchine dato operatore secondo gruppi (macchine/operatore)
|
|
/// </summary>
|
|
/// <param name="MatrOpr"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<MacchineModel>> MacchineByMatrOper(int MatrOpr)
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<MacchineModel>? result = new List<MacchineModel>();
|
|
// cerco in _redisConn...
|
|
string currKey = $"{redisBaseKey}:Macc:{MatrOpr}";
|
|
RedisValue rawData = await _redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<MacchineModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = await Task.FromResult(dbController.MacchineByMatrOper(MatrOpr));
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await _redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<MacchineModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"MacchineGetFilt | MatrOpr: {MatrOpr} | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Methods
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
// Free managed resources here
|
|
dbController.Dispose();
|
|
}
|
|
|
|
// Free unmanaged resources here
|
|
_disposed = true;
|
|
}
|
|
|
|
// Call base class implementation.
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
private bool _disposed = false;
|
|
private string redisBaseKey = "MP:ALL:Cache";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria _redisConn dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
Log.Debug($"Richiesta flush pattern: {pattern}");
|
|
|
|
// 1. Target ONLY master (le replica sono in read-only)
|
|
var master = _redisConn.GetEndPoints()
|
|
.Where(ep => _redisConn.GetServer(ep).IsConnected && !_redisConn.GetServer(ep).IsReplica)
|
|
.FirstOrDefault();
|
|
|
|
if (master == null)
|
|
{
|
|
Log.Warn($"Nessun master Redis raggiungibile per il pattern {pattern}");
|
|
return false;
|
|
}
|
|
|
|
// 2. Flush intero DB se richiesto
|
|
if (pattern.ToString() == "*")
|
|
{
|
|
Log.Debug($"Full DB reset da pattern {pattern}");
|
|
if (master != null)
|
|
{
|
|
_redisConn.GetServer(master).FlushDatabase(_redisDb.Database);
|
|
Log.Info($"Flush database {_redisDb.Database} completato");
|
|
}
|
|
return true;
|
|
}
|
|
// altrimenti faccio ciclo!
|
|
var server = _redisConn.GetServer(master);
|
|
var db = _redisConn.GetDatabase(_redisDb.Database);
|
|
const int batchSize = 500;
|
|
var batch = new List<RedisKey>(batchSize);
|
|
int deletedCount = 0;
|
|
|
|
try
|
|
{
|
|
// KeysAsync usa SCAN automaticamente quando i risultati sono grandi
|
|
await foreach (var key in server.KeysAsync(
|
|
database: _redisDb.Database,
|
|
pattern: pattern.ToString(),
|
|
pageSize: batchSize))
|
|
{
|
|
batch.Add(key);
|
|
if (batch.Count >= batchSize)
|
|
{
|
|
// Esecuzione batch in parallelo controllato
|
|
await Task.WhenAll(batch.Select(k => db.KeyDeleteAsync(k)));
|
|
batch.Clear();
|
|
deletedCount += batchSize;
|
|
}
|
|
}
|
|
|
|
// Restanti
|
|
if (batch.Count > 0)
|
|
{
|
|
await Task.WhenAll(batch.Select(k => db.KeyDeleteAsync(k)));
|
|
deletedCount += batch.Count;
|
|
}
|
|
|
|
Log.Info("Flush pattern {Pattern}: eliminate {Count} chiavi", pattern, deletedCount);
|
|
return true;
|
|
}
|
|
catch (RedisConnectionException ex)
|
|
{
|
|
Log.Error(ex, "Connessione Redis persa durante il flush di {pattern}", pattern);
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Errore imprevisto nel flush pattern {pattern}", pattern);
|
|
throw;
|
|
}
|
|
#if false
|
|
bool answ = false;
|
|
var masterEndpoint = _redisConn.GetEndPoints()
|
|
.Where(ep => _redisConn.GetServer(ep).IsConnected && !_redisConn.GetServer(ep).IsReplica)
|
|
.FirstOrDefault();
|
|
|
|
// se pattern è "*" elimino intero DB...
|
|
if (masterEndpoint != null && (pat2Flush.Equals(new RedisValue("*")) || pat2Flush == RedisValue.Null))
|
|
{
|
|
_redisConn.GetServer(masterEndpoint).FlushDatabase(database: _redisDb.Database);
|
|
}
|
|
else
|
|
{
|
|
var server = _redisConn.GetServer(masterEndpoint);
|
|
var keys = server.Keys(database: _redisDb.Database, pattern: pat2Flush, pageSize: 1000);
|
|
|
|
var deleteTasks = new List<Task>();
|
|
foreach (var key in keys)
|
|
{
|
|
deleteTasks.Add(_redisDb.KeyDeleteAsync(key));
|
|
if (deleteTasks.Count >= 1000)
|
|
{
|
|
await Task.WhenAll(deleteTasks);
|
|
deleteTasks.Clear();
|
|
}
|
|
}
|
|
if (deleteTasks.Count > 0)
|
|
{
|
|
await Task.WhenAll(deleteTasks);
|
|
}
|
|
answ = true;
|
|
}
|
|
return answ;
|
|
#endif
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |