From ce77f20b680f32f0aea2b04994fa501ba75bc601 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Thu, 20 May 2021 12:40:46 +0200 Subject: [PATCH] Errore tipo async/await da debuggare.. --- MP.Stats/Data/MpStatsService.cs | 32 +++++++++++++++++++++++++++++--- MP.Stats/MP.Stats.csproj | 1 + MP.Stats/Startup.cs | 15 ++++++++++----- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/MP.Stats/Data/MpStatsService.cs b/MP.Stats/Data/MpStatsService.cs index 0ef4c259..e2054459 100644 --- a/MP.Stats/Data/MpStatsService.cs +++ b/MP.Stats/Data/MpStatsService.cs @@ -7,6 +7,9 @@ using System.Text; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using MP.Data; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; namespace MP.Stats.Data { @@ -18,6 +21,8 @@ namespace MP.Stats.Data private static ILogger _logger; private static List ActionsList = new List(); + private readonly IDistributedCache distributedCache; + private readonly IMemoryCache memoryCache; #endregion Private Fields @@ -36,10 +41,12 @@ namespace MP.Stats.Data #region Public Constructors - public MpStatsService(IConfiguration configuration, ILogger logger) + public MpStatsService(IConfiguration configuration, ILogger logger, IMemoryCache memoryCache, IDistributedCache distributedCache) { _logger = logger; _configuration = configuration; + this.memoryCache = memoryCache; + this.distributedCache = distributedCache; string connStr = _configuration.GetConnectionString("Mp.Stats"); if (string.IsNullOrEmpty(connStr)) { @@ -100,9 +107,28 @@ namespace MP.Stats.Data return Task.FromResult(dbController.StatScartiGetAll(DataStart, DataEnd, IdxMacchina, IdxODL, KeyRichiesta, CodArticolo).ToArray()); } - public Task StatTurniOeeGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo, string searchVal = "") + public async Task StatTurniOeeGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo, string searchVal = "") { - return Task.FromResult(dbController.StatTurniOeeGetAll(DataStart, DataEnd, IdxMacchina, IdxODL, KeyRichiesta, CodArticolo).ToArray()); + List answ = new List(); + var cacheKey = "oeeData"; + string serializedCustomerList; + var redisCustomerList = await distributedCache.GetAsync(cacheKey); + if (redisCustomerList != null) + { + serializedCustomerList = Encoding.UTF8.GetString(redisCustomerList); + answ = JsonConvert.DeserializeObject>(serializedCustomerList); + } + else + { + answ = dbController.StatTurniOeeGetAll(DataStart, DataEnd, IdxMacchina, IdxODL, KeyRichiesta, CodArticolo); + serializedCustomerList = JsonConvert.SerializeObject(answ); + redisCustomerList = Encoding.UTF8.GetBytes(serializedCustomerList); + var options = new DistributedCacheEntryOptions() + .SetAbsoluteExpiration(DateTime.Now.AddMinutes(10)) + .SetSlidingExpiration(TimeSpan.FromMinutes(2)); + await distributedCache.SetAsync(cacheKey, redisCustomerList, options); + } + return answ.ToArray(); } public Task StatTurniParetoGetAll(DateTime DataStart, DateTime DataEnd, string IdxMacchina, int IdxODL, string KeyRichiesta, string CodArticolo, string searchVal = "") diff --git a/MP.Stats/MP.Stats.csproj b/MP.Stats/MP.Stats.csproj index 51f424aa..b033953d 100644 --- a/MP.Stats/MP.Stats.csproj +++ b/MP.Stats/MP.Stats.csproj @@ -34,6 +34,7 @@ + diff --git a/MP.Stats/Startup.cs b/MP.Stats/Startup.cs index 57542753..905e67a9 100644 --- a/MP.Stats/Startup.cs +++ b/MP.Stats/Startup.cs @@ -79,11 +79,16 @@ namespace MP.Stats public void ConfigureServices(IServiceCollection services) { services.AddBlazorise(options => - { - options.ChangeTextOnKeyPress = true; // optional - }) - .AddBootstrapProviders() - .AddFontAwesomeIcons(); + { + options.ChangeTextOnKeyPress = true; // optional + }) + .AddBootstrapProviders() + .AddFontAwesomeIcons(); + + services.AddStackExchangeRedisCache(options => + { + options.Configuration = "localhost:6379"; + }); services.AddLocalization();