-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathindex.js
207 lines (192 loc) · 4.98 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {callbackify} from 'node:util';
import {createClient} from 'redis';
export async function redisStore(config) {
const redisCache = createClient(config);
await redisCache.connect();
return buildRedisStoreWithConfig(redisCache, config);
}
const buildRedisStoreWithConfig = (redisCache, config) => {
const isCacheableValue =
config.isCacheableValue || (value => value !== undefined && value !== null);
const set = async (key, value, options) => {
if (!isCacheableValue(value)) {
throw new Error(`"${value}" is not a cacheable value`);
}
const ttl = (options?.ttl || options?.ttl === 0) ? options.ttl : config.ttl;
if (ttl) {
return redisCache.setEx(key, ttl, encodeValue(value));
} else {
return redisCache.set(key, encodeValue(value));
}
};
const get = async (key, options) => {
const val = await redisCache.get(key);
if (val === null) {
return null;
}
return options.parse !== false ? decodeValue(val) : val;
};
const del = async (args) => {
let options = {};
if (isObject(args.at(-1))) {
options = args.pop();
}
return redisCache.del(args);
};
const mset = async (args) => {
let options = {};
if (isObject(args.at(-1))) {
options = args.pop();
}
const ttl = (options.ttl || options.ttl === 0) ? options.ttl : config.ttl;
// Zips even and odd array items into tuples
const items = args
.map((key, index) => {
if (index % 2 !== 0) return null;
const value = args[index + 1];
if (!isCacheableValue(value)) {
throw new Error(`"${value}" is not a cacheable value`);
}
return [key, encodeValue(value)];
})
.filter((key) => key !== null);
if (ttl) {
const multi = redisCache.multi();
for (const kv of items) {
const [key, value] = kv;
multi.setEx(key, ttl, value);
}
return multi.exec();
} else {
return redisCache.mSet(items);
}
};
const mget = async (...args) => {
let options = {};
if (isObject(args.at(-1))) {
options = args.pop();
}
return redisCache
.mGet(args)
.then((res) =>
res.map((val) => {
if (val === null) {
return null;
}
return options.parse !== false ? decodeValue(val) : val;
}),
);
};
const mdel = async (...args) => {
let options = {};
if (isObject(args.at(-1))) {
options = args.pop();
}
if (Array.isArray(args)) {
args = args.flat();
}
return redisCache.del(args);
};
const reset = async () => {
return redisCache.flushDb();
};
const keys = async (pattern) => {
return redisCache.keys(pattern);
};
const ttl = async (key) => {
return redisCache.ttl(key);
};
return {
name: 'redis',
getClient: () => redisCache,
isCacheableValue,
set: (key, value, options, cb) => {
if (typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
if (typeof cb === 'function') {
callbackify(set)(key, value, options, cb);
} else {
return set(key, value, options);
}
},
get: (key, options, cb) => {
if (typeof options === 'function') {
cb = options;
options = {};
}
options = options || {};
if (typeof cb === 'function') {
callbackify(get)(key, options, cb);
} else {
return get(key, options);
}
},
del: (...args) => {
if (typeof args.at(-1) === 'function') {
const cb = args.pop();
callbackify(del)(args, cb);
} else {
return del(args);
}
},
mset: (...args) => {
if (typeof args.at(-1) === 'function') {
const cb = args.pop();
callbackify(mset)(args, cb);
} else {
return mset(args);
}
},
mget: (...args) => {
if (typeof args.at(-1) === 'function') {
const cb = args.pop();
callbackify(() => mget(...args))(cb);
} else {
return mget(...args);
}
},
mdel: (...args) => {
if (typeof args.at(-1) === 'function') {
const cb = args.pop();
callbackify(() => mdel(...args))(cb);
} else {
return mdel(...args);
}
},
reset: (cb) => {
if (typeof cb === 'function') {
callbackify(reset)(cb);
} else {
return reset();
}
},
keys: (pattern = '*', cb) => {
if (typeof cb === 'function') {
callbackify(() => keys(pattern))(cb);
} else {
return keys(pattern);
}
},
ttl: (key, cb) => {
if (typeof cb === 'function') {
callbackify(() => ttl(key))(cb);
} else {
return ttl(key);
}
},
};
};
function encodeValue(value) {
return JSON.stringify(value) || '"undefined"';
}
function decodeValue(val) {
return JSON.parse(val);
}
function isObject(object) {
return typeof object === 'object'
&& !Array.isArray(object)
&& object !== null;
}