Bird
0
0

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:
  1. Step 1: Identify global interceptor setup

    Global interceptors are set by providing them in the app module and calling app.useGlobalInterceptors().
  2. 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.
  3. Final Answer:

    By adding CacheInterceptor to the app module's providers and using app.useGlobalInterceptors() -> Option A
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes