3.0 KiB
3.0 KiB
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 thenStringSeton miss). - Some methods use the new
GetOrFetchAsyncpattern (Memory + Redis + DB). GetOrFetchAsyncleveragesIFusionCache, which handles the multi-level complexity and provides better resiliency (fail-safe).
Target Pattern: GetOrFetchAsync
All read methods should be refactored to:
- Use
GetOrFetchAsyncto abstract cache management. - Define a
cacheKey(typically matching the existing Redis key). - Provide an
expiration(TimeSpan). - Provide a
fetchFuncthat calls thedbControllermethod.
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:
- Convert to Async: If the method is synchronous (e.g.,
AnagEventiGeneral), convert it toTask<T>to match theGetOrFetchAsyncsignature. - Map Keys: Ensure the
cacheKeypassed toGetOrFetchAsyncis identical to the old Redis key to prevent cache fragmentation. - Set Expiration: Use appropriate
TimeSpan(e.g.,redisLongTimeCacheorredisShortTimeCacheconverted toTimeSpan). - Cleanup: Remove manual
JsonConvertlogic andredisDb.StringGet/Setcalls. - Verify Tracing: Ensure
ActivitySourceandLogTraceare preserved or integrated within theGetOrFetchAsyncwrapper.
3. Validation
- Ensure all refactored methods are still called correctly by consumers.
- Verify that
GetOrFetchAsynccorrectly hits Memory first, then Redis, then DB. - Confirm that
LogTracestill reports the correct source (MEMORY, REDIS, or DB).