Skip to content

sdvcrx/promise-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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