Appearance
缓存中间件
🌐 Cache Middleware
缓存中间件使用了 Web 标准的 Cache API。
🌐 The Cache middleware uses the Web Standards' Cache API.
缓存中间件目前支持使用自定义域的 Cloudflare Workers 项目以及使用 Deno 1.26+ 的 Deno 项目。同样适用于 Deno Deploy。
🌐 The Cache middleware currently supports Cloudflare Workers projects using custom domains and Deno projects using Deno 1.26+. Also available with Deno Deploy.
Cloudflare Workers 会尊重 Cache-Control 头并返回缓存的响应。有关详细信息,请参阅 Cloudflare 文档中的缓存。Deno 不会尊重头,因此如果你需要更新缓存,你需要自己实现机制。
🌐 Cloudflare Workers respects the Cache-Control header and return cached responses. For details, refer to Cache on Cloudflare Docs. Deno does not respect headers, so if you need to update the cache, you will need to implement your own mechanism.
请参见下方的 使用说明,了解每个平台的操作指南。
🌐 See Usage below for instructions on each platform.
导入
🌐 Import
ts
import { Hono } from 'hono'
import { cache } from 'hono/cache'用法
🌐 Usage
ts
app.get(
'*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
})
)ts
// Must use `wait: true` for the Deno runtime
app.get(
'*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
wait: true,
})
)选项
🌐 Options
required 缓存名称: string | (c: Context) => string | Promise<string>
缓存的名称。可以用于存储具有不同标识符的多个缓存。
🌐 The name of the cache. Can be used to store multiple caches with different identifiers.
optional 等待:boolean
一个布尔值,指示 Hono 是否应在继续处理请求之前等待 cache.put 函数的 Promise 解析。在 Deno 环境中必须为 true。默认值是 false。
🌐 A boolean indicating if Hono should wait for the Promise of the cache.put function to resolve before continuing with the request. Required to be true for the Deno environment. The default is false.
optional 缓存控制: string
Cache-Control 头的指令串。更多信息请参阅 MDN 文档。当未提供此选项时,请求中不会添加 Cache-Control 头。
🌐 A string of directives for the Cache-Control header. See the MDN docs for more information. When this option is not provided, no Cache-Control header is added to requests.
optional 变化: string | string[]
在响应中设置 Vary 头。如果原始响应头已经包含 Vary 头,则会合并这些值,并移除任何重复项。将其设置为 * 会导致错误。有关 Vary 头及其对缓存策略的影响的更多详细信息,请参阅 MDN 文档。
🌐 Sets the Vary header in the response. If the original response header already contains a Vary header, the values are merged, removing any duplicates. Setting this to * will result in an error. For more details on the Vary header and its implications for caching strategies, refer to the MDN docs.
optional keyGenerator: (c: Context) => string | Promise<string>
为 cacheName 存储中的每个请求生成键。这可以用于根据请求参数或上下文参数缓存数据。默认值是 c.req.url。
🌐 Generates keys for every request in the cacheName store. This can be used to cache data based on request parameters or context parameters. The default is c.req.url.
optional 可缓存状态码: number[]
应缓存的状态码数组。默认值是 [200]。使用此选项来缓存具有特定状态码的响应。
🌐 An array of status codes that should be cached. The default is [200]. Use this option to cache responses with specific status codes.
ts
app.get(
'*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
cacheableStatusCodes: [200, 404, 412],
})
)optional 缓存不可用: (() => void | Promise<void>) | false
一个回调函数或 false,用于控制当缓存 API 在全局作用域中不可用时的行为。默认情况下,会使用 console.log 记录一条消息。你可以提供一个自定义函数来定制行为,或者将其设置为 false 完全禁止日志记录。
🌐 A callback function or false that controls the behavior when the Cache API is not available in the global scope. By default, a message is logged with console.log. You can provide a custom function to customize the behavior, or set it to false to suppress the log entirely.
ts
// Custom logging
app.use(
cache({
cacheName: 'my-app-v1',
onCacheNotAvailable: () => {
console.log('Custom log: Cache API is not available.')
},
})
)ts
// Suppress logging
app.use(
cache({
cacheName: 'my-app-v1',
onCacheNotAvailable: false,
})
)