← Back to problems

5. Key-Value Store with TTL

MEDIUM
CACHEDESIGNLLDTTL

Implement an in-memory key-value store with per-key expiry driven by a logical clock.

Implement a key-value store over a logical clock that starts at time 0. Keys and values are strings. Commands: - SET <key> <value> <ttl> : store the key. If <ttl> is 0, the key never expires; otherwise it expires at (currentTime + ttl). Re-SETting an existing key overwrites its value AND resets its TTL. - GET <key> : print the value, or "NIL" if the key is absent or expired. - TTL <key> : print the remaining time to live: the seconds until expiry if it has one, "-1" if the key exists with no expiry, or "-2" if the key is absent or expired. - DELETE <key> : remove the key; print "1" if a live key was removed, "0" otherwise. - TICK <n> : advance the logical clock by n units. - COUNT : print the number of currently live (non-expired) keys. A key with expiry time E is considered expired once currentTime >= E (expiry is inclusive).
Log in to submit a solution

Comments

Log into join the discussion.