npm package: @playding/redis-cacher
Description: A simple interface cacher based on ioredis
Latest version: 1.1.0

# interface-cacher

[![master](https://github.com/yidinghan/interface-cacher/actions/workflows/node.js.yml/badge.svg)](https://github.com/yidinghan/interface-cacher/actions/workflows/node.js.yml)

A simple cacher based on ioredis.

# usage

```sh
npm i @playding/redis-cacher
```

```js
const Cacher = require('@playding/redis-cacher');
const cacher = new Cacher();

const data = await cacher.get({
  key: 'ding',
  executor: async () => {
    // logic
    return 'dingding;
  },
});
```

# changelogs

## 20220913 lru mem cache

```js
const data = await cache.get({
  key: 'ding',
  executor: () => 'dingding',
  // 启用内存缓存
  mem: true,
});
```

有些场景下，缓存数据是静态的。例如首页广告位，在运营配置后一般短时间不会改变，也不会随着入参变化。

在之前的版本中，数据从执行函数中生成后，通过 json stringify 变为 string 放到 redis 中。而后的其他服务实例可以通过固定的 key 从 redis 获取该 string，反过来通过 json parse 解析到实际数据如 object|array。

对于静态数据，此时反序列化成为了最耗时的操作，特别是对于大对象。通过内存二级缓存，减少 json parse，降低 cpu 时间，提速操作。

需要注意的是，该特性是通过增加内存资源消耗来实现，所以如果 mem.max 放的很高，或者 cache obj 很大，会带来比较明显的内存使用增加。

# JSDoc

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

- [constructor](#constructor)
  - [Parameters](#parameters)
- [get](#get)
  - [Parameters](#parameters-1)
  - [Examples](#examples)
- [delete](#delete)
  - [Parameters](#parameters-2)

## constructor

### Parameters

- `payload` &#x20;

  - `payload.redis` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** 用于 redis 的连接

    - `payload.redis.host` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** host ip of redis (optional, default `localhost`)
    - `payload.redis.port` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** port of redis (optional, default `6379`)
    - `payload.redis.db` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** cache db of redis (optional, default `12`)

  - `payload.prefix` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** key 的默认前缀 (optional, default `cache.`)
  - `payload.expire` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** key 的有效期，单位 s (optional, default `5`)
  - `payload.mem` **([object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean))?** 内存缓存配置，传 false 表示不启用。全部参数可以看这个[文档说明](https://github.com/isaacs/node-lru-cache#usage)

    - `payload.mem.minRedisTtl` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 最小可放内存的 redis 过期时间阈值 ms。默认 1000ms，redis.ttl 结果小于 1000ms 的就不会放到内存。0 代表有效 ttl 会全放。
    - `payload.mem.max` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 内存缓存 keys 数量上限

## get

使用 redis 为接口加缓存

### Parameters

- `payload` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**&#x20;

  - `payload.key` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 要查找的 key
  - `payload.executor` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** 如果未击中，要执行的方法
  - `payload.expire` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 失效时间, 单位 s
  - `payload.raw` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 是否不用 decode/encode 数据 (optional, default `false`)
  - `payload.mem` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 是否对当前 key 启用内存缓存，默认不启用 (optional, default `false`)

### Examples

```javascript
说明：以给getShops接口加缓存为例
要点：executor为一个返回bluebird 的promise
getShops接口如下：
const getShops = (type) => {
  if (type === 0) {
    return Promise.reject(new Error('bad params'));
  }
  return Promise.resolve(['shop01', 'shop02']);
};

使用方式：
const Cacher = require('interface-cacher');

const cacher = new Cacher();

const payload = {
  key: 'getShops',
  executor: getShops.bind(null, 1),
  expire: 100
};

cacher.get(payload)
 .then((data) => {
   // process the data
 })
 .catch((err) => {
   // handle the exception when encounter with error
 });

const data = await cacher.get({
  key: 'getShopes'
  executor: getShops.bind(null, 1),
  // 启用内存缓存，如果内存命中自己返回内存结果
  // 如果内存没有，就会获取 redis 结果，解析后放到内存中
  mem: true
}
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)>** 缓存中数据(击中) 或 executor 返回数据(未击中)

## delete

删除指定缓存

### Parameters

- `key` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 要删除 key

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>** n 删除的 key 的数量, 同 ioredis.del