@ btravstack/ cach
缓存端口用于
@btravstack/core页:1Cache申请 取决于背后的记忆和Redis适配器 以及选择进入 每一次通话的间隔、计数和记录.
pnpm add @btravstack/cache @btravstack/core @btravstack/config @btravstack/di unthrown
4个同伴依赖关系,加 redis ——可选的,只有你需要
作"雷地斯"适配器. 仪器不需要自己的同伴:
Logger, (中文(简体) ), Tracer 财务报告和审定财务报表 Meter 这取决于是
内核的端口, (中文(简体) ),
财务报告和审定财务报表 @btravstack/observability 申请是用来满足的
他们。 节点 >=22。 。 。 .
实例
一个读取,这是一个呼吁——退化政策生活在港口:
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 },
),
}),
});
而成分——适配器是唯一的决定:
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],
});
选项
| 选项 | 何处 | 这是什么 |
|---|---|---|
adapter | cache({ adapter }) | 适配器模块提供 CacheBackend 所需数额 |
clock | memoryCache | 计算 ttl 相对于什么( 默认: 内核) systemClock(中文(简体) ) |
REDIS_URL | 环境,读作 redisCache() | 连接 URL — 需要, 在图表构建中验证 |
ttlMs | cache.set 编号 : cache.getOrSet | 每入为过期, 四舍五入为整毫秒( 默认: 无) —— 条目在被删除或被删除之前将保留; 一个值不四舍五入为整毫秒的至少 1 表示条目没有被存储, 所以 0.6 保存为 1 财务报告和审定财务报表 0.4 完全没有存储) |
完整表——默认,语义和推理——活在 参考页, (中文(简体) ), 这张名单是详细的.
它决定什么,它没有
决定 错过的是 Ok(undefined) 绝不然,那确是失败的
后端为模型 CacheUnavailable 而不是一个被吞噬的
值为 unknown 由适配器编码, 而密钥是普通字符串
召者作会.
它决定一件事给你:每通电话都通过
Observers不需要接受这些报告——一个图表
没有可观察性汇编,开始和支付一个惰性呼叫——所以决定
您需要一个端口列表, 前提是您想要跨度和仪器, 然后
你编曲 observability() 财务报告和审定财务报表 otel() 和改变什么在这里.
它没有决定 无法获取的缓存是否降解 a get ——恢复
CacheUnavailable 你叫它的地方 钥匙是什么样子 什么时候
以使他们失效。 它决定的地方是 getOrSet时
无法到达的缓存运行您的加载器, 而失败的写入不是您的错误 。 那边
没有印花保护,没有驱逐内存适配器,也没有命名空间
参数 - 每个原因都在
参考页, (中文(简体) ),
同样的推理是 AGENTS.md 用于贡献者.
许可证
麻省理工学院