Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * A simple LRU cache implementation
 * @param K - Key type
 * @param V - Value type
 * @example
 * const cache = new LRUCache<string, number>(10);
 * cache.set("foo", 1);
 * cache.set("bar", 2);
 * cache.set("baz", 3);
 * cache.set("qux", 4);
 */
export class LRUCache<K, V> {
  private maxSize: number;
  private map: Map<K, V>;
 
  constructor(maxSize: number) {
    this.maxSize = maxSize;
    this.map = new Map();
  }
 
  get(key: K): V | undefined {
    if (!this.map.has(key)) return undefined;
    // Touch: move to end (most recently used)
    const value = this.map.get(key)!;
    this.map.delete(key);
    this.map.set(key, value);
    return value;
  }
 
  set(key: K, value: V): void {
    if (this.map.has(key)) {
      this.map.delete(key);
    } else if (this.map.size >= this.maxSize) {
      // Remove least recently used (first item)
      const lru = this.map.keys().next();
      if (!lru.done) {
        this.map.delete(lru.value);
      }
    }
    this.map.set(key, value);
  }
 
  has(key: K): boolean {
    return this.map.has(key);
  }
 
  get size(): number {
    return this.map.size;
  }
 
  values(): IterableIterator<V> {
    return this.map.values();
  }
}