How do you enable caching globally in a NestJS application using CacheInterceptor?
easy📝 Conceptual Q2 of 15
NestJS - Interceptors
How do you enable caching globally in a NestJS application using CacheInterceptor?
ABy adding CacheInterceptor to the app module's providers and using app.useGlobalInterceptors()
BBy importing CacheInterceptor in every controller manually
CBy setting cache: true in the main.ts bootstrap function
DBy decorating each method with @Cacheable() decorator
Step-by-Step Solution
Solution:
Step 1: Identify global interceptor setup
Global interceptors are set by providing them in the app module and calling app.useGlobalInterceptors().
Step 2: Eliminate incorrect options
By importing CacheInterceptor in every controller manually requires manual imports per controller, not global. By setting cache: true in the main.ts bootstrap function is invalid syntax. By decorating each method with @Cacheable() decorator uses a non-existent decorator.
Final Answer:
By adding CacheInterceptor to the app module's providers and using app.useGlobalInterceptors() -> Option A
Quick Check:
Global caching setup = useGlobalInterceptors(CacheInterceptor) [OK]
Quick Trick:Use app.useGlobalInterceptors(new CacheInterceptor()) to enable global caching [OK]
Common Mistakes:
Trying to enable caching by importing CacheInterceptor only
Assuming a @Cacheable decorator exists
Setting cache: true in bootstrap without interceptor
Master "Interceptors" in NestJS
9 interactive learning modes - each teaches the same concept differently