cache-manager is a Node.js caching module that provides a uniform API for multiple cache backends (in-memory, Redis, memcached, etc.). In NestJS, it serves as the foundation for the CacheModule abstraction.

NestJS integration

  • cache-manager-redis-store is the Redis adapter, providing the redisStore object passed to CacheModule.register().
  • The Cache type (imported from cache-manager) is used to type the injected CACHE_MANAGER token.
  • Provides get(key), set(key, value, { ttl }), del(key), and reset() methods.

Install

npm install cache-manager @types/cache-manager
npm install cache-manager-redis-store
npm install --save-dev @types/cache-manager-redis-store

Tradeoffs vs. direct ioredis

cache-manager simplifies caching by providing a backend-agnostic API and working with NestJS decorators. However, it abstracts away Redis-specific features (pub/sub, pipelines, key scanning, cluster config). For advanced Redis usage, direct ioredis integration is preferable.