CacheInterceptor is a NestJS built-in interceptor that automatically caches HTTP responses for @Get() endpoints. It is part of the CacheModule system and works with any store backend (in-memory, Redis, etc.).

Usage

Global registration in providers:

{ provide: APP_INTERCEPTOR, useClass: CacheInterceptor }

Per-controller:

@UseInterceptors(CacheInterceptor)
@Controller('products')
export class ProductsController {}

Customization

  • @CacheKey('custom-key') — override the auto-generated cache key (default: route path)
  • @CacheTTL(10) — override the global TTL (in seconds) for a specific endpoint

Gotchas

  • Only @Get() routes are auto-cached. @Post(), @Put(), @Delete() are not intercepted.
  • The cache key is the route path by default — different query parameters on the same route may return stale data unless a custom key is set.
  • Requires cache-manager-redis-store as the Redis backend adapter.