Registry / database / redis-lru

redis-lru

JSON →
library0.6.0jsnpmunverified

A Redis-backed LRU cache for Node.js, version 0.6.0. It allows caching that persists across process restarts and is shareable between multiple Node.js processes via Redis. The API is inspired by node-lru-cache and supports both node_redis and ioredis clients. Offers configurable options such as max size, namespace, maxAge, custom scoring, and incremental scoring (usable as LFU). All methods return Promises. Compared to in-memory LRU caches, this provides durability and sharing, at the cost of network latency. Active development, with a steady release cadence (latest: 0.6.0).

npm install redis-lru
INSTALL
IMPORT
SIG · REDIS-LRU
R
redis-lru
databasejavascriptv0.6.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.

lru
import lru from 'redis-lru'
const lru = require('redis-lru')
Package exports a default function (factory). Use ESM default import (ESM support since v0.5.0).
lru (CommonJS)
const lru = require('redis-lru')
const { lru } = require('redis-lru')
CommonJS users require the function as default; named destructuring is incorrect.
LRUCache
const cache = lru(redisClient, options)
new lru(redisClient, options)
lru is a factory function, not a constructor. Do not use 'new'.
bluebird
import lru from 'redis-lru'; const cache = lru(redisClient, { max: 10 })
The returned cache object provides methods (set, get, etc.) that return Promises. No additional imports needed.

Shows how to create, use, and see eviction in a Redis-backed LRU cache with both in-memory and TTL expiry.

import lru from 'redis-lru'; import { createClient } from 'redis'; const client = createClient({ url: process.env.REDIS_URL ?? 'redis://localhost:6379' }); await client.connect(); const cache = lru(client, { max: 5, maxAge: 60000 }); await cache.set('user:1', { name: 'Alice', role: 'admin' }); const user = await cache.get('user:1'); console.log(user); // { name: 'Alice', role: 'admin' } await cache.set('user:2', { name: 'Bob' }); await cache.set('user:3', { name: 'Charlie' }); const count = await cache.count(); console.log(count); // 3 // Demonstrate eviction: add more items than max await cache.set('user:4', { name: 'Diana' }); await cache.set('user:5', { name: 'Eve' }); await cache.set('user:6', { name: 'Frank' }); // evicts oldest (user:1) const evicted = await cache.get('user:1'); console.log(evicted); // null await client.quit();
Debug
Known issues
breakingThe package switched from callbacks to promises in v0.5.0. Calling methods without .then() or await will not get results.
fix
Use .then() or async/await on all cache methods.
affects: >=0.5.0
deprecatedThe 'namespace' option was previously called 'prefix' in older versions (pre-0.5.0).
fix
Use 'namespace' instead of 'prefix'.
affects: <0.5.0
gotchaValues are serialized via JSON.stringify. Custom objects with methods or circular references will fail.
fix
Ensure values are JSON-serializable. Use a custom serialize/deserialize approach if needed.
affects: >=0.0.0
gotchaKeys must be strings. Non-string keys (e.g., numbers or objects) will be coerced via toString().
fix
Always pass string keys to avoid unexpected behavior.
affects: >=0.0.0
gotchaThe cache relies on Redis sorted sets. Very large collections may degrade performance.
fix
Monitor Redis performance; consider partitioning or using an in-memory LRU for high-throughput scenarios.
affects: >=0.0.0
breakingIn v0.5.0, the default export changed from a constructor-like function to a simpler factory. 'new' no longer works.
fix
Call lru(client, options) without 'new'.
affects: >=0.5.0
Errors
Common errors & fixes
const lru = require('redis-lru'); const cache = new lru(client, 5); TypeError: lru is not a constructor
Using 'new' on the factory function in v0.5.0+.
fix
const cache = lru(client, { max: 5 });
TypeError: cache.get(...).then is not a function
Using an older version (<0.5.0) that uses callbacks, or mixing promise and callback styles.
fix
Update to v0.5.0+ and use Promises. Or check the version in package.json.
Error IRCError: ERR wrong number of arguments for 'zadd' command
The 'score' option returns a non-numeric value, breaking Redis sorted set commands.
fix
Ensure the 'score' function returns a number (e.g., Date.now()).
Error: Namespace must be a string
Passing an invalid namespace option (e.g., number or object).
fix
Provide a valid string for 'namespace'.
Upgrade
Version history
0.6.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
23
Meta
1
Amazon
1
OpenAI (training)
1
Resources
redis-lru — npm install redis-lru · libregistry