# make-cacheable

> Decorates functions to make them cacheable with Catbox

Latest version **2.0.2** (published 2019-05-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install make-cacheable
pnpm add make-cacheable
yarn add make-cacheable
bun add make-cacheable
```

## 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 | 2.0.2 |
| Published | 2019-05-23 |
| First published | 2016-11-14 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 94.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Rubén Norte |
| Maintainers | softonic |

## Links

- npm: https://www.npmjs.com/package/make-cacheable
- Repository: https://github.com/softonic/make-cacheable
- Issues: https://github.com/softonic/make-cacheable/issues
- npm.io page: https://npm.io/package/make-cacheable

## Dependencies (3)

- [ms](https://npm.io/package/ms.md) ^2.1.1
- [object-hash](https://npm.io/package/object-hash.md) ^1.3.1
- [@hapi/catbox](https://npm.io/package/@hapi/catbox.md) ^10.2.1

## Recent versions

- 2.0.2 (latest) — 2019-05-23
- 2.0.1 — 2018-12-05
- 2.0.0 — 2018-12-05
- 1.3.0 — 2017-05-19
- 1.2.0 — 2017-04-24
- 1.1.0 — 2017-04-20
- 1.0.0 — 2016-11-14

## README

# make-cacheable

Decorates functions to cache their results in a given [catbox](https://github.com/hapijs/catbox) cache client.

## Installation

```bash
npm install make-cacheable
```

## Usage

```js
// CommonJS
// const makeCacheable = require('make-cacheable');

// ES6
import makeCacheable from 'make-cacheable/es';

import catbox from 'catbox';

// See https://github.com/hapijs/catbox#client
const cacheClient = new catbox.Client(/* ... */);

function hardToComputeFunction(param1, param2) {
  /* ... */
}

const cachedFunction = makeCacheable(hardToComputeFunction, {
  cacheClient,
  segment: 'hard-to-compute-function', // Unique name within the cache client
  key: (param1, param2) => {
    return `unique-cache-key-for-the-received-params-${param1}-${param2}`;
  },
  ttl: '5h', // TTL in miliseconds or in the 'ms' package duration format

  // Optional
  ttlRandomFactor: 0.5, // TTL = ttl +- ttl * ttlRandomFactor
  dropIf: (param1, param2) => {
    return param1 === 'always refresh cache for this value';
  },
  onMiss: (...args) => { console.log('MISS', args); },
  onHit: (...args) => { console.log('HIT', args); },
  onDrop: (...args) => { console.log('DROP', args); },
});

(async () => {
  let result = await cachedFunction(param1, param2);
  // Cached!
  result = await cachedFunction(param1, param2);

  // Cache values on demand
  await cachedFunction.setCached(['param1', 'param2'], 42);
})();
```

## Testing

Clone the repository and execute:

```bash
npm test
```

## Contribute

1. Fork it: `git clone https://github.com/softonic/make-cacheable.git`
2. Create your feature branch: `git checkout -b feature/my-new-feature`
3. Commit your changes: `git commit -am 'Added some feature'`
4. Check the build: `npm run build`
5. Push to the branch: `git push origin my-new-feature`
6. Submit a pull request :D

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