Server Cache
The cache option on a GET action, invalidated after a write
The report action takes about 800 ms. With cache: { maxAge: 30 }, the first request runs it and the requests in the next 30 seconds get the stored result. Middleware such as authentication and rate limits would still run on every request, because the cache sits after them.
A fast request with the same "generated at" time was served from the cache. The cache lives in the server's memory here; an app on several instances or serverless functions would use a shared store.
How it works
// server/actions/cached-report.get.ts
export default defineAction({
input: z.object({}).optional(),
cache: { maxAge: 30 },
handler: async () => {
await delay(800)
return { generatedAt: new Date().toISOString() }
},
})
// server/actions/refresh-report.post.ts
export default defineAction({
handler: async () => {
await invalidateActionCache('cached-report')
return { invalidated: true }
},
})