Rimozione cache vecchio stile
This commit is contained in:
@@ -51,9 +51,11 @@ namespace MP.Land.Data
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
||||
};
|
||||
|
||||
#if false
|
||||
// conf cache
|
||||
this.memoryCache = memoryCache;
|
||||
this.distributedCache = distributedCache;
|
||||
this.distributedCache = distributedCache;
|
||||
#endif
|
||||
// conf DB
|
||||
string connStr = _configuration.GetConnectionString("MP.Land");
|
||||
if (string.IsNullOrEmpty(connStr))
|
||||
@@ -239,6 +241,7 @@ namespace MP.Land.Data
|
||||
{
|
||||
source = "REDIS";
|
||||
var tempResult = JsonConvert.DeserializeObject<List<UserDirittiModel>>(rawData);
|
||||
#if false
|
||||
if (tempResult == null)
|
||||
{
|
||||
dbResult = new List<UserDirittiModel>();
|
||||
@@ -246,7 +249,9 @@ namespace MP.Land.Data
|
||||
else
|
||||
{
|
||||
dbResult = tempResult;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
dbResult = tempResult ?? new List<UserDirittiModel>();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -321,13 +326,44 @@ namespace MP.Land.Data
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
public async Task ResetCache()
|
||||
public async Task FlushRedisCache()
|
||||
{
|
||||
RedisValue pattern = new RedisValue($"{redisBaseAddr}:*");
|
||||
bool answ = await ExecFlushRedisPattern(pattern);
|
||||
#if false
|
||||
string cacheKey = ":MP:VOCAB";
|
||||
await distributedCache.RemoveAsync(cacheKey);
|
||||
await distributedCache.RemoveAsync(cacheKey);
|
||||
#endif
|
||||
// reset in RAM
|
||||
Vocabolario = new Dictionary<string, string>();
|
||||
CheckVoc();
|
||||
await CheckVoc();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esegue flush memoria redis dato pattern
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
||||
{
|
||||
bool answ = false;
|
||||
var listEndpoints = redisConn.GetEndPoints();
|
||||
foreach (var endPoint in listEndpoints)
|
||||
{
|
||||
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
|
||||
var server = redisConn.GetServer(endPoint);
|
||||
if (server != null)
|
||||
{
|
||||
var keyList = server.Keys(redisDb.Database, pattern);
|
||||
foreach (var item in keyList)
|
||||
{
|
||||
await redisDb.KeyDeleteAsync(item);
|
||||
}
|
||||
answ = true;
|
||||
}
|
||||
}
|
||||
|
||||
return answ;
|
||||
}
|
||||
|
||||
public string Traduci(string lemma, string lingua = "IT")
|
||||
@@ -407,30 +443,29 @@ namespace MP.Land.Data
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected void CheckVoc()
|
||||
protected async Task CheckVoc()
|
||||
{
|
||||
string cacheKey = ":MP:VOCAB";
|
||||
string rawData;
|
||||
if (Vocabolario.Count == 0)
|
||||
{
|
||||
var redisDataList = distributedCache.Get(cacheKey);
|
||||
if (redisDataList != null)
|
||||
string source = "DB";
|
||||
string currKey = $"{redisBaseAddr}:MP:VOCAB";
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
string? rawData = await redisDb.StringGetAsync(currKey);
|
||||
if (!string.IsNullOrEmpty(rawData) && rawData.Length > 2)
|
||||
{
|
||||
rawData = Encoding.UTF8.GetString(redisDataList);
|
||||
Vocabolario = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
||||
source = "REDIS";
|
||||
var tempResult = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
||||
Vocabolario = tempResult ?? new Dictionary<string, string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
Vocabolario = dbController.VocabolarioGetAll().ToDictionary(x => $"{x.Lingua}#{x.Lemma}", x => x.Traduzione);
|
||||
rawData = JsonConvert.SerializeObject(Vocabolario);
|
||||
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
||||
distributedCache.Set(cacheKey, redisDataList, cacheOptLong);
|
||||
stopWatch.Stop();
|
||||
TimeSpan ts = stopWatch.Elapsed;
|
||||
Log.Trace($"Effettuato rilettura da DB + caching per VocabolarioModel: {ts.TotalMilliseconds} ms");
|
||||
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
||||
}
|
||||
sw.Stop();
|
||||
Log.Trace($"Rilettura Vocabolario | {source} | {sw.ElapsedMilliseconds} ms");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,9 +485,11 @@ namespace MP.Land.Data
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private static string Modulo = "";
|
||||
#if false
|
||||
private readonly IDistributedCache distributedCache;
|
||||
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Durata cache lunga IN SECONDI
|
||||
@@ -464,6 +501,7 @@ namespace MP.Land.Data
|
||||
/// </summary>
|
||||
private int cacheTtlShort = 60 * 1;
|
||||
|
||||
#if false
|
||||
/// <summary>
|
||||
/// Durata assoluta massima della cache
|
||||
/// </summary>
|
||||
@@ -473,7 +511,8 @@ namespace MP.Land.Data
|
||||
/// 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;
|
||||
private int chSliExp = 5;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Oggetto per connessione a REDIS
|
||||
@@ -493,6 +532,7 @@ namespace MP.Land.Data
|
||||
|
||||
#region Private Properties
|
||||
|
||||
#if false
|
||||
private DistributedCacheEntryOptions cacheOpt
|
||||
{
|
||||
get
|
||||
@@ -507,7 +547,8 @@ namespace MP.Land.Data
|
||||
{
|
||||
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(chAbsExp * 10)).SetSlidingExpiration(TimeSpan.FromMinutes(chSliExp));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private string CodApp { get; set; } = "";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user