0
0
NestJSframework~20 mins

Cache decorators in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Decorators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using @Cacheable with a method returning a random number?

Consider a NestJS service method decorated with @Cacheable() that returns a random number. What will happen when you call this method twice in a row?

NestJS
import { Cacheable } from '@nestjs/cache-manager';

class RandomService {
  @Cacheable()
  getRandom() {
    return Math.random();
  }
}

const service = new RandomService();
const first = service.getRandom();
const second = service.getRandom();
console.log(first === second);
Atrue - The second call returns the cached value from the first call
Bfalse - Each call generates a new random number
CThrows a runtime error because caching is not configured
Dundefined - The method does not return anything
Attempts:
2 left
💡 Hint

Think about what caching does to method results.

lifecycle
intermediate
2:00remaining
When is the cache cleared if you use @CacheEvict on a method?

In NestJS, if a method is decorated with @CacheEvict(), when does the cache get cleared?

NestJS
import { CacheEvict } from '@nestjs/cache-manager';

class UserService {
  @CacheEvict()
  updateUser(id: string, data: any) {
    // update user logic
    return true;
  }
}
AAfter the method successfully completes
BOnly if the method throws an error
CCache is never cleared automatically
DBefore the method runs
Attempts:
2 left
💡 Hint

Think about when you want to remove stale data after an update.

📝 Syntax
advanced
2:00remaining
Which option correctly uses @CacheKey and @CacheTTL decorators together?

Choose the correct way to set a custom cache key and TTL (time to live) for a method in NestJS.

NestJS
import { CacheKey, CacheTTL, Cacheable } from '@nestjs/cache-manager';

class ProductService {
  @Cacheable()
  @CacheKey('custom_key')
  @CacheTTL(120)
  getProduct(id: string) {
    return { id, name: 'Product' };
  }
}
A
@CacheTTL(120)
@CacheKey('custom_key')
@Cacheable()
getProduct(id: string) { return { id, name: 'Product' }; }
B
@CacheKey('custom_key')
@CacheTTL(120)
@Cacheable()
getProduct(id: string) { return { id, name: 'Product' }; }
C
@CacheTTL(120)
@Cacheable()
@CacheKey('custom_key')
getProduct(id: string) { return { id, name: 'Product' }; }
D
@Cacheable()
@CacheKey('custom_key')
@CacheTTL(120)
getProduct(id: string) { return { id, name: 'Product' }; }
Attempts:
2 left
💡 Hint

Remember the order decorators are applied matters in NestJS.

🔧 Debug
advanced
2:00remaining
What error occurs if you use @Cacheable on a method returning a Promise but forget to await it?

Given a method decorated with @Cacheable() that returns a Promise, what happens if you call it without await and try to cache the result?

NestJS
import { Cacheable } from '@nestjs/cache-manager';

class AsyncService {
  @Cacheable()
  async fetchData() {
    return 'data';
  }
}

const service = new AsyncService();
const result = service.fetchData();
console.log(typeof result);
A'string' - The cache automatically unwraps the Promise
BThrows a TypeError because caching expects a string
C'object' - The cached value is a Promise object, not the resolved data
Dundefined - The method returns nothing
Attempts:
2 left
💡 Hint

Think about what happens when you don't await an async function.

🧠 Conceptual
expert
2:00remaining
Which statement about NestJS cache decorators is TRUE?

Choose the true statement about how NestJS cache decorators work.

A@Cacheable caches method results and automatically invalidates cache on data changes
B@CacheEvict can be used to clear cache entries manually after specific method calls
C@CacheKey sets the cache expiration time in seconds for the method
D@CacheTTL defines a custom cache key string for the cached method
Attempts:
2 left
💡 Hint

Recall the purpose of each decorator.