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

@loipv/nestjs-cache

v1.1.0

Published

Decorator-based caching for NestJS: Redis (ioredis or node-redis, Cluster, Sentinel) and two-tier stores, tags, stale-while-revalidate, stampede protection, HTTP caching and Prometheus metrics

Readme

@loipv/nestjs-cache

Decorator-based caching for NestJS: Redis (ioredis or node-redis, Cluster, Sentinel) and two-tier stores, tags, stale-while-revalidate, stampede protection, HTTP caching and Prometheus metrics

  • @Cacheable, @CachePut and @CacheEvict decorators, plus a CacheService for direct calls.
  • @HttpCache for opt-in caching of controller routes (Express and Fastify).
  • Named stores: in-memory, Redis (ioredis or node-redis, standalone, Cluster or Sentinel) and a two-tier L1/L2 store synced over Pub/Sub.
  • Concurrent misses on the same key are coalesced into one load (per process), or across instances with the optional distributed lock.
  • Expiry strategies: fixed, swr (stale-while-revalidate) and sliding.
  • Per-store circuit breaker for Redis outages.
  • Optional gzip or brotli compression of large payloads (compressedSerializer).
  • Optional null caching and TTL jitter, both off by default.
  • No runtime dependencies. Dual ESM and CommonJS build.

Install

npm install @loipv/nestjs-cache

Peer dependencies: @nestjs/common, @nestjs/core (^11 || ^12), reflect-metadata (^0.2) and rxjs (^7.1, which Nest itself requires, so a Nest app already has it). ioredis (^5 || ^6) and redis (^5 || ^6, node-redis) are optional peers; install the one you use for the Redis store.

Quick start

With no options the module uses an in-memory store named default. No Redis is needed.

import { Injectable, Module } from '@nestjs/common';
import { CacheModule, Cacheable } from '@loipv/nestjs-cache';

@Injectable()
export class UserService {
  @Cacheable({ namespace: 'users', key: 'user:{0}', ttl: 60_000 })
  async findById(id: string): Promise<{ id: string } | null> {
    return { id }; // for example, a database call
  }
}

@Module({
  imports: [CacheModule.forRoot({ isGlobal: true })],
  providers: [UserService],
})
export class AppModule {}

With memory only, the cache is private to each process: it is not shared between instances and is lost on restart. All durations are in milliseconds and must be finite numbers greater than 0.

For Redis, Redis Cluster or Sentinel and the two-tier store, see Stores. A runnable NestJS 12 app with tags, @HttpCache, optional Redis and Prometheus is in examples/basic.

Documentation

  • Configuration: module options and defaults, null caching, TTL jitter and the expiry strategies (fixed, swr with softTimeout, sliding).
  • Decorators: @Cacheable, @CachePut and @CacheEvict, their options, and dynamic keys.
  • HTTP caching: @HttpCache on controller routes, on Express and Fastify.
  • CacheService: get, set, delete, clear, getOrSet and the batch methods getMany, setMany and getOrSetMany.
  • Stores: memory, Redis (ioredis or node-redis; standalone, Cluster or Sentinel), the two-tier store, custom stores and invalidation buses.
  • Versioned namespaces and tags: an O(1) namespace clear, and tag invalidation with invalidateTags and @CacheEvict({ tags }).
  • Serializers: custom serializers and gzip or brotli compression (compressedSerializer).
  • Resilience: the distributed lock, the circuit breaker, and how errors are handled.
  • Events and observability: events, Prometheus metrics, the @nestjs/terminus health check, logging and a sample Grafana dashboard.
  • Tracing: opt-in OpenTelemetry spans for every cache call, with CacheTracingModule.
  • Security and Caveats.
  • Stability and semver: what 1.x keeps stable, and what counts as a major, minor or patch change.
  • Migrating from @nestjs/cache-manager.

Compatibility

The semver contract is in Stability and semver.

| Component | Supported | | ------------------ | ---------------------------------------------------------- | | NestJS | 11 (CommonJS) and 12 (ESM) | | Node.js | >=22 (LTS 22, 24, 26) | | TypeScript | >=5.4 (the published .d.ts is checked against 5.4) | | ioredis | ^5 \|\| ^6 (optional peer; Cluster needs 5.6.0 or later) | | redis (node-redis) | ^5 \|\| ^6 (optional peer; Sentinel supported) | | rxjs | ^7.1 (peer, as for @nestjs/common) | | Redis server | 6.2 or later (Cluster: 7 or later) |

The decorators (@Cacheable, @CachePut, @CacheEvict, @HttpCache) are legacy decorators: consumers need experimentalDecorators: true in tsconfig.json. Standard (TC39) decorators are not supported.

The package ships both ESM and CommonJS builds. The injection tokens use Symbol.for('@loipv/nestjs-cache:...'), so they are shared when both builds are loaded in one process, and instanceof CacheConfigError matches errors from either build. Classes are not shared: CacheService, CacheEvents and the other classes are injected by class, so import the package from one build only (do not mix import and require of it in one app). The library does not rely on emitDecoratorMetadata.

Roadmap

  • 0.2 - Resilience (done): distributed lock (cross-instance stampede protection), per-store circuit breaker, 'swr' (stale-while-revalidate) and 'sliding' TTL strategies.
  • 0.3 - Observability (done): Prometheus metrics (CachePrometheusModule) with a sample Grafana dashboard, configurable log level with debug logs, circuit breaker logs, and a @nestjs/terminus health indicator (CacheHealthIndicator). All from the main entry point.
  • 0.4 - Versioned namespaces and batches (done): O(1) clear for opt-in versioned namespaces, getMany/setMany/getOrSetMany, escapeKeyArgs, lock keys under the prefix, and an optional ping.
  • 0.5 - Redis Cluster and node-redis (done): Redis Cluster through ioredis, nodeRedisAdapter for redis v5 (standalone and cluster), CacheStore.getStrict/setStrict, RedisClientAdapter.scanIterator, and the tiered-store fix for versioned namespaces while L2 is down.
  • 0.6 - HTTP caching (done): @HttpCache and HttpCacheInterceptor for opt-in route caching on Express and Fastify, and a migration guide from @nestjs/cache-manager.
  • 0.7 - Tags (done): tags on @Cacheable, @CachePut, @HttpCache and the CacheService writes, CacheService.invalidateTags and @CacheEvict({ tags }).
  • 0.8 - Compression, Sentinel and softTimeout (done): compressedSerializer (gzip and brotli), swr softTimeout, Redis Sentinel (ioredis and node-redis), and the tag token re-read.
  • 0.9 - Redis v6, docs and example (done): redis v6 support, the docs split into docs/, an example app, and an API tidy-up (see the changelog for the breaking changes).
  • 0.10 - Tracing (done): opt-in OpenTelemetry tracing (CacheTracingModule).
  • 1.0 - API freeze (done): the public API is frozen and follows semver from there.
  • After 1.0 - Replica reads (planned): reads from Redis replicas.

Changelog and releasing

Each version's changes are listed in CHANGELOG.md. RELEASING.md describes how releases are published and the rules the CI workflows follow.

License

MIT