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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nestjs-mod/cache-manager

v1.4.1

Published

A cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface for NestJS-mod (Wrapper for https://www.npmjs.com/package/cache-manager)

Downloads

171

Readme

@nestjs-mod/cache-manager

A cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface for NestJS-mod (Wrapper for https://www.npmjs.com/package/cache-manager)

NPM version monthly downloads Telegram bot

Installation

npm i --save [email protected] [email protected] [email protected] @nestjs-mod/cache-manager

Modules

| Link | Category | Description | | ---- | -------- | ----------- | | CacheManagerModule | core | A cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface for NestJS-mod (Wrapper for https://www.npmjs.com/package/cache-manager) |

Modules descriptions

CacheManagerModule

A cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface for NestJS-mod (Wrapper for https://www.npmjs.com/package/cache-manager)

Use in NestJS

Use with forRoot options.

import { NestFactory } from '@nestjs/core';
import { randomUUID } from 'crypto';

import { CacheManagerModule, CacheManagerService } from '@nestjs-mod/cache-manager';
import { isInfrastructureMode } from '@nestjs-mod/common';
import { Injectable, Module } from '@nestjs/common';

const userFeatureName = 'prisma-user';

@Injectable()
export class AppService {
  constructor(private readonly cacheManagerService: CacheManagerService) {}

  async setCache({ externalUserId }: { externalUserId: string }) {
    return await this.cacheManagerService.set('cache', externalUserId, 0);
  }

  async getCache() {
    return await this.cacheManagerService.get('cache');
  }
}

process.env.PRISMA_USER_REDIS_URL = 'redis://:redis_password@localhost:6379';

@Module({
  imports: [
    CacheManagerModule.forRoot({
      staticConfiguration: {
        type: isInfrastructureMode() ? 'memory' : 'redis',
        cacheFeatureName: userFeatureName,
      },
    }),
  ],
  providers: [AppService],
})
export class AppModule {}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);

  const service = app.get<AppService>(AppService);
  const externalUserId = randomUUID();
  await service.setCache({ externalUserId });
  console.log(await service.getCache()); // output: '568a823e-65ea-46ba-aa57-0194ee67e0f9'
}

bootstrap();

An example of access to module services with forFeature.

import { NestFactory } from '@nestjs/core';
import { randomUUID } from 'crypto';

import { CacheManagerModule, CacheManagerService } from '@nestjs-mod/cache-manager';
import { isInfrastructureMode } from '@nestjs-mod/common';
import { Injectable, Module } from '@nestjs/common';

const userFeatureName = 'prisma-user';

@Injectable()
export class FeatureService {
  constructor(private readonly cacheManagerService: CacheManagerService) {}

  async setCache({ externalUserId }: { externalUserId: string }) {
    return await this.cacheManagerService.set('cache', externalUserId, 0);
  }

  async getCache() {
    return await this.cacheManagerService.get('cache');
  }
}
@Module({
  imports: [
    CacheManagerModule.forFeature({
      featureModuleName: 'FeatureModule',
    }),
  ],
  providers: [FeatureService],
})
export class FeatureModule {}

process.env.PRISMA_USER_REDIS_URL = 'redis://:redis_password@localhost:6379';

@Module({
  imports: [
    CacheManagerModule.forRoot({
      staticConfiguration: {
        type: isInfrastructureMode() ? 'memory' : 'redis',
        cacheFeatureName: userFeatureName,
      },
    }),
    FeatureModule,
  ],
})
export class AppModule {}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);

  const service = app.get<FeatureService>(FeatureService);
  const externalUserId = randomUUID();
  await service.setCache({ externalUserId });
  console.log(await service.getCache()); // output: '568a823e-65ea-46ba-aa57-0194ee67e0f9'
}

bootstrap();

Use in NestJS-mod

An example of using forRoot with parameters, you can see the full example here https://github.com/nestjs-mod/nestjs-mod-contrib/tree/master/apps/example-cache-manager.

For work with Redis, you must first connect the Docker Compose module and the Docker Compose module to work with the Redis.

