Bird
0
0

You want to customize the cache key for a route in NestJS using CacheInterceptor. Which approach correctly achieves this?

hard📝 Application Q15 of 15
NestJS - Interceptors
You want to customize the cache key for a route in NestJS using CacheInterceptor. Which approach correctly achieves this?
AExtend CacheInterceptor and override the <code>trackBy</code> method to return a custom key
BPass a custom key string directly to <code>@UseInterceptors(CacheInterceptor)</code>
CSet a custom key in the controller constructor
DUse <code>@CacheKey('customKey')</code> decorator on the method
Step-by-Step Solution
Solution:
  1. 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.
  2. 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.
  3. Final Answer:

    Extend CacheInterceptor and override the trackBy method to return a custom key -> Option A
  4. Quick Check:

    Override trackBy method = D [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes