A better caching module for NestJS, fully compatible with @nestjs/cache-manager v3
npm install @nestjs-kitchen/cache-manager
!NPM License

A better caching module for NestJS, fully compatible with @nestjs/cache-manager v3.
---
- ✅ Supports caching for regular methods.
- ✅ Dynamically set cache key and TTL.
- ✅ Fully compatible with @nestjs/cache-manager API.
- ✅ 100% test coverage.
``bash`
$ npm install --save @nestjs-kitchen/cache-manager cache-manager
`typescript
import { Module } from '@nestjs/common';
import { CacheModule } from '@nestjs-kitchen/cache-manager';
import { AppController } from './app.controller';
@Module({
imports: [CacheModule.register()],
controllers: [AppController],
})
export class AppModule {}
`
`typescript`
@Controller()
@UseInterceptors(CacheInterceptor)
export class AppController {
@Get()
findAll(): string[] {
return [];
}
}
`typescript`
@Injectable()
export class AppService {
@CacheResult()
@Get()
findAll(): string[] {
return [];
}
}
- For non-regular methods (HTTP, WebSocket, Microservice, or GraphQL), the callback receives ExecutionContext as the argument.
`typescriptuser:${request.params.id}
@Controller()
@CacheKey((context: ExecutionContext) => {
const request = context.switchToHttp().getRequest();
return ;`
})
@CacheTTL((context: ExecutionContext) => {
return context.switchToHttp().getRequest().query.cacheTime || 60;
})
@UseInterceptors(CacheInterceptor)
export class AppController {
@Get()
findAll(): string[] {
return [];
}
}
- For regular methods, the callback receives the method arguments.
`typescriptuser:${args[0]}
@Injectable()
export class AppService {
@CacheKey((args: any[]) => )`
@CacheTTL((args: any[]) => 120)
@CacheResult()
@Get()
findAll(): string[] {
return [];
}
}
Same as @nestjs/cache-manager CacheModule.
Same as @nestjs/cache-manager CacheInterceptor.
Decorator that applies a cache mechanism to regular method, enabling caching for method results.
- Only applicable to regular methods (non-HTTP/WebSocket/Microservice/GraphQL methods).
- Compatible with @CacheKey and @CacheTTL for cache control.
- Automatically disabled when applied to HTTP, WebSocket, Microservice, or GraphQL methods.
Example:
`typescript
class ExampleService {
@CacheResult()
async fetchData() { ... }
@CacheResult()
@CacheKey((args: any[]) => data:${args[0]})`
@CacheTTL(60)
async getItemById(id: number) { ... }
}
Decorator that sets the caching key used to store/retrieve cached items.
Supports both static string keys and dynamic keys via a callback function.
- When applied to HTTP, WebSocket, Microservice, or GraphQL methods, the callback receives an ExecutionContext as an argument.
- When applied to regular methods, the callback receives the corresponding method parameters.
- When applied to a class, only a callback function is allowed.
Example:
`typescript
@CacheKey('events') // Static key
async fetchData() { ... }
@CacheKey((context: ExecutionContext) => context.switchToHttp().getRequest().url)
async fetchDataWithDynamicKey() { ... }
@CacheKey((args: any[]) => args[0]) // First argument as key
async getItemById(id: string) { ... }
`
Decorator that sets the cache TTL (time-to-live) duration for cache expiration.
Supports both static numeric values and dynamic TTL values via a callback function.
- When applied to HTTP, WebSocket, Microservice, or GraphQL methods, the callback receives an ExecutionContext as an argument.
- When applied to regular methods, the callback receives the corresponding method parameters.
Eexample:
`typescript
@CacheTTL(5) // Static TTL of 5 seconds
async fetchData() { ... }
@CacheTTL((context: ExecutionContext) => context.getHandler().name === 'fastQuery' ? 2 : 10)
async fetchDataWithDynamicTTL() { ... }
@CacheTTL((args: any[]) => args[0] > 10 ? 30 : 60) // TTL based on first argument
async getItemById(id: number) { ... }
``
For more usage, please see Caching.
MIT License