npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nestjs-cachex

v1.1.3

Published

NestJS decorator-based caching module with SWR, Single-flight, msgpack, zstd, and multi-level cache

Readme

English | 한국어 | 日本語

nestjs-cachex

npm version license TypeScript

A decorator-based caching module for NestJS.

SWR, Single-flight, msgpack + zstd, and multi-level cache — all in one package.


Introduction

Most caching modules work well as long as the cache is alive. The moment TTL expires, every subsequent request hits the DB or external API directly, and under load, this leads to Cache Stampede.

nestjs-cachex solves this at the root.

  • SWR — Even after TTL expires, the stale data is returned immediately while the cache is refreshed in the background.
  • Single-flight — A Redis distributed lock and Pub/Sub ensure that only one refresh runs per key, even across multiple instances.
  • Multi-level cache — L1 (in-memory) and L2 (Redis) in a layered structure, eliminating unnecessary network overhead.

| Feature | @nestjs/cache-manager | nestjs-cachex | |---|---|---| | SWR | ✗ | ✓ | | Single-flight | ✗ | ✓ | | Distributed lock | ✗ | ✓ | | msgpack serialization | ✗ | ✓ | | zstd compression | ✗ | ✓ | | Multi-level cache | ✗ | ✓ |


Quick Start

import { Cacheable, CacheEvict } from 'nestjs-cachex';

@Injectable()
export class UserService {
  @Cacheable({ ttl: 60, name: 'users', key: (id: string) => id })
  async findUser(id: string) {
    return this.userRepository.findOne(id);
  }

  @CacheEvict({ name: 'users', key: (id: string) => id })
  async updateUser(id: string, data: UpdateUserDto) {
    return this.userRepository.update(id, data);
  }
}

Features

  • Decorator-based — Separate caching logic from business code entirely with @Cacheable and @CacheEvict
  • SWR — Instant response even on cache expiry. Revalidation happens in the background
  • Single-flight — Distributed lock + Pub/Sub guarantees a single refresh per key, even across instances
  • Jitter — Random TTL offset prevents synchronized expiration and Cache Stampede
  • msgpack — Binary serialization instead of JSON — smaller payload, faster throughput
  • zstd compression — Data exceeding the threshold is compressed automatically
  • Multi-level cache — L1 hit skips Redis entirely. L1 miss falls through to L2 with Write-back
  • Dynamic keys — Generate keys from a static string or a function per request
  • Conditional caching — Fine-grained control with condition and unless
  • Full TypeScript support — Type inference on all options

Requirements

  • Node.js 18+
  • NestJS 9+
  • Redis 6+ (optional, for Redis / Multi-level cache)

Installation

npm install nestjs-cachex
# or
yarn add nestjs-cachex
# or
pnpm add nestjs-cachex

If you're using Redis, install ioredis as well.

npm install ioredis
# or
yarn add ioredis
# or
pnpm add ioredis

Usage

Module Registration

Static registration

import { CacheXModule, CacheManager } from 'nestjs-cachex';

@Module({
  imports: [
    CacheXModule.forRoot({
      defaults: {
        ttl: 300,
        cacheManager: CacheManager.REDIS,
      },
    }),
  ],
})
export class AppModule {}

Async registration

CacheXModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  redisToken: REDIS_CLIENT,
  subscriberToken: REDIS_SUBSCRIBER,
  useFactory: async (config: ConfigService) => ({
    defaults: {
      ttl: config.get('CACHE_TTL'),
      cacheManager: CacheManager.REDIS,
    },
    swr: {
      enabled: true,
      defaultStaleMultiplier: 5, // ttl × 5 when staleTtl is not set
      pubSubTimeoutMs: 2000,
    },
    compression: {
      enabled: true,
      threshold: 20 * 1024, // compress only if >= 20KB
      level: 3,
    },
  }),
})

Redis client setup

Pub/Sub requires a dedicated connection. Make sure to inject it separately from the main connection.

export const REDIS_CLIENT = 'REDIS_CLIENT';
export const REDIS_SUBSCRIBER = 'REDIS_SUBSCRIBER';

