diff --git a/MP.Land/Data/AppAuthService.cs b/MP.Land/Data/AppAuthService.cs index 665a7977..74092871 100644 --- a/MP.Land/Data/AppAuthService.cs +++ b/MP.Land/Data/AppAuthService.cs @@ -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>(rawData); +#if false if (tempResult == null) { dbResult = new List(); @@ -246,7 +249,9 @@ namespace MP.Land.Data else { dbResult = tempResult; - } + } +#endif + dbResult = tempResult ?? new List(); } 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(); - CheckVoc(); + await CheckVoc(); + } + + /// + /// Esegue flush memoria redis dato pattern + /// + /// + /// + private async Task 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>(rawData); + source = "REDIS"; + var tempResult = JsonConvert.DeserializeObject>(rawData); + Vocabolario = tempResult ?? new Dictionary(); } 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 /// /// Durata cache lunga IN SECONDI @@ -464,6 +501,7 @@ namespace MP.Land.Data /// private int cacheTtlShort = 60 * 1; +#if false /// /// Durata assoluta massima della cache /// @@ -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) /// - private int chSliExp = 5; + private int chSliExp = 5; +#endif /// /// 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; } = "";