forked from dabroek/node-cache-manager-ioredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (125 loc) · 3.62 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const Redis = require('ioredis');
const isObject = (value) => value instanceof Object && value.constructor === Object;
const redisStore = (...args) => {
let redisCache = null;
if (args.length > 0 && args[0].redisInstance) {
redisCache = args[0].redisInstance;
} else if (args.length > 0 && args[0].clusterConfig) {
const {
nodes,
options
} = args[0].clusterConfig;
redisCache = new Redis.Cluster(nodes, options || {});
} else {
redisCache = new Redis(...args);
}
const storeArgs = redisCache.options;
let self = {
name: 'redis',
isCacheableValue: storeArgs.isCacheableValue || (value => value !== undefined && value !== null),
};
self.getClient = () => redisCache;
self.set = (key, value, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
if (!self.isCacheableValue(value)) {
return cb(new Error(`"${value}" is not a cacheable value`));
}
const ttl = (options.ttl || options.ttl === 0) ? options.ttl : storeArgs.ttl;
const val = JSON.stringify(value) || '"undefined"';
if (ttl) {
redisCache.setex(key, ttl, val, handleResponse(cb));
} else {
redisCache.set(key, val, handleResponse(cb));
}
})
);
self.get = (key, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
}
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
redisCache.get(key, handleResponse(cb, { parse: true }));
})
);
self.mget = function () {
var args = Array.prototype.slice.apply(arguments);
var cb;
var options = {};
if (typeof args[args.length - 1] === 'function') {
cb = args.pop();
}
if (isObject(args[args.length - 1])) {
options = args.pop();
}
if (Array.isArray(args[0])) {
args = args[0];
}
return new Promise((resolve, reject) => {
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
redisCache.mget(args, handleResponse(cb, { parse: true }));
});
}
self.del = (key, options, cb) => (
new Promise((resolve, reject) => {
if (typeof options === 'function') {
cb = options;
}
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
redisCache.del(key, handleResponse(cb));
})
);
self.reset = cb => redisCache.flushdb(handleResponse(cb));
self.keys = (pattern, cb) => (
new Promise((resolve, reject) => {
if (typeof pattern === 'function') {
cb = pattern;
pattern = '*';
}
if (!cb) {
cb = (err, result) => (err ? reject(err) : resolve(result));
}
redisCache.keys(pattern, handleResponse(cb));
})
);
self.ttl = (key, cb) => redisCache.ttl(key, handleResponse(cb));
return self;
};
function handleResponse(cb, opts = {}) {
return (err, result) => {
if (err) {
return cb && cb(err);
}
if (opts.parse) {
const isMultiple = Array.isArray(result);
if (!isMultiple) {
result = [result];
}
try {
result = result.map((_result) => JSON.parse(_result));
} catch (e) {
return cb && cb(e);
}
result = isMultiple ? result : result[0];
}
return cb && cb(null, result);
};
}
const methods = {
create: (...args) => redisStore(...args),
};
module.exports = methods;