| 1 | /** Simple LRU cache backed by a Map's insertion-order iteration. @public */ |
| 2 | export class LruCache<K, V> { |
| 3 | private map = new Map<K, V>() |
| 4 | constructor(private maxSize: number) {} |
| 5 | |
| 6 | get(key: K): V | undefined { |
| 7 | if (!this.map.has(key)) return undefined |
| 8 | const value = this.map.get(key)! |
| 9 | // Move to most-recent position |
| 10 | this.map.delete(key) |
| 11 | this.map.set(key, value) |
| 12 | return value |
| 13 | } |
| 14 | |
| 15 | set(key: K, value: V): void { |
| 16 | if (this.map.has(key)) this.map.delete(key) |
| 17 | this.map.set(key, value) |
| 18 | if (this.map.size > this.maxSize) { |
| 19 | // Evict oldest entry |
| 20 | this.map.delete(this.map.keys().next().value!) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | has(key: K): boolean { |
| 25 | return this.map.has(key) |
| 26 | } |
| 27 | |
| 28 | // eslint-disable-next-line tldraw/no-setter-getter |
| 29 | get size(): number { |
| 30 | return this.map.size |
| 31 | } |
| 32 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…