Skip to content

fix: use IsNullOrDefault in RedisBackedHzCache GetOrSet/GetOrSetAsync for value type support#27

Merged
JoelNygren-Norce merged 2 commits into
mainfrom
devin/1781515580-fix-getorset-value-types
Jun 18, 2026
Merged

fix: use IsNullOrDefault in RedisBackedHzCache GetOrSet/GetOrSetAsync for value type support#27
JoelNygren-Norce merged 2 commits into
mainfrom
devin/1781515580-fix-getorset-value-types

Conversation

@JoelNygren-Norce

Copy link
Copy Markdown
Contributor

Summary

The 0.0.17 early-return optimization in GetOrSet<T> / GetOrSetAsync<T> used value != null to detect cache hits. For non-nullable value types (int, bool, etc.), default(T) boxes to a non-null object, so the check always returned true on cache miss — returning default(T) without ever calling the factory.

This broke commerce-admin where GetOrSetAsync<int> caches Application.HostClientId: the factory was never invoked, 0 was returned instead of the real client ID, producing "Unknown application id 1" errors on every request.

Fix: Replace value != null with HzMemoryCache.IsNullOrDefault(value) — the same check the inner HzMemoryCache.GetOrSet already uses.

- if (value != null)
+ if (!HzMemoryCache.IsNullOrDefault(value))

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

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>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@JoelNygren-Norce
JoelNygren-Norce requested a review from Copilot June 15, 2026 09:59
@JoelNygren-Norce
JoelNygren-Norce marked this pull request as ready for review June 15, 2026 10:00
@JoelNygren-Norce
JoelNygren-Norce requested a review from a team as a code owner June 15, 2026 10:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RedisBackedHzCache GetOrSet* methods to use HzMemoryCache.IsNullOrDefault for 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.

Comment thread UnitTests/IntegrationTests.cs
Comment thread UnitTests/IntegrationTests.cs
Comment thread UnitTests/IntegrationTests.cs
Comment thread RedisBackedHzCache/RedisBackedHzCacheAsync.cs
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@JoelNygren-Norce
JoelNygren-Norce merged commit 2076b5f into main Jun 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants