What you're building
A lightweight key-value store with time-to-live (TTL) expiry — the core primitive behind Redis, Memcached, and every in-memory cache.
Requirements
Build a KVStore class:
set(key, value, ttlMs)— store a value. IfttlMsis provided (> 0), the entry expires after that many milliseconds. IfttlMsis omitted or 0, the entry lives forever.get(key)— return the value, orundefinedif missing or expired. Expired entries should be treated as absent.delete(key)— remove a key. Returntrueif it existed (and had not expired),falseotherwise.has(key)— returntrueif the key exists and has not expired.size()— return the count of non-expired keys.
Expiry is lazy: check at access time using Date.now(). No setInterval or timers.
Stretch goals
keys()— return an array of all non-expired keys.flush()— delete all entries.
What this proves
You can model time-sensitive state, understand lazy evaluation vs. eager cleanup, and implement the exact pattern Redis uses for key expiry. Every caching layer in a web app relies on this.
⚡ 40 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
set and get a value
missing key returns undefined
delete returns true then false
size counts only live keys
Run your code to check these.