Bird
Raised Fist0
NestJSframework~10 mins

TTL configuration in NestJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a TTL of 60 seconds in the cache manager options.

NestJS
CacheModule.register({ ttl: [1] })
Drag options to blanks, or click blank then click option'
A-1
B60
C0
D6000
Attempts:
3 left
💡 Hint
Common Mistakes
Using milliseconds instead of seconds.
Setting TTL to 0 which disables expiration.
2fill in blank
medium

Complete the code to inject the cache manager service in a NestJS service constructor.

NestJS
constructor(@Inject(CACHE_MANAGER) private [1]: Cache) {}
Drag options to blanks, or click blank then click option'
AcacheClient
BcacheService
Ccache
DcacheManager
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not match the injected token.
Forgetting to mark the variable as private.
3fill in blank
hard

Fix the error in setting a TTL for a specific cache key using the cache manager's set method.

NestJS
await this.cacheManager.set('user_123', userData, [1]);
Drag options to blanks, or click blank then click option'
A{ ttl: 60 }
B60000
C60
D{ ttl: 60000 }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing TTL as a raw number instead of an options object.
Confusing seconds and milliseconds units.
4fill in blank
hard

Fill both blanks to configure the cache module with a TTL of 120 seconds and a max cache size of 100 items.

NestJS
CacheModule.register({ ttl: [1], max: [2] })
Drag options to blanks, or click blank then click option'
A120
B60
C100
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up TTL and max values.
Using TTL in milliseconds instead of seconds.
5fill in blank
hard

Fill all three blanks to create a cache key with a prefix, set a TTL of 30 seconds, and store the value using the cache manager.

NestJS
const key = `session:[1]`;
await this.cacheManager.set(key, [2], { ttl: [3] });
Drag options to blanks, or click blank then click option'
AuserId
BsessionData
C30
DuserToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable for the key or value.
Setting TTL as a string instead of a number.

Practice

(1/5)
1. What does TTL stand for in NestJS caching configuration?
easy
A. Timed Token Lock, a security feature for cache access
B. Time To Live, the duration cached data stays before removal
C. Temporary Transfer Lock, a pause on cache updates
D. Total Transaction Limit, the max number of cache writes

Solution

  1. Step 1: Understand TTL meaning in caching

    TTL means how long cached data remains valid before it is removed automatically.
  2. Step 2: Match TTL to the correct full form

    TTL stands for Time To Live, which fits the caching context perfectly.
  3. Final Answer:

    Time To Live, the duration cached data stays before removal -> Option B
  4. Quick 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
A. CacheModule.register({ ttl: 10 })
B. CacheModule.register({ timeToLive: 10 })
C. CacheModule.register({ ttl: 10000 })
D. CacheModule.register({ expire: 10 })

Solution

  1. Step 1: Identify TTL unit in NestJS cache config

    TTL is set in seconds, so 10 means 10 seconds.
  2. 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.
  3. Final Answer:

    CacheModule.register({ ttl: 10 }) -> Option A
  4. Quick 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:
CacheModule.register({ ttl: 5 })

What happens to cached data after 6 seconds?
medium
A. Cached data throws an error after 6 seconds
B. Cached data remains indefinitely until manually cleared
C. Cached data is refreshed automatically every 6 seconds
D. Cached data is automatically removed after 5 seconds

Solution

  1. Step 1: Understand TTL effect on cached data

    TTL of 5 means cached data expires and is removed after 5 seconds.
  2. Step 2: Check what happens after 6 seconds

    Since 6 seconds is more than 5, cached data is no longer valid and is removed.
  3. Final Answer:

    Cached data is automatically removed after 5 seconds -> Option D
  4. Quick 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
A. TTL property name is incorrect, should be timeToLive
B. TTL cannot be set globally, only per item
C. TTL value should be a number, not a string
D. TTL value must be in milliseconds, not seconds

Solution

  1. Step 1: Check TTL value type

    TTL expects a number representing seconds, but '30' is a string.
  2. Step 2: Confirm correct property and units

    The property ttl is correct and uses seconds, so only the value type is wrong.
  3. Final Answer:

    TTL value should be a number, not a string -> Option C
  4. Quick 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
A. Set global TTL to 60, then use @CacheTTL(120) decorator on admin methods
B. Set global TTL to 120, then manually clear cache for non-admin users
C. Set TTL only in controller constructor based on user role
D. Use two separate CacheModules with different TTLs and switch dynamically

Solution

  1. Step 1: Set global TTL for general users

    Configure CacheModule with ttl: 60 to cover normal users.
  2. Step 2: Override TTL for admin users

    Use the @CacheTTL(120) decorator on admin-specific methods to extend TTL.
  3. Final Answer:

    Set global TTL to 60, then use @CacheTTL(120) decorator on admin methods -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard