Luxar Viewer API Documentation - v2026.9.22
    Preparing search index...

    Class LRUCache<V>

    LRU Cache using Map for O(1) get/set/delete operations. Map maintains insertion order, enabling efficient LRU tracking.

    Type Parameters

    • V
    Index
    • Type Parameters

      • V

      Parameters

      • maxSize: number
      • getSize: (v: V) => number
      • OptionalonEvict: (key: string, value: V) => void

      Returns LRUCache<V>

    cache: Map<string, V> = ...
    maxSize: number
    currentSize: number = 0
    getSize: (v: V) => number
    onEvict?: (key: string, value: V) => void
    pinned: Set<string> = ...
    hits: number = 0
    misses: number = 0
    evictions: number = 0
    • Get value from cache with LRU promotion.

      On hit, moves item to end of Map (most recently used position). This ensures least recently used items are at the beginning for eviction.

      Parameters

      • key: string

        Cache key to lookup

      Returns V | undefined

      Cached value if present, undefined if not found

      const cache = new LRUCache<Uint8Array>(1024 * 1024, v => v.byteLength);
      const chunk = cache.get('positions/0.0.0');
      if (chunk) {
      // Cache hit - chunk moved to MRU position
      console.log(`Hit: ${chunk.byteLength} bytes`);
      } else {
      // Cache miss - need to fetch
      }

      Performance: O(1) - two Map operations (delete + set for reordering)

    • Set value in cache with automatic LRU eviction.

      If key exists, updates value and recalculates size. If cache is full, evicts least recently used items until space available. New item is always added at end (most recently used position).

      Eviction policy: Remove items from beginning of Map (oldest) until sufficient space. JavaScript Map maintains insertion order.

      Parameters

      • key: string

        Cache key

      • value: V

        Value to cache (size calculated via getSize function)

      • Optionalopts: { evictMostRecent?: boolean }

      Returns void

      const cache = new LRUCache<Uint8Array>(64 * 1024, v => v.byteLength);

      // Add chunk (may trigger eviction if cache full)
      const chunk = new Uint8Array(32 * 1024); // 32KB
      cache.set('positions/0.0.0', chunk);
      console.log(`Cache: ${cache.count} items, ${cache.size} bytes`);
      // Eviction demonstration
      cache.set('a', new Uint8Array(30 * 1024)); // 30KB
      cache.set('b', new Uint8Array(30 * 1024)); // 30KB, total 60KB
      cache.set('c', new Uint8Array(30 * 1024)); // 30KB, evicts 'a' (LRU)
      console.log(cache.has('a')); // false - evicted
      console.log(cache.has('b')); // true - still cached

      Performance: O(k) where k = number of evictions needed (typically 0-2)

    • Choose the next eviction victim. Prefers an UNPINNED key — the oldest (Map head) under the default LRU policy, or the newest (Map tail) under the scan policy (evictMostRecent). If every entry is pinned, falls back to the plain oldest/newest so the byte budget can still be honored (pinned entries are eviction-exempt only best-effort, never a hard reservation).

      Fast path when nothing is pinned (the norm — only the SliceCache pins, and only during active prefetch): the LRU victim is the Map head in O(1); this keeps L0/L1 chunk eviction — which never pins and can hold tens of thousands of entries — cheap. Only when pins exist do we pay the O(n) scan to skip them (SliceCache, n at most a few hundred multi-MB slices). The MRU branch is O(n) regardless — a Map has no reverse iterator — matching the pre-pin behavior.

      Parameters

      • evictMostRecent: boolean

      Returns string | undefined

    • Protect key from routine eviction until unpin (best-effort).

      Parameters

      • key: string

      Returns void

    • Release a pin so key is eligible for eviction again.

      Parameters

      • key: string

      Returns void

    • Read a value WITHOUT LRU promotion and WITHOUT touching the hit/miss counters. For bookkeeping reads (e.g. "is the stored entry longer than what I'm about to store?") that must not perturb eviction order or the hit-rate statistic the monitor reports.

      Parameters

      • key: string

      Returns V | undefined