import { CacheManagerModule } from '@nestjs-mod/cache-manager';
import {
  DefaultNestApplicationInitializer,
  DefaultNestApplicationListener,
  InfrastructureMarkdownReportGenerator,
  PACKAGE_JSON_FILE,
  ProjectUtils,
  bootstrapNestApplication,
  isInfrastructureMode,
} from '@nestjs-mod/common';
import { DOCKER_COMPOSE_FILE, DockerCompose, DockerComposeRedis } from '@nestjs-mod/docker-compose';
import { join } from 'path';
import { userFeatureName } from './app/app.constants';
import { AppModule } from './app/app.module';

const rootFolder = join(__dirname, '..', '..', '..');
const appFolder = join(rootFolder, 'apps', 'example-cache-manager');

bootstrapNestApplication({
  globalConfigurationOptions: { debug: true },
  globalEnvironmentsOptions: { debug: true },
  modules: {
    system: [
      ProjectUtils.forRoot({
        staticConfiguration: {
          applicationPackageJsonFile: join(
            __dirname,
            '..',
            '..',
            '..',
            'apps/example-cache-manager',
            PACKAGE_JSON_FILE
          ),
          packageJsonFile: join(rootFolder, PACKAGE_JSON_FILE),
          envFile: join(rootFolder, '.env'),
        },
      }),
      DefaultNestApplicationInitializer.forRoot(),
      DefaultNestApplicationListener.forRoot({
        staticConfiguration: {
          // When running in infrastructure mode, the backend server does not start.
          mode: isInfrastructureMode() ? 'silent' : 'listen',
        },
      }),
    ],
    core: [
      CacheManagerModule.forRoot({
        staticConfiguration: {
          type: isInfrastructureMode() ? 'memory' : 'redis',
          cacheFeatureName: userFeatureName,
        },
      }),
    ],
    feature: [AppModule.forRoot()],
    infrastructure: [
      InfrastructureMarkdownReportGenerator.forRoot({
        staticConfiguration: {
          markdownFile: join(appFolder, 'INFRASTRUCTURE.MD'),
          skipEmptySettings: true,
        },
      }),
      DockerCompose.forRoot({
        configuration: {
          dockerComposeFileVersion: '3',
          dockerComposeFile: join(appFolder, DOCKER_COMPOSE_FILE),
        },
      }),
      DockerComposeRedis.forRoot({ staticConfiguration: { featureName: userFeatureName } }),
    ],
  },
});

New environment variable

EXAMPLE_CACHE_MANAGER_CACHE_MANAGER_USER_REDIS_URL=redis://:redis_password@localhost:6379

When launched in the infrastructure documentation generation mode, the module creates an .env file with a list of all required variables, as well as an example example.env, where you can enter example variable values.

Shared providers

CacheManagerService

Environments

| Key | Description | Sources | Constraints | Default | Value | | ------ | ----------- | ------- | ----------- | ------- | ----- | |redisUrl|Connection string for Redis, if it empty memoryStore use for store data (example: redis://:redis_password@localhost:6379)|obj['redisUrl'], process.env['REDIS_URL']|optional|-|-|

Static configuration

| Key | Description | Constraints | Default | Value | | ------ | ----------- | ----------- | ------- | ----- | |type|Type of storage for store data (memory or redis)|optional|memory|-| |defaultLogger|Default logger|optional|-|-| |featureName|Feature name for generate prefix to environments keys|optional|-|-| |ttl|TTL|optional|-|-| |refreshThreshold|Refresh threshold|optional|-|-| |isCacheable|Is cacheable|optional|-|-|

Back to Top

Links

  • https://github.com/nestjs-mod/nestjs-mod - A collection of utilities for unifying NestJS applications and modules
  • https://github.com/nestjs-mod/nestjs-mod-contrib - Contrib repository for the NestJS-mod
  • https://github.com/nestjs-mod/nestjs-mod-example - Example application built with @nestjs-mod/schematics
  • https://github.com/nestjs-mod/nestjs-mod/blob/master/apps/example-basic/INFRASTRUCTURE.MD - A simple example of infrastructure documentation.
  • https://github.com/nestjs-mod/nestjs-mod-contrib/blob/master/apps/example-prisma/INFRASTRUCTURE.MD - An extended example of infrastructure documentation with a docker-compose file and a data base.
  • https://dev.to/endykaufman/collection-of-nestjs-mod-utilities-for-unifying-applications-and-modules-on-nestjs-5256 - Article about the project NestJS-mod

License

MIT