# @brokerloop/ttlcache 中文文档

规范地址：https://npmcn.com/package/%40brokerloop/ttlcache/

版本：3\.2\.3
npm 元数据更新时间：2022-04-04T20:45:49.714Z
中文译文更新时间：2026-11-08T15:59:09.180Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40brokerloop/ttlcache
原文对照：https://npmcn.com/package/%40brokerloop/ttlcache/?lang=original
项目主页：https://github.com/Brokerloop/ttlcache#readme
源码仓库：https://github.com/Brokerloop/ttlcache

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：13.3 kB，5 个文件
- TypeScript 类型：未声明
- 运行依赖：1 个
- 首次发布：2018-06-28T04:32:24.738Z
- 最新版本发布：2020-05-24T19:38:22.025Z
- 近 30 天下载量：56329（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：29（近 12 个月 0 个）

## 中文简介

活动为节点JS和浏览器的 LRU TTL 缓存 .

## 安装

```sh
npm install @brokerloop/ttlcache
```

也可以使用：`pnpm add @brokerloop/ttlcache`、`yarn add @brokerloop/ttlcache`、`bun add @brokerloop/ttlcache`

## README

# @ 经纪人/ tlcache

[![Build Status](https://travis-ci.org/Brokerloop/ttlcache.svg?branch=master)](https://travis-ci.org/Brokerloop/ttlcache) [![Coverage Status](https://coveralls.io/repos/github/Brokerloop/ttlcache/badge.svg?branch=coverage)](https://coveralls.io/github/Brokerloop/ttlcache?branch=coverage) [![Dependency Status](https://david-dm.org/Brokerloop/ttlcache/status.svg)](https://david-dm.org/Brokerloop/ttlcache) [![npm version](https://badge.fury.io/js/%40brokerloop%2Fttlcache.svg)](https://badge.fury.io/js/%40brokerloop%2Fttlcache)

**用于节点和浏览器的 LRU TRL 缓存**

-   含有可配置的过期条目数的缓存 .
-   读取已过期的条目会将其从缓存中移除 .
-   当缓存满了, [最近使用d](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU) 条目首先被删除.

### 安装

```sh
npm install @brokerloop/ttlcache --save
```

### 用法( 文本脚本)

```ts
import { TTLCache } from '@brokerloop/ttlcache';

const cache = new TTLCache<string, number>({
  ttl:   5000,
  max:   10,
  clock: Date
});

cache.set('a', 123);
cache.has('a');      // true
cache.get('a');      // 123
cache.get('b');      // undefined
cache.delete('a');   // 123

cache.empty.on(() => {
  // cache is empty
});

cache.full.on(() => {
  // cache is full
});

cache.evict.on(({ key, val }) => {
  // entry evicted
});
```

### 选项

```js
{
  ttl:   1000,         // default entry TTL in ms
  max:   Infinity,     // max number of entries in cache
  clock: Date as Clock // cache-relative clock
}
```

#### `clock`

默认情况下,缓存使用 `Date.now()` 以备过期的条目。 这在系统日期/时间不变的情况下有效. 你可以自行执行 `Clock` 接口 :

```ts
interface Clock {
  now:         () => number; // must be monotonically increasing
  [_: string]: any;
}
```

```ts
const clock = (() => {
  let time = 0;

  const clock: Clock = {
    now:  () => time,
    pass: (ms: number) => time += ms
  };

  return clock;
})();
```

### 属性

#### `size`

返回缓存的大小,包括已过期的条目。 运行 `cleanup()` 首先获取有效的缓存大小.

### 方法

#### `keys(): Iterator<K>`

返回从最新到最老的有效缓存条目密钥上的浏览器 。 过期的条目不被拆迁.

#### `values(): Iterator<V>`

返回从最新到最古老的有效缓存条目值的浏览器。 过期的条目不被拆迁.

#### `entries(): Iterator<{ key: K, val: V }>`

返回从最新到最老的有效缓存条目的浏览器。 过期的条目不被拆迁.

#### `has(key: K): boolean`

检查是否 `key` 在缓存中存在。 过期时不驱逐该条目.

#### `get(key: K): V|undefined`

按给定条目查找 `key`. 回归 `undefined` 如果未找到或该条目已过期,也可将其驱逐.

#### `set(key: K, val: V): void`

创建条目于 `key`如果缓存满了,则将缓存的 LRU 条目驱逐出境。 如果已过期的条目存在于 `key`它的LRU年龄被刷新,但并没有被逐出.

#### `delete(key: K): V|undefined`

查找并删除条目 : `key`。如果条目值被删除,则返回,或者 `undefined` 否则.

#### `cleanup(opts = { emit: true }): void`

Evicts 缓存中所有已过期的条目 。 过 `emit: false` 无法发射 `evict` 事件.

#### `resize(max: number, opts = { emit: true }): void`

将缓存大小调整到给定 `max` 尺寸。 当成长时,没有任何条目被逐出. 在收缩时,根据需要,按最年长的路轮运输公司年龄,将入口驱逐,直到新的 `max` 已经到达。 过 `emit: false` 无法发射 `evict` 事件.

#### `clear(): void`

启用缓存, 删除所有条目, 不显示发射信号 .

### 活动(通过) [信号](https://github.com/soncodi/signal)(中文(简体) )

#### `empty`

缓存变成空后发射信号 。 不要开火 `clear()`。 。 。 .

#### `full`

缓存满后发射信号.

#### `evict`

缓存进入后发射信号 。 被逐出门 `{ key: K, val: V }` 被通过作为一个争论。 不要开火 `delete()` 财务报告和审定财务报表 `clear()`。 。 。 .

### 许可证

[自由主义](https://npmcn.com/package/%40brokerloop/ttlcache/LICENSE)

## 常见问题

### @brokerloop/ttlcache 是什么？

活动为节点JS和浏览器的 LRU TTL 缓存 \.

### 如何安装 @brokerloop/ttlcache？

在项目目录运行 npm install @brokerloop/ttlcache，或使用 pnpm add / yarn add / bun add。

### @brokerloop/ttlcache 支持 TypeScript 吗？

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

### @brokerloop/ttlcache 可以商用吗？

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

### @brokerloop/ttlcache 的下载量有多少？

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

### @brokerloop/ttlcache 还在维护吗？

最新版本 3\.2\.3 发布于 2020/05/25（6 年前）；至今共发布 29 个版本，其中过去 12 个月发布了 0 个；首次发布于 2018/06/28。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

