PrivatecachePrivatemaxPrivatecurrentPrivategetPrivate OptionalonPrivatepinnedPrivatehitsPrivatemissesPrivateevictionsGet 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.
Cache key to lookup
Cached value if present, undefined if not found
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.
Cache key
Value to cache (size calculated via getSize function)
Optionalopts: { evictMostRecent?: boolean }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
PrivateselectChoose 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.
Release a pin so key is eligible for eviction again.
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.
LRU Cache using Map for O(1) get/set/delete operations. Map maintains insertion order, enabling efficient LRU tracking.