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.
$ npm install --save @nestjs-kitchen/cache-manager cache-manager
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 {}
@Controller()
@UseInterceptors(CacheInterceptor)
export class AppController {
@Get()
findAll(): string[] {
return [];
}
}
@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.@Controller() @CacheKey((context: ExecutionContext) => { const request = context.switchToHttp().getRequest(); return `user:${request.params.id}`; }) @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
.@Injectable() export class AppService { @CacheKey((args: any[]) => `user:${args[0]}`) @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:
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:
@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:
@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