Registry / storage / lrucache

lrucache

JSON →
library1.0.3jsnpmunverified

LRUCache is a minimal LRU cache implementation for Node.js and browsers (v1.0.3). It uses a linked list to enforce LRU eviction; get, set, and update operations promote accessed keys. Unlike popular alternatives like lru-cache, it offers a smaller API surface with methods like staleKey() and popStale(). Active development appears stalled (last release Jul 2022, few updates). Suitable for simple caching needs where a lightweight dependency is preferred.

npm install lrucache
INSTALL
IMPORT
SIG · LRUCACHE
L
lrucache
storagejavascriptv1.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LRUCache
import LRUCache from 'lrucache'
import { LRUCache } from 'lrucache'
Default export only. Named import {} will fail.
LRUCache (CJS)
const LRUCache = require('lrucache')
const { LRUCache } = require('lrucache')
CommonJS require returns the class directly; destructuring will be undefined.
LRUCache (browser global)
<script src='lrucache.js'></script> const cache = LRUCache(100)
Exposes global LRUCache function when loaded via script tag (no module system).

Creates an LRU cache with capacity 3, sets keys, promotes via get, and triggers eviction. Shows has(), keys(), info(), staleKey(), and popStale().

import LRUCache from 'lrucache'; const cache = LRUCache(3); cache.set('a', 1); cache.set('b', 2); cache.set('c', 3); cache.get('a'); // 1, promotes 'a' cache.set('d', 4); // evicts 'b' (least recently used) console.log(cache.has('b')); // false console.log(cache.keys()); // ['a', 'c', 'd'] console.log(cache.info()); // { capacity: 3, size: 3 } const staleKey = cache.staleKey(); // 'c' or 'd' depending on order console.log(cache.popStale()); // returns the stalest value and removes it
Debug
Known issues
gotchaget() returns value but does NOT clone; mutations affect the cache.
fix
If mutation is a concern, return a deep copy of the value.
affects: >=1.0.0
gotchaset() with a key that already exists does NOT update the value; use update() or remove+set.
fix
Use update() or manually remove then set to overwrite.
affects: >=1.0.0
gotcharemoveAll() does NOT exist; the correct method is removeAll()? Actually the doc says `removeAll(key)` but key is optional; calling without key clears the entire cache.
fix
Call cache.removeAll() without arguments to clear all entries.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: LRUCache is not a constructor
Using import { LRUCache } instead of default import.
fix
Use: import LRUCache from 'lrucache'
Cannot find module 'lrucache'
Package not installed or typo in require/import.
fix
Run: npm install lrucache
TypeError: cache.update is not a function
update() only works on existing keys; if key doesn't exist, method not available.
fix
Check key existence with has() before calling update().
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
24
OpenAI (training)
1
Resources
lrucache — npm install lrucache · libregistry