This recipe creates one cache layer. It is not edible, but neither are most cache layers.

Ingredients
- One stale response.
- Two tablespoons of
max-age. - A pinch of panic.
- A monitoring dashboard nobody opens during normal business hours.
Method
const pantry = new Map();
export function remember(key, value, ttlMs) {
pantry.set(key, {
value,
expiresAt: Date.now() + ttlMs,
});
}
export function recall(key) {
const entry = pantry.get(key);
if (!entry || entry.expiresAt < Date.now()) {
pantry.delete(key);
return undefined;
}
return entry.value;
}
Warning
Do not season a cache with mystery invalidation unless you are ready to answer questions from your future self.
If the feature still feels slow, rename it to “eventually fresh” and write a blog post about tradeoffs.