Registry / storage / level-lru-cache

level-lru-cache

JSON →
library1.0.0jsnpmunverified

Simple LRU (Least Recently Used) cache implementation on top of LevelUP. Version 1.0.0 is the current stable release with no active development. It uses timestamps for eviction ordering, trading off worst-case consistency for simplicity. Unlike other caches, it avoids race conditions by performing all operations atomically within LevelUP batches, making it suitable for single-machine concurrent usage but not recommended for distributed systems. The cache stores multiple records per key with timestamps, cleaning up old records on each put and get. It requires a LevelUP instance with any LevelDOWN backend and sets a loose limit for key count.

npm install level-lru-cache
INSTALL
IMPORT
SIG · LEVEL-LRU-CACHE
L
level-lru-cache
storagejavascriptv1.0.0
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.

Cache
const Cache = require('level-lru-cache')
import Cache from 'level-lru-cache'
This package uses CommonJS and does not support ESM imports directly. Use require().
Cache
const { default: Cache } = await import('level-lru-cache')
In Node.js with ESM, use dynamic import with .default.
levelup
const levelup = require('levelup')
import levelup from 'levelup'
levelup also uses CommonJS; import may not work depending on module system.
memdown
const memdown = require('memdown')
import memdown from 'memdown'
For testing, memdown is a common LevelDOWN choice.

Create a LevelUP instance with memdown, initialize a level-lru-cache with limit 100, then put and get a value asynchronously.

const Cache = require('level-lru-cache'); const levelup = require('levelup'); const memdown = require('memdown'); async function main() { const db = levelup(memdown()); const cache = new Cache(db, 100); // limit to 100 keys cache.put('key1', 'value1', (err) => { if (err) console.error('put error:', err); }); cache.get('key1', (err, value) => { if (err) console.error('get error:', err); else console.log('Got:', value); // 'value1' }); } main();
Debug
Known issues
gotchaTimestamps are used for eviction ordering, but if multiple operations occur quickly on the same machine, timestamps may be equal, causing incorrect eviction.
fix
Consider using monotonic clock sources or add microsecond precision to avoid ties.
affects: >=1.0.0
gotchaThe cache does not support TTL (time-to-live) expiration; it only evicts based on count limit.
fix
Implement your own expiration layer on top, or use a different caching library like lru-cache with TTL support.
affects: >=1.0.0
gotchaThe cache stores multiple records per key (old and new versions). On get, all old records are deleted and a new record is written. This could degrade performance under heavy write load.
fix
If write-heavy, consider tuning the limit or using a different cache.
affects: >=1.0.0
gotchaThe limit parameter is a 'loose limit'; the cache may temporarily store more keys than the limit due to asynchronous batch operations.
fix
Set a slightly smaller limit than desired if exactness is needed.
affects: >=1.0.0
Errors
Common errors & fixes
Error: The "db" keyword must be a LevelUP instance
First argument to Cache constructor is not a LevelUP instance.
fix
const db = levelup(memdown()); const cache = new Cache(db, 100);
TypeError: cache.put is not a function
Cache was not instantiated with 'new'.
fix
const cache = new Cache(db, 100);
Error: LevelUP instance not provided
Missing LevelUP instance as first argument.
fix
Pass a valid LevelUP instance. Example: const db = levelup(memdown()); const cache = new Cache(db, 100);
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
leveluprequiredlevel-lru-cache wraps a LevelUP database instance; LevelUP is a peer dependency.
leveldownrequiredLevelUP requires a LevelDOWN backend (like leveldown, memdown, level-js) to operate.
Agent activity
11 hits · last 30 days
node
10
Amazon
1
Resources
level-lru-cache — npm install level-lru-cache · libregistry