1.0.1 • Published 6 years ago

p-cache v1.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

p-cache

Decorator to memoize the results of async functions via lru-cache.

NPM Build Status JavaScript Style Guide

Features

  • Uses lru-cache for caching impl
  • Uses object-hash for implicit cache key derivation from function args
  • Thoroughly tested in production

Install

npm install --save p-cache

Usage

const request = require('request-promise-native')

const pCache = require('p-cache')({
  label: 'test-service',
  // pass args to lru-cache here
  max: 1000
})

// returns an async function which gets the given url, with the results being 
// cached via lru-cache up to 1000 urls
module.exports = pCache(async (url) => {
  return request(url)
})

API

function pCache(opts)

Returns a function decorator that will wrap calls to the target function in an lru-cache instance defined by these opts.

  • opts - object, optional
  • opts.label - string, optional label to use for displaying cache hits and misses
  • opts.log - function, optional function to use for displaying cache hits and misses (default: console.log)

Note all unrecognized options are passed onto lru-cache

  • opts.max - number, optional passed onto lru-cache
  • opts.maxAge - number, optional passed onto lru-cache
  • opts.length - function, optional passed onto lru-cache
  • opts.dispose - function, optional passed onto lru-cache
  • opts.stale - boolean, optional passed onto lru-cache
  • opts.noDisposeOnSet - boolean, optional passed onto lru-cache

pCache(opts) => Function<Promise>(async function(...args))

License

MIT © Travis Fischer