Class: CacheInterceptor
Defined in: packages/cache/src/interceptors/cache.interceptor.ts:95
Interceptor that handles HTTP caching using cache-manager.
This interceptor checks the cache before executing a request handler. If a cached value exists, it returns that value immediately. Otherwise, it executes the handler and stores the result in cache.
Features:
- Automatic cache key generation from HTTP request URL
- Support for custom cache keys via
Cache Key
decorator
- Configurable TTL via
Cache TTL
decorator
- Cache hit/miss headers (X-Cache)
- Only caches GET requests by default
Example
@Controller('users')
@UseInterceptors(CacheInterceptor)
export class UserController {
@Get(':id')
@CacheTTL(60) // Cache for 60 seconds
async findOne(@Param('id') id: string) {
return this.userService.findOne(id);
}
}Implements
Constructors
Constructor
new CacheInterceptor(
cacheManager,
reflector,
httpAdapterHost?
): CacheInterceptor;Defined in: packages/cache/src/interceptors/cache.interceptor.ts:115
Parameters
| Parameter | Type | Description |
|---|---|---|
cacheManager | Cache | The cache manager instance. |
reflector | Reflector | The reflector for reading metadata. |
httpAdapterHost? | HttpAdapterHost | Optional HTTP adapter host; absent in non-HTTP contexts. |
Returns
CacheInterceptor
Methods
intercept()
intercept(context, next): Promise<Observable<unknown>>;Defined in: packages/cache/src/interceptors/cache.interceptor.ts:130
Intercepts the request and handles caching logic.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | ExecutionContext | The execution context. |
next | CallHandler | The next handler in the chain. |
Returns
Promise<Observable<unknown>>
An observable of the response.
Implementation of
isRequestCacheable()
protected isRequestCacheable(context): boolean;Defined in: packages/cache/src/interceptors/cache.interceptor.ts:272
Checks if the current request method is cacheable.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | ExecutionContext | The execution context. |
Returns
boolean
True if the request method is in allowedMethods.
setHeadersWhenHttp()
protected setHeadersWhenHttp(context, value): void;Defined in: packages/cache/src/interceptors/cache.interceptor.ts:289
Sets cache headers (X-Cache: HIT/MISS) on HTTP response.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | ExecutionContext | The execution context. |
value | unknown | The cached value (null/undefined means miss). |
Returns
void
trackBy()
protected trackBy(context): string | undefined;Defined in: packages/cache/src/interceptors/cache.interceptor.ts:231
Generates a cache key for the current request.
Parameters
| Parameter | Type | Description |
|---|---|---|
context | ExecutionContext | The execution context. |
Returns
string | undefined
The cache key or undefined if not cacheable.
Properties
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
allowedMethods | readonly | readonly string[] | List of HTTP methods that are allowed to be cached. | packages/cache/src/interceptors/cache.interceptor.ts:99 |
cacheManager | readonly | Cache | The cache manager instance. | packages/cache/src/interceptors/cache.interceptor.ts:116 |
httpAdapterHost? | readonly | HttpAdapterHost | Optional HTTP adapter host; absent in non-HTTP contexts. | packages/cache/src/interceptors/cache.interceptor.ts:120 |
reflector | readonly | Reflector | The reflector for reading metadata. | packages/cache/src/interceptors/cache.interceptor.ts:117 |