# p-cache

> Async function wrapper to cache results via lru-cache

Latest version **1.0.1** (published 2018-03-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-cache
pnpm add p-cache
yarn add p-cache
bun add p-cache
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-03-14 |
| First published | 2017-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 2 |
| Unpacked size | 4.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | Travis Fischer |
| Maintainers | fisch0920 |

## Links

- npm: https://www.npmjs.com/package/p-cache
- Repository: https://github.com/fisch0920/p-cache
- npm.io page: https://npm.io/package/p-cache

## Dependencies (2)

- [lru-cache](https://npm.io/package/lru-cache.md) ^4.1.1
- [object-hash](https://npm.io/package/object-hash.md) ^1.1.8

## Recent versions

- 1.0.1 (latest) — 2018-03-14
- 1.0.0 — 2017-07-27

## 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.org/transitive-bullshit/p-cache.svg?branch=master)](https://travis-ci.org/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<Promise>(async function(...args))`

## License

MIT © [Travis Fischer](https://github.com/fisch0920)

---
_Source: https://npm.io/package/p-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
