typescript 44 lines · 7 steps

A tenant-aware HTTP cache in NestJS

Subclassing CacheInterceptor to key responses per tenant and read TTL from route metadata.

Explained by highlit
1import {
2 CACHE_MANAGER,
3 CacheInterceptor,
4} from '@nestjs/cache-manager';
5import {
6 ExecutionContext,
7 Inject,
8 Injectable,
9} from '@nestjs/common';
10import { Reflector } from '@nestjs/core';
11import { Cache } from 'cache-manager';
12import { Request } from 'express';
13 
14import { CACHE_TTL_METADATA } from './cache-ttl.decorator';
15 
16@Injectable()
17export class HttpCacheInterceptor extends CacheInterceptor {
18 constructor(
19 @Inject(CACHE_MANAGER) cacheManager: Cache,
20 protected readonly reflector: Reflector,
21 ) {
22 super(cacheManager, reflector);
23 }
24 
25 protected trackBy(context: ExecutionContext): string | undefined {
26 const request = context.switchToHttp().getRequest<Request>();
27 
28 if (request.method !== 'GET') {
29 return undefined;
30 }
31 
32 const tenant = request.headers['x-tenant-id'] ?? 'public';
33 return `${tenant}:${request.originalUrl}`;
34 }
35 
36 protected getTtl(context: ExecutionContext): number | undefined {
37 const handlerTtl = this.reflector.getAllAndOverride<number>(
38 CACHE_TTL_METADATA,
39 [context.getHandler(), context.getClass()],
40 );
41 
42 return handlerTtl ?? 30_000;
43 }
44}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Overriding trackBy lets you control exactly what counts as a unique cache key.
  2. 2Returning undefined from a cache key is the idiomatic way to opt a request out of caching.
  3. 3Reflector with getAllAndOverride reads per-handler metadata so config lives next to the route.

Related explainers

Share this explainer

Here's the card — post it anywhere.

A tenant-aware HTTP cache in NestJS — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code