Skip to content

Improve ctx.cache ergonomics#6588

Merged
tewaro merged 1 commit intomainfrom
tewaro/ctx-cache-on-cloudflare-workers
Apr 21, 2026
Merged

Improve ctx.cache ergonomics#6588
tewaro merged 1 commit intomainfrom
tewaro/ctx-cache-on-cloudflare-workers

Conversation

@tewaro
Copy link
Copy Markdown
Contributor

@tewaro tewaro commented Apr 16, 2026

Allow cache to be imported from cloudflare:workers.

@tewaro tewaro requested review from a team as code owners April 16, 2026 01:40
Comment thread src/cloudflare/internal/workers.d.ts Outdated
@danlapid danlapid requested a review from jasnell April 16, 2026 01:43
Copy link
Copy Markdown
Contributor

@ask-bonk ask-bonk Bot left a comment

Choose a reason for hiding this comment

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

I'm Bonk, and I've done a quick review of your PR.

This PR adds a cache export to cloudflare:workers that proxies through to the current request's CacheContext (i.e. ctx.cache), allowing module-level imports instead of threading the execution context.

Issues (ranked by severity)

  1. Missing TypeScript type definitions for the new cache export — The runtime implementation exports cache from cloudflare:workers, but types/defines/rpc.d.ts (which defines the CloudflareWorkersModule namespace / cloudflare:workers module declaration) does not include a cache export. Users doing import { cache } from 'cloudflare:workers' will get a TypeScript compilation error even though it works at runtime. You need to add something like export const cache: CacheContext | undefined; to the CloudflareWorkersModule namespace in types/defines/rpc.d.ts (the CacheContext interface is already generated from C++ RTTI as a global ambient type) and then run just generate-types to update the snapshot.

  2. Silent undefined return when accessed outside a request context — When getCurrentCache() is called outside an active IoContext (e.g., at module top-level), every property access on the cache proxy silently returns undefined. Compare with waitUntil, which throws when called outside a request. Silently returning undefined makes misuse harder to diagnose. Consider throwing an informative error instead, or document the intended behavior.

  3. Proxy get trap calls getCurrentCache() on every property access — Each call to cache.purge(...) calls getCurrentCache() once in get (to look up the method), and operations like JSON.stringify(cache) or Object.keys(cache) call it multiple times across different traps. This is consistent with how the env proxy works, so it may be intentional, but worth noting.

Comment thread src/cloudflare/workers.ts
Comment thread src/cloudflare/internal/workers.d.ts Outdated
@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk Bot commented Apr 16, 2026

Review posted successfully on PR #6588. Here's a summary of my findings:

  1. Missing TypeScript type definitions (highest severity) — types/defines/rpc.d.ts doesn't export cache from the cloudflare:workers module declaration, so users will get TS errors when importing it. Needs export const cache: CacheContext | undefined; added and just generate-types run.

  2. Silent undefined on misuse — Unlike waitUntil which throws outside a request context, the cache proxy silently returns undefined for all property accesses when there's no active IoContext. This makes debugging harder.

  3. Per-access overheadgetCurrentCache() is called on every proxy trap invocation, consistent with the env proxy pattern but worth noting.

github run

@tewaro tewaro force-pushed the tewaro/ctx-cache-on-cloudflare-workers branch from 348ea17 to 1a74a6d Compare April 16, 2026 01:50
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 16, 2026

Merging this PR will not alter performance

✅ 72 untouched benchmarks
⏩ 129 skipped benchmarks1


Comparing tewaro/ctx-cache-on-cloudflare-workers (61977ab) with main (432350d)

Open in CodSpeed

Footnotes

  1. 129 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@tewaro tewaro force-pushed the tewaro/ctx-cache-on-cloudflare-workers branch 3 times, most recently from 0f82ae9 to 7054827 Compare April 21, 2026 18:06
@tewaro tewaro requested a review from a team as a code owner April 21, 2026 18:06
@tewaro tewaro requested a review from ascorbic April 21, 2026 18:06
@tewaro tewaro force-pushed the tewaro/ctx-cache-on-cloudflare-workers branch 2 times, most recently from bdc12b4 to c13415b Compare April 21, 2026 18:38
Comment thread src/cloudflare/workers.ts
@tewaro tewaro force-pushed the tewaro/ctx-cache-on-cloudflare-workers branch from c13415b to 6c9d682 Compare April 21, 2026 19:19
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 21, 2026

The generated output of @cloudflare/workers-types matches the snapshot in types/generated-snapshot 🎉

Allow `cache` to be imported from `cloudflare:workers`.
@tewaro tewaro force-pushed the tewaro/ctx-cache-on-cloudflare-workers branch from 6c9d682 to 61977ab Compare April 21, 2026 19:35
@tewaro tewaro enabled auto-merge April 21, 2026 22:22
@tewaro tewaro merged commit d9d8fa6 into main Apr 21, 2026
35 of 36 checks passed
@tewaro tewaro deleted the tewaro/ctx-cache-on-cloudflare-workers branch April 21, 2026 22:25
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