CacheInterceptor
Automatically caches GET responses using the configured cache store.
Syntax
@UseInterceptors(CacheInterceptor) | CacheModule.register(options) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | CacheModuleOptions | No | Store, ttl, and max settings for the cache. |
Returns
NestInterceptor — An interceptor that reads and writes the cache.
Examples
import { Module } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
@Module({
imports: [CacheModule.register({ ttl: 30_000, isGlobal: true })],
})
export class AppModule {}
import {
Controller, Get, UseInterceptors, CacheTTL,
} from '@nestjs/common';
import { CacheInterceptor } from '@nestjs/cache-manager';
@Controller('cats')
@UseInterceptors(CacheInterceptor)
export class CatsController {
@Get()
@CacheTTL(10_000)
findAll() {
return ['kitty'];
}
}
Notes
Install @nestjs/cache-manager and cache-manager. Only GET routes are
cached by default. Use @CacheKey() and @CacheTTL() to override the key
and lifetime per handler.