Using Cache Decorators in NestJS
📖 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