interface-cacher
A simple interface cacher based on ioredis.
JSDoc
Table of Contents
constructor
Parameters
-
payload
get
使用redis为接口加缓存
Parameters
-
payload
Object
Examples
说明:以给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
};
cache.get(payload)
.then((data) => {
// process the data
})
.catch((err) => {
// handle the exception when encounter with error
});
Returns Promise<Object> 缓存中数据(击中) 或executor返回数据(未击中)
delete
删除指定缓存
Parameters
-
key
string 要删除key