incrementalCacheHandlerPath
在 Next.js 中,默认缓存处理器使用文件系统缓存。这无需任何配置,但您可以通过在 next.config.js
中使用 incrementalCacheHandlerPath
字段来自定义缓存处理器。
module.exports = {
experimental: {
incrementalCacheHandlerPath: require.resolve('./cache-handler.js'),
},
}
以下是一个自定义缓存处理器的示例:
const cache = new Map()
module.exports = class CacheHandler {
constructor(options) {
this.options = options
this.cache = {}
}
async get(key) {
return cache.get(key)
}
async set(key, data) {
cache.set(key, {
value: data,
lastModified: Date.now(),
})
}
}
API 参考
缓存处理器可以实现以下方法:get
、set
和 revalidateTag
。
get()
参数 | 类型 | 描述 |
---|---|---|
key | string | 缓存值的键名。 |
返回缓存值,如果未找到则返回 null
。
set()
参数 | 类型 | 描述 |
---|---|---|
key | string | 存储数据所用的键名。 |
data | Data 或 null | 要缓存的数据。 |
返回 Promise<void>
。
revalidateTag()
参数 | 类型 | 描述 |
---|---|---|
tag | string | 要重新验证的缓存标签。 |
返回 Promise<void>
。了解更多关于重新验证数据或 revalidateTag()
函数的信息。