0
0
NestJSframework~10 mins

Custom cache keys in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the CacheInterceptor from NestJS.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
ACacheInterceptor
BCacheModule
CCacheService
DCacheKey
Attempts:
3 left
💡 Hint
Common Mistakes
Importing CacheModule instead of CacheInterceptor
Using CacheService which is not a valid import here
2fill in blank
medium

Complete the code to create a custom cache key by overriding the trackBy method.

NestJS
export class CustomCacheInterceptor extends CacheInterceptor {
  protected trackBy(context: ExecutionContext): string | undefined {
    const request = context.switchToHttp().getRequest();
    return request.method + ':' + [1];
  }
}
Drag options to blanks, or click blank then click option'
Arequest.headers
Brequest.body
Crequest.params
Drequest.url
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.body which may vary and cause cache misses
Using request.headers which is not a stable cache key
3fill in blank
hard

Fix the error in the custom cache key function to correctly handle query parameters.

NestJS
protected trackBy(context: ExecutionContext): string | undefined {
  const request = context.switchToHttp().getRequest();
  const key = request.method + ':' + request.url + '?' + [1];
  return key;
}
Drag options to blanks, or click blank then click option'
Arequest.params
Brequest.query
Crequest.body
Drequest.headers
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.params which holds route parameters, not query parameters
Using request.body which is for POST data
4fill in blank
hard

Fill both blanks to create a cache key that includes method, URL, and a user ID from headers.

NestJS
protected trackBy(context: ExecutionContext): string | undefined {
  const request = context.switchToHttp().getRequest();
  const userId = request.headers['[1]'];
  return request.method + ':' + request.url + ':' + [2];
}
Drag options to blanks, or click blank then click option'
Ax-user-id
Bauthorization
CuserId
Duser-agent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'authorization' header which contains tokens, not user ID
Using 'user-agent' which is unrelated to user identity
5fill in blank
hard

Fill all three blanks to define a custom cache interceptor class with a trackBy method that uses method, URL, and a query param 'lang'.

NestJS
import { CacheInterceptor, ExecutionContext } from '@nestjs/common';

export class CustomCacheInterceptor extends [1] {
  protected trackBy(context: [2]): string | undefined {
    const request = context.switchToHttp().getRequest();
    const lang = request.query['[3]'];
    return request.method + ':' + request.url + ':' + lang;
  }
}
Drag options to blanks, or click blank then click option'
ACacheInterceptor
BExecutionContext
Clang
DCacheService
Attempts:
3 left
💡 Hint
Common Mistakes
Using CacheService instead of CacheInterceptor as base class
Using wrong type for context parameter
Using incorrect query parameter key