Bird
0
0

If CacheInterceptor is applied globally in a NestJS app, what will happen when the /products endpoint is called twice consecutively?

medium📝 component behavior Q4 of 15
NestJS - Interceptors
If CacheInterceptor is applied globally in a NestJS app, what will happen when the /products endpoint is called twice consecutively?
ABoth calls execute the handler and return fresh data
BThe first call fetches fresh data; the second returns cached data without executing the handler
CThe second call throws an error due to cache conflicts
DThe cache is cleared after each request, so both calls fetch fresh data
Step-by-Step Solution
Solution:
  1. Step 1: Understand global CacheInterceptor behavior

    When applied globally, CacheInterceptor caches responses after the first request.
  2. Step 2: Analyze repeated calls

    The first call executes the handler and caches the response; the second call returns cached data without re-executing the handler.
  3. Final Answer:

    The first call fetches fresh data; the second returns cached data without executing the handler -> Option B
  4. Quick Check:

    Global CacheInterceptor caches first response, serves cached on second [OK]
Quick Trick: First request caches; second serves cached response [OK]
Common Mistakes:
  • Assuming handler runs on every request despite caching
  • Believing cache causes errors on repeated calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes