0
0
NestJSframework~20 mins

Custom cache keys in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Key Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does a custom cache key affect caching in NestJS?

Consider a NestJS controller method using a custom cache key function. What will be the effect on caching behavior?

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

@Controller('items')
export class ItemsController {
  @Get()
  @CacheKey('custom_key')
  @CacheTTL(30)
  findAll() {
    return ['item1', 'item2'];
  }
}
AThe cache will always use 'custom_key' as the key for storing the response, ignoring request parameters.
BThe cache key will be generated dynamically based on request parameters automatically.
CThe cache will not store any response because custom keys disable caching.
DThe cache key will be a combination of method name and parameters, ignoring the custom key.
Attempts:
2 left
💡 Hint

Think about what the @CacheKey decorator does in NestJS.

📝 Syntax
intermediate
2:00remaining
Identify the correct syntax to set a custom cache key in NestJS

Which of the following code snippets correctly sets a custom cache key for a controller method in NestJS?

A
@CacheKey('myKey')
@Get()
getData() { return 'data'; }
B
@CacheKey(myKey)
@Get()
getData() { return 'data'; }
C
@CacheKey({ key: 'myKey' })
@Get()
getData() { return 'data'; }
D
@CacheKey()
@Get()
getData() { return 'data'; }
Attempts:
2 left
💡 Hint

Remember how decorators accept string literals for keys.

🔧 Debug
advanced
3:00remaining
Why does the custom cache key not work as expected?

Given this code, why does the cache not store separate responses for different user IDs?

NestJS
import { Controller, Get, Param } from '@nestjs/common';
import { CacheKey } from '@nestjs/cache-manager';

@Controller('users')
export class UsersController {
  @Get(':id')
  @CacheKey('user_data')
  getUser(@Param('id') id: string) {
    return { id, name: 'User ' + id };
  }
}
ABecause @CacheKey does not work with route parameters.
BBecause the method lacks @CacheTTL decorator to enable caching.
CBecause the cache key is fixed to 'user_data', responses for all user IDs overwrite each other.
DBecause the cache key should be a function returning a key string.
Attempts:
2 left
💡 Hint

Think about how cache keys identify cached data.

🧠 Conceptual
advanced
3:00remaining
How to create dynamic custom cache keys in NestJS?

Which approach correctly creates dynamic cache keys based on method parameters in NestJS?

ANestJS automatically generates dynamic cache keys without extra code.
BUse @CacheKey decorator with a template string including parameters directly.
CUse @CacheKey decorator multiple times with different keys for each parameter value.
DUse a custom interceptor that generates cache keys including method arguments dynamically.
Attempts:
2 left
💡 Hint

Think about how to customize cache keys beyond static strings.

state_output
expert
4:00remaining
What is the cache key used and output after calling the method twice with different params?

Given the following NestJS controller and custom interceptor, what cache keys are used and what is the output after two calls with different parameters?

NestJS
import { CacheInterceptor, ExecutionContext, Injectable, Controller, Get, Param, UseInterceptors } from '@nestjs/common';

@Injectable()
class CustomCacheInterceptor extends CacheInterceptor {
  trackBy(context: ExecutionContext): string | undefined {
    const request = context.switchToHttp().getRequest();
    const id = request.params.id;
    return `user_${id}`;
  }
}

@Controller('users')
@UseInterceptors(CustomCacheInterceptor)
export class UsersController {
  @Get(':id')
  getUser(@Param('id') id: string) {
    return { id, name: 'User ' + id };
  }
}

// Calls:
// 1) GET /users/1
// 2) GET /users/2
ACache keys used: 'users_getUser'; outputs: same cached response for both calls
BCache keys used: 'user_1' and 'user_2'; outputs: {id:'1', name:'User 1'} and {id:'2', name:'User 2'}
CCache keys used: 'user_data'; outputs: first call cached, second call overwrites
DNo cache keys used; outputs: fresh response each call
Attempts:
2 left
💡 Hint

Look at how the custom interceptor builds the cache key.