feat: implement cacheForRequest() per-request factory cache#646
feat: implement cacheForRequest() per-request factory cache#646JamesbbBriz wants to merge 1 commit intocloudflare:mainfrom
Conversation
Implements the cacheForRequest API discussed in cloudflare#623. Factory function reference is the cache key — no string keys, no collision risk between modules. Async factories cache the Promise itself, so concurrent calls share one invocation. Outside request scope, the factory runs every time with no caching.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f2f1d15111
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (!isInsideUnifiedScope()) { | ||
| // Outside request scope: no caching, run factory every time. | ||
| return factory(); |
There was a problem hiding this comment.
Cache in execution-context request scopes
cacheForRequest() disables caching whenever isInsideUnifiedScope() is false, but Pages Router middleware runs in a request scope without unified ALS (runMiddleware() only wraps _runWithExecutionContext, and prod server currently calls runMiddleware(webRequest, undefined)). In that path, repeated calls within a single request always re-run the factory, so this API does not provide the advertised per-request behavior for middleware/server code on Pages Router production flows.
Useful? React with 👍 / 👎.
|
Re: middleware scope — this is intentional. Caching without a proper request scope would risk leaking state across requests, which is worse than re-running the factory. Pages Router middleware is the only path without unified scope; wrapping it would be a separate middleware pipeline change. The fallback (factory runs every time) is safe and predictable. |
Summary
Implements
cacheForRequest()from RFC #623 — a per-request factory cache that lazily initializes values on first call and returns the cached result for subsequent calls within the same request.API
Key design decisions:
await getDb()share one invocation)Changes
shims/cache-for-request.tscacheForRequest()implementation (17 lines of logic)shims/unified-request-context.tsrequestCache: WeakMap<Function, unknown>field + shallow-clone comment syncpackage.json"./cache-for-request"exportBlast radius
Zero. New file + one new field with
new WeakMap()default (~0.1μs per request). No existing behavior changes.Test plan
Tested with Prisma v7 + Hyperdrive on Workers. Happy to add unit tests — let me know the preferred test file location.
Addresses #623 · Relates to #537