-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (72 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* 利用 redis 实现cache
* @param {object} redis ioredis 实例
* @param {object} _ lodash 库
* @class
* @return {Cache} Instance
*/
function Cache(redis, _) {
/**
* 获取缓存的值
* @memberof Cache
* @instance
*
* @param {string} key 缓存的 key
*
* @return {Anyone} 缓存的值
*/
const get = key => redis.get(key);
/**
* 设置一个cache
* @memberof Cache
* @instance
*
* @param {string} key 缓存的 key
* @param {string} value 缓存的值
* @param {number} life 有效期,单位秒,默认 0 永久有效
*
* @return {Anyone} 缓存的值
*/
const set = (key, value, life = 0) => {
if (life === 0) return redis.set(key, value);
return redis.setex(key, life, value);
};
/**
* 删除缓存
* @memberof Cache
* @instance
*
* @param {string} key 缓存的 key
*
* @return {number} 删除的条目数
*/
const del = key => redis.del(key);
/**
* caching 让某个函数具有cache的能力
* @memberof Cache
* @instance
*
* @param {function} fn 要加工的函数
* @param {number} life 缓存有效期,单位秒
* @param {function} getKey 计算缓存key的函数
*
* @return {async function} 具有了cache能力的函数
*/
const caching = (fn, life, getKey) => {
if (!_.isFunction(fn)) throw Error("The first argument must be a function");
if (!_.isNumber(life))
throw Error("The second argument must be a number and great then 0");
if (!_.isFunction(getKey))
throw Error("The third argument must be a function");
return async (...args) => {
const key = getKey(...args);
const data = await redis.get(key);
if (data) return JSON.parse(data);
const res = await fn(...args);
set(key, JSON.stringify(res), life);
return res;
};
};
return { redis, get, set, del, caching };
}
module.exports = Cache;