0.0.1 • Published 2 years ago

cache-on-function v0.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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 'meycustom_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])
PropertyDefaultDescription
cache_defaulttrueDefine if default response will usage cache or not
key_prefixcacheCache key prefix, every cache storage will contain this prefix
expire_time3600Cache 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])
PropertyDefaultDescription
keyDefine a custom key to use on redis cache key and to invalidate
expireAs3600Cache expiration time in seconds