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

v1.0.4

Published

Nestjs cache decorator

Downloads

91

Readme

Table of Contents

Installation

npm install nestjs-cache-decorator

pnpm install nestjs-cache-decorator

Configuration

Static redis cache configuration

import {Module} from "@nestjs/common";
import {LocalCacheDecoratorModule} from "nestjs-cache-decorator";
import redisStore from "cache-manager-redis-store";

@Module({
    imports: [
        CacheDecoratorModule.register({
            host: "host",
            port: 6379,
            password: "password",
            db: 0,
            store: redisStore,
        }),
    ],
})
export class AppModule {
}

Static local cache configuration

import {Module} from "@nestjs/common";
import {LocalCacheDecoratorModule} from "nestjs-cache-decorator";

@Module({
    imports: [
        CacheDecoratorModule.register({}), // default `store: "memory"`
    ],
})
export class AppModule {
}

Async redis cache configuration

import {Module} from "@nestjs/common";
import {LocalCacheDecoratorModule} from "nestjs-cache-decorator";
import redisStore from "cache-manager-redis-store";

@Module({
    imports: [
        CacheDecoratorModule.registerAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: async (configService: ConfigService): Promise<CacheModuleOptions> => {
                const config = configService.get("redis");
                return {...config, store: redisStore};
            },
        }),
    ],
})
export class AppModule {
}

Usage (Controller)

@APICache({})

The first cache module that is imported will serve as the storage for the controller cache.

For instance, in this scenario, the controller's cache storage would be set to "memory".

@Module({
    imports: [
        CacheDecoratorModule.register({ store: "memory"}),
        CacheDecoratorModule.register({ store : redisStore}),
    ],
})
export class AppModule {

If the key field is empty, the default cache key will be HTTP URL Path where / is replaced to :. If the key field exists, the cache key will be {http url path}:{http url path}:...:{key}.

Use the validate filed function to verify the result of the method.

The logger creates a log when data is cached or when there is a cache hit. When the logger is set to console.log, the output will be as follows:

  • When data is cached : Cached { cacheKey: 'example:redis' }
  • When cache hit : Cache Hit { cacheKey: 'example:redis' }
@Controller("example")
@UseInterceptors(ControllerCacheInterceptor)
export class ExampleController {

    @RedisCache({
        ttl: 60,
        validate: (value: any) => Boolean(value),
        logger: console.log,
    })
    @Get("redis")
    async example() {
    }
}

Usage (Provider)

@RedisCache({}), @LocalCache({})

If the key field is empty, the default cache key will be {className}:{methodName}:{methodArgs}. If the key field exists, the cache key will be {className}:{methodName}:{key}.

Use the validate filed function to verify the result of the method.

The logger creates a log when data is cached or when there is a cache hit. When the logger is set to console.log, the output will be as follows:

  • When data is cached : Cached { cacheKey: 'ExampleService:cacheExample:test' }
  • When cache hit : Cache Hit { cacheKey: 'ExampleService:cacheExample:test' }

Cache by TTL

Caching takes place when the function cacheExample() is called and the result is not already cached.

The cache will expire after the TTL (Time To Live) duration.

@Injectable()
export class ExampleService {

    @RedisCache({
        ttl: 60,        // expire after 60s
        key: "test",   // cache key : `ExampleService:cacheExample:test`
        validate: (value: any) => Boolean(value),
        logger: console.log,
    })
    redisCacheExample() {
    }

    @LocalCache({
        ttl: 60,        // expire after 60s
        key: "test",   // cache key : `ExampleService:cacheExample:test`
        validate: (value: any) => Boolean(value),
        logger: console.log,
    })
    localCacheExample() {
    }
}

Cache by Cron

For the example below, cacheExample() method will be called each time the current second is 45. In other words, the method will be run once per minute, at the 45 second mark.

The cron field supports all standard cron patterns:

  • Asterisk (e.g. *)
  • Ranges (e.g. 1-3,5)
  • Steps (e.g. */2)
@Injectable()
export class ExampleService {

    @RedisCache({
        cron: "45 * * * * *",   // The method is executed every 45 second, and the result is cached.
        key: "test",
        validate: (value: any) => Boolean(value),
        logger: console.log,
    })
    redisCacheExample() {
    }

    @LocalCache({
        cron: "45 * * * * *",   // The method is executed every 45 second, and the result is cached.
        key: "test",
        validate: (value: any) => Boolean(value),
        logger: console.log,
    })
    localCacheExample() {
    }
}

In the example above, we passed 45 * * * * * to the decorator. The following key shows how each position in the cron pattern string is interpreted:

* * * * * *
| | | | | |
| | | | | day of week
| | | | months
| | | day of month
| | hours
| minutes
seconds (optional)

Decorator options

export interface CacheOptions {
    cron?: string;

    key?: string;

    ttl?: number;

    validate?: Function;

    logger?: Function;
}