@fastify/caching

中文文档
9.0.5发布于 2026/10/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 10.9万

A plugin for Fastify to enable management of cache control headers

原始使用文档
文档目录8 节
  1. Install
  2. Compatibility
  3. Example
  4. API
  5. Options
  6. reply.etag(string, number)
  7. reply.expires(date)
  8. License

@fastify/caching

CI NPM version neostandard javascript style

@fastify/caching is a plugin for the Fastify framework that provides server-side caching and mechanisms for manipulating HTTP cache headers according to RFC 2616 §14.9.

This plugin fully supports Fastify's encapsulation. Therefore, routes that should have differing cache settings should be registered within different contexts.

In addition to providing header manipulation, the plugin also decorates the server instance with an object that can be used for caching items. Note: the default cache should not be used in a "production" environment. It is an LRU, in-memory cache that is capped at 100,000 items. It is highly recommended that a full-featured cache object be supplied, e.g. abstract-cache-redis.

Install

npm i @fastify/caching

Compatibility

Plugin versionFastify version
>=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

Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin in the table above. See Fastify's LTS policy for more details.

Example

This example shows using the plugin to disable client-side caching of all routes.

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'])
  })
})

This example shows how to register the plugin such that it only provides a server-local cache. It will not set any cache control headers on responses. It also shows how to retain a reference to the cache object so that it can be re-used.

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

Options

@fastify/caching accepts the options object:

{
  privacy: 'value',
  expiresIn: 300,
  cache: {get, set},
  cacheSegment: 'segment-name'
}
  • privacy (Default: undefined): can be set to any string that is valid for a cache-response-directive as defined by RFC 2616.
  • expiresIn (Default: undefined): a value, in seconds, for the max-age the resource may be cached. When this is set, and privacy is not set to no-cache, then ', max-age=<value>' will be appended to the cache-control header.
  • cache (Default: abstract-cache.memclient): an abstract-cache protocol-compliant cache object. Note: the plugin requires a cache instance to properly support the ETag mechanism. Therefore, if a falsy value is supplied the default will be used.
  • cacheSegment (Default: '@fastify/caching'): segment identifier to use when communicating with the cache.
  • serverExpiresIn (Default: undefined): a value, in seconds, for the length of time the resource is fresh and may be held in a shared cache (e.g. a CDN). Shared caches will ignore max-age when this is specified, though browsers will continue to use max-age. Should be used with expiresIn, not in place of it. When this is set, and privacy is set to public, then ', s-maxage=<value>' will be appended to the cache-control header.

reply.etag(string, number)

This method allows setting of the etag header. It accepts any arbitrary string. Be sure to supply a string that is valid for HTTP headers.

If a tag string is not supplied then uid-safe will be used to generate a tag. This operation will be performed synchronously. It is recommended to always supply a value to this method to avoid this operation.

All incoming requests to paths affected by this plugin will be inspected for the if-none-match header. If the header is present, the value will be used to lookup the tag within the cache associated with this plugin. If the tag is found, then the response will end with a 304 status code sent to the client.

Tags will be cached according to the second parameter. If the second parameter is supplied, and it is an integer, then it will be used as the time to cache the etag for generating 304 responses. The time must be specified in milliseconds. The default lifetime, when the parameter is not specified, is 3600000.

reply.expires(date)

This method allows setting of the expires header. It accepts a regular Date object or a string that is a valid date string according to RFC 2616 §14.21.

License

Licensed under MIT.

版本与发布记录

@fastify/caching 共发布 15 个版本,首次发布于 2022/04/27;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
9.0.5latest2026/10/091 个月前
9.0.42026/08/083 个月前
9.0.32025/01/051.9 年前
9.0.22024/12/141.9 年前
9.0.12024/09/222 年前
9.0.0next2024/09/062 年前

关于 @fastify/caching 的常见问题

@fastify/caching 是什么?

A plugin for Fastify to enable management of cache control headers

如何安装 @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 和提交记录判断。