Update API x email e lettura dati proj
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using GPW.CORE.Api.Data;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text;
|
||||
|
||||
namespace GPW.CORE.Api.Controllers
|
||||
{
|
||||
@@ -8,21 +10,32 @@ namespace GPW.CORE.Api.Controllers
|
||||
public class CheckProj : ControllerBase
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Dataservice x accesso DB
|
||||
/// </summary>
|
||||
protected ApiDataService dataService { get; set; }
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public CheckProj(ILogger<WeatherForecastController> logger)
|
||||
public CheckProj(ILogger<WeatherForecastController> logger, ApiDataService DataService)
|
||||
{
|
||||
_logger = logger;
|
||||
dataService = DataService;
|
||||
_logger.LogInformation("Avviata classe CheckProj");
|
||||
}
|
||||
|
||||
[HttpGet("TryLock")]
|
||||
public async Task<string> Get()
|
||||
{
|
||||
string answ = "ND";
|
||||
// provo ad eseguire...
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// recupero progetti attivi...
|
||||
var currProj = await dataService.AnagProjActiv();
|
||||
sb.Append($"Found {currProj.Count} Active Projects");
|
||||
|
||||
await Task.Delay(100);
|
||||
|
||||
answ = sb.ToString();
|
||||
return answ;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
using NLog;
|
||||
using GPW.CORE.Data.DbModels;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using StackExchange.Redis.Extensions.Core.Abstractions;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace GPW.CORE.Api.Data
|
||||
{
|
||||
public class ApiDataService
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private static IConfiguration _configuration;
|
||||
|
||||
private static ILogger<ApiDataService> _logger;
|
||||
@@ -14,17 +21,36 @@ namespace GPW.CORE.Api.Data
|
||||
|
||||
//private readonly IDistributedCache distributedCache;
|
||||
private readonly IRedisCacheClient _redisCacheClient;
|
||||
|
||||
/// <summary>
|
||||
/// Elenco obj in cache
|
||||
/// </summary>
|
||||
private List<string> cachedDataList = new List<string>();
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Protected Fields
|
||||
|
||||
protected const string rKeyProjAct = "Cache:ProjAct";
|
||||
|
||||
/// <summary>
|
||||
/// TTL da 1 min x cache Redis
|
||||
/// </summary>
|
||||
protected const int shortTTL = 60 * 5;
|
||||
|
||||
#endregion Protected Fields
|
||||
|
||||
#region Public Fields
|
||||
|
||||
/// <summary>
|
||||
/// Classe Accesso metodi DB
|
||||
/// </summary>
|
||||
public static CORE.Data.Controllers.GPWController dbController;
|
||||
|
||||
#endregion Public Fields
|
||||
|
||||
#region Public Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Init classe
|
||||
/// </summary>
|
||||
@@ -39,7 +65,6 @@ namespace GPW.CORE.Api.Data
|
||||
_configuration = configuration;
|
||||
_emailSender = emailSender;
|
||||
_redisCacheClient = redisCacheClient;
|
||||
//this.distributedCache = distributedCache;
|
||||
|
||||
// conf DB
|
||||
string connStrDB = _configuration.GetConnectionString("GPW.DB");
|
||||
@@ -53,5 +78,110 @@ namespace GPW.CORE.Api.Data
|
||||
_logger.LogInformation("DbController OK");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
/// <summary>
|
||||
/// Recupero chiave da redis
|
||||
/// </summary>
|
||||
/// <param name="rKey"></param>
|
||||
/// <returns></returns>
|
||||
protected async Task<string> getRSV(string rKey)
|
||||
{
|
||||
string answ = await _redisCacheClient.GetDbFromConfiguration().GetAsync<string>(rKey);
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Salvataggio chiave in redis
|
||||
/// </summary>
|
||||
/// <param name="rKey"></param>
|
||||
/// <param name="rVal"></param>
|
||||
/// <param name="ttlSec"></param>
|
||||
/// <returns></returns>
|
||||
protected async Task<bool> setRSV(string rKey, string rVal, int ttlSec)
|
||||
{
|
||||
bool fatto = false;
|
||||
await _redisCacheClient.GetDbFromConfiguration().AddAsync(rKey, rVal, DateTimeOffset.Now.AddSeconds(ttlSec));
|
||||
fatto = true;
|
||||
return fatto;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Salvataggio chiave in redis
|
||||
/// </summary>
|
||||
/// <param name="rKey"></param>
|
||||
/// <param name="rValInt"></param>
|
||||
/// <param name="ttlSec"></param>
|
||||
/// <returns></returns>
|
||||
protected async Task<bool> setRSV(string rKey, int rValInt, int ttlSec)
|
||||
{
|
||||
bool fatto = false;
|
||||
await _redisCacheClient.GetDbFromConfiguration().AddAsync<int>(rKey, rValInt, DateTimeOffset.Now.AddSeconds(ttlSec));
|
||||
fatto = true;
|
||||
return fatto;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registra in cache chiave se non fosse già in elenco
|
||||
/// </summary>
|
||||
/// <param name="newKey"></param>
|
||||
protected void trackCache(string newKey)
|
||||
{
|
||||
if (!cachedDataList.Contains(newKey))
|
||||
{
|
||||
cachedDataList.Add(newKey);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Protected Methods
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Recupera l'elenco progetti (tutti)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<AnagProgettiModel>> AnagProjActiv()
|
||||
{
|
||||
List<AnagProgettiModel> dbResult = new List<AnagProgettiModel>();
|
||||
string cacheKey = $"{rKeyProjAct}";
|
||||
trackCache(cacheKey);
|
||||
string rawData = await getRSV(cacheKey);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
dbResult = JsonConvert.DeserializeObject<List<AnagProgettiModel>>(rawData);
|
||||
}
|
||||
else
|
||||
{
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
dbResult = dbController.AnagProjAll(true);
|
||||
rawData = JsonConvert.SerializeObject(dbResult, Formatting.None, new JsonSerializerSettings(){ ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
||||
await setRSV(cacheKey, rawData, shortTTL);
|
||||
stopWatch.Stop();
|
||||
TimeSpan ts = stopWatch.Elapsed;
|
||||
Log.Trace($"Effettuata lettura da DB per AttivazioniByLic: {ts.TotalMilliseconds} ms");
|
||||
}
|
||||
|
||||
return await Task.FromResult(dbResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// invalida tutta la cache in caso di update
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task InvalidateAllCache()
|
||||
{
|
||||
foreach (var item in cachedDataList)
|
||||
{
|
||||
await _redisCacheClient.GetDbFromConfiguration().RemoveAsync(item);
|
||||
}
|
||||
cachedDataList = new List<string>();
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace GPW.CORE.Api.Data
|
||||
{
|
||||
public interface IEmailSender
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace GPW.CORE.Api.Data
|
||||
{
|
||||
public interface IRedisCacheClient
|
||||
{
|
||||
}
|
||||
}
|
||||
+39
-1
@@ -1,12 +1,50 @@
|
||||
using GPW.CORE.Api.Data;
|
||||
using GPW.CORE.Data;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using StackExchange.Redis.Extensions.Core.Configuration;
|
||||
using StackExchange.Redis.Extensions.Newtonsoft;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// configuration setup
|
||||
ConfigurationManager configuration = builder.Configuration;
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// abilitazione x email management con MailKit
|
||||
builder.Services.AddTransient<IEmailSender, MailKitEmailSender>();
|
||||
builder.Services.Configure<MailKitEmailSenderOptions>(options =>
|
||||
{
|
||||
options.Host_Address = configuration["ExternalProviders:MailKit:SMTP:Address"];
|
||||
options.Host_Port = Convert.ToInt32(configuration["ExternalProviders:MailKit:SMTP:Port"]);
|
||||
options.Host_Username = configuration["ExternalProviders:MailKit:SMTP:Account"];
|
||||
options.Host_Password = configuration["ExternalProviders:MailKit:SMTP:Password"];
|
||||
options.Sender_EMail = configuration["ExternalProviders:MailKit:SMTP:SenderEmail"];
|
||||
options.Sender_Name = configuration["ExternalProviders:MailKit:SMTP:SenderName"];
|
||||
});
|
||||
|
||||
//services.AddStackExchangeRedisCache(options =>
|
||||
//{
|
||||
// options.Configuration = Configuration.GetConnectionString("Redis");
|
||||
//});
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(c => c.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
|
||||
|
||||
builder.Services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>((options) =>
|
||||
{
|
||||
return configuration.GetSection("Redis").Get<RedisConfiguration>();
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<ApiDataService>();
|
||||
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
@@ -25,5 +25,19 @@
|
||||
"MailDest": {
|
||||
"Admin": "samuele@steamware.net",
|
||||
"ProcOp": "ceo@steamware.net"
|
||||
},
|
||||
"Redis": {
|
||||
"Password": "",
|
||||
"AllowAdmin": false,
|
||||
"Ssl": false,
|
||||
"ConnectTimeout": 6000,
|
||||
"ConnectRetry": 2,
|
||||
"Hosts": [
|
||||
{
|
||||
"Host": "localhost",
|
||||
"Port": "6379"
|
||||
}
|
||||
],
|
||||
"Database": 15
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ namespace GPW.CORE.UI.Data
|
||||
{
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
dbResult = dbController.AnagProjAll();
|
||||
dbResult = dbController.AnagProjAll(false);
|
||||
rawData = JsonConvert.SerializeObject(dbResult, JSSettings);
|
||||
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
||||
await distributedCache.SetAsync(currKey, redisDataList, cacheOpt(true));
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Version>3.0.2201.2514</Version>
|
||||
<Version>3.0.2201.2517</Version>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<body>
|
||||
<i>GPW - Gestione Presenze Web</i>
|
||||
<h4>Versione: 3.0.2201.2514</h4>
|
||||
<h4>Versione: 3.0.2201.2517</h4>
|
||||
<br /> Note di rilascio:
|
||||
<ul>
|
||||
<li>
|
||||
|
||||
@@ -1 +1 @@
|
||||
3.0.2201.2514
|
||||
3.0.2201.2517
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<item>
|
||||
<version>3.0.2201.2514</version>
|
||||
<version>3.0.2201.2517</version>
|
||||
<url>http://nexus.steamware.net/repository/SWS/GWMS/stable/0/GWMS.UI.zip</url>
|
||||
<changelog>http://nexus.steamware.net/repository/SWS/GWMS/stable/0/ChangeLog.html</changelog>
|
||||
<mandatory>false</mandatory>
|
||||
|
||||
Reference in New Issue
Block a user