A Cache Interceptor in NestJS is a tool that automatically stores the response of a request so that future requests can get the stored response quickly without running the full logic again.
Click to reveal answer
intermediate
How do you enable caching globally in a NestJS application?
You enable caching globally by importing and configuring CacheModule in the root module's imports array, and providing { provide: APP_INTERCEPTOR, useClass: CacheInterceptor } in the providers array.
Click to reveal answer
intermediate
Which decorator is used to disable caching on a specific route in NestJS?
The @CacheKey() decorator sets a custom cache key, but to disable caching on a route, you use the @CacheTTL(0) decorator or avoid applying the CacheInterceptor on that route.
Click to reveal answer
beginner
What is the role of CacheModule in NestJS caching?
CacheModule provides the caching functionality and configuration, like setting the store type (memory, redis), default TTL (time to live), and other options for caching in NestJS.
Click to reveal answer
intermediate
How does CacheInterceptor decide when to return cached data?
CacheInterceptor checks if a cached response exists for the request key. If yes, it returns the cached data immediately. If not, it runs the handler, caches the response, then returns it.
Click to reveal answer
What does CacheInterceptor do in NestJS?
AStores and returns cached responses automatically
BHandles user authentication
CManages database connections
DFormats HTTP responses
✗ Incorrect
CacheInterceptor automatically caches responses to speed up repeated requests.
Which module must be imported to use caching in NestJS?
AHttpModule
BCacheModule
CConfigModule
DAuthModule
✗ Incorrect
CacheModule provides caching features and configuration in NestJS.
How do you apply CacheInterceptor globally in NestJS?
AUse @GlobalCache() decorator
BAdd CacheInterceptor in controller constructor
CSet cache: true in main.ts
DProvide { provide: APP_INTERCEPTOR, useClass: CacheInterceptor } in AppModule providers
✗ Incorrect
You apply CacheInterceptor globally by providing it with the APP_INTERCEPTOR token in the root module's providers array.
What happens if CacheInterceptor finds no cached data for a request?
AReturns an error
BSkips the request
CExecutes the handler and caches the response
DReturns null
✗ Incorrect
If no cached data exists, CacheInterceptor runs the handler, caches the result, then returns it.
Which decorator can set a custom cache key in NestJS?
A@CacheKey()
B@CacheTTL()
C@CacheDisable()
D@CacheControl()
✗ Incorrect
@CacheKey() sets a custom key for caching responses.
Explain how CacheInterceptor works in NestJS and how it improves application performance.
Think about how saving answers helps you avoid repeating work.
You got /4 concepts.
Describe the steps to enable caching globally in a NestJS app using CacheModule and CacheInterceptor.
Remember the setup involves both module import and interceptor application.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the CacheInterceptor in NestJS?
easy
A. To handle authentication and authorization
B. To store responses and speed up repeated requests
C. To log all incoming requests
D. To format response data before sending
Solution
Step 1: Understand the role of CacheInterceptor
The CacheInterceptor caches HTTP responses to avoid recomputing them on repeated requests.
Step 2: Compare with other options
Logging, authentication, and formatting are handled by other interceptors or middleware, not CacheInterceptor.
Final Answer:
To store responses and speed up repeated requests -> Option B
Quick Check:
CacheInterceptor caches responses = C [OK]
Hint: CacheInterceptor caches responses to speed up requests [OK]
Common Mistakes:
Confusing CacheInterceptor with logging interceptors
Thinking it handles security tasks
Assuming it formats data
2. Which of the following is the correct way to apply the CacheInterceptor to a controller method in NestJS?
easy
A. Add CacheInterceptor in the constructor of the controller
B. @Cacheable() decorator on the method
C. @UseInterceptors(CacheInterceptor) above the method
D. Use @CacheInterceptor() as a method parameter
Solution
Step 1: Recall how to apply interceptors in NestJS
Interceptors are applied using the @UseInterceptors() decorator above methods or controllers.
Step 2: Check each option
Only @UseInterceptors(CacheInterceptor) above the method uses the correct decorator syntax. Options B, C, and D are invalid or do not exist in NestJS.
Final Answer:
@UseInterceptors(CacheInterceptor) above the method -> Option C
Quick Check:
UseInterceptors decorator = A [OK]
Hint: Use @UseInterceptors(CacheInterceptor) to apply caching [OK]
Common Mistakes:
Using non-existent @Cacheable decorator
Trying to inject interceptor in constructor
Passing interceptor as method parameter
3. Given this code snippet, what will be the output on the second request to /hello?
A. "Hello count: 1" first request, "Hello count: 2" second request
B. Error because count is private
C. "Hello count: 0" on both requests
D. "Hello count: 1" on first and second request
Solution
Step 1: Understand CacheInterceptor behavior
CacheInterceptor caches the response of the first request and returns it for subsequent requests without calling the method again.
Step 2: Analyze the count variable increment
On the first request, count increments to 1 and returns "Hello count: 1". On the second request, the cached response is returned, so count does not increment again.
Final Answer:
"Hello count: 1" on first and second request -> Option D
Quick Check:
Cache returns cached response = B [OK]
Hint: CacheInterceptor returns cached response, method not re-executed [OK]
Common Mistakes:
Assuming count increments on every request
Thinking private variables cause errors here
Ignoring caching effect on method execution
4. Identify the error in this NestJS CacheInterceptor usage:
import { Controller, Get, UseInterceptors } from '@nestjs/common';
import { CacheInterceptor } from '@nestjs/cache-manager';
@Controller()
export class TestController {
@UseInterceptors(CacheInterceptor())
@Get('test')
test() {
return 'Test response';
}
}
medium
A. CacheInterceptor should not be called as a function
B. Missing CacheModule import in the controller
C. Method test must be async to use CacheInterceptor
D. CacheInterceptor must be applied globally, not per method
Solution
Step 1: Check how CacheInterceptor is applied
CacheInterceptor is a class, not a function, so it should be passed without parentheses to @UseInterceptors().
Step 2: Verify other options
CacheModule import is required but not shown here (not an error in this snippet). Method does not need to be async. CacheInterceptor can be applied per method.
Final Answer:
CacheInterceptor should not be called as a function -> Option A
Quick Check:
Use class name without () = A [OK]
Hint: Pass CacheInterceptor class, not call it as function [OK]
Common Mistakes:
Calling CacheInterceptor with parentheses
Assuming method must be async
Thinking CacheModule import error here
5. You want to customize the cache key for a route in NestJS using CacheInterceptor. Which approach correctly achieves this?
hard
A. Extend CacheInterceptor and override the trackBy method to return a custom key
B. Pass a custom key string directly to @UseInterceptors(CacheInterceptor)
C. Set a custom key in the controller constructor
D. Use @CacheKey('customKey') decorator on the method
Solution
Step 1: Understand how to customize cache keys
CacheInterceptor allows customization by extending it and overriding the trackBy method to define how keys are generated.
Step 2: Evaluate other options
Passing a string to @UseInterceptors() is invalid. Setting keys in constructor or using @CacheKey decorator does not exist in NestJS.
Final Answer:
Extend CacheInterceptor and override the trackBy method to return a custom key -> Option A
Quick Check:
Override trackBy method = D [OK]
Hint: Override trackBy method in extended CacheInterceptor [OK]
Common Mistakes:
Trying to pass key directly to decorator
Using non-existent @CacheKey decorator
Setting keys in constructor instead of method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.