CacheModule is a NestJS module (@nestjs/common) that provides a store-agnostic caching abstraction. It is configured once in AppModule and provides the CACHE_MANAGER injection token for manual cache operations.
Configuration
import * as redisStore from 'cache-manager-redis-store';
CacheModule.register({
store: redisStore,
host: 'localhost',
port: 6379,
ttl: 5, // default TTL in seconds
})Manual operations via CACHE_MANAGER
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
await this.cacheManager.get('key'); // returns value or null
await this.cacheManager.set('key', 'value', { ttl: 1000 }); // ttl in ms
await this.cacheManager.del('key'); // remove one entry
await this.cacheManager.reset(); // clear all cachevs. manual ioredis
| Aspect | CacheModule | Manual ioredis |
|---|---|---|
| Setup | Low (config only) | Medium (FactoryProvider + service) |
| Features | Basic get/set/del/reset | Full Redis API (pub/sub, pipelines, etc.) |
| Backend portability | Yes (swap store) | No (tied to Redis) |
| Decorator integration | Yes (CacheInterceptor) | No (must write manually) |