cacheHandler
如果你想要将缓存的页面和数据持久保存到持久存储,或者在 Next.js 应用的多个容器或实例之间共享缓存,你可以配置 Next.js 缓存位置。
¥You can configure the Next.js cache location if you want to persist cached pages and data to durable storage, or share the cache across multiple containers or instances of your Next.js application.
查看 自定义缓存处理程序 示例并了解更多实现信息。
¥View an example of a custom cache handler and learn more about the implementation.
API 参考
¥API Reference
缓存处理程序可以实现以下方法:get
、set
、revalidateTag
和 resetRequestCache
。
¥The cache handler can implement the following methods: get
, set
, revalidateTag
, and resetRequestCache
.
get()
参数 | 类型 | 描述 |
---|---|---|
key | string | 缓存值的键。 |
返回缓存的值,如果未找到则返回 null
。
¥Returns the cached value or null
if not found.
set()
参数 | 类型 | 描述 |
---|---|---|
key | string | 存储数据的键。 |
data | 数据或 null | 要缓存的数据。 |
ctx | { tags: [] } | 提供的缓存标签。 |
返回 Promise<void>
。
¥Returns Promise<void>
.
revalidateTag()
参数 | 类型 | 描述 |
---|---|---|
tag | string 或 string[] | 要重新验证的缓存标签。 |
返回 Promise<void>
。了解有关 重新验证数据 或 revalidateTag()
函数的更多信息。
¥Returns Promise<void>
. Learn more about revalidating data or the revalidateTag()
function.
resetRequestCache()
此方法会在下一个请求之前重置单个请求的临时内存缓存。
¥This method resets the temporary in-memory cache for a single request before the next request.
返回 void
。
¥Returns void
.
需要了解:
¥Good to know:
revalidatePath
是缓存标签之上的便利层。调用revalidatePath
将调用revalidateTag
函数,然后你可以选择是否要根据路径标记缓存键。¥
revalidatePath
is a convenience layer on top of cache tags. CallingrevalidatePath
will call yourrevalidateTag
function, which you can then choose if you want to tag cache keys based on the path.
平台支持
¥Platform Support
部署选项 | 支持 |
---|---|
Node.js 服务器 | 是 |
Docker 容器 | 是 |
静态导出 | 否 |
适配器 | 平台相关 |
了解如何在自托管 Next.js 时进行 配置 ISR。
¥Learn how to configure ISR when self-hosting Next.js.
版本历史
¥Version History
版本 | 更改 |
---|---|
v14.1.0 | 重命名为 cacheHandler 并变得稳定。 |
v13.4.0 | incrementalCacheHandlerPath 支持 revalidateTag 。 |
v13.4.0 | incrementalCacheHandlerPath 支持独立输出。 |
v12.2.0 | 添加了实验性 incrementalCacheHandlerPath 。 |