BeginnerCoder

Lesson 20 — Cache with TTL

Values that expire

A cache keeps answers you already computed so the next request is fast. A TTL (time to live) says how long an entry stays valid; after that it counts as stale and must be fetched again. Each entry stores the value and the time it was saved.

To make the result predictable we pass the current time in as now (in real code you would use Date.now()).

Why it matters: caches make sites fast, and TTLs stop them from showing old data forever.

Want it another way?
Task

In get(), return null when the entry is missing or older than ttl (now - entry.t > ttl).

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Node.js Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.