0
0
NestJSframework~10 mins

Cache interceptor 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'
AController
BCacheModule
CHttpException
DCacheInterceptor
Attempts:
3 left
💡 Hint
Common Mistakes
Importing CacheModule instead of CacheInterceptor
Using unrelated imports like Controller or HttpException
2fill in blank
medium

Complete the code to apply the CacheInterceptor globally using app.useGlobalInterceptors.

NestJS
app.[1](new CacheInterceptor());
Drag options to blanks, or click blank then click option'
AuseGlobalGuards
BuseGlobalFilters
CuseGlobalInterceptors
DuseGlobalPipes
Attempts:
3 left
💡 Hint
Common Mistakes
Using useGlobalGuards or useGlobalFilters which are for other purposes
Forgetting to instantiate CacheInterceptor with new
3fill in blank
hard

Fix the error in the code to enable caching on a controller method using the CacheInterceptor.

NestJS
@UseInterceptors([1])
@Get('data')
getData() {
  return { message: 'Hello' };
}
Drag options to blanks, or click blank then click option'
ALogger
BCacheInterceptor
CHttpException
DCacheModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using CacheModule instead of CacheInterceptor in the decorator
Using unrelated classes like Logger or HttpException
4fill in blank
hard

Fill both blanks to configure the CacheModule with a TTL of 10 seconds.

NestJS
imports: [CacheModule.[1]({ ttl: [2] })]
Drag options to blanks, or click blank then click option'
Aregister
B10
C5
DforRoot
Attempts:
3 left
💡 Hint
Common Mistakes
Using forRoot instead of register for CacheModule
Setting TTL to 5 instead of 10
5fill in blank
hard

Fill both blanks to create a custom cache interceptor class that extends CacheInterceptor and overrides trackBy method to cache only GET requests.

NestJS
export class CustomCacheInterceptor extends CacheInterceptor {
  trackBy(context: ExecutionContext): string | undefined {
    const request = context.switchToHttp().getRequest();
    if (request.method !== [1]) {
      return undefined;
    }
    return super.[2](context);
  }
}
Drag options to blanks, or click blank then click option'
A'POST'
BtrackBy
C'GET'
Dintercept
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'POST' instead of 'GET'
Calling super.intercept instead of super.trackBy