What is the purpose of the @Cacheable() decorator in NestJS?
The @Cacheable() decorator marks a method so that its result is stored in cache. When the method is called again with the same arguments, the cached result is returned instead of running the method again, improving performance.
Click to reveal answer
intermediate
How does the @CacheEvict() decorator work in NestJS?
The @CacheEvict() decorator is used to remove specific cache entries when a method is called. This helps keep the cache updated by deleting stale data after changes like updates or deletes.
Click to reveal answer
intermediate
Explain the role of the @CachePut() decorator in NestJS.
The @CachePut() decorator updates the cache with the method's result every time the method is called, regardless of whether the cache already has a value. It is useful for refreshing cache data.
Click to reveal answer
beginner
What is the default cache store used by NestJS cache decorators?
By default, NestJS uses an in-memory cache store for caching. This means cached data is stored in the server's memory and is lost when the server restarts.
Click to reveal answer
intermediate
Why is it important to use cache decorators with proper cache keys?
Proper cache keys ensure that cached data is correctly identified and retrieved. Without unique keys, different method calls might overwrite each other's cache or return wrong data.
Click to reveal answer
Which NestJS cache decorator stores the method result only if it is not already cached?
A@Cacheable()
B@CacheEvict()
C@CachePut()
D@CacheClear()
✗ Incorrect
@Cacheable() caches the result only if it is not already cached. @CachePut() always updates the cache.
What does the @CacheEvict() decorator do?
AUpdates cache without checking
BStores method result in cache
CRemoves cache entries
DDisables caching for a method
✗ Incorrect
@CacheEvict() removes cache entries to keep cache data fresh.
If you want to refresh cache data every time a method runs, which decorator should you use?
A@CachePut()
B@CacheEvict()
C@Cacheable()
D@CacheDisable()
✗ Incorrect
@CachePut() updates the cache every time the method is called.
What happens to the default in-memory cache when the NestJS server restarts?
ACache persists on disk
BCache is cleared
CCache is backed up automatically
DCache is sent to clients
✗ Incorrect
In-memory cache is lost when the server restarts.
Why should cache keys be unique for different method calls?
ATo speed up method execution
BTo encrypt cached data
CTo disable caching
DTo prevent cache overwriting
✗ Incorrect
Unique keys prevent different cached results from overwriting each other.
Describe how you would use cache decorators in NestJS to improve performance of a data fetching method.
Think about storing, updating, and clearing cache for data methods.
You got /4 concepts.
Explain the difference between @Cacheable(), @CachePut(), and @CacheEvict() decorators in NestJS.
Focus on when each decorator affects the cache.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using cache decorators like @Cacheable in a NestJS service method?
easy
A. To store the method's result and reuse it for faster responses
B. To log every method call for debugging
C. To automatically retry failed method calls
D. To encrypt the method's output before sending
Solution
Step 1: Understand cache decorator role
Cache decorators like @Cacheable save the output of a method so it can be reused without recalculating.
Step 2: Identify the benefit
This reuse speeds up responses by avoiding repeated work.
Final Answer:
To store the method's result and reuse it for faster responses -> Option A
Quick Check:
Cache decorators improve speed by storing results [OK]
Hint: Cache decorators save method results to speed up calls [OK]
Common Mistakes:
Thinking cache decorators log method calls
Confusing cache with retry logic
Assuming cache encrypts data
2. Which of the following is the correct way to apply a cache decorator with a 10-second TTL on a NestJS method?
easy
A. @Cacheable({ ttl: 10 })
B. @CacheEvict({ ttl: 10 })
C. @Cacheable(ttl = 10)
D. @Cacheable(ttl: 10)
Solution
Step 1: Recall correct decorator syntax
The @Cacheable decorator accepts an options object with ttl as a key.
What is the main issue with the @CacheEvict usage here?
medium
A. The method must be async to use @CacheEvict
B. It should be @Cacheable instead of @CacheEvict
C. It lacks a key or target to specify which cache to clear
D. TTL must be set on @CacheEvict as well
Solution
Step 1: Understand @CacheEvict purpose
@CacheEvict clears cached data, but needs to know which cache entry or key to clear.
Step 2: Identify missing key or target
The code does not specify a key or target, so it cannot clear the intended cache.
Final Answer:
It lacks a key or target to specify which cache to clear -> Option C
Quick Check:
@CacheEvict needs key/target to clear cache [OK]
Hint: CacheEvict must specify what cache to clear [OK]
Common Mistakes:
Using CacheEvict without specifying key
Confusing CacheEvict with Cacheable
Assuming async required for CacheEvict
5. You want to cache a method result but clear the cache automatically when a related update method runs. Which combination of decorators correctly achieves this in NestJS?
hard
A. Use @CacheEvict on fetch method and @Cacheable on update method
B. Use @CacheEvict on both methods to clear cache
C. Use @Cacheable on both methods to cache results
D. Use @Cacheable on the fetch method and @CacheEvict on the update method with matching cache keys