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-cache-mediator

v1.0.0

Published

A fully abstract, type-safe cache-first mediator for NestJS using BullMQ and Redis for distributed caching and job scheduling. Optimized for performance and concurrency.

Readme

nestjs-cache-mediator

npm version
Build Status

nestjs-cache-mediator is a highly abstract, type‑safe NestJS module that implements a cache‑first strategy using BullMQ for distributed job scheduling and a pluggable cache driver (via an interface) for storing results (e.g. in Redis). This package is designed to optimize performance and concurrency by ensuring that multiple concurrent requests for the same cache key share the same job.

Features

  • Abstract & Generic API:
    Define your own job handlers with full TypeScript support and plug them into the cache‑first workflow.

  • BullMQ Integration:
    Uses BullMQ to schedule and execute expensive data retrieval tasks, using the cache key as a unique job identifier to prevent duplicate work.

  • Pluggable Cache Driver:
    Implements an ICacheDriver interface so you can easily use any cache system (e.g. Redis via ioredis).

  • Warm-Up Support:
    Provides a method to force a cache refresh (for instance, on application boot).

  • Optimized for Concurrency:
    Designed for distributed environments where multiple instances share the same Redis/BullMQ configuration, so duplicate work is avoided.

Installation

Install the package via npm:

npm install nestjs-cache-mediator

Setup

Provide a Cache Driver

Implement the ICacheDriver interface to integrate your chosen cache (e.g. Redis). For example, using ioredis:

// redis-cache-driver.ts
import { ICacheDriver } from 'nestjs-cache-mediator';
import * as Redis from 'ioredis';

export class RedisCacheDriver implements ICacheDriver {
  private client: Redis.Redis;
  constructor(options?: Redis.RedisOptions) {
    this.client = new Redis(options);
  }
  async get(key: string): Promise<string | null> {
    return this.client.get(key);
  }
  async set(key: string, value: string, ttl: number): Promise<void> {
    await this.client.set(key, value, 'EX', ttl);
  }
  async del(key: string): Promise<void> {
    await this.client.del(key);
  }
}

Module Integration

In your NestJS application's module, import CacheFirstModule and provide your cache driver:

// app.module.ts
import { Module } from '@nestjs/common';
import { CacheFirstModule, ICacheDriver } from 'nestjs-cache-mediator';
import { RedisCacheDriver } from './redis-cache-driver';

@Module({
  imports: [CacheFirstModule],
  providers: [
    {
      provide: ICacheDriver,
      useValue: new RedisCacheDriver({ host: 'localhost', port: 6379 }),
    },
  ],
})
export class AppModule {}

Usage

Register a Job Handler

Register your job handler for a given job type. This should be done early (for example, in a bootstrap function or in the module's initialization):

import { CacheFirstService } from 'nestjs-cache-mediator';

interface SomeDataType {
  foo: string;
  bar: number;
}

CacheFirstService.registerHandler<{ extraParam: string }, SomeDataType>(
  'getSomeData',
  async (params) => {
    // Implement your data retrieval logic here.
    return { foo: `Hello ${params.extraParam}`, bar: 42 };
  }
);

Use the Cache-First Service

Inject the CacheFirstService into your own service and call its generic method:

import { Injectable } from '@nestjs/common';
import { CacheFirstService } from 'nestjs-cache-mediator';

@Injectable()
export class MyDataService {
  constructor(private readonly cacheFirstService: CacheFirstService) {}

  async getData(extraParam: string): Promise<{ foo: string; bar: number }> {
    const cacheKey = `myData:getSomeData:${extraParam}`;
    return this.cacheFirstService.cacheFirst<{ foo: string; bar: number }, { extraParam: string }>(
      cacheKey,
      3600,  // Redis TTL: 1 hour
      10000, // Job TTL: 10 seconds
      'getSomeData',
      { extraParam }
    );
  }
}

Warm Up the Cache

You can force a refresh (warm-up) of a cache key during application boot or on demand:

await this.cacheFirstService.warmCacheForKey<{ foo: string; bar: number }, { extraParam: string }>(
  'myData:getSomeData:example',
  3600,  // 1 hour
  10000, // 10 seconds
  'getSomeData',
  { extraParam: 'example' }
);

API Reference

CacheFirstService Methods

  • cacheFirst<T, P>(cacheKey: string, redisTTL: number, jobTTL: number, jobType: string, params: P): Promise<T>
    Retrieves data using a cache-first strategy.

    • cacheKey: A unique cache key (also used as the Bull job id).
    • redisTTL: Time-to-live in seconds for the cached data.
    • jobTTL: Maximum wait time (in milliseconds) for the job to complete.
    • jobType: Identifier for the job handler.
    • params: Parameters passed to the job handler.
  • warmCacheForKey<T, P>(cacheKey: string, redisTTL: number, jobTTL: number, jobType: string, params: P): Promise<T>
    Forces a refresh of the cache by deleting any existing entry and scheduling a new job.

Job Handler Registration

  • CacheFirstService.registerHandler<P, T>(jobType: string, handler: CacheJobHandler<P, T>): void
    Register a function that will be used to fetch data when a cache miss occurs.

BullMQ Processor

The processor automatically executes jobs added to the cacheFirstQueue by calling the registered handler for the job type.

Contributing

Contributions, improvements, and bug fixes are welcome. Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.