0
0
NestJSframework~20 mins

TTL configuration in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TTL Configuration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does TTL affect cache expiration in NestJS?

Consider a NestJS cache configured with a TTL (time-to-live) of 5 seconds. What happens when you try to retrieve a cached value after 6 seconds?

NestJS
import { CacheModule, Injectable, Inject, CACHE_MANAGER } from '@nestjs/common';

@Injectable()
export class SampleService {
  constructor(@Inject(CACHE_MANAGER) private readonly cacheManager) {}

  async getCachedValue() {
    return await this.cacheManager.get('key');
  }
}

// CacheModule registered with TTL: 5 seconds
AThe cached value is null or undefined because it expired after 5 seconds.
BThe cached value is returned because TTL only affects cache insertion, not retrieval.
CAn error is thrown because the cache key is invalid after TTL expires.
DThe cache automatically refreshes the value after TTL expires, so the value is returned.
Attempts:
2 left
💡 Hint

Think about what TTL means for cached data lifetime.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets a TTL of 10 seconds in NestJS CacheModule?

Choose the correct way to configure the CacheModule with a TTL of 10 seconds.

NestJS
import { CacheModule } from '@nestjs/common';

CacheModule.register({
  // TTL configuration here
});
Attl: 10 * 1000
Bttl: 10
Cttl: 10000
Dttl: '10s'
Attempts:
2 left
💡 Hint

TTL is specified in seconds as a number.

🔧 Debug
advanced
2:00remaining
Why does the cached value never expire despite TTL set?

Given this NestJS cache configuration, the cached values never expire. What is the likely cause?

NestJS
CacheModule.register({
  ttl: 5,
  max: 100
});

// Usage:
await cacheManager.set('key', 'value');
// After 10 seconds
const val = await cacheManager.get('key'); // val is still 'value'
AThe max option disables TTL expiration when set.
BThe TTL value must be set in milliseconds, not seconds.
CThe cache store used does not support TTL expiration.
DThe cacheManager.set method overrides TTL to infinite by default.
Attempts:
2 left
💡 Hint

Consider the cache store implementation and its TTL support.

state_output
advanced
2:00remaining
What is the output after setting different TTLs for cache keys?

Consider this code snippet using NestJS cache manager:

NestJS
await cacheManager.set('short', 'A', { ttl: 2 });
await cacheManager.set('long', 'B', { ttl: 5 });

// After 3 seconds
const shortVal = await cacheManager.get('short');
const longVal = await cacheManager.get('long');

console.log(shortVal, longVal);
Aundefined 'B'
B'A' undefined
Cundefined undefined
D'A' 'B'
Attempts:
2 left
💡 Hint

Think about which keys expire after 3 seconds.

🧠 Conceptual
expert
3:00remaining
How does TTL configuration interact with distributed cache in NestJS?

In a NestJS app using Redis as a distributed cache, what is the effect of setting TTL in CacheModule configuration?

ATTL causes Redis to replicate keys indefinitely without expiration.
BTTL is ignored because Redis does not support key expiration.
CTTL only applies locally in NestJS memory cache, not in Redis.
DTTL is enforced by Redis, so cached keys expire automatically after the set time.
Attempts:
2 left
💡 Hint

Think about Redis capabilities and how TTL works in distributed caches.