Code Monkey home page Code Monkey logo

promise-cache-store's Introduction

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.

Param Value Description Required
store Array<{key: string, action: () => Promise}> Array of predefined actions false
cacheTime Number Valid age of cached value false

set

Add new key to cache.

Param Value Description Required
key string key value for new cache true
options.action () => Promise function returning promise true
options.updateArray [string] Keys to mark for update. Marked key will be forced to update on next call. false

get

Get data from key.

Param Value Description Required
key string key value for cache true
options.refresh boolean Force call to refresh cache false

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' */

promise-cache-store's People

Contributors

mighty683 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.