@Module({
  providers: [
    { provide: REDIS_CLIENT, useFactory: () => new Redis({ host: 'localhost', port: 6379 }) },
    { provide: REDIS_SUBSCRIBER, useFactory: () => new Redis({ host: 'localhost', port: 6379 }) },
  ],
  exports: [REDIS_CLIENT, REDIS_SUBSCRIBER],
})
export class RedisModule {}

@Cacheable

// SWR pattern
@Cacheable({
  ttl: 10,       // fresh for 10s
  staleTtl: 290, // serve stale for 290s + background refresh
  name: 'dashboard',
  key: (userId: string) => userId,
})
async getDashboard(userId: string) { ... }

// Conditional caching
@Cacheable({
  ttl: 60,
  name: 'items',
  condition: (id: string) => id !== 'guest', // skip cache if false
  unless: (result) => result === null,        // skip storing if true
})
async findItem(id: string) { ... }

// Multi-level cache
@Cacheable({ ttl: 60, name: 'hot-data', cacheManager: CacheManager.MULTI })
async getHotData(id: string) { ... }

@CacheEvict

// Evict a specific entry
@CacheEvict({ name: 'users', key: (id: string) => id })
async updateUser(id: string, data: UpdateUserDto) { ... }

// Evict entire namespace
@CacheEvict({ name: 'users', allEntries: true })
async deleteAllUsers() { ... }

// Evict multiple namespaces
@CacheEvict({ name: ['users', 'profiles', 'sessions'], allEntries: true })
async clearAll() { ... }

// Evict before method execution
@CacheEvict({ name: 'users', allEntries: true, beforeInvocation: true })
async refreshUsers() { ... }

// Debounce eviction — useful when the same key may be invalidated many times in quick succession
@CacheEvict({ name: 'reports', allEntries: true, debounceMs: 3000 })
async onDataChanged() { ... }

Combined usage

@Cacheable({ ttl: 60, name: 'user-detail', key: (id: string) => id })
@CacheEvict({ name: 'user-list', allEntries: true })
async getUser(id: string) { ... }

API

@Cacheable

| Option | Type | Default | Description | |--------|------|---------|-------------| | ttl | number | defaults.ttl | Cache TTL in seconds | | staleTtl | number | ttl × staleMultiplier | Stale window after TTL expires (seconds) | | swr | boolean | module config | SWR enabled override | | name | string \| (...args) => string | auto-generated | Cache namespace | | key | string \| (...args) => string | auto-hash | Cache key | | cacheManager | CacheManager | REDIS | REDIS | MEMORY | MULTI | | condition | (...args) => boolean | — | Skip cache lookup/store if false | | unless | (result, ...args) => boolean | — | Skip storing if true | | compression | boolean \| { threshold?, level? } | module config | Compression override |

@CacheEvict

| Option | Type | Default | Description | |--------|------|---------|-------------| | name | string \| string[] \| (...args) => string \| string[] | auto-generated | Namespace(s) to evict | | key | string \| (...args) => string | auto-hash | Key to evict. Ignored if allEntries: true | | cacheManager | CacheManager | REDIS | Target backend | | allEntries | boolean | false | Evict entire namespace | | beforeInvocation | boolean | false | Evict before method execution | | condition | (...args) => boolean | — | Skip eviction if false | | debounceMs | number | — | Collapse bursts: multiple evictions for the same key within this window (ms) are merged into one |


How It Works

SWR + Single-flight

Request
   │
   ├─[Fresh]───────────────────────────────────────────► Return immediately
   │
   ├─[Stale]───► Return stale data immediately
   │              └─ Background:
   │                  tryLock ──[OK]──► fetch → store → Pub/Sub notify
   │                           └[fail]─ skip (another instance is handling it)
   │
   └─[Miss]────► tryLock ──[OK]──► fetch → store ──────────────────► Return
                           └[fail]─ Pub/Sub wait
                                      ├─[notified]──► get cache ───► Return
                                      └─[timeout]───► polling (exp. backoff)
                                                        └─[max retries]─► direct fetch

Cache Key Format

{name}::{key}          cache data
lock:{name}::{key}     distributed lock
pending:{name}::{key}  Pub/Sub channel

License

MIT