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

@mondart/nestjs-common-module-caching

v3.2.3

Published

Caching module for NestJS.

Readme

@mondart/nestjs-common-module-caching

Redis-backed caching for NestJS: a CachingService for direct key/value access, an HTTP response-caching interceptor, and a DistributedLockService for coordinating work across multiple instances of the same service (e.g. so only one replica runs a given cron tick).

Registration

import { CachingModule } from '@mondart/nestjs-common-module-caching';

@Module({
  imports: [
    CachingModule.register({
      host: 'localhost',
      port: 6379,
      password: 'secret',
      database: 0,
      namespace: 'my-service',
      ttl: 60_000, // default TTL in ms
    }),
  ],
})
export class AppModule {}

Or asynchronously:

CachingModule.registerAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({
    host: config.get('REDIS_HOST'),
    port: config.get('REDIS_PORT'),
    password: config.get('REDIS_PASSWORD'),
  }),
});

CachingModule is @Global(), so CachingService and DistributedLockService are available for injection anywhere without re-importing the module.

CachingService

constructor(private readonly cachingService: CachingService) {}

await this.cachingService.set('user:42', user, 60); // ttlSeconds
const user = await this.cachingService.get<User>('user:42');
await this.cachingService.setMany([{ key: 'a', value: 1, ttlSeconds: 30 }]);
const [a, b] = await this.cachingService.getMany<number>(['a', 'b']);
await this.cachingService.has('user:42');
await this.cachingService.ttl('user:42');
await this.cachingService.del('user:42');
await this.cachingService.delMany(['a', 'b']);
await this.cachingService.clear();

Caching HTTP responses

CachingModule registers CachingInterceptor globally, but it only caches a GET route when the handler (or controller) is explicitly opted in — nothing is cached by default.

import {
  UseCachingInterceptorDecorator,
  SkipCachingInterceptorDecorator,
} from '@mondart/nestjs-common-module-caching';

@UseCachingInterceptorDecorator({ ttl: 30_000 })
@Get()
findAll() { ... }

@SkipCachingInterceptorDecorator()
@Get('live')
findLive() { ... } // never cached, even if the controller opts in

key/ttl accept the same static value or factory function types as @nestjs/cache-manager's own CacheKey/CacheTTL. When no key is given, requests are cached per ControllerName_methodName:<url+query hash>.

Distributed locking

Use this to stop the same piece of work (typically a @Cron() handler) running concurrently across multiple instances of a service.

Programmatic: DistributedLockService.runExclusive

constructor(private readonly lockService: DistributedLockService) {}

@Cron('*/5 * * * *')
async syncJob() {
  await this.lockService.runExclusive(
    'sync-job',
    async ({ signal }) => {
      for (const item of items) {
        if (signal.aborted) break; // lock could no longer be renewed
        await process(item);
      }
    },
    { ttlMs: 30_000, autoExtend: true }, // autoExtend defaults to true
  );
}
  • Returns undefined without invoking the callback when another instance already holds the lock — the expected outcome on replicas that lose the race for a given tick.
  • With autoExtend (default true), the lock is renewed on an interval (ttlMs / 3 by default, or extensionIntervalMs) for as long as the callback runs. If renewal ever falls behind the lock's real TTL, the callback's signal is aborted so long-running work can check signal.aborted and stop early instead of continuing to run without exclusivity.
  • acquire, release, and extend are also available individually for manual lock management.

Declarative: @DistributedLockDecorator

import { DistributedLockDecorator } from '@mondart/nestjs-common-module-caching';

@Cron('*/5 * * * *')
@DistributedLockDecorator({ key: 'sync-job', ttlMs: 30_000 })
async syncJob() { ... }

key can also be a function of the method's arguments, e.g. key: (tenantId: string) => \sync-job:${tenantId}`to scope the lock per call. This works on methods that Nest itself never routes through an interceptor (like@Cron()handlers) becauseDistributedLockExplorer` wraps decorated methods directly at application bootstrap, rather than relying on the request pipeline.

Both approaches share the same DistributedLockService under the hood, so lock keys are namespaced the same way (CachingModuleOptions.namespace, if set) regardless of which style you use.