← 输入框|github.com|刷新
Skip to content

fix(hooks): don't let cache store a read that an invalidation overtook - #79

Merged
fratzinger merged 1 commit into
mainfrom
fix/cache-stale-read-after-invalidation
Oct 19, 2026
Merged

fratzinger merged 1 commit into
mainfrom
fix/cache-stale-read-after-invalidation

Conversation

@fratzinger

Copy link
Copy Markdown
Member

A get/find that missed the cache and was still reading while a write invalidated the cache stored its result afterwards. That result may predate the write, so it brought back what the invalidation had removed, and the stale entry stayed until it was evicted or its ttl ran out.

  • Every invalidation (through the hooks or cacheHook.invalidate) bumps a generation counter.
  • A miss records the generation it started reading in. set only stores the result if no invalidation ran in between.
  • Hits are still not stored again. They are no longer tracked: only misses are, in a WeakMap that replaces the hits WeakSet.
  • The docs list the new edge case.

Tests: two new cases hold a slow find between the database and the cache while a write runs, once through create and once through invalidate. One existing test now shares a single context between before and after, as a real call does.

🤖 Generated with Claude Code

A `get`/`find` that missed the cache and was still reading while a write
invalidated the cache stored its result afterwards - a result that may
predate the write, bringing back what the invalidation removed until the
entry was evicted or its `ttl` ran out.

Every invalidation now bumps a generation counter. A miss remembers the
generation it started reading in, and `set` only stores the result if no
invalidation ran in between. Hits are still not stored again; they are
simply no longer tracked, since only misses are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying feathers-utils with  Cloudflare Pages  Cloudflare Pages

Latest commit: f9022dd
Status: ✅  Deploy successful!
Preview URL: https://5c242744.feathers-utils.pages.dev
Branch Preview URL: https://fix-cache-stale-read-after-i.feathers-utils.pages.dev

View logs

@fratzinger
fratzinger merged commit 9ca3e25 into main Oct 19, 2026
9 checks passed
@fratzinger
fratzinger deleted the fix/cache-stale-read-after-invalidation branch October 19, 2026 17:41
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