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.
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.
A quick 3-question quiz on this lesson — instant feedback.
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.
Project-based Node.js Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Node.js Foundations books — check the publication year so the examples match current versions.
Search Amazon →