diff --git a/LiMan.Api/Data/ApiDataService.cs b/LiMan.Api/Data/ApiDataService.cs index 255625f..1b428c6 100644 --- a/LiMan.Api/Data/ApiDataService.cs +++ b/LiMan.Api/Data/ApiDataService.cs @@ -28,7 +28,7 @@ namespace LiMan.APi.Data private static ILogger _logger; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); private readonly IEmailSender _emailSender; - private readonly IDistributedCache distributedCache; + //private readonly IDistributedCache distributedCache; private readonly IRedisCacheClient _redisCacheClient; /// @@ -74,7 +74,21 @@ namespace LiMan.APi.Data protected async Task setRSV(string rKey, string rVal, int ttlSec) { bool fatto = false; - await _redisCacheClient.GetDbFromConfiguration().AddAsync(rKey, rVal, DateTimeOffset.Now.AddSeconds(ttlSec)); + await _redisCacheClient.GetDbFromConfiguration().AddAsync(rKey, rVal, DateTimeOffset.Now.AddSeconds(ttlSec)); + fatto = true; + return fatto; + } + /// + /// Salvataggio chiave in redis + /// + /// + /// + /// + /// + protected async Task setRSV(string rKey, int rValInt, int ttlSec) + { + bool fatto = false; + await _redisCacheClient.GetDbFromConfiguration().AddAsync(rKey, rValInt, DateTimeOffset.Now.AddSeconds(ttlSec)); fatto = true; return fatto; } @@ -90,13 +104,14 @@ namespace LiMan.APi.Data /// /// /// - public ApiDataService(IConfiguration configuration, ILogger logger, IDistributedCache distributedCache, IEmailSender emailSender, IRedisCacheClient redisCacheClient) + public ApiDataService(IConfiguration configuration, ILogger logger, IEmailSender emailSender, IRedisCacheClient redisCacheClient) + //public ApiDataService(IConfiguration configuration, ILogger logger, IDistributedCache distributedCache, IEmailSender emailSender, IRedisCacheClient redisCacheClient) { _logger = logger; _configuration = configuration; _emailSender = emailSender; - this.distributedCache = distributedCache; _redisCacheClient = redisCacheClient; + //this.distributedCache = distributedCache; // conf DB string connStrDB = _configuration.GetConnectionString("LiMan.DB"); @@ -332,6 +347,7 @@ namespace LiMan.APi.Data { LicenzaModel dbResult = new LicenzaModel(); string cacheKey = $"DATA:Licenze:ListByKey:{chiave}"; +#if false string rawData; var redisDataList = await distributedCache.GetAsync(cacheKey); if (redisDataList != null) @@ -350,7 +366,26 @@ namespace LiMan.APi.Data stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata lettura da DB per LicenzaByMasterKey: {ts.TotalMilliseconds} ms"); + } +#endif + + string rawData = await getRSV(cacheKey); + if (!string.IsNullOrEmpty(rawData)) + { + dbResult = JsonConvert.DeserializeObject(rawData); } + else + { + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + dbResult = dbController.LicenzaByKey(chiave); + rawData = JsonConvert.SerializeObject(dbResult); + await setRSV(cacheKey, rawData, hourTTL); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Trace($"Effettuata lettura da DB per LicenzaByMasterKey: {ts.TotalMilliseconds} ms"); + } + return await Task.FromResult(dbResult); } @@ -482,7 +517,7 @@ namespace LiMan.APi.Data int.TryParse(rawVal, out currCount); } currCount++; - answ = await setRSV(rKey, $"{currCount}", 2 * hourTTL); + answ = await setRSV(rKey, currCount, 2 * hourTTL); return answ; } /// @@ -493,7 +528,7 @@ namespace LiMan.APi.Data { bool answ = false; int currCount = 0; - answ = await setRSV(rKey, $"{currCount}", 2 * hourTTL); + answ = await setRSV(rKey, currCount, 2 * hourTTL); return answ; } diff --git a/LiMan.Api/LiMan.APi.xml b/LiMan.Api/LiMan.APi.xml index 3fb4243..024eacb 100644 --- a/LiMan.Api/LiMan.APi.xml +++ b/LiMan.Api/LiMan.APi.xml @@ -242,7 +242,16 @@ - + + + Salvataggio chiave in redis + + + + + + + Init classe