@brust/cache-cloudflare

中文文档
2.0.0-alpha.38发布于 2026/11/05MITES Module零依赖近 30 天下载 4460

brust cache adapters for Cloudflare Workers: KV store, Durable Object broadcast/store, and their combination

原始使用文档
文档目录5 节
  1. Wrangler bindings (you declare them; brust never adds them)
  2. Options ( [cache.cloudflare] )
  3. What each configuration guarantees
  4. Free-plan numbers
  5. Behaviour notes

@brust/cache-cloudflare

Cloudflare Workers adapters for brust's shared second cache tier ([cache] in brust.toml, brust build --target cloudflare):

  • cloudflare-kv: pages in Workers KV (a store, no broadcast).
  • cloudflare-do: invalidations through a Durable Object (a pull broadcast), optionally also the page store (do_store = true, 16 shards).
  • cloudflare-kv + broadcast = "cloudflare-do": the recommended pair (combine). KV holds the bodies, the Durable Object tells every isolate what was invalidated.

Workers only (runtimes: ['workers']). No bun/bun:* import. Peer dependency: @brust/core.

[cache]
adapter = "cloudflare-kv"
broadcast = "cloudflare-do"
epoch_interval_ms = 5000

[cache.cloudflare]
kv_min_ttl_s = 300

Wrangler bindings (you declare them; brust never adds them)

brust build --target cloudflare checks the top level and every env.* block of your wrangler config and fails with this snippet when something is missing:

{
  "kv_namespaces": [{ "binding": "BRUST_CACHE", "id": "<wrangler kv namespace create brust-cache>" }],
  "durable_objects": { "bindings": [{ "name": "BRUST_CACHE_DO", "class_name": "BrustCacheDO" }] },
  "migrations": [{ "tag": "brust-cache-v1", "new_sqlite_classes": ["BrustCacheDO"] }]
}

BrustCacheDO must be a SQLite-backed class (new_sqlite_classes, or exports.BrustCacheDO = { "type": "durable-object", "storage": "sqlite" }). brust build writes dist/cf/worker/main.js, which re-exports it; do not write your own.

Options ([cache.cloudflare])

keyused bydefaultrule
kv_bindingKV, pair"BRUST_CACHE"a binding name in kv_namespaces
kv_min_ttl_sKV, pair3000..=31536000; a route with a smaller ttl is not written to KV (KV's own floor is 60 s)
do_bindingDO, pair"BRUST_CACHE_DO"a durable_objects binding with class_name "BrustCacheDO"
do_storeDOfalsetrue = bodies in 16 Durable Object shards (ignored with the pair)

Programmatic forms (for adapter = "custom"): kvAdapter({ binding?, min_ttl_s? }), doAdapter({ binding?, store? }), combine(kv, do). An unknown key is a TypeError naming it.

What each configuration guarantees

configstoreother isolates after cache.invalidatestale body re-served?Free-plan cost driver
cloudflare-kv aloneKV, unguardedup to the ttl (no broadcast)yes, up to the ttl (at least 300 s)1 KV write per MISS
cloudflare-do, do_store = falsenone (tier 1 only)epoch_interval_ms + RTTno1 DO request per poll per isolate
cloudflare-do, do_store = trueDO shards, exact write guardepoch_interval_ms + RTTno (rows are deleted)+ 1 DO request per tier-1 miss, + 18 per invalidation
cloudflare-kv + broadcast = "cloudflare-do"KV, floor-guardedepoch_interval_ms + RTTno once the floor is known; KV lag is hidden by each entry's entryStampKV write per MISS + DO polls

KV alone never invalidates. An L2 key hashes prefix, method, path and query, so a tag or a path cannot be enumerated in KV without extra index writes. Pair it with the Durable Object broadcast unless a stale page for up to its ttl is fine.

With the pair, each isolate keeps invalidation floors in JS (seeded from the Durable Object on first contact and after a generation change), so a freshly started isolate never serves a KV body that was invalidated before it started.

Free-plan numbers

driverper dayFree quotanote
epoch polls, 1 busy isolate, epoch_interval_ms 100086,400 DO requests100,000 DO requests86 % for ONE isolate
same at 5000 ms17,280100,000about 5 busy isolates
same at 60000 ms1,440100,000about 69 isolates; staleness about 60 s
do_store tier-1 miss1 DO request each100,000plus rows read
invalidation1 DO request (18 with do_store) + at most 5 rows written100,000 rows written
KV write-through1 KV write per MISS of a ttl >= 300 s page1,000 writesthe binding constraint on Free
KV read-through1 KV read per tier-1 miss100,000 reads

On Free, set epoch_interval_ms >= 5000 and prefer KV + DO over do_store. A do_store invalidation issues about 18 subrequests inside the action's invocation (Free allows 50); an action that already makes more than 30 will see shard invalidations fail.

Behaviour notes

  • The Durable Object persists its seq and the name-to-seq floors in its SQLite storage, so seqs never regress across a restart. The event ring is in memory and gen is new on every construction, so every isolate clears tier 1 once after a restart.
  • A KV write is skipped when this isolate wrote an identical page less than 60 s ago (KV allows one write per second per key).
  • KV is eventually consistent (up to about 60 s across locations). Local wrangler dev KV is strongly consistent, so propagation lag is not covered by the test suite.

版本与发布记录

@brust/cache-cloudflare 共发布 37 个版本,首次发布于 2026/10/12;过去 12 个月发布了 37 个版本。

版本发布日期距今
2.0.0-alpha.38latest2026/11/053 天前
2.0.0-alpha.372026/11/053 天前
2.0.0-alpha.362026/11/044 天前
2.0.0-alpha.352026/11/044 天前
2.0.0-alpha.342026/11/045 天前
2.0.0-alpha.332026/10/2910 天前

关于 @brust/cache-cloudflare 的常见问题

@brust/cache-cloudflare 是什么?

brust cache adapters for Cloudflare Workers: KV store, Durable Object broadcast/store, and their combination

如何安装 @brust/cache-cloudflare?

在项目目录运行 npm install @brust/cache-cloudflare,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@brust/core(2.0.0-alpha.38)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@brust/cache-cloudflare 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/brust__cache-cloudflare 是否存在。

@brust/cache-cloudflare 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@brust/cache-cloudflare 可以商用吗?

@brust/cache-cloudflare 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brust/cache-cloudflare 的下载量有多少?

根据 npm 官方下载统计,@brust/cache-cloudflare 在 2026/10/09 – 2026/11/07 这 30 天内被下载 4,460 次,日均约 149 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brust/cache-cloudflare 还在维护吗?

最新版本 2.0.0-alpha.38 发布于 2026/11/05(3 天前);至今共发布 37 个版本,其中过去 12 个月发布了 37 个;首次发布于 2026/10/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。