All files / src LRUCache.ts

100% Statements 18/18
87.5% Branches 7/8
100% Functions 6/6
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                      272x 272x                 71x     15x 15x 15x 15x                 68x   1x 67x   4x 4x 4x     68x         6x         1x         2x      
/**
 * Generic LRU (Least Recently Used) cache with bounded capacity.
 *
 * Used internally by TemplateMiner for caching parameter extraction
 * regexes — each unique template generates a compiled regex, and the
 * cache prevents redundant compilation for frequently seen templates.
 *
 * Eviction policy: when capacity is exceeded, the least recently
 * accessed item is removed. An item's access time is updated on
 * every `get()` and `set()` call.
 *
 * Maps to Python `LRUCache` used by Drain3's parameter extraction
 * caching (via `cachedmethod` decorator).
 *
 * @typeParam K - The key type.
 * @typeParam V - The value type.
 *
 * @internal
 */
export class LRUCache<K, V> {
  private readonly _store: Map<K, V>;
  private readonly _maxSize: number;
 
  /**
   * @param maxSize - Maximum number of entries before eviction.
   */
  constructor(maxSize: number) {
    this._maxSize = maxSize;
    this._store = new Map();
  }
 
  /**
   * Retrieves a cached value and updates its access time (LRU promotion).
   *
   * @returns The cached value, or undefined if the key is not present.
   */
  get(key: K): V | undefined {
    if (!this._store.has(key)) return undefined;
 
    // LRU promotion: move accessed item to the end
    const value = this._store.get(key)!;
    this._store.delete(key);
    this._store.set(key, value);
    return value;
  }
 
  /**
   * Stores a value in the cache, evicting the LRU item if capacity is
   * exceeded. If the key already exists, updates the value and promotes
   * the access time.
   */
  set(key: K, value: V): void {
    if (this._store.has(key)) {
      // Update existing entry — promote access time
      this._store.delete(key);
    } else if (this._store.size >= this._maxSize) {
      // Evict the least recently used entry (first key in insertion order)
      const firstKey = this._store.keys().next().value;
      Eif (firstKey !== undefined) {
        this._store.delete(firstKey);
      }
    }
    this._store.set(key, value);
  }
 
  /** Number of entries currently in the cache. */
  get size(): number {
    return this._store.size;
  }
 
  /** Removes all entries. */
  clear(): void {
    this._store.clear();
  }
 
  /** Checks whether a key exists. */
  has(key: K): boolean {
    return this._store.has(key);
  }
}