Conversation
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.
Review: Request-scoped cache hit/miss counter
Change summary
Adds an
AsyncLocalStorage-backed per-request cache layer forDemoStatewith hit/miss counters, exposed via/api/health. Specifically:packages/db/src/cache-counter.ts(new) — UsesAsyncLocalStorageto hold a{ hits, misses, state }store. ExportsrunWithCacheCounter,getCacheStats,getCachedState,setCachedState,recordHit,recordMiss,invalidateCachedState.packages/db/src/demo-store.ts—readDemoState()now checks the ALS-scoped cache before reading from disk;writeDemoState()invalidates it.packages/db/src/index.ts— Re-exportsCacheStats,getCacheStats,runWithCacheCounter.apps/web/app/api/health/route.ts— Addscache: getCacheStats()to health response.packages/auth/package.json+package-lock.json— Bumpsdrizzle-orm ^0.44.5 → ^0.45.1(unrelated).packages/db/test/cache-counter.test.ts(new) — 5 tests covering isolation, null-outside-scope, hit/miss tracking, state caching, and invalidation.Validation confidence: Medium-High
cache-counter.tsmodule is clean and well-scoped. Tests cover the core API surface.readDemoState/writeDemoStateintegration is straightforward.runWithCacheCounterexist in the diff — the ALS scope is never actually entered, sogetCacheStats()in the health route will always returnnulltoday.Risks
runWithCacheCounter— middleware or request handler needs to call it for the feature to function. Without that, everygetCacheStats()returnsnulland everygetCachedState()returnsnull(cache is a no-op). This is the critical missing piece.readDemoStateis cached but mutations go throughwriteDemoState→readDemoState→ mutate →writeDemoState. If two concurrent requests share the same ALS scope (they won't, but worth noting), the in-memoryDemoStateobject is mutable and could be mutated in-place between the cache-set and a later cache-get, serving stale/corrupted state. Within a single request scope this is fine, but the cached object is the same reference — any in-place mutation beforewriteDemoStateis called silently alters the cached copy.drizzle-orm ^0.44.5 → ^0.45.1is a semver-minor bump sneaked into this branch. Should be a separate commit/PR or at least called out.getCacheStats()outside any ALS scope — it will always benullthere regardless of whether middleware wraps other requests. The health endpoint itself isn't wrapped. Consider whethercache: nullin the health response is the intended UX or if global aggregate stats would be more useful for observability.Recommendation
Request changes — the feature is well-structured but incomplete. The main blocker is risk #1: add middleware (or a Next.js instrumentation hook) that wraps each incoming request in
runWithCacheCounter()so the counters actually accumulate. Without that, this is dead code behind a nice API.