# @fastify/caching 中文文档

规范地址：https://npmcn.com/package/%40fastify/caching/

版本：9\.0\.5
npm 元数据更新时间：2026-10-18T12:30:26.333Z
中文译文更新时间：2026-11-14T11:23:07.513Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：37.5 kB，15 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2022-04-27T13:41:48.889Z
- 最新版本发布：2026-10-09T14:44:05.671Z
- 近 30 天下载量：109398（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：15（近 12 个月 2 个）

## 中文简介

用于快速化的插件, 以启用缓存控制头的管理

## 安装

```sh
npm install @fastify/caching
```

也可以使用：`pnpm add @fastify/caching`、`yarn add @fastify/caching`、`bun add @fastify/caching`

## README

# @ fastify/ caching (帮助)

[![CI](https://github.com/fastify/fastify-caching/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-caching/actions/workflows/ci.yml) [![NPM version](https://img.shields.io/npm/v/@fastify/caching.svg?style=flat)](https://www.npmjs.com/package/@fastify/caching) [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

_@ fastify/ caching (帮助)_ 是一个插件 [快捷化](https://fastify.dev/) 框架 以提供服务器侧缓存和机制来根据 [RFC 2616 第14.9节](https://datatracker.ietf.org/doc/html/rfc2616#section-14.9)。 。 。 .

此插件完全支持快取封装. 因此,路线 缓存设置应不同 环境.

该插件除了提供头操作外,还装饰了 服务器实例,其对象可用于缓存项目. **说明:** 默认缓存不应在“生产”环境中使用。 没错 a LRU,内存缓存,上限为10万件. 没错 _高级_ 建议提供全功能缓存对象,例如:. [抽象缓存](https://www.npmjs.com/package/abstract-cache-redis)。 。 。 .

## 安装

```
npm i @fastify/caching
```

### 兼容性

| 插件版本 | 快速化版本 |
| --- | --- |
| `>=9.x` | `^5.x` |
| `^8.x` | `^4.x` |
| `>=6.x <8.x` | `^3.x` |
| `>=4.x <6.x` | `^2.x` |
| `>=1.x <4.x` | `^1.x` |

请注意,如果一个快取版本没有支持,那么该插件的相应版本也一样 上表列出. 见 [快速化 LTS 政策](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) 更多细节.

## 示例

此示例显示使用插件禁用客户端缓存所有 路线.

```js
const http = require('node:http')
const fastify = require('fastify')()
const fastifyCaching = require('@fastify/caching')

fastify.register(
  fastifyCaching,
  {privacy: fastifyCaching.privacy.NOCACHE},
  (err) => { if (err) throw err }
)

fastify.get('/', (req, reply) => {
  reply.send({hello: 'world'})
})

fastify.listen({ port: 3000 }, (err) => {
  if (err) throw err

  http.get('http://127.0.0.1:3000/', (res) => {
    console.log(res.headers['cache-control'])
  })
})
```

此示例显示如何注册插件, 使其仅提供 一个服务器本地缓存。 它不会为响应设置任何缓存控制头 . 它还显示如何保留缓存对象的引用,以便它能够 重新使用.

```js
const IORedis = require('ioredis')
const redis = new IORedis({host: '127.0.0.1'})
const abcache = require('abstract-cache')({
  useAwait: false,
  driver: {
    name: 'abstract-cache-redis', // must be installed via `npm i`
    options: {client: redis}
  }
})

const fastify = require('fastify')()
fastify
  .register(require('@fastify/redis'), {client: redis})
  .register(require('@fastify/caching'), {cache: abcache})

fastify.get('/', (req, reply) => {
  fastify.cache.set('hello', {hello: 'world'}, 10000, (err) => {
    if (err) return reply.send(err)
    reply.send({hello: 'world'})
  })
})

fastify.listen({ port: 3000 }, (err) => {
  if (err) throw err
})
```

## API 密码

### 选项

_@ fastify/ caching (帮助)_ 接受选项对象 :

```js
{
  privacy: 'value',
  expiresIn: 300,
  cache: {get, set},
  cacheSegment: 'segment-name'
}
```

-   `privacy` (默认 : `undefined`: 可以设置为任何有效的字符串 (单位:千美元) _缓存响应方向_ 由RFC 2616定义.
-   `expiresIn` (默认 : `undefined`: 一个值,以秒计,用于 _最大年龄_ 页:1 资源可能被缓存。 当这个设定,和 `privacy` 未设定为 `no-cache`, (中文(简体) ), 接下来 `', max-age=<value>'` 将附在 `cache-control` 头曰.
-   `cache` (默认 : `abstract-cache.memclient`: 一个 [抽象缓存](https://www.npmjs.com/package/abstract-cache) 符合协议的缓存对象。 注意: 插件需要缓存实例 适当支持ETag机制。 因此,如果提供了病态值 默认将被使用.
-   `cacheSegment` (默认 : `'@fastify/caching'`: 当 与缓存通信.
-   `serverExpiresIn` (默认 : `undefined`:一个值,以秒为单位,表示资源是新鲜的并可能保存在一个共享缓存(如CDN)中的时间长度. 共享缓存会在指定时忽略最大年龄, 尽管浏览器会继续使用最大年龄 。 应随过期一起使用 In,而不是取而代之. 当这个设定,和 `privacy` 设置为 `public`,则 `', s-maxage=<value>'` 将附在 `cache-control` 头曰.

### `reply.etag(string, number)`

此方法允许设置 `etag` 头曰. 它接受任意行为 绳子。 请确定为 HTTP 信头提供有效的字符串 .

如果标签字符串没有提供, 那么 [安全无虞](https://www.npmjs.com/package/uid-safe) 将用来 生成标签。 这次行动将进行 _**同步**_这是 建议始终为这种方法提供价值以避免这一操作.

所有对受此插件影响的路径的请求都会被检查 页:1 `if-none-match` 头曰. 如果信头存在, 将使用该值 查找此插件相关缓存中的标记。 如果标签是 后,回复将以发送到 客户.

标记将根据第二个参数被缓存 。 如果第二个参数 它是一个整数,然后用作缓存的时间 生成304个响应的调试器。 时间必须在 毫秒长. 未指定参数时,默认寿命为 `3600000`。 。 。 .

### `reply.expires(date)`

此方法允许设置 `expires` 头曰. 它接受常规的 `Date` 对象或字符串,根据 [RFC 2616 第14.21节](https://datatracker.ietf.org/doc/html/rfc2616#section-14.21)。 。 。 .

## 许可证

许可证根据 [麻省理工学院](https://npmcn.com/package/%40fastify/caching/LICENSE)。 。 。 .

## 常见问题

### @fastify/caching 是什么？

用于快速化的插件, 以启用缓存控制头的管理

### 如何安装 @fastify/caching？

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

### @fastify/caching 支持 TypeScript 吗？

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

### @fastify/caching 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @fastify/caching 可以商用吗？

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

### @fastify/caching 的下载量有多少？

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

### @fastify/caching 还在维护吗？

最新版本 9\.0\.5 发布于 2026/10/09（1 个月前）；至今共发布 15 个版本，其中过去 12 个月发布了 2 个；首次发布于 2022/04/27。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

