In a NestJS application, why does using caching typically reduce the response latency for repeated requests?
Think about what happens when the server has already done the work once and saves the result.
Caching stores the output of expensive or slow operations so that when the same data is requested again, the server can return the cached result immediately without repeating the work. This reduces the time the server spends processing and thus lowers response latency.
Consider a NestJS controller method that uses caching. What is the behavior when a cached value exists for a request?
Think about why caching is used: to avoid repeating work.
When a cached value exists, the NestJS method returns that value immediately without running the full logic again. This saves time and reduces latency.
Which option correctly applies the built-in cache interceptor to a NestJS controller method to enable caching?
import { Controller, Get, UseInterceptors, CacheInterceptor } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get() @UseInterceptors(/* ??? */) findAll() { // expensive operation return ['item1', 'item2']; } }
Look for the interceptor class that handles caching.
The CacheInterceptor is the built-in NestJS interceptor that enables caching for controller methods. It should be passed to @UseInterceptors.
Given a NestJS endpoint that takes 2 seconds to compute a response, what will be the approximate response time for the second request if caching is enabled?
Think about how caching avoids repeating the expensive operation.
After the first request, the result is cached. The second request returns the cached data immediately, so the response time is very short, close to zero.
Consider this NestJS controller method using caching, but response times remain slow on repeated requests. What is the likely cause?
import { Controller, Get, UseInterceptors, CacheInterceptor } from '@nestjs/common'; @Controller('data') export class DataController { @Get() @UseInterceptors(CacheInterceptor) async fetchData() { const result = await this.expensiveOperation(); return result; } async expensiveOperation() { // Simulate delay await new Promise(res => setTimeout(res, 2000)); return { data: 'value' }; } }
Check if the caching system is properly set up in the app module.
In NestJS, caching requires importing CacheModule globally or in the module. Without this, CacheInterceptor does not cache responses, so repeated requests remain slow.