@gmod/shared-read-cache

中文文档
2.1.0发布于 2026/10/15MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4.4万

A promise cache whose shared reads are cancelled only once every caller has given up

原始使用文档
文档目录8 节
  1. Why
  2. Docs
  3. Budgets are opt-in
  4. idleTimeoutMs — reclaiming while nothing is happening
  5. One budget across several caches
  6. Why sizeOf
  7. Behaviour
  8. Relationship to @gmod/abortable-promise-cache

@gmod/shared-read-cache

A promise cache for reads that several callers want at once. One read per key, shared by everyone who asks for it while it is in flight, cancelled only once every one of them has given up.

Why

When a cache memoizes a bare promise built from the first caller's signal, that caller's abort rejects everyone else awaiting it. In a genome browser, panning away from one block then fails its still-wanted siblings.

import { SharedReadCache } from '@gmod/shared-read-cache'

const cache = new SharedReadCache({
  cacheKey: chunk => chunk.toString(),
  fill: (chunk, signal) => readChunk(chunk, { signal }),
})

const data = await cache.get(chunk, opts.signal)

This package caches what a read produced, keyed however you like. @gmod/range-cache-filehandle sits below it and caches the raw HTTP byte ranges underneath a filehandle.

Docs

Budgets are opt-in

There is no default limit, because a sensible one depends on what you cache:

const cache = new SharedReadCache({
  maxSize: 100 * 2 ** 20,
  sizeOf: chunk => chunk.byteLength,
  cacheKey: chunk => chunk.toString(),
  fill: (chunk, signal) => readChunk(chunk, { signal }),
})

A budget bounds retained memory, not request size:

  • The cache never turns a value away for being too large. A value bigger than the whole budget still goes in.
  • Eviction never touches a read in flight, because dropping the promise would lose the de-duplication its callers rely on.
  • Eviction only discards values the cache has already handed back once, so the worst a budget can cost is a re-read.

With no budget the cache grows for the life of the object. @gmod/tabix measured 2GB RSS panning a dense VCF before it bounded this. Pass a budget if the values are large or the object is long-lived.

Setting cache.maxSize = n later evicts immediately, so a consumer can shed memory under pressure.

idleTimeoutMs — reclaiming while nothing is happening

The cache checks maxSize when a read settles, so an idle cache stays at the level it reached, and no budget alone lowers it.

const cache = new SharedReadCache({
  maxSize: 1024 * 2 ** 20, // the ceiling under load
  idleTimeoutMs: 180_000, // ...but only while it is being used
  sizeOf: chunk => chunk.byteLength,
  fill: (chunk, signal) => readChunk(chunk, { signal }),
})

Use the two options together:

  • Set maxSize generously. Below one request's working set it caches nothing, because the cache evicts each value before the next request can reuse it, while the reads in flight hold their memory anyway.
  • idleTimeoutMs makes a generous ceiling affordable, because memory peaks under load and falls back when idle.

How the idle clock works:

  • It runs from the last read of an entry, or from the fill settling if nothing has read it since.
  • An entry fetched once and used every second never expires.
  • A slow read still gets the full timeout for reuse after its download ends.
  • The sweep skips reads in flight.
  • cache.sweepIdle() reclaims on demand, for example when a tab goes hidden.

The sweep timer:

  • The first read to settle arms it, and the first sweep that finds no settled entry stops it.
  • It holds its cache weakly, so a dropped cache is collected without a dispose() call.
  • It calls unref where that exists, so it never holds a Node process open.

One budget across several caches

A per-cache ceiling does not bound a consumer that opens many caches, such as one per open file. SharedBudget bounds them in aggregate and evicts the globally least-recently-used entry, so a busy cache can use the space an idle one is not using:

const budget = new SharedBudget(1024 * 2 ** 20)
const cache = new SharedReadCache({ budget, sizeOf, fill })
  • SharedBudget works alongside maxSize; it does not replace it.
  • It holds its members weakly, so there is no unregister to call.
  • Every member must measure size in the same unit. A budget that adds one cache's bytes to another's entry counts bounds neither.

docs/memory.md has the measurement for dividing a ceiling by the file count instead.

Why sizeOf

sizeOf weighs a settled value against the budget; without it, the budget counts entries. The cache can only call sizeOf once a read settles, so a cache that budgets this way has to own its entries, which a plain LRU package cannot do. docs/consumers.md lists what each gmod package weighs.

Behaviour

  • A caller with no signal pins the read. It can never abort, so aborting every other caller does not stop the read, and the read is uncancellable for everyone joined to it.
  • The cache drops a rejection rather than caching it, so one transient failure does not make the key fail for the life of the cache.

Relationship to @gmod/abortable-promise-cache

This package replaces @gmod/abortable-promise-cache, which had two bugs:

  • It never removed its listener from a caller's signal, so a long-lived signal accumulated one per key.
  • It counted a caller that arrived already aborted as a waiter. An abort listener never fires on an already-aborted signal, so the count never reached zero and the read became uncancellable for everyone joined to it.

版本与发布记录

@gmod/shared-read-cache 共发布 15 个版本,首次发布于 2026/08/06;过去 12 个月发布了 15 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.0latest2026/10/151 个月前
2.0.12026/10/121 个月前
2.0.02026/10/091 个月前
1.7.22026/08/222 个月前
1.7.12026/08/222 个月前
1.7.02026/08/222 个月前

关于 @gmod/shared-read-cache 的常见问题

@gmod/shared-read-cache 是什么?

A promise cache whose shared reads are cancelled only once every caller has given up

如何安装 @gmod/shared-read-cache?

在项目目录运行 npm install @gmod/shared-read-cache,或使用 pnpm add / yarn add / bun add。

@gmod/shared-read-cache 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gmod/shared-read-cache 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gmod/shared-read-cache 可以商用吗?

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

@gmod/shared-read-cache 的下载量有多少?

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

@gmod/shared-read-cache 还在维护吗?

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