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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Overriding trackBy lets you control exactly what counts as a unique cache key.
- 2Returning undefined from a cache key is the idiomatic way to opt a request out of caching.
- 3Reflector with getAllAndOverride reads per-handler metadata so config lives next to the route.
Related explainers
typescript
import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; import { ApiProperty } from '@nestjs/swagger';
Wiring validation and Swagger docs in NestJS
validation
openapi
decorators
Intermediate
8 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json}; use dashmap::DashMap; use serde::Serialize; use std::sync::Arc;
Request coalescing in an Axum handler
caching
concurrency
request-coalescing
Advanced
8 steps
typescript
import { Component, Input } from '@angular/core'; interface Order { id: string;
How Angular ICU plurals localize an order summary
i18n
pluralization
standalone-component
Intermediate
8 steps
php
<?php namespace App\Providers;
Subdomain multi-tenancy routing in Laravel
multi-tenancy
service-container
route-binding
Advanced
7 steps
typescript
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, catchError, concatMap, finalize } from 'rxjs'; import { DataSource, QueryRunner } from 'typeorm';
Wrapping requests in a transaction with NestJS
interceptors
transactions
rxjs
Advanced
7 steps
typescript
type CsvColumn<T> = { header: string; value: (row: T) => string | number | boolean | null | undefined; };
Building a type-safe CSV writer in TypeScript
generics
serialization
escaping
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-tenant-aware-http-cache-in-nestjs-explained-typescript-5725/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.