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 cache

vs. manual ioredis

AspectCacheModuleManual ioredis
SetupLow (config only)Medium (FactoryProvider + service)
FeaturesBasic get/set/del/resetFull Redis API (pub/sub, pipelines, etc.)
Backend portabilityYes (swap store)No (tied to Redis)
Decorator integrationYes (CacheInterceptor)No (must write manually)