1.0.1 • Published 6 years ago

promise-cache-store v1.0.1

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

promise-cache

Cache store for Promise execution

pipeline status coverage report

Mainly designed as store for cache request.

npm install promise-cache-store

Example:

  const cache = new CacheStore([] , 60)
  cache.set('request', () => axios.get('http://my.url'))
  const response = await cache.get('request')
  const secondResponse = await cache.get('request')

Get request will be called only once, get function will return result of initial call.

Constructor

Create cache store with passed options.

ParamValueDescriptionRequired
storeArray<{key: string, action: () => Promise}>Array of predefined actionsfalse
cacheTimeNumberValid age of cached valuefalse

set

Add new key to cache.

ParamValueDescriptionRequired
keystringkey value for new cachetrue
options.action() => Promisefunction returning promisetrue
options.updateArraystringKeys to mark for update. Marked key will be forced to update on next call.false

get

Get data from key.

ParamValueDescriptionRequired
keystringkey value for cachetrue
options.refreshbooleanForce call to refresh cachefalse

Examples

For more examples check test folder.

Update array: On call you are able to mark another keys for update. On next call marked keys will update result.

var testCache = new PromiseCache([
  {
    key: 'key_2',
    action: () => Promise.resolve('1'),
    data: '2'
  },
  {
    key: 'key_3',
    action: () => Promise.resolve(),
    updateArray: ['key_2']
  }
])
const result1 = await testCache.get('key_2')
/* result1 = '2' */
await testCache.get('key_3')
const result2 = await testCache.get('key_2')
/* result2 = '1' */