MCPcopy
hub / github.com/tldraw/tldraw / LruCache

Class LruCache

packages/utils/src/lib/LruCache.ts:2–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1/** Simple LRU cache backed by a Map's insertion-order iteration. @public */
2export 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…