fix: use IsNullOrDefault in RedisBackedHzCache GetOrSet/GetOrSetAsync for value type support#27
Merged
JoelNygren-Norce merged 2 commits intoJun 18, 2026
Conversation
The early-return optimization added in 0.0.17 used 'value != null' to check for a cache hit. For non-nullable value types (int, bool, etc.), default(T) is not null when boxed, so the check always returned true on cache miss — returning default(T) without ever calling the factory. This broke commerce-admin where GetOrSetAsync<int> was used to cache Application.HostClientId: the factory was never invoked, returning 0 instead of the real client ID, causing 'Unknown application id' errors. Fix: use HzMemoryCache.IsNullOrDefault() consistently, matching the check the inner HzMemoryCache.GetOrSet already uses. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
There was a problem hiding this comment.
Pull request overview
Fixes an incorrect cache-hit check in RedisBackedHzCache.GetOrSet<T> / GetOrSetAsync<T> that prevented factories from running for non-nullable value types (e.g., int) by replacing value != null with !HzMemoryCache.IsNullOrDefault(value).
Changes:
- Update sync and async
RedisBackedHzCacheGetOrSet*methods to useHzMemoryCache.IsNullOrDefaultfor cache-hit detection. - Add integration tests covering value-type factory invocation (sync/async) and cached reuse behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| UnitTests/IntegrationTests.cs | Adds integration coverage for value-type (int) GetOrSet* behavior. |
| RedisBackedHzCache/RedisBackedHzCacheAsync.cs | Fixes async cache-hit check for value types (and touches GetOrSetAsync flow). |
| RedisBackedHzCache/RedisBackedHzCache.cs | Fixes sync cache-hit check for value types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
johan-bjerling
approved these changes
Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The 0.0.17 early-return optimization in
GetOrSet<T>/GetOrSetAsync<T>usedvalue != nullto detect cache hits. For non-nullable value types (int,bool, etc.),default(T)boxes to a non-null object, so the check always returnedtrueon cache miss — returningdefault(T)without ever calling the factory.This broke
commerce-adminwhereGetOrSetAsync<int>cachesApplication.HostClientId: the factory was never invoked,0was returned instead of the real client ID, producing"Unknown application id 1"errors on every request.Fix: Replace
value != nullwithHzMemoryCache.IsNullOrDefault(value)— the same check the innerHzMemoryCache.GetOrSetalready uses.Three integration tests added covering: sync factory call, async factory call, and cached-value reuse — all for
int(value type).Link to Devin session: https://app.devin.ai/sessions/05ffe056c1f549819e082a624b536749
Requested by: @JoelNygren-Norce