Timed Cache with Sidecar Cleanup
Implement a key-value cache where each entry has its own TTL.
Support the following operations:
SET <key> <value> <ttlSeconds> <now>GET <key> <now>CLEANUP <now>
GET is the hot path, so it should not scan the whole cache to remove expired entries. A separate sidecar or background process may perform proactive cleanup.
Return the output lines produced by the operation batch. GET should print the current value or NULL if the key is missing or expired.
Complete the function runTimedCache in the editor below.
runTimedCache has the following parameter:
String[] operations: cache operations executed in order
Returns
String[]: the output lines produced by the batch.
1Example 1
Key a expires at time 5. It is still valid at time 3 and expired by time 6.
2Example 2
By time 3, key b has expired and may be removed during cleanup. Key a is still alive.
Constraints
Limits and guarantees your solution can rely on.
GETshould stay cheap even when the cache is large.- Each key has a per-item TTL.
- A cleanup helper may delete expired entries outside the hot path.