Cache lifetimes
A 'use cache' scope has a lifetime, and cacheLife sets it. Import it from
next/cache and call it inside the cached function:
import { cacheLife } from 'next/cache';
export async function getProducts() {
'use cache';
cacheLife('hours');
return db.query('SELECT * FROM products');
}
Next.js ships built-in profiles — seconds, minutes, hours, days, weeks,
and max — each a preset of three timings: stale (how long the client serves
it before revalidating), revalidate (the server refresh interval), and expire
(when the entry must be dropped). The default profile, used when you omit
cacheLife, is 5 minutes stale, 15 minutes revalidate, and never expires by time.
For finer control, pass an object:
'use cache';
cacheLife({ stale: 3600, revalidate: 7200, expire: 86400 });
Pair every 'use cache' with a cacheLife so the lifetime is explicit at the
call site instead of depending on the default profile. A cache that is too
short-lived — the seconds profile, revalidate: 0, or an expire under five
minutes — is excluded from prerenders and becomes a dynamic hole instead.
You are about to answer a short quiz on cacheLife profiles.