{"id":14481,"library":"cache-manager-redis-store","title":"Redis Store for node-cache-manager","description":"cache-manager-redis-store is a dedicated Redis store for the `node-cache-manager` library, providing a performant and reliable caching layer for Node.js applications. The current stable version is 3.0.1, released in October 2022. It generally follows an irregular release cadence, often aligning with major dependency updates or critical bug fixes. This package differentiates itself from older alternatives like `node-cache-manager-redis` by avoiding the `redis-pool` library, instead offering a simpler wrapper that directly passes configuration to the underlying `redis` client (upgraded to `redis@^4` in v3.0.0). It aims to provide a straightforward integration for Redis caching within the `node-cache-manager` ecosystem, with robust support for both single and multi-store caching setups. The library also provides TypeScript types, enhancing the developer experience for TypeScript projects.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/dabroek/node-cache-manager-redis-store","tags":["javascript","typescript"],"install":[{"cmd":"npm install cache-manager-redis-store","lang":"bash","label":"npm"},{"cmd":"yarn add cache-manager-redis-store","lang":"bash","label":"yarn"},{"cmd":"pnpm add cache-manager-redis-store","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This 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.","package":"cache-manager","optional":false},{"reason":"The 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.","package":"redis","optional":false}],"imports":[{"note":"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()`.","wrong":"import { redisStore } from 'cache-manager-redis-store';","symbol":"redisStore","correct":"import redisStore from 'cache-manager-redis-store';"}],"quickstart":{"code":"import { caching, multiCaching } from 'cache-manager';\nimport redisStore from 'cache-manager-redis-store';\n\n// Configure Redis client options (e.g., host, port, password)\nconst redisOptions = {\n  host: process.env.REDIS_HOST ?? 'localhost',\n  port: parseInt(process.env.REDIS_PORT ?? '6379', 10),\n  password: process.env.REDIS_PASSWORD ?? '', // Use environment variables for sensitive info\n  db: 0,\n  ttl: 600, // Default TTL in seconds\n};\n\nasync function initializeAndUseCache() {\n  // Create an instance of the Redis store\n  const redisCacheStore = redisStore(redisOptions);\n\n  // Initialize a single cache manager with the Redis store instance\n  const redisCache = caching({ store: redisCacheStore, ttl: 600 });\n\n  // Listen for Redis connection errors\n  // Note: For redis@^4, client events might be handled differently, \n  // often through the client instance returned by `redisCacheStore.getClient()`.\n  const redisClient = await redisCacheStore.getClient();\n  redisClient.on('error', (error: Error) => {\n    console.error('Redis Client Error:', error);\n  });\n\n  // Set a value\n  await redisCache.set('foo', 'bar', 5); // TTL of 5 seconds\n  console.log('Set foo to bar with TTL 5s');\n\n  // Get a value\n  const result = await redisCache.get('foo');\n  console.log(`Get foo: ${result}`); // Expected: 'bar'\n\n  // Wrap: fetches from cache, or executes function and caches result\n  async function getSlowUser(id: number) {\n    console.log(`Returning user ${id} from slow database.`);\n    return new Promise(resolve => setTimeout(() => resolve({ id, name: `User ${id}` }), 100));\n  }\n\n  const userId = 123;\n  const userKey = `user_${userId}`;\n\n  const user1 = await redisCache.wrap(userKey, () => getSlowUser(userId), { ttl: 10 });\n  console.log('Wrapped user (first call):', user1); // Logs 'Returning user from slow database.'\n\n  const user2 = await redisCache.wrap(userKey, () => getSlowUser(userId), { ttl: 10 });\n  console.log('Wrapped user (second call):', user2); // Fetches from cache, no 'slow database' log\n\n  // --- Multi-store caching ---\n  const memoryCacheStore = caching({ store: 'memory', max: 100, ttl: 60 });\n  const multiCache = multiCaching([memoryCacheStore, redisCache]);\n\n  const multiUserKey = 'multiUser_456';\n  const multiUser = await multiCache.wrap(multiUserKey, () => getSlowUser(456), { ttl: 15 });\n  console.log('Multi-cache wrapped user (first call):', multiUser);\n\n  // Subsequent calls fetch from the highest priority cache (memoryCacheStore)\n  const multiUser2 = await multiCache.wrap(multiUserKey, () => getSlowUser(456), { ttl: 15 });\n  console.log('Multi-cache wrapped user (second call):', multiUser2);\n\n  // Clean up\n  await redisCache.del('foo');\n  await redisCache.del(userKey);\n  await multiCache.del(multiUserKey);\n  console.log('Cleaned up cache entries.');\n}\n\ninitializeAndUseCache().catch(console.error);","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade `cache-manager` to v4.x or newer. Refactor cache interactions to use the promise-based API (async/await or .then()).","message":"Version 3.0.0 upgraded the underlying Redis client to `redis@^4`. This version of `redis` is entirely promise-based, deprecating callback-style APIs. Ensure your `cache-manager` version is compatible (typically v4.x or newer) and update your code to use async/await or `.then()` for cache operations.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Modify `cacheManager.caching()` configuration from `store: redisStore` to `store: redisStore(yourRedisOptions)`.","message":"Since v3.0.0, the Redis store must be explicitly instantiated *before* being passed to `cacheManager.caching()`. You can no longer pass the factory function directly and expect `cache-manager` to instantiate it internally. The `store` option for `caching()` should receive an *instance* of the store, e.g., `store: redisStore(options)` instead of `store: redisStore`.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade your Node.js environment to version 16.18.0 or newer.","message":"Support for Node.js versions older than 16.18 was dropped in v3.0.0. Running this package on older Node.js environments will result in runtime errors.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Review your application's caching logic to ensure it doesn't rely on caching `null` or `undefined`. If you need to cache these, consider transforming them to a cacheable sentinel value before storage.","message":"In v2.0.0, the `set` method was updated to strictly check `isCacheableValue` before storing a value. This means `null` or `undefined` values might no longer be cached, potentially altering behavior for applications that relied on caching such values.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"If migrating from `node-cache-manager-redis`, thoroughly review the usage examples and breaking changes for `cache-manager-redis-store` to adapt your code. This is not a drop-in replacement.","message":"This package (`cache-manager-redis-store`) is a completely separate and actively maintained project from the older `node-cache-manager-redis`. It uses a simpler wrapper and `redis@^4` directly, avoiding `redis-pool`. If migrating, expect API differences and breaking changes.","severity":"gotcha","affected_versions":"all"},{"fix":"Upgrade to `cache-manager-redis-store` version 3.0.1 or newer to ensure correct `del` behavior, especially in multi-cache setups.","message":"A bug in v3.0.0 prevented the `redisStore.del` method from correctly accepting an options object, which particularly impacted the `multiCaching` interface where `del` operations might not propagate correctly across all caches. This was fixed in v3.0.1.","severity":"gotcha","affected_versions":"3.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you call the `redisStore` factory function to get an instance before passing it: `const myRedisStore = redisStore(options); const cache = caching({ store: myRedisStore });`","cause":"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.","error":"TypeError: store.create is not a function"},{"fix":"For 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.","cause":"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.","error":"ERR_MODULE_NOT_FOUND: Cannot find package 'cache-manager-redis-store' imported from ..."},{"fix":"Verify 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.","cause":"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.","error":"Error: connect ECONNREFUSED <ip-address>:<port>"}],"ecosystem":"npm"}