Fix GetOrAddMany incorrect key-value mapping.#24734
Conversation
Refactored GetOrAddMany and GetOrAddManyAsync in DistributedCache to ensure returned key-value pairs are correctly mapped by key, regardless of order from the factory. Added unit tests to verify correct mapping and concurrency behavior.
GetOrAddMany incorrect key-value mapping.
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the GetOrAddMany and GetOrAddManyAsync methods where returned key-value pairs were not correctly mapped by key when the factory returned values in a different order than expected. The fix refactors both methods to use a dictionary-based approach that ensures the returned array maintains the original input key order while correctly mapping each key to its value regardless of the order returned by the factory or retrieved from cache.
Changes:
- Refactored
GetOrAddManyandGetOrAddManyAsyncto build a dictionary of cached results and map missing keys correctly - Added test
GetOrAddManyAsync_Should_Return_Values_By_Key_With_Uowto verify correct mapping when using Unit of Work - Added test
GetOrAddManyAsync_Should_Map_By_Key_Under_Concurrencyto verify correct mapping under concurrent access when factory returns values in reversed order
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs |
Refactored GetOrAddMany and GetOrAddManyAsync to use dictionary-based mapping ensuring results are returned in the same order as input keys; removed trailing whitespace on line 22 |
framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs |
Added two new tests to verify correct key-value mapping behavior with UoW and under concurrency; added System.Threading import |
framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs
Show resolved
Hide resolved
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## rel-10.0 #24734 +/- ##
============================================
- Coverage 51.02% 51.01% -0.02%
============================================
Files 3294 3299 +5
Lines 107441 107713 +272
Branches 8255 8266 +11
============================================
+ Hits 54823 54950 +127
- Misses 50923 51058 +135
- Partials 1695 1705 +10 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Refactored
GetOrAddManyandGetOrAddManyAsyncinDistributedCacheto ensure returned key-value pairs are correctly mapped by key, regardless of order from the factory. Added unit tests to verify correct mapping and concurrency behavior.