126 lines
3.7 KiB
C#
126 lines
3.7 KiB
C#
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<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
return Task.FromResult(dbController.ConfigGetAll().ToList());
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco link JQM dato filtro tipo
|
|
/// </summary>
|
|
/// <param name="tipoLink"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<LinkMenu>> ListLinkFilt(string tipoLink)
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<LinkMenu>? result = new List<LinkMenu>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisMenuDataKey}:{tipoLink}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (!string.IsNullOrEmpty($"{rawData}"))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<LinkMenu>>($"{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<LinkMenu>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ListLinkFilt | tipoLink: {tipoLink} | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected 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!;
|
|
|
|
#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
|
|
}
|
|
} |