0.0.2 • Published 4 years ago

cached-call v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Cached call

Cache the function result by wrapping it

JavaScript Style Guide npm package

Installation

npm i cached-call

Basic Usage

const CachedCall = require('cached-call')
const cache = new CachedCall()

cache({ someHeavyJob (v) { return v }, maxAge: 1000 })(v)

Options

const cache = new CachedCall()
cache({ [name]: func, maxAge, max, stale, key })(...args)
namedescriptiondefaultexample
nameFunction name for cache key(required)
funcCache target function(required)
keyKey for create cache key (function or value)(...args) => args1, 'moo'
maxAgeMaximum age in ms(required)300, 100
argsArguments that will be passed to key function(required)
maxThe maximum size of the cacheInfinity1000, 100
stalereturn the stale value before deleting itfalsetrue
cacheErrorCache error and rejection for msundefined1000

Auto generated internal cache key by default

JSON.stringify([name, ...args])

Examples

Custom key function

Use only 3rd, 4th arguments for key

const key = (nan, fn, n1, n2) => [n1, n2]
const pickArgs = cache({ someFunction, key, maxAge: 100 })
pickArgs(NaN, () => {}, 1, 2) // cache key : ["someFunction", 1 ,2]

Clear cache at 10 or 20 minutes for every hour

const dayjs = require('dayjs')
const everyHour = min => () => dayjs().add(60 - min, 'm').startOf('m').minute(min) - Date.now()
const every10minutes = cache({ someFunction, maxAge: everyHour(10) })
const every20minutes = cache({ someFunction, maxAge: everyHour(20) })

Retry after 1s when error

const retryThrottle = cache({ someFunction, maxAge: 30000, cacheError: 1000 })
retryThrottle()

License

The MIT License (MIT)
Copyright (c) 2020 Elevista

0.0.2

4 years ago

0.0.1

4 years ago