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

cache-on-function

v0.0.1

Published

A simple redis cache library to functions

Downloads

2

Readme

cache-on-function

Cache on function is a simple library to implement caching over a function easily and without changes to the original function.

Install

$ npm install cache-on-function

Usage

Then create a cache-on-function instance passing redis and cache options:

const { CacheOnFunction } = require('cache-on-function')
const cacheEngine = new CacheOnFunction({
  host: 'localhost',
  port: 6379,
  connect_timeout: 3600000,
  retry_strategy: (options) => {
    return 2000
  },
},{
    cache_default: true,
    expire_time: 6000,
    key_prefix: 'cache-on-function',
})
create cache over async function

Create asynchronous function with you implementation;

The only requirement is that the function only takes one argument, it is necessary to pass an object if it needs multiple arguments.

const myAsyncFunction = ({arga,argb})=>{
  return new Promise((resolve,reject)=> {
    resolve('Resolved')
  })
}

Creates a layer surrounding the function

const myFunctionWithCache = cacheEngine.async(myAsyncFunction)

Call a function with cache the same way called original function

await myFunctionWithCache({arga:'Argment a',argb: 'Argument b'})

If you define default cache as True, the simple call will usage cache by default, but is possible call function without cache

await myFunctionWithCache.nocache({arga:'Argment a',argb: 'Argument b'})

If you define default cache as False, the simple call will not use cache by default, but is possible call function with cache

await myFunctionWithCache.withcache({arga:'Argment a',argb: 'Argument b'})
create cache over sync function

Create synchronous function with you implementation

The function only takes one argument, it is necessary to pass an object if it needs multiple arguments. The function with cache will return a promise, then need resolve promise to receive value

const mySyncFunction = ({arga,argb})=>{
  return 'My sync response'
}

Creates a layer surrounding the function

const myFunctionWithCache = cacheEngine.sync(mySyncFunction)

If you define default cache as True, simple call will return cache function as promise, else return original function synchronous

await myFunctionWithCache({arga:'Argment a',argb: 'Argument b'})

If you define default cache as True, the simple call will usage cache by default, but is possible call function without cache

Note that in this case the original function will be called so it will return the synchronous response

myFunctionWithCache.nocache({arga:'Argment a',argb: 'Argument b'})

If you define default cache as False, the simple call will not use cache by default, but is possible call function with cache

await myFunctionWithCache.withcache({arga:'Argment a',argb: 'Argument b'})
create cache over callback function

Create callback function with you implementation

The function only takes one argument, it is necessary to pass an object if it needs multiple arguments. The function cache and no cache will response using callback.

const myCbFunction = ({arga,argb},callback = (err,data)=>{})=>{
  callback(null,'My sync response')
}

Creates a layer surrounding the function

const myCbFunctionWithCache = cacheEngine.callback(myCbFunction)

Call a function with cache the same way called a original function

myCbFunctionWithCache({arga:'Argment a',argb: 'Argument b'},(err,data)=>{
  if (err) console.log('Error',err)
  else console.log('Response',data)
})

If you define default cache as True, the simple call will usage cache by default, but is possible call function without cache

myCbFunctionWithCache.nocache({arga:'Argment a',argb: 'Argument b'},(err,data)=>{
  if (err) console.log('Error',err)
  else console.log('Response',data)
})

If you define default cache as False, the simple call will not use cache by default, but is possible call function with cache

myCbFunctionWithCache.withcache({arga:'Argment a',argb: 'Argument b'},(err,data)=>{
  if (err) console.log('Error',err)
  else console.log('Response',data)
})
Invalidate function cache

By default caches will expire after time, but you can invalidate using a combination with function name and parameters, predefined key on surrounding function or using patterns

Invalidate using name function and paramters

Use the original name of function before surrounding cache

await cacheEngine.invalidateCache('mySyncFunction',{arga:'Argment a',argb: 'Argument b'})

Invalidate using key predefined on surrounding function

On surrounding function define key

Note you can use arguments name to composite a cache key, use 'mey_custom_key_{{name_of_argument}}'

const myFunctionWithCache = cacheEngine.async(myAsyncFunction, {key: 'mey_custom_key'})

Then invalidate cache using custom key

await cacheEngine.invalidateCache('mey_custom_key')

Invalidate using patters

This way can be used for function original name or custom key

await cacheEngine.invalidateCache('mySync*',{},true)

options object properties

The cache instance accepts argument options :

  • new CacheOnRedis(redisOptions[,options])

| Property | Default | Description | | ------------ | ------- | -------------------------------------------------------------- | | cache_default| true | Define if default response will usage cache or not | | key_prefix | cache | Cache key prefix, every cache storage will contain this prefix | | expire_time | 3600 | Cache expiration time in seconds |

The cache mathods accepts argument options:

The same options is valid to async, sync and callback methods

  • cacheEngine.async(myFunction[,options])

| Property | Default | Description | | ------------ | ------- | -------------------------------------------------------------- | | key | | Define a custom key to use on redis cache key and to invalidate| | expireAs | 3600 | Cache expiration time in seconds |