-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (44 loc) · 971 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
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const NodeCache = require('node-cache');
function NodeCachePromiseAll() {
const self = this;
const cache = new NodeCache(...arguments);
function wrap(func) {
return function () {
const args = Array.from(arguments);
return new Promise(function(resolve, reject) {
func(...args, function(error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
}
const functionsToWrap = [
'set',
'get',
'mget',
'del',
'mdel',
'ttl',
'getTtl',
'keys',
'on'
];
const nonWrappedFunctions = [
'getStats',
'flushAll',
'close'
];
functionsToWrap.forEach(functionName => {
self[functionName] = wrap(cache[functionName]);
});
nonWrappedFunctions.forEach(functionName => {
self[functionName] = cache[functionName];
});
return self;
}
module.exports = NodeCachePromiseAll;