📖 Scenario: You are building a simple NestJS service that fetches user data. To improve performance, you want to cache the results of the data fetching method.
🎯 Goal: Build a NestJS service that uses the @Cacheable() decorator to cache the result of a method that returns user information.
📋 What You'll Learn
Create a NestJS service class named UserService.
Add a method getUser() that returns a user object with id and name.
Add a cache configuration variable cacheTTL set to 30 seconds.
Use the @Cacheable() decorator on getUser() with the ttl set to cacheTTL.
Ensure the service is ready to be used in a NestJS module.
💡 Why This Matters
🌍 Real World
Caching is used in real-world applications to speed up repeated data fetching and reduce load on databases or APIs.
💼 Career
Understanding cache decorators in NestJS is important for backend developers to optimize performance and scalability of server applications.
Progress0 / 4 steps
1
Create the UserService class with getUser method
Create a NestJS service class called UserService with a method getUser() that returns an object { id: 1, name: 'Alice' }.
NestJS
Hint
Define a class named UserService and add a method getUser() that returns the user object.
2
Add cacheTTL configuration variable
Inside the UserService class, add a variable cacheTTL and set it to 30 to represent 30 seconds.
NestJS
Hint
Add a class property cacheTTL and assign it the number 30.
3
Import and apply @Cacheable decorator with ttl
Import @Cacheable from @nestjs/cache-manager and apply it as a decorator on the getUser() method. Set the decorator option ttl to the class variable cacheTTL.
NestJS
Hint
Import @Cacheable and decorate getUser() with @Cacheable({ ttl: 30 }). Use the number 30 directly for simplicity.
4
Export UserService for module use
Add export keyword before the UserService class declaration to make it available for import in other modules.
NestJS
Hint
Make sure the class declaration starts with export class UserService.
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