|
| 1 | +/** |
| 2 | + * Tiny in-process bounded TTL cache shared by the realtime feeds. |
| 3 | + * |
| 4 | + * Entries expire after `ttlMs`. An expired entry is evicted when read (`get`); on |
| 5 | + * write, if the cache is at `maxEntries`, expired entries are swept and, if it's |
| 6 | + * still full (pathologically all live), the oldest insertion is dropped. Node is |
| 7 | + * single-threaded so no locking is needed. Used where a miss is cheap and |
| 8 | + * correctness-safe (read-through hydration, per-handle working sets, per-org flag |
| 9 | + * resolution). |
| 10 | + * |
| 11 | + * A stored value of `undefined` cannot be distinguished from a miss; callers that |
| 12 | + * need to cache "absence" should store an explicit sentinel (e.g. `null`). |
| 13 | + */ |
| 14 | +export class BoundedTtlCache<V> { |
| 15 | + readonly #entries = new Map<string, { value: V; expiresAt: number }>(); |
| 16 | + |
| 17 | + constructor( |
| 18 | + private readonly ttlMs: number, |
| 19 | + private readonly maxEntries: number |
| 20 | + ) {} |
| 21 | + |
| 22 | + get(key: string): V | undefined { |
| 23 | + const entry = this.#entries.get(key); |
| 24 | + if (!entry) { |
| 25 | + return undefined; |
| 26 | + } |
| 27 | + if (entry.expiresAt > Date.now()) { |
| 28 | + return entry.value; |
| 29 | + } |
| 30 | + // Evict on read so expired entries don't linger until the next at-capacity |
| 31 | + // sweep — important for read-heavy / low-churn caches (per-handle working sets). |
| 32 | + this.#entries.delete(key); |
| 33 | + return undefined; |
| 34 | + } |
| 35 | + |
| 36 | + set(key: string, value: V): void { |
| 37 | + if (this.#entries.size >= this.maxEntries) { |
| 38 | + const now = Date.now(); |
| 39 | + for (const [key, entry] of this.#entries) { |
| 40 | + if (entry.expiresAt <= now) { |
| 41 | + this.#entries.delete(key); |
| 42 | + } |
| 43 | + } |
| 44 | + if (this.#entries.size >= this.maxEntries) { |
| 45 | + const oldest = this.#entries.keys().next().value; |
| 46 | + if (oldest !== undefined) { |
| 47 | + this.#entries.delete(oldest); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + this.#entries.set(key, { value, expiresAt: Date.now() + this.ttlMs }); |
| 52 | + } |
| 53 | + |
| 54 | + get size(): number { |
| 55 | + return this.#entries.size; |
| 56 | + } |
| 57 | +} |
0 commit comments