promise-cache-memorize

2.0.4 • Public • Published

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.

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

  • [x] Provide options for cache type
  • [x] Multiple cache backend support

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i promise-cache-memorize

Weekly Downloads

2

Version

2.0.4

License

MIT

Unpacked Size

13.7 kB

Total Files

18

Last publish

Collaborators

  • sdvcrx