TTL configuration helps you set how long data should live before it expires automatically. This keeps your app's data fresh and avoids clutter.
TTL configuration in NestJS
Introduction
Syntax
NestJS
CacheModule.register({
ttl: number_in_seconds
})ttl means 'time to live' in seconds.
After this time, cached data is removed automatically.
Examples
NestJS
CacheModule.register({ ttl: 60 })NestJS
CacheModule.register({ ttl: 0 })NestJS
CacheModule.register({ ttl: 300 })Sample Program
This example sets up a cache with a default TTL of 10 seconds. The setCache route stores a value with a 5-second TTL. The getCache route retrieves it. After 5 seconds, the cached value expires and returns a message.
NestJS
import { CacheModule, Module, Controller, Get, Inject, CACHE_MANAGER } from '@nestjs/common'; import { Cache } from 'cache-manager'; @Controller() export class AppController { constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {} @Get('set') async setCache() { await this.cacheManager.set('key', 'Hello TTL', { ttl: 5 }); return 'Value cached with 5 seconds TTL'; } @Get('get') async getCache() { const value = await this.cacheManager.get('key'); return value ?? 'Cache expired or not set'; } } @Module({ imports: [CacheModule.register({ ttl: 10 })], controllers: [AppController], }) export class AppModule {}
Important Notes
You can set TTL globally in CacheModule.register() or per item when setting cache.
TTL is in seconds, so 60 means one minute.
Setting TTL to 0 means the cache never expires automatically.
Summary
TTL controls how long cached data stays before automatic removal.
Use TTL to keep data fresh and save memory.
You can set TTL globally or per cached item.
Practice
1. What does TTL stand for in NestJS caching configuration?
easy
Solution
Step 1: Understand TTL meaning in caching
TTL means how long cached data remains valid before it is removed automatically.Step 2: Match TTL to the correct full form
TTL stands for Time To Live, which fits the caching context perfectly.Final Answer:
Time To Live, the duration cached data stays before removal -> Option BQuick Check:
TTL = Time To Live [OK]
Hint: TTL means how long cache data lives before removal [OK]
Common Mistakes:
- Confusing TTL with security or transaction terms
- Thinking TTL is a cache size limit
- Assuming TTL is a manual cache clear command
2. Which is the correct way to set a global TTL of 10 seconds in NestJS cache module?
easy
Solution
Step 1: Identify TTL unit in NestJS cache config
TTL is set in seconds, so 10 means 10 seconds.Step 2: Check correct property name and value type
The property is ttl, and the value is a number in seconds, so ttl: 10 is correct.Final Answer:
CacheModule.register({ ttl: 10 }) -> Option AQuick Check:
TTL in seconds uses 'ttl' property [OK]
Hint: TTL value is seconds, use 'ttl' property exactly [OK]
Common Mistakes:
- Using milliseconds instead of seconds
- Using wrong property names like timeToLive or expire
- Passing ttl as string instead of number
3. Given this NestJS cache config snippet:
What happens to cached data after 6 seconds?
CacheModule.register({ ttl: 5 })What happens to cached data after 6 seconds?
medium
Solution
Step 1: Understand TTL effect on cached data
TTL of 5 means cached data expires and is removed after 5 seconds.Step 2: Check what happens after 6 seconds
Since 6 seconds is more than 5, cached data is no longer valid and is removed.Final Answer:
Cached data is automatically removed after 5 seconds -> Option DQuick Check:
TTL 5 seconds means removal after 5 seconds [OK]
Hint: Data expires after TTL seconds, so 6 > 5 means removal [OK]
Common Mistakes:
- Thinking data stays forever without manual clear
- Assuming TTL refreshes cache automatically
- Believing cache throws errors on expiry
4. Identify the error in this NestJS cache TTL setup:
CacheModule.register({ ttl: '30' })medium
Solution
Step 1: Check TTL value type
TTL expects a number representing seconds, but '30' is a string.Step 2: Confirm correct property and units
The property ttl is correct and uses seconds, so only the value type is wrong.Final Answer:
TTL value should be a number, not a string -> Option CQuick Check:
TTL must be numeric seconds, not string [OK]
Hint: TTL must be a number, not quoted string [OK]
Common Mistakes:
- Using string instead of number for TTL
- Changing property name incorrectly
- Confusing seconds with milliseconds
5. You want to cache user data with a TTL of 60 seconds, but override it to 120 seconds for admin users only. How do you configure TTL in NestJS?
hard
Solution
Step 1: Set global TTL for general users
Configure CacheModule with ttl: 60 to cover normal users.Step 2: Override TTL for admin users
Use the @CacheTTL(120) decorator on admin-specific methods to extend TTL.Final Answer:
Set global TTL to 60, then use @CacheTTL(120) decorator on admin methods -> Option AQuick Check:
Global TTL + @CacheTTL override [OK]
Hint: Use global TTL plus @CacheTTL decorator for overrides [OK]
Common Mistakes:
- Trying to set TTL only in constructor without decorators
- Using multiple CacheModules unnecessarily
- Setting global TTL too high and clearing cache manually
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
