Skip to content

fix: Redis invalidate not working when there is only 1 entry#26

Merged
sanny-io merged 2 commits into
devfrom
fix/invalidating-one-entry
Jul 19, 2026
Merged

fix: Redis invalidate not working when there is only 1 entry#26
sanny-io merged 2 commits into
devfrom
fix/invalidating-one-entry

Conversation

@sanny-io

@sanny-io sanny-io commented Jul 19, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Fixed cache invalidation so tagged and global invalidation removes matching entries even when only one cache entry exists.
  • Tests

    • Added coverage to verify that invalidating a single cached entry correctly results in a cache miss.

@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Redis tag-based and global invalidation now delete cache keys when scans return a single matching key. A Redis test verifies that invalidating one tagged cached entry changes its subsequent status to miss.

Changes

Redis invalidation

Layer / File(s) Summary
Handle single-key invalidation
src/providers/redis.ts, tests/redis.test.ts
Both invalidation paths delete non-empty key lists, and a test covers invalidating a single tagged cache entry.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the core fix: Redis invalidation now works when only one cache entry matches.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/invalidating-one-entry

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sanny-io
sanny-io merged commit caa063b into dev Jul 19, 2026
1 of 2 checks passed

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/providers/redis.ts (1)

63-69: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Await the Redis deletions before resolving invalidation.
invalidate() and invalidateAll() resolve when the scan stream ends, not when the async DEL calls inside data handlers finish. That can report success while keys are still being deleted, and any DEL rejection is dropped.

  • src/providers/redis.ts#L53-L72: await the per-tag delete promises before resolving.
  • src/providers/redis.ts#L76-L91: do the same for the global scan.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/redis.ts` around lines 63 - 69, Update invalidate() at
src/providers/redis.ts:53-72 and invalidateAll() at src/providers/redis.ts:76-91
so Redis DEL promises from scan data handlers are collected and awaited before
resolving on stream end; propagate any deletion rejection instead of dropping
it.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/redis.test.ts`:
- Around line 1210-1228: Update the cache invalidation test around
extDb.user.exists and extDb.$cache.invalidate to deterministically wait for the
first read’s asynchronous cache.set/tag registration to complete before
invalidating. Use the existing test setup or cache synchronization mechanism
rather than relying on timing, then preserve the final miss assertion.

---

Outside diff comments:
In `@src/providers/redis.ts`:
- Around line 63-69: Update invalidate() at src/providers/redis.ts:53-72 and
invalidateAll() at src/providers/redis.ts:76-91 so Redis DEL promises from scan
data handlers are collected and awaited before resolving on stream end;
propagate any deletion rejection instead of dropping it.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: e6abba89-856e-4c0c-88e8-dca819644c80

📥 Commits

Reviewing files that changed from the base of the PR and between d67eaca and d70dae0.

📒 Files selected for processing (2)
  • src/providers/redis.ts
  • tests/redis.test.ts

Comment thread tests/redis.test.ts
Comment on lines +1210 to +1228
await extDb.user.exists({
cache: {
tags: ['user1'],
ttl: 60,
},
})

await extDb.$cache.invalidate({
tags: ['user1'],
})

await extDb.user.exists({
cache: {
tags: ['user1'],
ttl: 60,
},
})

expect(extDb.$cache.status).toBe('miss')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Synchronize the seed write before invalidating.

The first cached read schedules cache.set asynchronously in src/plugin.ts without awaiting it. Calling invalidate immediately can scan before the single entry or its tag membership exists, allowing the later read to hit for reasons unrelated to this fix. Add a deterministic synchronization step or await the cache write through the test setup.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/redis.test.ts` around lines 1210 - 1228, Update the cache invalidation
test around extDb.user.exists and extDb.$cache.invalidate to deterministically
wait for the first read’s asynchronous cache.set/tag registration to complete
before invalidating. Use the existing test setup or cache synchronization
mechanism rather than relying on timing, then preserve the final miss assertion.

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.

1 participant