2.0.4 • Published 3 years ago

promise-cache-memorize v2.0.4

Weekly downloads
1
License
MIT
Repository
-
Last release
3 years ago

promise-cache-memorize

Memoize promise-returning functions.

Build Status npm package

Usage

const Cache = require('promise-cache-memorize')('redis')

const requestWithCache = Cache.remember('request', request)

cacheFactory(type, options) -> Cache

Factory function that configuring and returning actual Cache object.

  • type - cache backend. Current support simple , lru or redis

  • options - cache backend configuration.

    • timeout - (default: 3600) cache expire timeout, 1 hour by default

Cache.remember(key, fn, maxAge) -> memorizedFn

Memorize promise-returning functions fn with key.

  • fn - function that returning promise.
  • key - string using as cache key.
  • maxAge - (default: options.timeout) cache expired after maxAge seconds.

Example

const request = require('request-promise')
const Cache = require('promise-cache-memorize')('redis')

const requestWithCache = Cache.remember('request', request)

function sendRequest () {
  return requestWithCache('http://httpbin.org/get')
}

sendRequest().then((res) => {
  console.log(res)

  // hit cache
  sendRequest().then((res) => {
    console.log(res)
  })
})

TODO

  • Provide options for cache type
  • Multiple cache backend support

License

MIT

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

6 years ago

2.0.0

6 years ago

1.1.0

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago