Recall & Review
beginner
What is a custom cache key in NestJS caching?
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?
✗ 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?
✗ 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?
✗ 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?
✗ 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?
✗ 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.