0
0
NestJSframework~20 mins

CacheModule setup in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CacheModule Mastery
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 CacheModule with default settings?

Consider a NestJS service that caches a value using CacheModule with default settings. What will be the behavior when retrieving a cached value immediately after setting it?

NestJS
import { CacheModule, Injectable, Module, Inject, CACHE_MANAGER } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Injectable()
class SampleService {
  constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}

  async cacheValue() {
    await this.cacheManager.set('key', 'value');
    return await this.cacheManager.get('key');
  }
}

@Module({
  imports: [CacheModule.register()],
  providers: [SampleService],
})
export class AppModule {}
AThe method returns 'value' as the cached data is immediately available.
BThe method throws a runtime error because CacheModule requires explicit store configuration.
CThe method returns undefined because caching is asynchronous and not ready yet.
DThe method returns null because the default cache store does not store data.
Attempts:
2 left
💡 Hint

Think about how the default in-memory cache store works in NestJS CacheModule.

📝 Syntax
intermediate
1:30remaining
Which CacheModule setup code is syntactically correct for setting a TTL of 5 seconds?

Choose the correct way to configure CacheModule with a time-to-live (TTL) of 5 seconds.

ACacheModule.register({ expire: 5 })
BCacheModule.register({ ttl: 5 })
CCacheModule.register({ timeToLive: 5 })
DCacheModule.register({ ttl: 5000 })
Attempts:
2 left
💡 Hint

Check the official CacheModule options for TTL property name and units.

🔧 Debug
advanced
2:30remaining
Why does this CacheModule setup cause a runtime error?

Examine the following CacheModule setup. Why will it cause a runtime error?

NestJS
import { CacheModule } from '@nestjs/common';

CacheModule.register({
  store: 'redis',
  host: 'localhost',
  port: 6379
});
ABecause CacheModule does not support Redis caching.
BBecause the 'host' and 'port' options are invalid for CacheModule.
CBecause 'store' must be a function or a valid store instance, not a string.
DBecause the CacheModule requires a 'ttl' option to be set.
Attempts:
2 left
💡 Hint

Think about how to properly configure a Redis cache store in NestJS CacheModule.

state_output
advanced
2:30remaining
What is the value of 'cachedData' after this code runs?

Given this NestJS service using CacheModule, what will be the value of cachedData after fetchData() is called twice in a row?

NestJS
import { Injectable, Inject, CACHE_MANAGER } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Injectable()
class DataService {
  constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}

  async fetchData() {
    const cached = await this.cacheManager.get('data');
    if (cached) return cached;
    const data = 'fresh data';
    await this.cacheManager.set('data', data, { ttl: 1 });
    return data;
  }
}

// Assume fetchData() is called twice within 2 seconds.
A'fresh data' both times because TTL is 1 second and cache expires quickly.
B'fresh data' first call, 'fresh data' second call because cache is still valid.
Cundefined both times because cache is not set properly.
D'fresh data' first call, undefined second call because cache is cleared.
Attempts:
2 left
💡 Hint

Consider the TTL of 1 second and the timing of calls.

🧠 Conceptual
expert
3:00remaining
Which statement about CacheModule global setup is true?

When configuring CacheModule globally in a NestJS app, which statement is correct?

ASetting <code>isGlobal: true</code> automatically enables Redis caching.
BCacheModule cannot be set globally; it must be imported in every module separately.
CGlobal CacheModule disables the ability to override cache settings in feature modules.
DSetting <code>isGlobal: true</code> in <code>CacheModule.register()</code> makes the cache available in all modules without importing it.
Attempts:
2 left
💡 Hint

Think about the purpose of the isGlobal option in NestJS modules.