Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the CacheModule in a NestJS module.
NestJS
import { CacheModule } from '@nestjs/cache-manager'; @Module({ imports: [CacheModule.[1]()], }) export class AppModule {}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'configure' or 'init' instead of 'register' causes errors.
Forgetting to import CacheModule before using it.
✗ Incorrect
Use CacheModule.register() to set up the cache module with default options.
2fill in blank
mediumComplete the code to set up Redis as the cache store in NestJS.
NestJS
CacheModule.register({
store: [1],
host: 'localhost',
port: 6379,
}), Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string 'redis' instead of the imported redisStore function.
Confusing memory store with Redis store.
✗ Incorrect
Use redisStore imported from cache-manager-redis-store as the Redis cache store.
3fill in blank
hardFix the error in the code to inject Cache correctly in a NestJS service.
NestJS
constructor(private readonly cache: [1]) {} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using CacheManager or CacheService instead of Cache causes injection errors.
Not marking the constructor parameter as private readonly.
✗ Incorrect
The correct injection type is Cache to access cache methods.
4fill in blank
hardFill both blanks to create a cache key with a prefix and set a TTL of 5 minutes.
NestJS
await this.cache.[1]('user:123', userData, { ttl: [2] });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'set' to store data.
Setting TTL to 60 seconds instead of 300.
✗ Incorrect
Use set to store data with a TTL of 300 seconds (5 minutes).
5fill in blank
hardFill all three blanks to configure CacheModule with Redis store, host, and port.
NestJS
CacheModule.register({
store: [1],
host: [2],
port: [3],
}), Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'memory-store' instead of redisStore.
Using port as a string instead of number.
Forgetting quotes around 'localhost'.
✗ Incorrect
Use redisStore for Redis, host as 'localhost', and port 6379.