TTLs with setInterval active sweep
Date.now() returns milliseconds since epoch. We store Date.now() + N * 1000 as the expiry. Every read checks; we also sweep periodically with setInterval.
Lazy plus active expiry in Node
const STORE = new Map();
const EXPIRES = new Map(); // key -> ms-since-epoch
function checkExpired(k) {
const ts = EXPIRES.get(k);
if (ts !== undefined && Date.now() >= ts) {
STORE.delete(k); EXPIRES.delete(k);
return true;
}
return false;
}Sidecar Map + checkExpired called from every read.
By default, an active timer keeps the Node process alive. .unref() says 'if I am the only thing keeping the process alive, exit anyway'. Critical for background timers.
Quiz: Quiz
Loading practice…