A custom cache key is a user-defined string or value used to uniquely identify cached data instead of the default key. It helps control how data is stored and retrieved from the cache.
Click to reveal answer
beginner
Why would you use a custom cache key in NestJS?
You use a custom cache key to avoid collisions, organize cached data better, or include dynamic parts like user IDs or query parameters to cache different results separately.
Click to reveal answer
intermediate
How do you define a custom cache key in a NestJS @Cacheable decorator?
You provide a function to the 'key' property in the @Cacheable decorator options. This function returns a string that will be used as the cache key based on method arguments or other logic.
Click to reveal answer
intermediate
Example: What does this custom cache key function do?
key: (args) => `user_${args[0]}_data`
It creates a cache key by taking the first argument (usually a user ID) and making a string like 'user_123_data'. This caches data specific to each user separately.
Click to reveal answer
beginner
What happens if you don't use a custom cache key in NestJS caching?
NestJS uses a default key based on the method name and arguments. This can cause cache collisions or less control over cache entries, especially when arguments are complex or dynamic.
Click to reveal answer
What is the main purpose of a custom cache key in NestJS?
ATo disable caching
BTo uniquely identify cached data
CTo increase server memory
DTo log cache hits
✗ Incorrect
Custom cache keys uniquely identify cached data to avoid collisions and organize cache entries.
How do you specify a custom cache key in NestJS caching decorators?
ABy renaming the cache folder
BBy setting a global environment variable
CBy passing a function to the 'key' option
DBy using a special cache service method
✗ Incorrect
You specify a custom cache key by providing a function to the 'key' option in the caching decorator.
Which of these is a good example of a custom cache key for user data?
Aprofile_cache
Bcache_default
Cdata123
Duser_${userId}_profile
✗ Incorrect
A key like `user_${userId}_profile` uniquely identifies cache entries per user.
What could happen if you don't use custom cache keys when caching dynamic data?
ACache collisions and incorrect data retrieval
BFaster cache performance
CAutomatic cache clearing
DNo caching at all
✗ Incorrect
Without custom keys, different data might overwrite each other causing wrong data to be served.
In NestJS, the custom cache key function receives what as input?
AThe method arguments
BThe server configuration
CThe cache store instance
DThe HTTP request headers
✗ Incorrect
The custom cache key function receives the method arguments to build a unique key.
Explain how to create and use a custom cache key in NestJS caching.
Think about how to make cache keys unique per input.
You got /4 concepts.
Why is it important to use custom cache keys when caching data that depends on user input?
Consider what happens if all users share the same cache key.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @CacheKey() in a NestJS service?
easy
A. To set the time duration for cache expiration
B. To clear all cached data automatically
C. To define a custom name for the cached data key
D. To disable caching for a method
Solution
Step 1: Understand the role of @CacheKey()
This decorator is used to specify a custom key name for the cached data instead of using a default or generated key.
Step 2: Differentiate from other cache decorators
@CacheTTL() sets cache duration, clearing cache is manual or by other methods, and disabling cache is not done by @CacheKey().
Final Answer:
To define a custom name for the cached data key -> Option C
Quick Check:
Custom cache key = @CacheKey() [OK]
Hint: Remember: CacheKey sets the cache name, TTL sets time [OK]
Common Mistakes:
Confusing @CacheKey() with @CacheTTL()
Thinking @CacheKey() clears cache
Assuming @CacheKey() disables caching
2. Which of the following is the correct way to apply a custom cache key to a method in NestJS?
easy
A. @CacheKey = 'myKey'\nasync getData() { return data; }
B. @CacheTTL(300)\nasync getData() { return data; }
C. cacheKey('myKey')\nasync getData() { return data; }
D. @CacheKey('myKey')\nasync getData() { return data; }
Solution
Step 1: Identify correct decorator syntax
In NestJS, decorators are applied with @DecoratorName(parameters) syntax above the method.
A. Because TTL value should be a number, not a string
B. Because @CacheKey must come after @CacheTTL
C. Because async methods cannot use caching decorators
D. Because the cache key 'dataKey' is invalid
Solution
Step 1: Check the @CacheTTL parameter type
The TTL decorator expects a number (seconds), but '300' is a string, which causes it to fail.
Step 2: Verify other decorator rules
Decorator order does not affect TTL, async methods support caching, and 'dataKey' is a valid string key.
Final Answer:
Because TTL value should be a number, not a string -> Option A
Quick Check:
TTL must be number, not string [OK]
Hint: TTL needs number type, not quoted string [OK]
Common Mistakes:
Passing TTL as string instead of number
Thinking decorator order matters here
Assuming async disables caching
5. You want to cache user data with a dynamic key based on user ID. Which approach correctly creates a custom cache key in NestJS for method getUserById(id: string)?
hard
A. Use @CacheKey(() => `user_${id}`) above the method
B. Manually generate the key inside the method and use @CacheKey() without parameters
C. Use @CacheKey('user_' + id) directly above the method
D. Use @CacheKey() and pass the key as a method argument
Solution
Step 1: Understand dynamic cache keys in NestJS
To create a dynamic cache key, you can pass a function to @CacheKey() that returns the key string using method parameters.
Step 2: Evaluate each option
Use @CacheKey(() => `user_${id}`) above the method correctly uses a function returning a template string with id. Manually generate the key inside the method and use @CacheKey() without parameters is invalid because @CacheKey() needs the key at decoration time. Use @CacheKey('user_' + id) directly above the method tries to concatenate directly but id is not available at decoration time. Use @CacheKey() and pass the key as a method argument is invalid as @CacheKey() does not accept method arguments.
Final Answer:
Use @CacheKey(() => `user_${id}`) above the method -> Option A
Quick Check:
Dynamic key = function in @CacheKey() [OK]
Hint: Use function in @CacheKey() for dynamic keys [OK]
Common Mistakes:
Trying to use method parameters directly in decorator string
Assuming @CacheKey() accepts method arguments
Generating key inside method but not linking to cache
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.