You want to cache responses for a controller but exclude caching for a specific method. How can you achieve this with CacheInterceptor?
hard📝 Application Q8 of 15
NestJS - Interceptors
You want to cache responses for a controller but exclude caching for a specific method. How can you achieve this with CacheInterceptor?
AApply CacheInterceptor globally and use @CacheKey() with a unique key on methods to exclude caching.
BApply CacheInterceptor globally and override the method with @UseInterceptors(CacheInterceptor) and set skipCache flag.
CApply CacheInterceptor globally and use @UseInterceptors(ExcludeCacheInterceptor) on the method to skip caching.
DApply CacheInterceptor globally and use @CacheTTL(0) on the method to disable caching for it.
Step-by-Step Solution
Solution:
Step 1: Understand CacheTTL decorator
@CacheTTL(0) disables caching for the decorated method by setting time-to-live to zero.
Step 2: Evaluate other options
Apply CacheInterceptor globally and use @CacheKey() with a unique key on methods to exclude caching misuses @CacheKey. Apply CacheInterceptor globally and use @UseInterceptors(ExcludeCacheInterceptor) on the method to skip caching mentions a non-existent ExcludeCacheInterceptor. Apply CacheInterceptor globally and override the method with @UseInterceptors(CacheInterceptor) and set skipCache flag is invalid syntax.
Final Answer:
Apply CacheInterceptor globally and use @CacheTTL(0) on the method to disable caching for it. -> Option D
Quick Check:
Use @CacheTTL(0) to skip caching on specific methods [OK]
Quick Trick:Use @CacheTTL(0) to disable caching on a method [OK]
Common Mistakes:
Trying to exclude caching with @CacheKey
Assuming a special exclude interceptor exists
Overriding CacheInterceptor incorrectly
Master "Interceptors" in NestJS
9 interactive learning modes - each teaches the same concept differently