Nextjs Fundamentals · Caching & Revalidation

Cache lifetimes

cacheLife profiles

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.

  1. cacheLife sets how long a cached function or component stays valid.
  2. Built-in cacheLife profiles include seconds, minutes, hours, days, weeks, and ___.
  3. The default profile, used when cacheLife is omitted, revalidates every 15 minutes and never expires by time.
  4. A custom cacheLife object can set stale, revalidate, and ___.
  5. A very short-lived cache, such as the seconds profile, is excluded from prerenders and becomes a dynamic hole.
  6. cacheLife can only be called outside a 'use cache' scope.