Next.js Cache Components and use cache
"use cache" marks a function as safe to cache. Next.js then stores its answer and reuses it.
The cached function
Two lines do the work. The first marks the function. The second sets the fresh time to one hour.
import { cacheLife } from 'next/cache';
export async function getCachedBookCount(): Promise<{ count: number; at: string }> {
'use cache';
cacheLife('hours');
return { count: books.length, at: new Date().toISOString() };
}The live part stays live
The live time reads the request, so it sits inside Suspense. The cached part sits outside it.
async function LiveTime() {
await connection();
return <p>Live time now: {new Date().toISOString()}</p>;
}Example
for i in 1 2; do curl -s http://localhost:3110/cached | grep -o "2026-11-11T[0-9:.]*Z" | sort -u; echo "---"; sleep 2; doneKey points
- "use cache" goes at the top of an async function. It marks the whole function as cacheable.
- cacheLife("hours") sets the fresh time. The answer stays fresh for one hour.
- The cached time froze at 2026-11-11T20:33:17.664Z. Three fetches showed the same time.
- The cached time survived a server restart. The cache lives on disk, not only in memory.
- connection() plus Suspense keeps the live time fresh. It changed on every fetch.
In this example, two fetches run two seconds apart. Each fetch prints two times. The first time is the cached time, and it is the same on both fetches: 2026-11-11T20:33:17.664Z. The second time is the live time, and it changes: 20:39:13.452Z, then 20:39:15.741Z. One page holds both. The cached part stays frozen, and the live part stays fresh.
Note
Do not cache a page that must be fresh for every reader. Cache the slow part and stream the live part.
Try it Yourself
Edit the code, then press Run.
Running is simulated · TypeScript Playground
Exercise
What does cacheLife("hours") set?