The decision
A shared cache entry is a stored representation with an owner and lifetime. Decide whether expired data may be served, which writes invalidate it, and whether the value is public enough to share.
Read, predict, run
The model receives a timestamp rather than reading Date.now(), so a test can
hit the boundary exactly. It treats the expiry moment itself as stale:
export function run({ now, expiresAt }) {
return now < expiresAt ? "fresh" : "stale";
}Before pressing Run, predict what run returns for {"now": 9, "expiresAt": 10} and {"now": 10, "expiresAt": 10}, from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
npm install and npm run dev. Check your prediction
- Input
{"now": 9, "expiresAt": 10}→"fresh". - Input
{"now": 10, "expiresAt": 10}→"stale".
The case table computes these results on the server side of the simulation. It compares values; it does not prove a production deployment or database works.
The same three decisions in Next.js 16
Every cache answers three questions: who may share an entry (scope), how
long it stays fresh (lifetime), and which write ends it early
(invalidation). Next.js 16's Cache Components answer them in code. The
simulator can't run this, so read it; the capstone on Day 7 runs on a real
server. It needs cacheComponents: true in next.config.js:
// lib/reviews.js
import { cacheLife, cacheTag } from "next/cache";
export async function publicReviewCounts() {
"use cache"; // scope: one entry, shared by every caller
cacheLife("minutes"); // lifetime: a built-in profile
cacheTag("review-counts"); // a name that writes can invalidate
return db.query("SELECT status, count(*) FROM reviews GROUP BY status");
}// app/reviews/actions.js
"use server";
import { updateTag } from "next/cache";
export async function approveReview(id, version) {
// ...authorize and write, as on Days 1 and 2...
updateTag("review-counts"); // invalidation: the next read recomputes
}| Decision | The contract above | Next.js 16 |
|---|---|---|
| Scope | private means bypass | "use cache" shares one entry for the same arguments; it may not read cookies() or headers(), which throws |
| Lifetime | now < expiresAt | cacheLife("minutes"), a named profile, or an object with stale, revalidate and expire in seconds |
| Invalidation | a later expiresAt | cacheTag names the entry; updateTag in a Server Action expires it so the person who wrote sees the change; revalidateTag(tag, "max") elsewhere refreshes it in the background |
| Never cached | bypass | leave out "use cache"; await connection() from next/server marks a component as rendered for every request |
The scope row is the one that leaks data. Because a "use cache" function
can't read the request, its result can only depend on its arguments, so the
same arguments must mean the same answer for everyone. Per-user data, such as
"my reviews", is read outside the cache, or passed in as an argument that
really identifies the user, never looked up inside.
The failure to avoid
The simulator runs pages dynamically and can't prove a production cache hit. Don't infer static rendering, revalidation timing or user isolation from this lab.
Compare a shared cache keyed by URL alone with a private bypass. Ada loads
/reviews/mine; the cache stores her list under that URL; Grace loads the same
URL and receives Ada's list. Marking the representation private and bypassing
the shared cache for it makes that impossible, at the cost of a slower page.
Extend the contract
A value marked private must return bypass even when fresh. Keep the normal expiry rule for public values.
Edit lib/contract.js, then rerun the original cases. If the requested rule
changes an earlier result, update that expected value deliberately. Keep the
other cases. Add the acceptance cases to examples so the preview checks them too.
- Input
{"now": 1, "expiresAt": 10, "private": true}must return"bypass".
npm install and npm run dev. Hint 1: the idea
Whether something may be shared at all comes before how long it stays fresh. A private representation is never served from a shared cache, fresh or not.
Hint 2: a first step
Classify the representation before deciding whether its cached copy is usable.
Show one solution
Replace the exported run function and any constants shown here; keep the
examples export underneath it. Compare the result with your own approach.
export function run({ now, expiresAt, private: isPrivate }) {
return isPrivate ? "bypass" : now < expiresAt ? "fresh" : "stale";
}The private check comes first, so a fresh private entry is still bypass;
freshness only matters for values that may be shared. The destructuring
renames private to isPrivate because private is reserved in strict-mode
code. Treating private data as always stale would still store it, which is the
leak you're avoiding.
Explain it without the code
For the Review Queue's item list, write whether it's public or private, and which write must invalidate it.
Going further
Read the Next.js reference for use cache, cacheLife and updateTag.
The preview runs code-playpad's Next.js simulation. Real caching, hydration,
authentication and persistence require the real-server checks described on Day 7.
Completion is saved in this browser only. It does not measure mastery.