0
0
NestJSframework~10 mins

Cache interceptor in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cache interceptor
Request received
Check cache for response
Return cached
Send cached
Send response
The cache interceptor checks if a response is cached for a request. If yes, it returns cached data. If no, it calls the handler, caches the result, then returns it.
Execution Sample
NestJS
import { CacheInterceptor, Controller, Get, UseInterceptors } from '@nestjs/common';

@Controller('items')
@UseInterceptors(CacheInterceptor)
export class ItemsController {
  @Get()
  findAll() { return ['item1', 'item2']; }
}
This code uses CacheInterceptor to cache the response of the findAll method in ItemsController.
Execution Table
StepActionCache Check ResultHandler CalledCache UpdatedResponse Sent
1Request to GET /itemsNo cache foundYesCache stores ['item1', 'item2']['item1', 'item2'] sent
2Request to GET /items againCache hitNoNo changeCached ['item1', 'item2'] sent
3Request to GET /items after cache expiryNo cache foundYesCache stores ['item1', 'item2']['item1', 'item2'] sent
💡 Execution stops after sending response to client.
Variable Tracker
VariableStartAfter 1After 2After 3
cacheempty['item1', 'item2']['item1', 'item2']['item1', 'item2'] (refreshed)
handlerCalledfalsetruefalsetrue
responseSentnone['item1', 'item2']['item1', 'item2']['item1', 'item2']
Key Moments - 2 Insights
Why does the handler not run on the second request?
Because the cache check found a stored response (see execution_table step 2), so the interceptor returns cached data without calling the handler.
What happens when the cache expires?
The cache is empty again (step 3), so the handler runs to get fresh data, which is then stored in cache for future requests.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'handlerCalled' at step 2?
Atrue
Bundefined
Cfalse
Dnull
💡 Hint
Check the 'handlerCalled' row in variable_tracker at 'After 2' column.
At which step does the cache get updated with new data?
AStep 1
BStep 2
CStep 3
DNo step updates cache
💡 Hint
Look at the 'Cache Updated' column in execution_table.
If the cache never expires, which step would the handler never be called?
AStep 1
BStep 3
CStep 2
DHandler always called
💡 Hint
Refer to 'handlerCalled' in variable_tracker and cache expiry explanation in key_moments.
Concept Snapshot
Cache Interceptor in NestJS:
- Checks cache before calling handler
- Returns cached response if available
- Calls handler and caches result if not
- Improves performance by avoiding repeated work
- Use @UseInterceptors(CacheInterceptor) on controllers or routes
Full Transcript
The Cache Interceptor in NestJS works by intercepting incoming requests. When a request arrives, it first checks if a cached response exists. If yes, it returns that cached response immediately without calling the handler method. If no cached response is found, it calls the handler to get fresh data, stores this data in the cache, and then sends it back to the client. This process repeats for each request. When the cache expires, the interceptor calls the handler again to refresh the cache. This mechanism helps improve performance by reducing repeated processing for the same requests.