Bird
Raised Fist0
NestJSframework~5 mins

CacheModule setup in NestJS - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of the CacheModule in NestJS?
CacheModule helps store data temporarily to speed up responses and reduce repeated work, like remembering answers to questions you've already asked.
Click to reveal answer
beginner
How do you import CacheModule globally in a NestJS application?
Use CacheModule.register({ isGlobal: true }) inside the imports array of your root module to make caching available everywhere without re-importing.
Click to reveal answer
beginner
What option do you set to define how long cached data should live in CacheModule?
You set the 'ttl' (time to live) option in seconds, which tells the cache how long to keep data before removing it.
Click to reveal answer
beginner
Show a simple example of CacheModule setup with a 5-minute cache duration.
CacheModule.register({ ttl: 300 }) // 300 seconds = 5 minutes
Click to reveal answer
intermediate
Why might you want to use a cache store like Redis with CacheModule?
Redis stores cache outside your app memory, so cached data stays even if your app restarts, and it can be shared across multiple app instances.
Click to reveal answer
Which method imports CacheModule globally in NestJS?
ACacheModule.setupGlobal()
BCacheModule.forRoot()
CCacheModule.global()
DCacheModule.register({ isGlobal: true })
What does 'ttl' stand for in CacheModule options?
ATime to live
BTotal time limit
CTemporary time lock
DTime to load
Which cache store is commonly used with CacheModule for persistent caching?
AMySQL
BMongoDB
CRedis
DLocalStorage
What happens if you don't set 'ttl' in CacheModule?
ACache expires immediately
BCache never expires
CDefault ttl is 5 seconds
DCache is disabled
How do you enable caching in a specific service after importing CacheModule?
AInject CACHE_MANAGER and use it
BCall CacheModule.enable()
CUse CacheService.start()
DAdd @Cacheable decorator
Explain how to set up CacheModule globally in a NestJS app and why you might want to do this.
Think about making caching easy to use in all parts of your app.
You got /4 concepts.
    Describe the role of 'ttl' in CacheModule and how it affects cached data.
    Consider how long you want your app to remember cached info.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of importing CacheModule in a NestJS application?
      easy
      A. To enable temporary storage of data to speed up responses
      B. To connect to a database automatically
      C. To handle user authentication
      D. To manage application logging

      Solution

      1. Step 1: Understand CacheModule's role

        CacheModule is used to store data temporarily to reduce repeated processing.
      2. Step 2: Compare with other options

        It does not handle database, authentication, or logging directly.
      3. Final Answer:

        To enable temporary storage of data to speed up responses -> Option A
      4. Quick Check:

        CacheModule = temporary data storage [OK]
      Hint: CacheModule is about caching data, not databases or auth [OK]
      Common Mistakes:
      • Confusing CacheModule with database modules
      • Thinking CacheModule manages authentication
      • Assuming CacheModule handles logging
      2. Which of the following is the correct way to import and register CacheModule with a TTL of 5 seconds in a NestJS module?
      easy
      A. CacheModule.register({ ttl: 5 })
      B. CacheModule.forRoot({ ttl: 5 })
      C. CacheModule.setup({ ttl: 5 })
      D. CacheModule.init({ ttl: 5 })

      Solution

      1. Step 1: Recall CacheModule registration method

        CacheModule uses the static method register to configure options like ttl.
      2. Step 2: Verify method names

        Methods like forRoot, setup, or init are not used by CacheModule.
      3. Final Answer:

        CacheModule.register({ ttl: 5 }) -> Option A
      4. Quick Check:

        Use register() to configure CacheModule [OK]
      Hint: CacheModule uses register(), not forRoot() or init() [OK]
      Common Mistakes:
      • Using forRoot() instead of register()
      • Using setup() or init() which do not exist
      • Forgetting to pass options inside register()
      3. Given this NestJS module setup:
      import { CacheModule, Module } from '@nestjs/common';
      
      @Module({
        imports: [CacheModule.register({ ttl: 10 })],
      })
      export class AppModule {}
      What will be the effect of the ttl: 10 option?
      medium
      A. Cache entries expire after 10 minutes
      B. Cache entries expire after 10 seconds
      C. Cache entries never expire
      D. Cache entries expire immediately

      Solution

      1. Step 1: Understand ttl meaning in CacheModule

        The ttl option sets the time-to-live in seconds for cached data.
      2. Step 2: Interpret ttl: 10

        Since ttl is 10, cached data expires after 10 seconds.
      3. Final Answer:

        Cache entries expire after 10 seconds -> Option B
      4. Quick Check:

        ttl = seconds cache lives [OK]
      Hint: ttl value is in seconds, not minutes [OK]
      Common Mistakes:
      • Assuming ttl is in minutes
      • Thinking ttl disables expiration
      • Confusing ttl with cache size
      4. Identify the error in this CacheModule setup:
      import { CacheModule, Module } from '@nestjs/common';
      
      @Module({
        imports: [CacheModule.register({ ttl: '20' })],
      })
      export class AppModule {}
      medium
      A. register() method does not accept options
      B. CacheModule cannot be imported from '@nestjs/common'
      C. ttl should be a number, not a string
      D. Module decorator is missing parentheses

      Solution

      1. Step 1: Check ttl option type

        ttl expects a number representing seconds, not a string.
      2. Step 2: Verify other parts

        CacheModule import and Module decorator usage are correct; register() accepts options.
      3. Final Answer:

        ttl should be a number, not a string -> Option C
      4. Quick Check:

        ttl must be numeric [OK]
      Hint: ttl must be a number, not quoted text [OK]
      Common Mistakes:
      • Passing ttl as a string instead of number
      • Thinking register() has no parameters
      • Misusing import paths
      5. You want to cache HTTP GET responses in a NestJS controller using CacheModule with a global TTL of 15 seconds. Which setup correctly applies caching to all routes in the module?
      hard
      A. Import CacheModule with ttl 15 and manually store cache in each method
      B. Import CacheModule with ttl 15 and add @Cacheable() decorator on each method
      C. Import CacheModule with ttl 15 and add @Cache() decorator on the module
      D. Import CacheModule with ttl 15 and add @UseInterceptors(CacheInterceptor) on the controller

      Solution

      1. Step 1: Understand CacheModule and CacheInterceptor usage

        CacheModule provides caching, and CacheInterceptor applies caching behavior to routes.
      2. Step 2: Apply CacheInterceptor globally on controller

        Using @UseInterceptors(CacheInterceptor) on the controller enables caching for all its routes.
      3. Step 3: Evaluate other options

        No @Cacheable() or @Cache() decorators exist in NestJS core; manual caching is possible but not automatic.
      4. Final Answer:

        Import CacheModule with ttl 15 and add @UseInterceptors(CacheInterceptor) on the controller -> Option D
      5. Quick Check:

        Use CacheInterceptor with CacheModule for route caching [OK]
      Hint: Use CacheInterceptor decorator to enable caching on controllers [OK]
      Common Mistakes:
      • Using non-existent decorators like @Cacheable or @Cache
      • Not applying CacheInterceptor to controller or routes
      • Trying manual caching instead of interceptor
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard