RImozione vecchio pacchetto cache redis completata anche da LicMan

This commit is contained in:
Samuele Locatelli
2024-09-03 18:18:43 +02:00
parent 2859979ad8
commit 292c7f9f24
8 changed files with 56 additions and 135 deletions
-7
View File
@@ -46,13 +46,6 @@ namespace MP.AppAuth
#endregion Public Constructors
#if false
/// <summary>
/// Singleton!
/// </summary>
public static HwSwInfo man = new HwSwInfo();
#endif
#region Public Properties
/// <summary>
+11 -70
View File
@@ -1,19 +1,15 @@
using Microsoft.Extensions.Configuration;
using System;
using Microsoft.Extensions.Logging;
using MP.AppAuth.Controllers;
using MP.AppAuth.Models;
using Newtonsoft.Json;
using NLog;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using System.Diagnostics;
using System.Text;
using Newtonsoft.Json;
using System.Diagnostics.Eventing.Reader;
using MP.AppAuth.Models;
using StackExchange.Redis;
using MP.AppAuth.Controllers;
namespace MP.Land.Data
{
@@ -24,15 +20,15 @@ namespace MP.Land.Data
// diritti (cablòato)
public const string RoleSuperAdmin = "MoonPro_SuperAdmin";
public static AppAuth.Controllers.AppAuthController dbController;
public static AppAuth.Controllers.MPController MpDbController;
public static AppAuth.Controllers.MPUserController userController;
public static AppAuthController dbController;
public static MPController MpDbController;
public static MPUserController userController;
#endregion Public Fields
#region Public Constructors
public AppAuthService(IConfiguration configuration, ILogger<AppAuthService> logger, IConnectionMultiplexer redisConnMult, IMemoryCache memoryCache, IDistributedCache distributedCache)
public AppAuthService(IConfiguration configuration, ILogger<AppAuthService> logger, IConnectionMultiplexer redisConnMult)
{
_logger = logger;
_configuration = configuration;
@@ -51,11 +47,6 @@ namespace MP.Land.Data
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
#if false
// conf cache
this.memoryCache = memoryCache;
this.distributedCache = distributedCache;
#endif
// conf DB
string connStr = _configuration.GetConnectionString("MP.Land");
if (string.IsNullOrEmpty(connStr))
@@ -241,16 +232,6 @@ namespace MP.Land.Data
{
source = "REDIS";
var tempResult = JsonConvert.DeserializeObject<List<UserDirittiModel>>(rawData);
#if false
if (tempResult == null)
{
dbResult = new List<UserDirittiModel>();
}
else
{
dbResult = tempResult;
}
#endif
dbResult = tempResult ?? new List<UserDirittiModel>();
}
else
@@ -330,10 +311,6 @@ namespace MP.Land.Data
{
RedisValue pattern = new RedisValue($"{redisBaseAddr}:*");
bool answ = await ExecFlushRedisPattern(pattern);
#if false
string cacheKey = ":MP:VOCAB";
await distributedCache.RemoveAsync(cacheKey);
#endif
// reset in RAM
Vocabolario = new Dictionary<string, string>();
await CheckVoc();
@@ -485,11 +462,6 @@ namespace MP.Land.Data
private static Logger Log = LogManager.GetCurrentClassLogger();
private static string Modulo = "";
#if false
private readonly IDistributedCache distributedCache;
private readonly IMemoryCache memoryCache;
#endif
/// <summary>
/// Durata cache lunga IN SECONDI
@@ -501,19 +473,6 @@ namespace MP.Land.Data
/// </summary>
private int cacheTtlShort = 60 * 1;
#if false
/// <summary>
/// Durata assoluta massima della cache
/// </summary>
private int chAbsExp = 15;
/// <summary>
/// Durata della cache in modalità inattiva (non acceduta) prima di venire rimossa NON
/// estende oltre il tempo massimo di validità della cache (chAbsExp)
/// </summary>
private int chSliExp = 5;
#endif
/// <summary>
/// Oggetto per connessione a REDIS
/// </summary>
@@ -532,24 +491,6 @@ namespace MP.Land.Data
#region Private Properties
#if false
private DistributedCacheEntryOptions cacheOpt
{
get
{
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(chAbsExp)).SetSlidingExpiration(TimeSpan.FromMinutes(chSliExp));
}
}
private DistributedCacheEntryOptions cacheOptLong
{
get
{
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(chAbsExp * 10)).SetSlidingExpiration(TimeSpan.FromMinutes(chSliExp));
}
}
#endif
private string CodApp { get; set; } = "";
/// <summary>
+40 -45
View File
@@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging;
using MP.AppAuth.Models;
using Newtonsoft.Json;
using RestSharp;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -28,12 +29,20 @@ namespace MP.Land.Data
/// </summary>
/// <param name="configuration"></param>
/// <param name="logger"></param>
/// <param name="distributedCache"></param>
public LicenseService(IConfiguration configuration, ILogger<LicenseService> logger, IDistributedCache distributedCache)
/// <param name="redisConnMult"></param>
public LicenseService(IConfiguration configuration, ILogger<LicenseService> logger, IConnectionMultiplexer redisConnMult)
{
_logger = logger;
_configuration = configuration;
this.distributedCache = distributedCache;
// Conf cache
redisConn = redisConnMult;
redisDb = this.redisConn.GetDatabase();
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
JSSettings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
}
#endregion Public Constructors
@@ -281,7 +290,8 @@ namespace MP.Land.Data
bool fatto = false;
string cacheKey = $"{rKeyAttByLic}:{MasterKey}";
var rawData = JsonConvert.SerializeObject(newActList);
await setRSV(cacheKey, rawData, numDays * cacheFact * 24);
TimeSpan cacheTs = TimeSpan.FromDays(numDays);
await setRSV(cacheKey, rawData, cacheTs);
fatto = true;
if (EA_InfoUpdated != null)
{
@@ -295,7 +305,8 @@ namespace MP.Land.Data
bool fatto = false;
string cacheKey = $"{rkeyAppInfo}:{MasterKey}";
var rawData = JsonConvert.SerializeObject(newAppInfo);
await setRSV(cacheKey, rawData, numDays * cacheFact * 24);
TimeSpan cacheTs = TimeSpan.FromDays(numDays);
await setRSV(cacheKey, rawData, cacheTs);
fatto = true;
if (EA_InfoUpdated != null)
{
@@ -362,12 +373,8 @@ namespace MP.Land.Data
/// <returns></returns>
protected async Task<string> getRSV(string rKey)
{
string answ = "";
var redisDataList = await distributedCache.GetAsync(rKey);
if (redisDataList != null)
{
answ = Encoding.UTF8.GetString(redisDataList);
}
var rawData = await redisDb.StringGetAsync(rKey);
string answ = rawData.HasValue ? $"{rawData}" : "";
return answ;
}
@@ -376,14 +383,11 @@ namespace MP.Land.Data
/// </summary>
/// <param name="rKey"></param>
/// <param name="rVal"></param>
/// <param name="cacheMult"></param>
/// <param name="cacheTS"></param>
/// <returns></returns>
protected async Task<bool> setRSV(string rKey, string rVal, int cacheMult)
protected async Task<bool> setRSV(string rKey, string rVal, TimeSpan cacheTS)
{
bool fatto = false;
var redisDataList = Encoding.UTF8.GetBytes(rVal);
await distributedCache.SetAsync(rKey, redisDataList, cacheOpt(cacheMult));
fatto = true;
bool fatto = await redisDb.StringSetAsync(rKey, rVal, cacheTS);
return fatto;
}
@@ -392,14 +396,11 @@ namespace MP.Land.Data
/// </summary>
/// <param name="rKey"></param>
/// <param name="rValInt"></param>
/// <param name="cacheMult"></param>
/// <param name="cacheTS"></param>
/// <returns></returns>
protected async Task<bool> setRSV(string rKey, int rValInt, int cacheMult)
protected async Task<bool> setRSV(string rKey, int rValInt, TimeSpan cacheTS)
{
bool fatto = false;
var redisDataList = Encoding.UTF8.GetBytes($"{rValInt}");
await distributedCache.SetAsync(rKey, redisDataList, cacheOpt(cacheMult));
fatto = true;
bool fatto = await setRSV(rkeyAppInfo, $"{rValInt}", cacheTS);
return fatto;
}
@@ -426,34 +427,40 @@ namespace MP.Land.Data
/// </summary>
private static string apiUrl = "https://liman.egalware.com/ELM.API/";
private static JsonSerializerSettings? JSSettings;
/// <summary>
/// Chiave redis x info della licenza
/// </summary>
private static string rkeyAppInfo = "LongCache:AppInfo";
//private static string apiUrl = "https://localhost:44351/";
private readonly IDistributedCache distributedCache;
/// <summary>
/// Elenco obj in cache
/// </summary>
private List<string> cachedDataList = new List<string>();
/// <summary>
/// Fattorte conversione cache sliding --> 1 h
/// Durata cache lunga IN SECONDI
/// </summary>
private int cacheFact = 12;
private int cacheTtlLong = 60 * 5;
/// <summary>
/// Durata assoluta massima della cache IN SECONDI
/// Durata cache breve IN SECONDI
/// </summary>
private int chAbsExp = 60 * 5;
private int cacheTtlShort = 60 * 1;
/// <summary>
/// Durata della cache IN SECONDI in modalità inattiva (non acceduta) prima di venire
/// rimossa NON estende oltre il tempo massimo di validità della cache (chAbsExp)
/// Oggetto per connessione a REDIS
/// </summary>
private int chSliExp = 60 * 1;
private IConnectionMultiplexer redisConn;
//ISubscriber sub = redis.GetSubscriber();
/// <summary>
/// Oggetto DB redis da impiegare x chiamate R/W
/// </summary>
private IDatabase redisDb = null!;
private Random rnd = new Random();
#endregion Private Fields
@@ -465,18 +472,6 @@ namespace MP.Land.Data
#region Private Methods
/// <summary>
/// Opzioni cache con moltiplicatore durata risp durata base (1/5 minuti)
/// </summary>
/// <param name="multFact"></param>
/// <returns></returns>
private DistributedCacheEntryOptions cacheOpt(int multFact)
{
var numSecAbsExp = chAbsExp * multFact;
var numSecSliExp = chSliExp * multFact;
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp));
}
/// <summary>
/// Elenco attivazioni attuali
/// </summary>
+2 -2
View File
@@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>MP.Land</RootNamespace>
<Version>6.16.2409.0317</Version>
<Version>6.16.2409.0318</Version>
</PropertyGroup>
<ItemGroup>
@@ -51,11 +51,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.12" />
<PackageReference Include="RestSharp" Version="112.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.12" />
</ItemGroup>
<ItemGroup>
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>Modulo Tablet MAPO - DotNet6</i>
<h4>Versione: 6.16.2409.0317</h4>
<h4>Versione: 6.16.2409.0318</h4>
<br />
Note di rilascio:
<ul>
+1 -1
View File
@@ -1 +1 @@
6.16.2409.0317
6.16.2409.0318
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>6.16.2409.0317</version>
<version>6.16.2409.0318</version>
<url>https://nexus.steamware.net/repository/SWS/MP-LAND/stable/LAST/MP.Land.zip</url>
<changelog>https://nexus.steamware.net/repository/SWS/MP-LAND/stable/LAST/ChangeLog.html</changelog>
<mandatory>false</mandatory>
-8
View File
@@ -118,14 +118,6 @@ namespace MP.Land
options.FallbackPolicy = options.DefaultPolicy;
});
services.AddStackExchangeRedisCache(options =>
{
//options.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions() { KeepAlive = 180, DefaultDatabase = 1, EndPoints = { { "localhost", 6379 } } };
options.Configuration = Configuration["ConnectionStrings:Redis"];
options.InstanceName = "MP:Land";
});
// REDIS setup
string connStringRedis = Configuration.GetConnectionString("Redis");
string redisSrvAddr = connStringRedis.Substring(0, connStringRedis.IndexOf(":"));