-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
42 lines (35 loc) · 837 Bytes
/
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
module.exports = function create(opts) {
opts = opts || {};
var ttl = opts.ttl || 1000 * 60 * 5;
var interval = opts.interval || 1000 * 60;
var cache = {};
var cacheInterval = setInterval(function () {
for (var key in cache) {
if (cache[key].expire <= Date.now()) {
delete cache[key];
}
}
}, interval);
return {
get: function get(key) {
var item = cache[key];
if (!item || item.expire <= Date.now()) {
return null;
}
return item.value;
},
set: function set(key, value) {
return cache[key] = {
expire: Date.now() + opts.ttl,
value: value,
};
},
get length() {
return Object.keys(cache).length;
},
destroy: function destroy() {
cache = {};
clearInterval(cacheInterval);
}
};
};