@btravstack/cache

中文文档
0.58.0发布于 2026/11/08MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 5903

A Cache port for @btravstack/core: an in-memory adapter, a Redis adapter, and a composition that reports every call to Observers

原始使用文档
文档目录4 节
  1. A worked example
  2. Options
  3. What it decides, and what it does not
  4. License

@btravstack/cache

The cache port for @btravstack/core: one Cache an application depends on, an in-memory and a Redis adapter behind it, and an opt-in composition that spans, counts and logs every call.

📖 Documentation · API Reference

pnpm add @btravstack/cache @btravstack/core @btravstack/config @btravstack/di unthrown

Four peer dependencies, plus redis — optional, and needed only if you compose the Redis adapter. Instrumentation needs no peer of its own: the Logger, Tracer and Meter it depends on are the kernel's ports, and @btravstack/observability is what an application composes to satisfy them. Node >=22.

A worked example

A read-through, which is one call — the degradation policy lives in the port:

import { Cache } from "@btravstack/cache";
import { Port, Provider } from "@btravstack/di";
import type { AsyncResult } from "unthrown";

class Customers extends Port("ReadmeCustomers")<{
  readonly find: (
    tenantId: string,
    id: string,
  ) => AsyncResult<CustomerView, CustomerNotFound>;
}> {}

const customers = Provider(Customers)({
  inject: { find: FindCustomer, cache: Cache },
  sync: ({ find, cache }) => ({
    find: (tenantId, id) =>
      cache.getOrSet<CustomerView, CustomerNotFound>(
        // The key carries the tenant, because the port does not: `Cache` takes
        // plain strings, and composing them is the application's job.
        `customers:${tenantId}:${id}`,
        () => find.execute(TenantId(tenantId), id).map(view),
        { ttlMs: 60_000 },
      ),
  }),
});

And the composition — the adapter is the only decision at it:

import { cache } from "@btravstack/cache";
import { redisCache } from "@btravstack/cache/redis";

export const CustomersApp = Module("CustomersApp")({
  imports: [
    CustomerApplicationModule,
    CustomerPersistenceModule,
    // Every call is reported through `Observers`. Drop the two modules
    // below and this composition is unchanged — it just reports to nobody.
    cache({ adapter: redisCache() }),
    observability(),
    otel(),
  ],
  provides: [customers],
  exports: [Customers, Logger],
});

Options

OptionWhereWhat it is
adaptercache({ adapter })the adapter module providing CacheBackend — required
clockmemoryCachewhat a ttl is measured against (default: the kernel's systemClock)
REDIS_URLenvironment, read by redisCache()the connection URL — required, validated at graph build
ttlMscache.set / cache.getOrSetper-entry expiry, rounded to whole milliseconds (default: none — the entry stays until it is deleted or evicted; a value that does not round to a whole millisecond of at least 1 means the entry is not stored, so 0.6 is stored as 1 and 0.4 is not stored at all)

The full table — defaults, semantics and the reasoning — lives on the reference page, which is this list's one detailed home.

What it decides, and what it does not

It decides that a miss is Ok(undefined) and not an error, that a failing backend is a modeled CacheUnavailable rather than a swallowed one, that values are unknown encoded by the adapter, and that keys are plain strings the caller composes.

It decides one thing for you: that every call is reported through Observers. Nothing is required to receive those reports — a graph composing no observability compiles, starts and pays an inert call — so the decision costs you a port list only if you want the spans and the instruments, and then you compose observability() and otel() and change nothing here.

It does not decide whether an unreachable cache degrades a get — recover CacheUnavailable where you call it — nor what your keys look like, nor when to invalidate them. The one place it does decide is getOrSet, where an unreachable cache runs your loader and a failed write is not your error. There is no stampede protection, no eviction on the memory adapter, and no namespace parameter — each reason is on the reference page, and the same reasoning is in AGENTS.md for contributors.

License

MIT

版本与发布记录

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

版本发布日期距今
0.58.0latest2026/11/08今天
0.57.02026/11/071 天前
0.56.12026/11/062 天前
0.56.02026/11/062 天前
0.55.12026/11/035 天前
0.55.02026/11/035 天前

关于 @btravstack/cache 的常见问题

@btravstack/cache 是什么?

A Cache port for @btravstack/core: an in-memory adapter, a Redis adapter, and a composition that reports every call to Observers

如何安装 @btravstack/cache?

在项目目录运行 npm install @btravstack/cache,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:unthrown(^5.0.0)、@btravstack/di(^0.58.0)、@btravstack/core(^0.58.0)、@btravstack/config(^0.58.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@btravstack/cache 支持 TypeScript 吗?

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

@btravstack/cache 支持 ESM 还是 CommonJS?

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

@btravstack/cache 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@btravstack/cache 可以商用吗?

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

@btravstack/cache 的下载量有多少?

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

@btravstack/cache 还在维护吗?

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