Update gestione vocabolario con FusionCache
This commit is contained in:
@@ -5,10 +5,11 @@
|
||||
- **Language**: C# (primary), PowerShell (scripts).
|
||||
- **Documentation/Comments**: MUST be in **Italiano**.
|
||||
- **Code Style**: Maintain existing region organization (`#region Public Methods`, etc.).
|
||||
- **Reference Docs**: See `Refactor_Plan.md` for the current migration status and detailed strategy.
|
||||
|
||||
## Development Workflow
|
||||
- **Build & Verification**:
|
||||
- Use a PowerShell script to build the solution to ensure continuous compilability.
|
||||
- Use `./build_all_par.ps1 --agent` to build all solutions silently.
|
||||
- Always verify that changes do not leave partial traces of old classes that break compilation.
|
||||
- **Refactoring Strategy (`MpDataService.cs`)**:
|
||||
- Use `GetOrFetchAsync<T>(string operationName, string cacheKey, Func<Task<T>> fetchFunc, TimeSpan expiration, params string[] tags)` as the standard for all data access.
|
||||
@@ -19,4 +20,5 @@
|
||||
## Architecture Notes
|
||||
- **Multi-Layer Caching**: The system is transitioning from a dual-layer (Redis + DB) to a triple-layer approach via `IFusionCache`.
|
||||
- **Service Responsibility**: `MpDataService` is the central hub for data access, interacting with `MpSpecController` (EFCore) and `MpMongoController` (MongoDB).
|
||||
- **Key Management**: Cache keys are heavily managed via `Utils.redis...` constants.
|
||||
- **Key Management**: Cache keys are heavily managed via `Utils.redis...` constants. Use these to prevent key mismatches.
|
||||
|
||||
|
||||
@@ -2627,21 +2627,36 @@ namespace MP.Data.Controllers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Elenco Vocabolario (completo)
|
||||
/// Elenco Vocabolario di una lingua
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<VocabolarioModel> VocabolarioGetAll()
|
||||
public Dictionary<string, string> VocabolarioGetLang(string lingua)
|
||||
{
|
||||
List<VocabolarioModel> dbResult = new List<VocabolarioModel>();
|
||||
using (var dbCtx = new MoonProContext(options))
|
||||
{
|
||||
dbResult = dbCtx
|
||||
using var dbCtx = new MoonProContext(options);
|
||||
var rawList = dbCtx
|
||||
.DbSetVocabolario
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Lingua.ToLower() == lingua.ToLower())
|
||||
.OrderBy(x => x.Lemma)
|
||||
.ToList();
|
||||
}
|
||||
return dbResult;
|
||||
// Proietto in dizionario
|
||||
return rawList
|
||||
.DistinctBy(t => t.Lemma, StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(t => t.Lemma, t => t.Traduzione, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Elenco Vocabolario (completo) async
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<VocabolarioModel>> VocabolarioGetAllAsync()
|
||||
{
|
||||
using var dbCtx = new MoonProContext(options);
|
||||
return await dbCtx
|
||||
.DbSetVocabolario
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Lemma)
|
||||
.ToListAsync() ?? new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -961,8 +961,6 @@ namespace MP.SPEC.Data
|
||||
string source = "REDIS";
|
||||
RedisValue pattern = Utils.RedValue("*");
|
||||
bool answ = await ExecFlushRedisPatternAsync(pattern);
|
||||
// rileggo vocabolario.,..
|
||||
ObjVocabolario = VocabolarioGetAll();
|
||||
activity?.Stop();
|
||||
LogTrace($"FlushRedisCache | Read from {source}: {activity?.Duration.TotalMilliseconds}ms");
|
||||
return answ;
|
||||
@@ -2260,53 +2258,31 @@ namespace MP.SPEC.Data
|
||||
/// <returns></returns>
|
||||
public string Traduci(string lemma, string lingua)
|
||||
{
|
||||
string answ = $"[{lemma}]";
|
||||
// verifico se ho qualcosa nell'obj vocabolario...
|
||||
if (ObjVocabolario == null || ObjVocabolario.Count == 0)
|
||||
{
|
||||
// inizializzo il vocabolario...
|
||||
ObjVocabolario = VocabolarioGetAll();
|
||||
}
|
||||
var record = ObjVocabolario.Where(x => x.Lingua == lingua && x.Lemma == lemma).FirstOrDefault();
|
||||
if (record != null)
|
||||
{
|
||||
answ = record.Traduzione;
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(lemma)) return string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(lingua)) return lemma;
|
||||
|
||||
/// <summary>
|
||||
/// Elenco completo tabella Vocabolario
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<VocabolarioModel> VocabolarioGetAll()
|
||||
{
|
||||
List<VocabolarioModel>? result = new List<VocabolarioModel>();
|
||||
using var activity = ActivitySource.StartActivity("VocabolarioGetAll");
|
||||
string source = "REDIS";
|
||||
// cerco in redis...
|
||||
RedisValue rawData = redisDb.StringGet(Utils.redisVocabolario);
|
||||
if (!string.IsNullOrEmpty($"{rawData}"))
|
||||
string linguaKey = lingua.ToLowerInvariant().Trim();
|
||||
string cacheKey = $"vocab:{linguaKey}";
|
||||
|
||||
// FusionCache gestisce il lock e recupera l'intero dizionario della lingua.
|
||||
// Se è in L1 (Memory), restituisce l'oggetto C# istantaneamente.
|
||||
// Se non c'è, passa a L2 (Redis) o invoca la factory per caricarlo.
|
||||
var dizionarioLingua = _cache.GetOrSet<Dictionary<string, string>>(
|
||||
cacheKey,
|
||||
_ => dbController.VocabolarioGetLang(linguaKey),
|
||||
options => options
|
||||
.SetDuration(TimeSpan.FromHours(8)) // Durata logica della cache
|
||||
.SetFailSafe(true, TimeSpan.FromHours(1)) // Se Redis/DB è giù, usa i vecchi dati L1
|
||||
);
|
||||
|
||||
// Ricerca O(1) nel dizionario in memoria
|
||||
if (dizionarioLingua != null && dizionarioLingua.TryGetValue(lemma, out var traduzione))
|
||||
{
|
||||
result = JsonConvert.DeserializeObject<List<VocabolarioModel>>($"{rawData}");
|
||||
return traduzione;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = dbController.VocabolarioGetAll();
|
||||
// serializzo e salvo...
|
||||
rawData = JsonConvert.SerializeObject(result);
|
||||
redisDb.StringSet(Utils.redisVocabolario, rawData, getRandTOut(redisLongTimeCache / 5));
|
||||
source = "DB";
|
||||
}
|
||||
if (result == null)
|
||||
{
|
||||
result = new List<VocabolarioModel>();
|
||||
}
|
||||
activity?.SetTag("data.source", source);
|
||||
activity?.SetTag("result.count", result.Count);
|
||||
activity?.Stop();
|
||||
LogTrace($"VocabolarioGetAll Read from {source}: {activity?.Duration.TotalMilliseconds}ms");
|
||||
return result;
|
||||
|
||||
// Fallback: se la parola non è censita, restituisce il lemma originale
|
||||
return lemma;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -2496,11 +2472,6 @@ namespace MP.SPEC.Data
|
||||
|
||||
private string MpIoNS = "";
|
||||
|
||||
/// <summary>
|
||||
/// Oggetto vocabolario x uso continuo traduzione
|
||||
/// </summary>
|
||||
private List<VocabolarioModel> ObjVocabolario = new List<VocabolarioModel>();
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
/// <summary>
|
||||
@@ -2551,6 +2522,10 @@ namespace MP.SPEC.Data
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifica caricamento dizionario ConfigData
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task EnsureConfigLoadedAsync()
|
||||
{
|
||||
if (_configData.Count == 0)
|
||||
@@ -2562,6 +2537,21 @@ namespace MP.SPEC.Data
|
||||
.ToDictionary(g => g.Key, g => g.First().Valore);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Verifica caricamento Vocabolario
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task EnsureVocabolarioLoadedAsync()
|
||||
{
|
||||
if (_configData.Count == 0)
|
||||
{
|
||||
var list = await ConfigGetAllAsync();
|
||||
|
||||
_configData = list
|
||||
.GroupBy(x => x.Chiave)
|
||||
.ToDictionary(g => g.Key, g => g.First().Valore);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementa gestione recupero cache da memoria o da obj esterno + cache memoria + tracking attività
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>MP.SPEC</RootNamespace>
|
||||
<Version>8.16.2605.2907</Version>
|
||||
<Version>8.16.2605.2908</Version>
|
||||
<UserSecretsId>1800a78a-6ff1-40f9-b490-87fb8bfc1394</UserSecretsId>
|
||||
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<body>
|
||||
<i>Modulo MAPOSPEC </i>
|
||||
<h4>Versione: 8.16.2605.2907</h4>
|
||||
<h4>Versione: 8.16.2605.2908</h4>
|
||||
<br /> Note di rilascio:
|
||||
<ul>
|
||||
<li>
|
||||
|
||||
@@ -1 +1 @@
|
||||
8.16.2605.2907
|
||||
8.16.2605.2908
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<item>
|
||||
<version>8.16.2605.2907</version>
|
||||
<version>8.16.2605.2908</version>
|
||||
<url>https://nexus.steamware.net/repository/SWS/MP-SPEC/stable/LAST/MP.SPEC.zip</url>
|
||||
<changelog>https://nexus.steamware.net/repository/SWS/MP-SPEC/stable/LAST/ChangeLog.html</changelog>
|
||||
<mandatory>false</mandatory>
|
||||
|
||||
+81
-3
@@ -14,16 +14,94 @@ Migrare la logica di caching manuale (Redis + DB) verso l'utilizzo di `IFusionCa
|
||||
## Stato Avanzamento
|
||||
|
||||
### Fase 1: Analisi e Mapping (Completata)
|
||||
- Analisi di `MpDataService.cs` effettuata.
|
||||
- Identificati i metodi con caching manuale (Redis/DB) e quelli già migrati a `GetOrFetchAsync`.
|
||||
|
||||
### Fase 2: Refactoring Metodi di Lettura (Cache-aside)
|
||||
# Piano di Refactoring: Migrazione a FusionCache in `MpDataService.cs`
|
||||
|
||||
Stiamo lavorando sul progetto MP-SPEC.sln, dentro la cartella MP-SPEC (ed i progetti da cui dipende).
|
||||
|
||||
#### Fase 4: Verifica
|
||||
Voglio ottimizzare il file Data\MpDataService.cs
|
||||
|
||||
## Obiettivo
|
||||
Migrare la logica di caching manuale (Redis + DB) verso l'utilizzo di `IFusionCache` per implementare un approccio multi-layer (Memory + Redis + DB), standardizzando l'accesso ai dati.
|
||||
|
||||
## Strategia di Migrazione
|
||||
- **Metodo Standard**: `GetOrFetchAsync<T>(string operationName, string cacheKey, Func<Task<T>> fetchFunc, TimeSpan expiration, params string[] tagList)`.
|
||||
- **Invalidazione**: Utilizzare i tag tramite `FlushCacheByTagsAsync`.
|
||||
|
||||
## Stato Avanzamento
|
||||
|
||||
### Fase 1: Analisi e Mapping (Completata)
|
||||
- Analisi di `MpDataService.cs` effettuata.
|
||||
- Identificati i metodi con caching manuale (Redis/DB) e quelli già migrati a `GetOrFetchAsync`.
|
||||
|
||||
### Fase 2: Refactoring Metodi di Lettura (Cache-aside) (In corso)
|
||||
|
||||
#### ✅ Metodi Migrati (Usano già `GetOrFetchAsync`)
|
||||
- `AnagEventiGeneralAsync`
|
||||
- `AnagStatiCommAsync`
|
||||
- `AnagTipoArtLvAsync`
|
||||
- `ArticleWithDossierAsync`
|
||||
- `ArticoliCountAsync`
|
||||
- `ArticoliCountSearchAsync`
|
||||
- `ArticoliGetByTipoAsync`
|
||||
- `ArticoliGetSearchAsync`
|
||||
- `ArticoliInKitAsync`
|
||||
- `ConfigGetAllAsync`
|
||||
- `DossiersGetLastFiltAsync`
|
||||
- `ElencoAziendeAsync`
|
||||
- `ElencoGruppiFaseAsync`
|
||||
- `ElencoLinkAsync`
|
||||
- `IstKitFiltAsync`
|
||||
- `ListGiacenzeAsync`
|
||||
- `MacchineGetFiltAsync`
|
||||
- `MacchineRecipeArchiveAsync`
|
||||
- `MacchineRecipeConfAsync`
|
||||
- `MacchineWithFluxAsync`
|
||||
- `MachineWithOdlAsync`
|
||||
- `MachIobConfAsync`
|
||||
- `OdlListGetFiltAsync`
|
||||
- `OperatoriGetFiltAsync`
|
||||
- `ParametriGetFiltAsync`
|
||||
- `PODL_getDictOdlPodlAsync` (Parziale/Ibrido)
|
||||
- `POdlGetByOdlAsync`
|
||||
- `POdlToKitListGetFiltAsync`
|
||||
- `StatoMacchinaAsync`
|
||||
- `TksScoreAsync`
|
||||
- `WipKitFiltAsync`
|
||||
|
||||
#### 🛠️ Metodi da Migrare (Usano ancora Redis/DB manuale)
|
||||
- [ ] Migrazione di `ActionGetReq` (linea 110: usa `redisDb.StringGetAsync`).
|
||||
- [ ] Migrazione di `ActionSetReq` (linea 136: usa `BroadastMsgPipe.saveAndSendMessage`).
|
||||
- [ ] Migrazione di `AnagGruppiDelete`/`Upsert` (linea 189/208: usa `ExecFlushRedisPattern`).
|
||||
- [ ] Migrazione di `ArticoliDeleteRecord`/`UpdateRecord` (linea 296/372: usa `resetCacheArticoli`).
|
||||
- [ ] Migrazione di `DbDedupStats` (linea 516: usa `redisDb.StringGet`).
|
||||
- [ ] Migrazione di `DossiersDeleteRecord` (linea 554: usa `ExecFlushRedisPatternAsync`).
|
||||
- [ ] Migrazione di `DossiersTakeParamsSnapshotLast` (linea 613: usa `ExecFlushRedisPatternAsync`).
|
||||
- [ ] Migrazione di `ElencoRepartiDTO` (linea 697: usa `redisDb.StringGet` e `StringSet`).
|
||||
- [ ] Migrazione di `MseGetAll` (linea 1460: usa `redisDb.StringGet` e `StringSetAsync`).
|
||||
- [ ] Migrazione di `OdlByBatch` (linea 1512: usa `redisDb.StringGet` e `StringSet`).
|
||||
- [ ] Migrazione di `OdlByKey` (linea 1546: usa `redisDb.StringGet` e `StringSet`).
|
||||
- [ ] Migrazione di `PODL_getByKey` (linea 1779: usa `redisDb.StringGet` e `StringSet`).
|
||||
- [ ] Migrazione di `PodlIstKitDelete` (linea 1842: usa `ExecFlushRedisPattern`).
|
||||
- [ ] Migrazione di `POdlListByKitParent` (linea 1863: usa `redisDb.StringGet` e `StringSet`).
|
||||
- [ ] Migrazione di `ProcFLStats` (linea 1992: usa `redisDb.StringSet`).
|
||||
- [ ] Migrazione di `RecDbMaintStat` (linea 2451: usa `redisDb.StringSet`).
|
||||
- [ ] Migrazione di `VocabolarioGetAll` (linea 2278: usa `redisDb.StringGet` e `StringSet`).
|
||||
|
||||
### Fase 4: Verifica
|
||||
- [ ] Verificare la compilazione della soluzione tramite script PowerShell.
|
||||
- [ ] Controllare che i log (NLog) continuino a riflettere correttamente le operazioni.
|
||||
- [ ] Controllare che i log (NLog) continuano a riflettere correttamente le operazioni.
|
||||
|
||||
## Rischi e Mitigazioni
|
||||
- **Rischio**: Discrepanza nelle chiavi di cache tra vecchio e nuovo sistema.
|
||||
- *Mitigazione*: Utilizzare rigorosamente le costanti in `Utils.redis...` per garantire che le chiavi siano identiche o gestite dal nuovo sistema.
|
||||
- **Rischio**: Errori di serializzazione.
|
||||
- *Mitigazione*: `FusionCache` gestisce la serializzazione, ma è necessario assicurarsi che i tipi di ritorno siano compatibili con le aspettative dei chiamanti.
|
||||
|
||||
|
||||
### Fase 4: Verifica
|
||||
- [ ] Verificare la compilazione della soluzione tramite script PowerShell.
|
||||
- [ ] Controllare che i log (NLog) continuano a riflettere correttamente le operazioni.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user