-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (103 loc) · 2.82 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const NodeCache = require('node-cache');
const Cache = require('./cache');
const Lock = require('./lock');
const debug = require('debug');
const md5 = require('./md5');
class Cacheable {
constructor(loader, ttl) {
if (typeof loader !== 'function') throw new Error('Cacheable need first parameter to be a function');
if (ttl === undefined) ttl = 30;
if (typeof ttl !== 'number') throw new Error('Cacheable need second parameter to be a number');
this.loader = loader;
this.ttl = ttl || 30;
this.cache = new Cache(new NodeCache({
stdTTL: ttl * 2 //default ttl of node-cache
}));
this.debug = debug(`cacheable`);
this.timeouts = {};
this.lock = new Lock(this.cache);
}
/**
* generate a cache key used for redis
* support object key
*/
getKey(key) {
if (typeof key !== 'string' && typeof key !== 'number') {
key = md5(JSON.stringify(key));
}
return String(key);
}
/**
* load data from cache or loader
*/
async load(origKey, ...args) {
let key = this.getKey(origKey);
return new Promise(async (done, reject) => {
let did = false;
let v = null;
this.debug(`try to load ${key} from cache`);
try {
v = await this.cache.get(key);
if (v && v.createTime) {
this.debug(`got ${key} from cache`);
done(v.value);
did = true;
} else {
this.debug(`${key} not found in cache`);
}
if (!v || (v && v.createTime && Date.now() - v.createTime > this.ttl * 1000)) {
let { executed } = await this.lock.race(origKey, async () => {
this.debug(`loading ${key} from loader`);
try {
let newData = await this.loader(origKey, ...args);
this.debug(`set ${key} to cache`);
await this.prime(origKey, newData);
if (!did) {
done(newData);
did = true;
}
} catch (err) {
if (typeof err === 'object') err.cacheable = 1;
if (err && err.code) throw err;
if (typeof err !== 'object') err = new Error(err);
err.message = `Cacheable ${key} Error: ${err.message}`;
throw err;
}
}, did);
if (!executed && !did) {
this.load(origKey).then(done).catch(reject);
}
}
} catch (err) {
if (!did) {
reject(err);
} else {
if (typeof err !== 'object') err = new Error(err);
err.message = `Cacheable ${key} Error: ${err.message}`;
console.error(err);
}
}
});
}
//清除缓存
clear(key) {
return this.cache.del(this.getKey(key)).then(r => r * 1);
}
//设置缓存
async prime(origKey, value) {
let key = this.getKey(origKey);
await this.cache.set(key, {
createTime: Date.now(),
value
}, 'EX', this.ttl * 2);
}
}
module.exports = Cacheable;
module.exports.cacheable = function(ttl) {
return function(origFunc) {
let loader = new Cacheable(origFunc, ttl);
return function(...args) {
return loader.load(...args);
};
};
};