Skip to content

Latest commit

 

History

History
67 lines (42 loc) · 1.83 KB

README.md

File metadata and controls

67 lines (42 loc) · 1.83 KB

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

  • Provide options for cache type
  • Multiple cache backend support

License

MIT