Skip to content

Repository files navigation

cache-manager-function

semantic-releaseXO code styleSnyk SecurityCodeQLOpenSSF Scorecard

Cache functions dynamically based on their arguments using cache-manager.

Installation

Install the package using npm:

npm install cache-manager-function

or with Yarn:

yarn add cache-manager-function

Usage

1. Initialize

Call getOrInitializeCache to manually initialize the cache.

constcache=awaitgetOrInitializeCache<RedisStore>({store: awaitredisStore({host: 'localhost',port: 6379,}),});

Alternatively, you can initialize the cache implicitly by providing the store directly to cachedFunction.

constmultiply=(x,y)=>x*y;constcachedMultiply=cachedFunction(multiply,{store: awaitredisStore({host: 'localhost',port: 6379,}),});// Initializes the cache and caches the resultawaitcachedMultiply(2,3);

2. Caching with cachedFunction

The selector option specifies which arguments should be used to generate the cache key.

typePerson={name: string;lastname: string};constgreet=(person: Person)=>`Hello, ${person.name}${person.lastname}!`;// Caches based on `person.name` and `person.lastname`constcachedGreet=cachedFunction(greet,{selector: ['0.name','0.lastname']});awaitcachedGreet({person: {name: `John`,lastname: `Doe`}});// Caches the result based on name=John and lastname=DoeawaitcachedGreet({person: {name: `Jane`,lastname: `Doe`}});// Caches the result based on name=Jane and lastname=Doe

3. Using the CacheOptions decorator

CacheOptions receives the exact same options that cachedFunction receives. It’s an alternative way to define the cache behavior directly on the function.

import{CacheOptions,cachedFunction}from`cache-manager-function`;classUserService{
@CacheOptions(['0'],300)// Specifies to cache based on the first argument (id), with a TTL of 300msasyncgetUser(id: number){return`User with ID: ${id}`;}}constservice=newUserService();constcachedFetchUser=cachedFunction(service.getUser);awaituserService.getUser(1);// Executes and caches based on the `id` argumentawaituserService.getUser(1);// Returns the cached result

API

getOrInitializeCache

Initializes or retrieves the cache.

  • Parameters:

    • options (Optional): Configuration options to initialize the cache.
  • Returns: A promise that resolves to the cache instance.

cachedFunction

Caches the result of a function based on its arguments.

  • Parameters:

    • function_: The function to cache.
    • options (Optional): Configuration options for caching.
  • Returns: A cached function that returns a promise with the result.

CacheOptions

Decorator to cache function results based on selected arguments.

  • Parameters:
    • selector: Paths of the arguments to include in the cache key.
    • ttl (Optional): Time-to-live for the cached result.

About

Cache functions dynamically based on their arguments using cache-manager.

Topics

Resources

Security policy

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages