CacheModule helps your app remember data temporarily to make it faster. It avoids doing the same work again and again.
CacheModule setup in NestJS
import { CacheModule } from '@nestjs/common'; @Module({ imports: [ CacheModule.register({ ttl: 60, // seconds to keep cache max: 100, // max items in cache }), ], }) export class AppModule {}
ttl means how long the cache stays before it clears automatically.
You can customize cache settings globally by passing options to register().
CacheModule.register({ ttl: 5 })CacheModule.register({ ttl: 10, max: 100 })CacheModule.register()
This example sets up caching for the /count route. The first time you call it, it increases the counter. For 10 seconds, repeated calls return the cached count without increasing it.
import { Module, Controller, Get, CacheInterceptor, UseInterceptors } from '@nestjs/common'; import { CacheModule } from '@nestjs/common'; @Controller() @UseInterceptors(CacheInterceptor) export class AppController { private counter = 0; @Get('count') getCount() { this.counter++; return { count: this.counter }; } } @Module({ imports: [CacheModule.register({ ttl: 10 })], controllers: [AppController], }) export class AppModule {}
CacheModule uses in-memory cache by default, which is good for simple apps or development.
For production, consider using external stores like Redis by using CacheModule.registerAsync() with custom store.
Remember to use @UseInterceptors(CacheInterceptor) on controllers or routes to enable caching behavior.
CacheModule helps speed up your app by storing data temporarily.
You set it up by importing and registering it with options like ttl.
Use cache interceptors to apply caching to routes easily.
Practice
CacheModule in a NestJS application?Solution
Step 1: Understand CacheModule's role
CacheModule is used to store data temporarily to reduce repeated processing.Step 2: Compare with other options
It does not handle database, authentication, or logging directly.Final Answer:
To enable temporary storage of data to speed up responses -> Option AQuick Check:
CacheModule = temporary data storage [OK]
- Confusing CacheModule with database modules
- Thinking CacheModule manages authentication
- Assuming CacheModule handles logging
Solution
Step 1: Recall CacheModule registration method
CacheModule uses the static methodregisterto configure options like ttl.Step 2: Verify method names
Methods likeforRoot,setup, orinitare not used by CacheModule.Final Answer:
CacheModule.register({ ttl: 5 }) -> Option AQuick Check:
Use register() to configure CacheModule [OK]
- Using forRoot() instead of register()
- Using setup() or init() which do not exist
- Forgetting to pass options inside register()
import { CacheModule, Module } from '@nestjs/common';
@Module({
imports: [CacheModule.register({ ttl: 10 })],
})
export class AppModule {}
What will be the effect of the ttl: 10 option?Solution
Step 1: Understand ttl meaning in CacheModule
The ttl option sets the time-to-live in seconds for cached data.Step 2: Interpret ttl: 10
Since ttl is 10, cached data expires after 10 seconds.Final Answer:
Cache entries expire after 10 seconds -> Option BQuick Check:
ttl = seconds cache lives [OK]
- Assuming ttl is in minutes
- Thinking ttl disables expiration
- Confusing ttl with cache size
import { CacheModule, Module } from '@nestjs/common';
@Module({
imports: [CacheModule.register({ ttl: '20' })],
})
export class AppModule {}Solution
Step 1: Check ttl option type
ttl expects a number representing seconds, not a string.Step 2: Verify other parts
CacheModule import and Module decorator usage are correct; register() accepts options.Final Answer:
ttl should be a number, not a string -> Option CQuick Check:
ttl must be numeric [OK]
- Passing ttl as a string instead of number
- Thinking register() has no parameters
- Misusing import paths
Solution
Step 1: Understand CacheModule and CacheInterceptor usage
CacheModule provides caching, and CacheInterceptor applies caching behavior to routes.Step 2: Apply CacheInterceptor globally on controller
Using@UseInterceptors(CacheInterceptor)on the controller enables caching for all its routes.Step 3: Evaluate other options
No@Cacheable()or@Cache()decorators exist in NestJS core; manual caching is possible but not automatic.Final Answer:
Import CacheModule with ttl 15 and add @UseInterceptors(CacheInterceptor) on the controller -> Option DQuick Check:
Use CacheInterceptor with CacheModule for route caching [OK]
- Using non-existent decorators like @Cacheable or @Cache
- Not applying CacheInterceptor to controller or routes
- Trying manual caching instead of interceptor
Ready to prove it?
Pass the test and this pattern moves unknown
