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

v0.0.6

Published

The nestjs cache module based on cache-manager & decorators 🍃

Downloads

6

Readme

zirus-cache

zirus-cache for Nest.JS - simple and modern cache library

Simple example

@CacheMethod()
@Get()
async getData(): Promise<string[]> {
  return data;
}

Installation

npm install cache-manager
npm install -D @types/cache-manager
npm install nestjs-zirus-cache

Basic usage

Import ZirusModule

@Module({
  imports: [
    ZirusModule.forRoot(),

    // Async registration
    ZirusModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => (({
        store: configService.get('STORE')
      })),
      inject: [ConfigService]
    }),
  ],
})
export class AppModule {}

Customize caching

@Module({
  imports: [
    ZirusModule.forRoot({
        store: 'memory', 
        ttl: 0, //time to live 
        max: 100 // max items in cache
    })
  ],
})
export class AppModule {}

Also you can use different storage like Redis:

npm install cache-manager-redis-store
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import * as redisStore from 'cache-manager-redis-store';

@Module({
  imports: [
    ZirusModule.forRoot({
      store: redisStore,
      max: 100,

      socket: {
        host: 'localhost',
        port: 6379
      }
  })
  ],
  controllers: [AppController]
})
export class AppModule {}

More storages: https://www.npmjs.com/package/cache-manager

Decorators

@CacheMethod()

This decorator can use for controller or endpoint alone

It should be used on any endpoint you want to cache (if it is not used globally on controller)

@CacheMethod()
@Controller()
export class DataController {
  @Get()
  async getData(): Promise<string[]> {
    return data;
  }

All GET methods of controller wil be cached

For one endpoint:


@Controller()
export class DataController {
  @CacheMethod()
  @Get()
  async getData(): Promise<string[]> {
    return data;
  }

@SetCacheKey()

This decorator sets a specific cache key for endpoint

By default, the key is generated from the URL (including query parameters)

  @SetCacheKey('key')
  @Get()
  async getData(): Promise<string[]> {
    return data;
  }
  @SetCacheKey((context: ExecutionContext) => {
    return context.switchToHttp().getRequest().headers.authorization
  })
  @Get()
  async getData(): Promise<string[]> {
    return data;
  }

@SetCacheTTL()

This decorator sets a specific TTL for endpoint

By default is 0

  @SetCacheTTL(10)
  @Get()
  async getData(): Promise<string[]> {
    return data;
  }
  @SetCacheTTL((context: ExecutionContext) => {
  const headers = context.switchToHttp().getRequest().headers;

  if (headers.flag !== undefined) {
    return 5;
  }

  return 0;
})

@SetCond()

This decorator determines will be endpoint cached or not It will be cached if the condition is true

  @SetCond((context) => {
    return context.switchToHttp().getRequest().headers.authorization !== undefined
  })
  @Get()
  async getData(): Promise<string[]> {
    return data;
  }

@Exclude()

This decorator can be used when you don't need to cache one or more endpoints

Example:

@CacheMethod()
@Controller()
export class AppController {

  @Get('/foo')
  async foo(): Promise<string[]> {
    return data;
  }

  @Exclude()
  @Get('/bar')
  async bar(): Promise<string[]> {
    return data;
  }

}

/bar endpoint will not be cached

@Exclude() decorator is literally short entry for

  @SetCond(() => {
    return false;
})

Own logics

if you need work with cache-manager, you can:

constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
Note:
import { CACHE_MANAGER } from 'nestjs-zirus-cache';
import { Cache } from 'cache-manager';