Skip to content

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

typescript
@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

ts
new CacheInterceptor(
   cacheManager, 
   reflector, 
   httpAdapterHost?
): CacheInterceptor;

Defined in: packages/cache/src/interceptors/cache.interceptor.ts:115

Parameters

ParameterTypeDescription
cacheManagerCacheThe cache manager instance.
reflectorReflectorThe reflector for reading metadata.
httpAdapterHost?HttpAdapterHostOptional HTTP adapter host; absent in non-HTTP contexts.

Returns

CacheInterceptor

Methods

intercept()

ts
intercept(context, next): Promise<Observable<unknown>>;

Defined in: packages/cache/src/interceptors/cache.interceptor.ts:130

Intercepts the request and handles caching logic.

Parameters

ParameterTypeDescription
contextExecutionContextThe execution context.
nextCallHandlerThe next handler in the chain.

Returns

Promise<Observable<unknown>>

An observable of the response.

Implementation of

NestInterceptor.intercept


isRequestCacheable()

ts
protected isRequestCacheable(context): boolean;

Defined in: packages/cache/src/interceptors/cache.interceptor.ts:272

Checks if the current request method is cacheable.

Parameters

ParameterTypeDescription
contextExecutionContextThe execution context.

Returns

boolean

True if the request method is in allowedMethods.


setHeadersWhenHttp()

ts
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

ParameterTypeDescription
contextExecutionContextThe execution context.
valueunknownThe cached value (null/undefined means miss).

Returns

void


trackBy()

ts
protected trackBy(context): string | undefined;

Defined in: packages/cache/src/interceptors/cache.interceptor.ts:231

Generates a cache key for the current request.

Parameters

ParameterTypeDescription
contextExecutionContextThe execution context.

Returns

string | undefined

The cache key or undefined if not cacheable.

Properties

PropertyModifierTypeDescriptionDefined in
allowedMethodsreadonlyreadonly string[]List of HTTP methods that are allowed to be cached.packages/cache/src/interceptors/cache.interceptor.ts:99
cacheManagerreadonlyCacheThe cache manager instance.packages/cache/src/interceptors/cache.interceptor.ts:116
httpAdapterHost?readonlyHttpAdapterHostOptional HTTP adapter host; absent in non-HTTP contexts.packages/cache/src/interceptors/cache.interceptor.ts:120
reflectorreadonlyReflectorThe reflector for reading metadata.packages/cache/src/interceptors/cache.interceptor.ts:117

Released under the MIT License.