Files
mapo-core/refactor_spec.md
T
2026-05-27 09:46:55 +02:00

64 lines
3.0 KiB
Markdown

# Refactoring Plan: MpDataService Cache Layer Upgrade
## Objective
Migrate all data access methods in `MpDataService.cs` to use the unified multi-level cache stack (IMemoryCache via `IFusionCache` + Redis + DB) using the `GetOrFetchAsync` pattern.
## Current State
- Some methods use a manual "Redis + DB" approach (checking `redisDb.StringGet`, deserializing, and then `StringSet` on miss).
- Some methods use the new `GetOrFetchAsync` pattern (Memory + Redis + DB).
- `GetOrFetchAsync` leverages `IFusionCache`, which handles the multi-level complexity and provides better resiliency (fail-safe).
## Target Pattern: `GetOrFetchAsync`
All read methods should be refactored to:
1. Use `GetOrFetchAsync` to abstract cache management.
2. Define a `cacheKey` (typically matching the existing Redis key).
3. Provide an `expiration` (TimeSpan).
4. Provide a `fetchFunc` that calls the `dbController` method.
## Proposed Work Items
### 1. Analysis & Categorization
Identify all methods in `MpDataService.cs` that currently implement manual Redis caching.
**Candidates for Refactoring (Redis + DB -> Multi-level):**
- `AnagEventiGeneral` (Lines 166-195)
- `AnagEventiGetByMacch` (Lines 201-230)
- `AnagKeyValGetAll` (Lines 273-302)
- `AnagTipoArtLV` (Lines 347-375)
- `ArticleWithDossier` (Lines 381-410)
- `ConfigGetAll` (Lines 907-936)
- `ConfigGetAllAsync` (Lines 943-970)
- `DbDedupStats` (Lines 1046-1067)
- `ElencoGruppiFase` (Lines 1216-1249)
- `ElencoRepartiDTO` (Lines 1267-1303)
- `FluxLogGetLastFilt` (Lines 1569-1605)
- `FluxLogPareto` (Lines 1609-1640)
- `MacchineRecipeArchive` (Lines 2100-2128)
- `MacchineRecipeConf` (Lines 2131-2163)
- `MacchineWithFlux` (Lines 2171-2200)
- `OdlByBatch` (Lines 2308-2336)
- `OdlListAll` (Lines 2490-2499) - *Currently no cache, needs addition?*
- `OdlListGetFilt` (Lines 2513-2542)
- `OperatoriGetFilt` (Lines 2549-2577)
- `ParametriGetFilt` (Lines 2585-2614)
- `POdlGetByKey` (Lines 2658-2697)
- `POdlGetByOdl` (Lines 2705-2744)
- `POdlListByKitParent` (Lines 2770-2800)
- `POdlListGetFilt` (Lines 2812-2841)
- `TksScore` (Lines 3343-3372)
- `VocabolarioGetAll` (Lines 3445-3477)
- `WipKitFilt` (Lines 3543-3570)
### 2. Implementation Steps
For each candidate method:
1. **Convert to Async**: If the method is synchronous (e.g., `AnagEventiGeneral`), convert it to `Task<T>` to match the `GetOrFetchAsync` signature.
2. **Map Keys**: Ensure the `cacheKey` passed to `GetOrFetchAsync` is identical to the old Redis key to prevent cache fragmentation.
3. **Set Expiration**: Use appropriate `TimeSpan` (e.g., `redisLongTimeCache` or `redisShortTimeCache` converted to `TimeSpan`).
4. **Cleanup**: Remove manual `JsonConvert` logic and `redisDb.StringGet/Set` calls.
5. **Verify Tracing**: Ensure `ActivitySource` and `LogTrace` are preserved or integrated within the `GetOrFetchAsync` wrapper.
### 3. Validation
- Ensure all refactored methods are still called correctly by consumers.
- Verify that `GetOrFetchAsync` correctly hits Memory first, then Redis, then DB.
- Confirm that `LogTrace` still reports the correct source (MEMORY, REDIS, or DB).