Gathering detailed insights and metrics for nestjs-cache-mediator
Gathering detailed insights and metrics for nestjs-cache-mediator
Gathering detailed insights and metrics for nestjs-cache-mediator
Gathering detailed insights and metrics for nestjs-cache-mediator
npm install nestjs-cache-mediator
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
4
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.
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.
Install the package via npm:
1npm install nestjs-cache-mediator
Implement the ICacheDriver
interface to integrate your chosen cache (e.g. Redis). For example, using ioredis:
1// redis-cache-driver.ts 2import { ICacheDriver } from 'nestjs-cache-mediator'; 3import * as Redis from 'ioredis'; 4 5export class RedisCacheDriver implements ICacheDriver { 6 private client: Redis.Redis; 7 constructor(options?: Redis.RedisOptions) { 8 this.client = new Redis(options); 9 } 10 async get(key: string): Promise<string | null> { 11 return this.client.get(key); 12 } 13 async set(key: string, value: string, ttl: number): Promise<void> { 14 await this.client.set(key, value, 'EX', ttl); 15 } 16 async del(key: string): Promise<void> { 17 await this.client.del(key); 18 } 19}
In your NestJS application's module, import CacheFirstModule
and provide your cache driver:
1// app.module.ts 2import { Module } from '@nestjs/common'; 3import { CacheFirstModule, ICacheDriver } from 'nestjs-cache-mediator'; 4import { RedisCacheDriver } from './redis-cache-driver'; 5 6@Module({ 7 imports: [CacheFirstModule], 8 providers: [ 9 { 10 provide: ICacheDriver, 11 useValue: new RedisCacheDriver({ host: 'localhost', port: 6379 }), 12 }, 13 ], 14}) 15export class AppModule {}
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):
1import { CacheFirstService } from 'nestjs-cache-mediator'; 2 3interface SomeDataType { 4 foo: string; 5 bar: number; 6} 7 8CacheFirstService.registerHandler<{ extraParam: string }, SomeDataType>( 9 'getSomeData', 10 async (params) => { 11 // Implement your data retrieval logic here. 12 return { foo: `Hello ${params.extraParam}`, bar: 42 }; 13 } 14);
Inject the CacheFirstService
into your own service and call its generic method:
1import { Injectable } from '@nestjs/common'; 2import { CacheFirstService } from 'nestjs-cache-mediator'; 3 4@Injectable() 5export class MyDataService { 6 constructor(private readonly cacheFirstService: CacheFirstService) {} 7 8 async getData(extraParam: string): Promise<{ foo: string; bar: number }> { 9 const cacheKey = `myData:getSomeData:${extraParam}`; 10 return this.cacheFirstService.cacheFirst<{ foo: string; bar: number }, { extraParam: string }>( 11 cacheKey, 12 3600, // Redis TTL: 1 hour 13 10000, // Job TTL: 10 seconds 14 'getSomeData', 15 { extraParam } 16 ); 17 } 18}
You can force a refresh (warm-up) of a cache key during application boot or on demand:
1await this.cacheFirstService.warmCacheForKey<{ foo: string; bar: number }, { extraParam: string }>( 2 'myData:getSomeData:example', 3 3600, // 1 hour 4 10000, // 10 seconds 5 'getSomeData', 6 { extraParam: 'example' } 7);
cacheFirst<T, P>(cacheKey: string, redisTTL: number, jobTTL: number, jobType: string, params: P): Promise<T>
Retrieves data using a cache-first strategy.
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.
CacheFirstService.registerHandler<P, T>(jobType: string, handler: CacheJobHandler<P, T>): void
The processor automatically executes jobs added to the cacheFirstQueue
by calling the registered handler for the job type.
Contributions, improvements, and bug fixes are welcome. Please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License.
No vulnerabilities found.
No security vulnerabilities found.