Registry /
database / cache-manager-redis-store
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
redisStore
✓ import redisStore from 'cache-manager-redis-store';
✗ import { redisStore } from 'cache-manager-redis-store';
The package exports a default function (a store factory). Avoid named imports. Since v3.0.0, this factory function must be called to create an instance *before* being passed to `cache-manager.caching()`.
Demonstrates initializing a Redis store for `cache-manager` v4+, performing basic set/get operations, using the `wrap` method for data fetching, and setting up multi-store caching with both Redis and in-memory caches. It highlights the correct store instantiation for v3.x and promises-based API.
import { caching, multiCaching } from 'cache-manager';
import redisStore from 'cache-manager-redis-store';
// Configure Redis client options (e.g., host, port, password)
const redisOptions = {
host: process.env.REDIS_HOST ?? 'localhost',
port: parseInt(process.env.REDIS_PORT ?? '6379', 10),
password: process.env.REDIS_PASSWORD ?? '', // Use environment variables for sensitive info
db: 0,
ttl: 600, // Default TTL in seconds
};
async function initializeAndUseCache() {
// Create an instance of the Redis store
const redisCacheStore = redisStore(redisOptions);
// Initialize a single cache manager with the Redis store instance
const redisCache = caching({ store: redisCacheStore, ttl: 600 });
// Listen for Redis connection errors
// Note: For redis@^4, client events might be handled differently,
// often through the client instance returned by `redisCacheStore.getClient()`.
const redisClient = await redisCacheStore.getClient();
redisClient.on('error', (error: Error) => {
console.error('Redis Client Error:', error);
});
// Set a value
await redisCache.set('foo', 'bar', 5); // TTL of 5 seconds
console.log('Set foo to bar with TTL 5s');
// Get a value
const result = await redisCache.get('foo');
console.log(`Get foo: ${result}`); // Expected: 'bar'
// Wrap: fetches from cache, or executes function and caches result
async function getSlowUser(id: number) {
console.log(`Returning user ${id} from slow database.`);
return new Promise(resolve => setTimeout(() => resolve({ id, name: `User ${id}` }), 100));
}
const userId = 123;
const userKey = `user_${userId}`;
const user1 = await redisCache.wrap(userKey, () => getSlowUser(userId), { ttl: 10 });
console.log('Wrapped user (first call):', user1); // Logs 'Returning user from slow database.'
const user2 = await redisCache.wrap(userKey, () => getSlowUser(userId), { ttl: 10 });
console.log('Wrapped user (second call):', user2); // Fetches from cache, no 'slow database' log
// --- Multi-store caching ---
const memoryCacheStore = caching({ store: 'memory', max: 100, ttl: 60 });
const multiCache = multiCaching([memoryCacheStore, redisCache]);
const multiUserKey = 'multiUser_456';
const multiUser = await multiCache.wrap(multiUserKey, () => getSlowUser(456), { ttl: 15 });
console.log('Multi-cache wrapped user (first call):', multiUser);
// Subsequent calls fetch from the highest priority cache (memoryCacheStore)
const multiUser2 = await multiCache.wrap(multiUserKey, () => getSlowUser(456), { ttl: 15 });
console.log('Multi-cache wrapped user (second call):', multiUser2);
// Clean up
await redisCache.del('foo');
await redisCache.del(userKey);
await multiCache.del(multiUserKey);
console.log('Cleaned up cache entries.');
}
initializeAndUseCache().catch(console.error);
Errors
Common errors & fixes
TypeError: store.create is not a function
Attempting to pass the `cache-manager-redis-store` factory function directly to `cacheManager.caching()` when using `cache-manager` v4.x or higher, which expects an already instantiated store object or an object with a `create` method.
fixEnsure you call the `redisStore` factory function to get an instance before passing it: `const myRedisStore = redisStore(options); const cache = caching({ store: myRedisStore });` ERR_MODULE_NOT_FOUND: Cannot find package 'cache-manager-redis-store' imported from ...
This error typically occurs when trying to `require()` an ESM-only package or `import` a CommonJS-only package. While this package ships types, its core export behavior might lead to issues in mixed environments.
fixFor ESM projects, use `import redisStore from 'cache-manager-redis-store';`. For CommonJS, use `const redisStore = require('cache-manager-redis-store');`. Ensure your `tsconfig.json` (for TypeScript) or Node.js environment is configured correctly for module resolution. Error: connect ECONNREFUSED <ip-address>:<port>
The Redis client failed to connect to the specified Redis server. This could be due to the Redis server not running, incorrect host/port configuration, or firewall issues.
fixVerify that your Redis server is running and accessible from your application's host. Double-check the `host` and `port` values in your `redisStore` options. Ensure no firewalls are blocking the connection.
Audit
Dependencies
cache-managerrequiredThis is the core caching library this store integrates with. Version 4.x or higher is generally required for compatibility with cache-manager-redis-store v3.x.
redisrequiredThe underlying Redis client used for communication with the Redis server. Version 4.x or higher is a direct dependency since cache-manager-redis-store v3.x.