paint-brush
How to Master Distributed Cache in Nest.JS by@axotion
341 reads
341 reads

How to Master Distributed Cache in Nest.JS

by Kamil Fronczak
Kamil Fronczak HackerNoon profile picture

Kamil Fronczak

@axotion

I’m a 2X-year-old tech dude from Poland, and this is...

April 3rd, 2025
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow
en-flagEN
Read this story in the original language, English!
ru-flagRU
Прочтите эту историю на русском языке!
es-flagES
Lee esta historia en Español!
pt-flagPT
Leia esta história em português!
ja-flagJA
この物語を日本語で読んでください!
eu-flagEU
Irakurri ipuin hau euskaraz!
si-flagSI
මේ කතාව සිංහලෙන් කියවන්න!
ur-flagUR
اس کہانی کو اردو میں پڑھیں!
tl-flagTL
Basahin ang kwentong ito sa Filipino!
ta-flagTA
இந்த கதையை தமிழில் படியுங்கள்!
ro-flagRO
Citiți această poveste în limba română!
sr-flagSR
Прочитајте ову причу на српском!
hr-flagHR
Pročitajte ovu priču na hrvatskom!
EN

Too Long; Didn't Read

@nestixis/cache-manager is a lightweight, Redis-powered library for NestJS. It provides a straightforward API for managing Redis caching, complete with configurable TTLs and support for advanced caching strategies.

Company Mentioned

Mention Thumbnail
GitHub
featured image - How to Master Distributed Cache in Nest.JS
1x
Read by Dr. One voice-avatar

Listen to this story

Kamil Fronczak HackerNoon profile picture
Kamil Fronczak

Kamil Fronczak

@axotion

I’m a 2X-year-old tech dude from Poland, and this is my blog about tech stuff: NestJS, Node

About @axotion
LEARN MORE ABOUT @AXOTION'S
EXPERTISE AND PLACE ON THE INTERNET.
0-item

STORY’S CREDIBILITY

Guide

Guide

Walkthroughs, tutorials, guides, and tips. This story will teach you how to do something new or how to do something better.

Caching can be a thorn in any developer’s side. I’ve spent too many hours wrestling with slow APIs and overburdened databases, searching for a solution that’s both effective and easy to implement.


That’s why I was thrilled when Karol (karol71927), a talented member of our open-source organization, Nestixis, created @nestixis/cache-manager.


This lightweight, Redis-powered library has streamlined caching in my NestJS projects, and I’m eager to share how it’s made a difference.


The Challenge: Caching Complexity in NestJS

The scenario is all too familiar: your application runs smoothly until traffic surges, and suddenly your database buckles under the load. Caching is the obvious fix—store data once, serve it quickly—but integrating it into NestJS often feels cumbersome. Redis offers powerful capabilities, but setting it up typically involves wrangling configurations, managing expiration policies, and defining custom cache keys.

I needed a tool that simplified the process while allowing precise control over what gets cached, like request parameters or queries.


So, Karol designed @nestixis/cache-manager to address these pain points with a clean, efficient approach. This package provides a straightforward API for managing Redis caching, complete with configurable TTLs and support for advanced caching strategies. It’s available at its GitHub repo, and its design reflects our team’s commitment to practical, reusable tools.

Getting Started: Seamless Setup

Installation is as simple as it gets:


npm i @nestixis/cache-manager


To integrate it into your NestJS app, register it in a module:


import { Module } from '@nestjs/common';
import { CacheModule } from '@nestixis/cache-manager';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    CacheModule.registerAsync({
      isGlobal: true,
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        redis: {
          host: configService.get('REDIS_HOST') || 'localhost',
          port: +configService.get('REDIS_PORT') || 6379,
        },
        cachePrefix: 'cache:',
        defaultCacheTTL: 1000, // 1-second default
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}


This sets up Redis caching across your app with minimal effort. From there, you can interact with it manually in a service:


import { Injectable } from '@nestjs/common';
import { CacheManager } from '@nestixis/cache-manager';

@Injectable()
export class MyService {
  constructor(private readonly cacheManager: CacheManager) {}

  async getData(key: string) {
    const cached = await this.cacheManager.get(key);
    if (cached) return cached;

    const data = await this.fetchData();
    await this.cacheManager.add(key, data, 1000); // Cache for 1 second
    return data;
  }

  async clearData(key: string) {
    await this.cacheManager.remove(key);
  }

  private fetchData() {
    return new Promise((resolve) => setTimeout(() => resolve('Data'), 2000));
  }
}


What sets this package apart is its ability to cache based on specific request details—a feature Karol thoughtfully included. Take this

controller:


import { Controller, Get, Post, Delete, Param, UseInterceptors } from '@nestjs/common';
import { CacheInterceptor, CacheRemoveInterceptor, CacheTrackBy } from '@nestixis/cache-manager';
import { MyService } from './my.service';

@Controller('site/:token')
@CacheTrackBy({
  prefix: 'site',
  ttl: 10000, // 10 seconds
  by: [
    {
      by: 'param',
      name: 'token',
    },
  ],
})
export class SiteController {
  constructor(private readonly service: MyService) {}

  @Get()
  @UseInterceptors(CacheInterceptor)
  async get(@Param('token') token: string) {
    return this.service.getData(`site:${token}`);
  }

  // Will clear cache on add or remove of resource to keep fresh state
  @Post()
  @UseInterceptors(CacheRemoveInterceptor)
  async add(@Param('token') token: string) {
    await this.service.getData(`site:${token}`); // Refresh data
  }

  @Delete()
  @UseInterceptors(CacheRemoveInterceptor)
  async remove(@Param('token') token: string) {
    await this.service.clearData(`site:${token}`);
  }
}


The @CacheTrackBy decorator is the key here. It ensures caching is tied to the :token parameter, so /site/abc and /site/xyz each get their own cache entry.


You can adjust it to use queries or other criteria instead, offering the flexibility I’d always wanted. The CacheInterceptor handles GET requests, while CacheRemoveInterceptor clears the cache on updates—elegant and intuitive.

L O A D I N G
. . . comments & more!

About Author

Kamil Fronczak HackerNoon profile picture
Kamil Fronczak@axotion
I’m a 2X-year-old tech dude from Poland, and this is my blog about tech stuff: NestJS, Node

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Hackernoon
Threads

Mentioned in this story

companies