0
0
NestJSframework~20 mins

Cache interceptor in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Interceptor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does NestJS CacheInterceptor affect response caching?

Consider a NestJS controller method decorated with @UseInterceptors(CacheInterceptor). What is the behavior of this interceptor when the method is called multiple times with the same parameters?

NestJS
import { Controller, Get, UseInterceptors } from '@nestjs/common';
import { CacheInterceptor } from '@nestjs/cache-manager';

@Controller('items')
@UseInterceptors(CacheInterceptor)
export class ItemsController {
  @Get()
  findAll() {
    console.log('Fetching items from DB');
    return ['item1', 'item2'];
  }
}
AThe interceptor causes an error because caching requires manual cache setup in the controller.
BThe first call logs 'Fetching items from DB' and returns the array; subsequent calls return cached data without logging.
CThe interceptor caches the response but still logs 'Fetching items from DB' on every call.
DEvery call logs 'Fetching items from DB' and returns fresh data; caching is disabled by default.
Attempts:
2 left
💡 Hint

Think about how caching avoids repeated work and what the console output tells you.

📝 Syntax
intermediate
2:00remaining
Which code correctly applies CacheInterceptor globally in NestJS?

You want to enable caching for all controllers in your NestJS app using CacheInterceptor. Which code snippet correctly sets this up in main.ts?

Aapp.useGlobalInterceptors(new CacheInterceptor());
Bapp.useGlobalInterceptors(new CacheInterceptor(app));
Capp.useGlobalInterceptors(CacheInterceptor());
Dapp.useGlobalInterceptors(CacheInterceptor);
Attempts:
2 left
💡 Hint

Remember how to instantiate classes and pass them to global interceptors.

🔧 Debug
advanced
2:00remaining
Why does CacheInterceptor not cache responses for POST requests?

Given this controller method:

  @Post('create')
  @UseInterceptors(CacheInterceptor)
  createItem() {
    return { success: true };
  }

Why does the CacheInterceptor not cache the response for this POST request?

ACacheInterceptor caches all HTTP methods equally; the issue is elsewhere.
BPOST requests require manual cache key setup; otherwise, caching is disabled.
CCacheInterceptor only caches GET requests by default; POST requests are excluded.
DThe interceptor throws an error on POST requests and skips caching.
Attempts:
2 left
💡 Hint

Think about HTTP methods and typical caching rules.

🧠 Conceptual
advanced
2:00remaining
What happens if CacheInterceptor is used without a cache manager provider?

You apply CacheInterceptor in a NestJS app but forget to import CacheModule. What will happen when a cached route is called?

AThe CacheInterceptor caches responses but clears cache immediately after.
BThe CacheInterceptor silently skips caching and returns fresh data every time.
CThe app logs a warning but caches responses in memory by default.
DThe app throws a runtime error because CacheInterceptor depends on CacheModule provider.
Attempts:
2 left
💡 Hint

Consider dependency injection and required modules in NestJS.

state_output
expert
2:00remaining
What is the cache key generated by CacheInterceptor for a GET request with query params?

Given this controller method:

  @Get('search')
  @UseInterceptors(CacheInterceptor)
  search(@Query('term') term: string) {
    return { results: [term] };
  }

When a client calls /search?term=book, what cache key does CacheInterceptor use internally?

AThe full URL path including query string, e.g., '/search?term=book'
BOnly the route path without query, e.g., '/search'
CA hash of the response body content
DThe HTTP method plus route path, e.g., 'GET:/search'
Attempts:
2 left
💡 Hint

Think about how caching differentiates requests with different query parameters.