# co-cache

> Cache result in redis for AsyncFunction.

Latest version **4.1.0** (published 2024-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install co-cache
pnpm add co-cache
yarn add co-cache
bun add co-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 | 4.1.0 |
| Published | 2024-06-05 |
| First published | 2015-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Maintainers | nswbmw |
| Keywords | co, cache, Promise, AsyncFunction, async, await |

## Links

- npm: https://www.npmjs.com/package/co-cache
- Repository: https://github.com/nswbmw/co-cache
- Homepage: https://github.com/nswbmw/co-cache#readme
- Issues: https://github.com/nswbmw/co-cache/issues
- npm.io page: https://npm.io/package/co-cache

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) 4.3.4
- [ioredis](https://npm.io/package/ioredis.md) 5.4.1

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2024-06-05
- 4.0.0 — 2024-05-10
- 3.1.0 — 2022-01-28
- 3.0.0 — 2019-08-03
- 2.5.0 — 2018-01-11
- 2.4.1 — 2017-04-12
- 2.4.0 — 2017-04-12
- 2.3.1 — 2017-04-12
- 2.3.0 — 2017-01-03
- 2.2.0 — 2016-04-15
- 2.1.1 — 2016-02-23
- 2.1.0 — 2016-01-18
- 2.0.1 — 2015-09-11
- 1.2.0 — 2015-09-11
- 1.1.1 — 2015-09-10
- … 8 more at https://npm.io/package/co-cache/versions

## README

## co-cache

Cache result in redis for AsyncFunction.

### Install

```bash
$ npm i co-cache --save
```

### Usage

```js
const cache = require('co-cache')(defaultConfig);
cache(fn[, options]) => AsyncFunction
```

defaultConfig {Object}:  
options {Object|Number->expire}:

- client: {Object} redis client of [ioredis](https://github.com/luin/ioredis).
- prefix: {String} prefix for redis cache, default `''`.
- key: {String|Function|AsyncFunction} prefix + key == cacheKey, default `fn.name`, if return `false`, skip get&set cache.
- expire: {Number} expire in ms.
- get: {Function|AsyncFunction} function to get cache.
- set: {Function|AsyncFunction} function to set cache.
- redisOpt: {Object} others options see [ioredis](https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options)

### Example

```sh
$ DEBUG=co-cache node example.js
```

```js
process.env.DEBUG = 'co-cache'

const cache = require('.')({
  prefix: 'cache:',
  expire: 10 * 60 * 1000 // default expire
})

;(async function () {
  const someAsyncFn = cache(async function someAsyncFn (number) {
    console.log(`someAsyncFn: ${number}`)
    return number
  }, {
    key: function (number) {
      if (number >= 4) {
        return false // only cache when number < 4
      }
      return this.name + ':' + number
    }
  })

  await someAsyncFn(1)
  await someAsyncFn(2)
  await someAsyncFn(2) // get from cache

  await someAsyncFn.get(3)
  await someAsyncFn.set(3, 'some value') // manually set cache
  await someAsyncFn.get(3) // get from cache

  await someAsyncFn(4) // not cache
  await someAsyncFn.raw(5) // skip cache

  await someAsyncFn.clear(1) // clear cache
  await someAsyncFn.clear(2) // clear cache
  await someAsyncFn.clear(3) // clear cache
  await someAsyncFn.clear(4) // no effect, because there is no cache
})().catch(console.error)
```

### Default get/set

```js
function defaultGet (redis, cacheKey) {
  return redis
    .get(cacheKey)
    .then((result) => {
      if (result != null) {
        return JSON.parse(result)
      }
    })
    .catch(() => {})
}

function defaultSet (redis, cacheKey, result, ms) {
  // cannot save `undefined`` value, `null` is ok
  if (result === undefined) {
    return
  }
  return redis
    .set(cacheKey, JSON.stringify(result), 'PX', ms)
    .catch(() => {})
}
```

### Test

```
$ npm run test
```

### License

MIT

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