Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/transitive-bullshit/p-cache
Decorator to memoize the results of async functions via lru-cache.
https://github.com/transitive-bullshit/p-cache
async cache memoize memoize-decorator promise
Last synced: 3 months ago
JSON representation
Decorator to memoize the results of async functions via lru-cache.
- Host: GitHub
- URL: https://github.com/transitive-bullshit/p-cache
- Owner: transitive-bullshit
- Created: 2017-07-27T23:43:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-07-11T23:12:59.000Z (over 4 years ago)
- Last Synced: 2024-10-20T01:09:09.928Z (4 months ago)
- Topics: async, cache, memoize, memoize-decorator, promise
- Language: JavaScript
- Size: 37.1 KB
- Stars: 23
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# p-cache
> Decorator to memoize the results of async functions via lru-cache.
[![NPM](https://img.shields.io/npm/v/p-cache.svg)](https://www.npmjs.com/package/p-cache) [![Build Status](https://travis-ci.com/transitive-bullshit/p-cache.svg?branch=master)](https://travis-ci.com/transitive-bullshit/p-cache) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
## Features
- Uses [lru-cache](https://www.npmjs.com/package/lru-cache) for caching impl
- Uses [object-hash](https://www.npmjs.com/package/hash-object) for implicit cache key derivation from function args
- Thoroughly tested in production## Install
```bash
npm install --save p-cache
```## Usage
```js
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](https://github.com/isaacs/node-lru-cache)
- `opts.max` - number, optional passed onto [lru-cache](https://github.com/isaacs/node-lru-cache)
- `opts.maxAge` - number, optional passed onto [lru-cache](https://github.com/isaacs/node-lru-cache)
- `opts.length` - function, optional passed onto [lru-cache](https://github.com/isaacs/node-lru-cache)
- `opts.dispose` - function, optional passed onto [lru-cache](https://github.com/isaacs/node-lru-cache)
- `opts.stale` - boolean, optional passed onto [lru-cache](https://github.com/isaacs/node-lru-cache)
- `opts.noDisposeOnSet` - boolean, optional passed onto [lru-cache](https://github.com/isaacs/node-lru-cache)`pCache(opts) => Function(async function(...args))`
## License
MIT © [Travis Fischer](https://github.com/fisch0920)
Support my OSS work by following me on twitter