cache-manager 7.2.7 → 7.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +366 -369
- package/dist/index.d.cts +99 -97
- package/dist/index.d.mts +115 -0
- package/dist/index.mjs +352 -0
- package/package.json +12 -20
- package/dist/index.d.ts +0 -113
- package/dist/index.js +0 -349
package/dist/index.cjs
CHANGED
|
@@ -1,380 +1,377 @@
|
|
|
1
|
-
"
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
9
|
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
19
18
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
KeyvAdapter: () => KeyvAdapter,
|
|
34
|
-
createCache: () => createCache
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(index_exports);
|
|
37
|
-
var import_node_events = __toESM(require("events"), 1);
|
|
38
|
-
var import_utils = require("@cacheable/utils");
|
|
39
|
-
var import_keyv = require("keyv");
|
|
40
|
-
|
|
41
|
-
// src/keyv-adapter.ts
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let node_events = require("node:events");
|
|
25
|
+
node_events = __toESM(node_events, 1);
|
|
26
|
+
let _cacheable_utils = require("@cacheable/utils");
|
|
27
|
+
let keyv = require("keyv");
|
|
28
|
+
//#region src/keyv-adapter.ts
|
|
42
29
|
var KeyvAdapter = class {
|
|
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
|
-
/* c8 ignore next 5 */
|
|
84
|
-
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
85
|
-
on(event, listener) {
|
|
86
|
-
this._cache.on?.(event, listener);
|
|
87
|
-
return this;
|
|
88
|
-
}
|
|
89
|
-
async disconnect() {
|
|
90
|
-
await this._cache.disconnect?.();
|
|
91
|
-
}
|
|
30
|
+
opts;
|
|
31
|
+
namespace;
|
|
32
|
+
_cache;
|
|
33
|
+
constructor(store) {
|
|
34
|
+
this._cache = store;
|
|
35
|
+
}
|
|
36
|
+
async get(key) {
|
|
37
|
+
const value = await this._cache.get(key);
|
|
38
|
+
if (value !== void 0 && value !== null) return value;
|
|
39
|
+
}
|
|
40
|
+
async set(key, value, ttl) {
|
|
41
|
+
await this._cache.set(key, value, ttl);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
async delete(key) {
|
|
45
|
+
await this._cache.del(key);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
async clear() {
|
|
49
|
+
return this._cache.reset?.();
|
|
50
|
+
}
|
|
51
|
+
async has(key) {
|
|
52
|
+
if (await this._cache.get(key)) return true;
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
async getMany(keys) {
|
|
56
|
+
return this._cache.mget(...keys).then((values) => values.map((value) => value));
|
|
57
|
+
}
|
|
58
|
+
async deleteMany(key) {
|
|
59
|
+
await this._cache.mdel(...key);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
/* c8 ignore next 5 */
|
|
63
|
+
on(event, listener) {
|
|
64
|
+
this._cache.on?.(event, listener);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
async disconnect() {
|
|
68
|
+
await this._cache.disconnect?.();
|
|
69
|
+
}
|
|
92
70
|
};
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/index.ts
|
|
73
|
+
const _storeLabel = (i) => i === 0 ? "primary" : `secondary:${i - 1}`;
|
|
74
|
+
const createCache = (options) => {
|
|
75
|
+
const eventEmitter = new node_events.default();
|
|
76
|
+
const keyv$1 = new keyv.Keyv();
|
|
77
|
+
keyv$1.serialize = void 0;
|
|
78
|
+
keyv$1.deserialize = void 0;
|
|
79
|
+
const stores = options?.stores?.length ? options.stores : [keyv$1];
|
|
80
|
+
const nonBlocking = options?.nonBlocking ?? false;
|
|
81
|
+
const _cacheId = options?.cacheId ?? Math.random().toString(36).slice(2);
|
|
82
|
+
const get = async (key) => {
|
|
83
|
+
let result;
|
|
84
|
+
if (nonBlocking) try {
|
|
85
|
+
result = await Promise.race(stores.map(async (store) => store.get(key)));
|
|
86
|
+
if (result === void 0) return;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
eventEmitter.emit("get", {
|
|
89
|
+
key,
|
|
90
|
+
error
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else for (let i = 0; i < stores.length; i++) {
|
|
94
|
+
const store = stores[i];
|
|
95
|
+
try {
|
|
96
|
+
const cacheValue = await store.get(key);
|
|
97
|
+
if (cacheValue !== void 0) {
|
|
98
|
+
result = cacheValue;
|
|
99
|
+
eventEmitter.emit("get", {
|
|
100
|
+
key,
|
|
101
|
+
value: result,
|
|
102
|
+
store: _storeLabel(i)
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
} catch (error) {
|
|
107
|
+
eventEmitter.emit("get", {
|
|
108
|
+
key,
|
|
109
|
+
error,
|
|
110
|
+
store: _storeLabel(i)
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
};
|
|
116
|
+
const mget = async (keys) => {
|
|
117
|
+
let result = keys.map(() => void 0);
|
|
118
|
+
if (nonBlocking) try {
|
|
119
|
+
result = await Promise.race(stores.map(async (store) => store.getMany(keys)));
|
|
120
|
+
} catch (error) {
|
|
121
|
+
eventEmitter.emit("mget", {
|
|
122
|
+
keys,
|
|
123
|
+
error
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else for (const store of stores) try {
|
|
127
|
+
const missingValues = result.map((value, index) => value === void 0 ? {
|
|
128
|
+
originalIndex: index,
|
|
129
|
+
key: keys[index]
|
|
130
|
+
} : void 0).filter((v) => v !== void 0);
|
|
131
|
+
if (missingValues.length === 0) break;
|
|
132
|
+
const missingKeys = missingValues.map((v) => v.key);
|
|
133
|
+
const cacheValue = await store.getMany(missingKeys);
|
|
134
|
+
for (const [index, value] of cacheValue.entries()) {
|
|
135
|
+
if (value === void 0) continue;
|
|
136
|
+
const { originalIndex } = missingValues[index];
|
|
137
|
+
result[originalIndex] = value;
|
|
138
|
+
}
|
|
139
|
+
} catch (error) {
|
|
140
|
+
eventEmitter.emit("mget", {
|
|
141
|
+
keys,
|
|
142
|
+
error
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
eventEmitter.emit("mget", {
|
|
146
|
+
keys,
|
|
147
|
+
values: result
|
|
148
|
+
});
|
|
149
|
+
return result;
|
|
150
|
+
};
|
|
151
|
+
const ttl = async (key) => {
|
|
152
|
+
let result;
|
|
153
|
+
if (nonBlocking) try {
|
|
154
|
+
result = await Promise.race(stores.map(async (store) => store.get(key, { raw: true })));
|
|
155
|
+
if (result === void 0) return;
|
|
156
|
+
} catch (error) {
|
|
157
|
+
eventEmitter.emit("ttl", {
|
|
158
|
+
key,
|
|
159
|
+
error
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
else for (const store of stores) try {
|
|
163
|
+
const cacheValue = await store.get(key, { raw: true });
|
|
164
|
+
if (cacheValue !== void 0) {
|
|
165
|
+
result = cacheValue;
|
|
166
|
+
eventEmitter.emit("ttl", {
|
|
167
|
+
key,
|
|
168
|
+
value: result
|
|
169
|
+
});
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
} catch (error) {
|
|
173
|
+
eventEmitter.emit("ttl", {
|
|
174
|
+
key,
|
|
175
|
+
error
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
if (result?.expires) return result.expires;
|
|
179
|
+
};
|
|
180
|
+
const set = async (stores, key, value, ttl) => {
|
|
181
|
+
try {
|
|
182
|
+
const promises = stores.map(async (store, i) => {
|
|
183
|
+
await store.set(key, value, ttl ?? options?.ttl);
|
|
184
|
+
eventEmitter.emit("set", {
|
|
185
|
+
key,
|
|
186
|
+
value,
|
|
187
|
+
store: _storeLabel(i)
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
if (nonBlocking) {
|
|
191
|
+
Promise.all(promises);
|
|
192
|
+
eventEmitter.emit("set", {
|
|
193
|
+
key,
|
|
194
|
+
value
|
|
195
|
+
});
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
198
|
+
await Promise.all(promises);
|
|
199
|
+
eventEmitter.emit("set", {
|
|
200
|
+
key,
|
|
201
|
+
value
|
|
202
|
+
});
|
|
203
|
+
return value;
|
|
204
|
+
} catch (error) {
|
|
205
|
+
eventEmitter.emit("set", {
|
|
206
|
+
key,
|
|
207
|
+
value,
|
|
208
|
+
error
|
|
209
|
+
});
|
|
210
|
+
return Promise.reject(error);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
const mset = async (stores, rawList) => {
|
|
214
|
+
const list = rawList.map(({ key, value, ttl }) => ({
|
|
215
|
+
key,
|
|
216
|
+
value,
|
|
217
|
+
ttl: ttl ?? options?.ttl
|
|
218
|
+
}));
|
|
219
|
+
try {
|
|
220
|
+
const promises = stores.map(async (store) => store.setMany(list));
|
|
221
|
+
if (nonBlocking) {
|
|
222
|
+
Promise.all(promises);
|
|
223
|
+
eventEmitter.emit("mset", { list });
|
|
224
|
+
return list;
|
|
225
|
+
}
|
|
226
|
+
await Promise.all(promises);
|
|
227
|
+
eventEmitter.emit("mset", { list });
|
|
228
|
+
return list;
|
|
229
|
+
} catch (error) {
|
|
230
|
+
/* v8 ignore next -- @preserve */
|
|
231
|
+
eventEmitter.emit("mset", {
|
|
232
|
+
list,
|
|
233
|
+
error
|
|
234
|
+
});
|
|
235
|
+
/* v8 ignore next -- @preserve */
|
|
236
|
+
return Promise.reject(error);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
const del = async (key) => {
|
|
240
|
+
try {
|
|
241
|
+
if (nonBlocking) {
|
|
242
|
+
Promise.all(stores.map(async (store) => store.delete(key)));
|
|
243
|
+
eventEmitter.emit("del", { key });
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
await Promise.all(stores.map(async (store) => store.delete(key)));
|
|
247
|
+
eventEmitter.emit("del", { key });
|
|
248
|
+
return true;
|
|
249
|
+
} catch (error) {
|
|
250
|
+
eventEmitter.emit("del", {
|
|
251
|
+
key,
|
|
252
|
+
error
|
|
253
|
+
});
|
|
254
|
+
return Promise.reject(error);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
const mdel = async (keys) => {
|
|
258
|
+
try {
|
|
259
|
+
const promises = [];
|
|
260
|
+
for (const key of keys) promises.push(...stores.map(async (store) => store.delete(key)));
|
|
261
|
+
if (nonBlocking) {
|
|
262
|
+
Promise.all(promises);
|
|
263
|
+
eventEmitter.emit("mdel", { keys });
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
await Promise.all(promises);
|
|
267
|
+
eventEmitter.emit("mdel", { keys });
|
|
268
|
+
return true;
|
|
269
|
+
} catch (error) {
|
|
270
|
+
/* v8 ignore next -- @preserve */
|
|
271
|
+
eventEmitter.emit("mdel", {
|
|
272
|
+
keys,
|
|
273
|
+
error
|
|
274
|
+
});
|
|
275
|
+
/* v8 ignore next -- @preserve */
|
|
276
|
+
return Promise.reject(error);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
const clear = async () => {
|
|
280
|
+
try {
|
|
281
|
+
if (nonBlocking) {
|
|
282
|
+
Promise.all(stores.map(async (store) => store.clear()));
|
|
283
|
+
eventEmitter.emit("clear");
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
await Promise.all(stores.map(async (store) => store.clear()));
|
|
287
|
+
eventEmitter.emit("clear");
|
|
288
|
+
return true;
|
|
289
|
+
} catch (error) {
|
|
290
|
+
eventEmitter.emit("clear", error);
|
|
291
|
+
return Promise.reject(error);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
const wrap = async (key, fnc, ttlOrOptions, refreshThresholdParameter) => (0, _cacheable_utils.coalesceAsync)(`${_cacheId}::${key}`, async () => {
|
|
295
|
+
let value;
|
|
296
|
+
let rawData;
|
|
297
|
+
let i = 0;
|
|
298
|
+
let remainingTtl;
|
|
299
|
+
const { ttl, refreshThreshold, raw } = (0, _cacheable_utils.isObject)(ttlOrOptions) ? ttlOrOptions : {
|
|
300
|
+
ttl: ttlOrOptions,
|
|
301
|
+
refreshThreshold: refreshThresholdParameter
|
|
302
|
+
};
|
|
303
|
+
const resolveTtl = (result) => (0, _cacheable_utils.runIfFn)(ttl, result) ?? options?.ttl;
|
|
304
|
+
for (; i < stores.length; i++) try {
|
|
305
|
+
const data = await stores[i].get(key, { raw: true });
|
|
306
|
+
if (data !== void 0) {
|
|
307
|
+
value = data.value;
|
|
308
|
+
rawData = data;
|
|
309
|
+
if (typeof data.expires === "number") remainingTtl = Math.max(0, data.expires - Date.now());
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
} catch {}
|
|
313
|
+
if (value === void 0) {
|
|
314
|
+
const result = await fnc();
|
|
315
|
+
const ttl = resolveTtl(result);
|
|
316
|
+
await set(stores, key, result, ttl);
|
|
317
|
+
return raw ? {
|
|
318
|
+
value: result,
|
|
319
|
+
expires: Date.now() + ttl
|
|
320
|
+
} : result;
|
|
321
|
+
}
|
|
322
|
+
const shouldRefresh = (0, _cacheable_utils.lessThan)(remainingTtl, (0, _cacheable_utils.runIfFn)(refreshThreshold, value) ?? options?.refreshThreshold);
|
|
323
|
+
if (shouldRefresh) (0, _cacheable_utils.coalesceAsync)(`+++${_cacheId}__${key}`, fnc).then(async (result) => {
|
|
324
|
+
try {
|
|
325
|
+
await set(options?.refreshAllStores ? stores : stores.slice(0, i + 1), key, result, resolveTtl(result));
|
|
326
|
+
eventEmitter.emit("refresh", {
|
|
327
|
+
key,
|
|
328
|
+
value: result
|
|
329
|
+
});
|
|
330
|
+
} catch (error) {
|
|
331
|
+
eventEmitter.emit("refresh", {
|
|
332
|
+
key,
|
|
333
|
+
value,
|
|
334
|
+
error
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}).catch((error) => {
|
|
338
|
+
eventEmitter.emit("refresh", {
|
|
339
|
+
key,
|
|
340
|
+
value,
|
|
341
|
+
error
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
if (!shouldRefresh && i > 0) await set(stores.slice(0, i), key, value, resolveTtl(value));
|
|
345
|
+
return raw ? rawData : value;
|
|
346
|
+
});
|
|
347
|
+
const on = (event, listener) => eventEmitter.addListener(event, listener);
|
|
348
|
+
/* v8 ignore next -- @preserve */
|
|
349
|
+
const off = (event, listener) => eventEmitter.removeListener(event, listener);
|
|
350
|
+
const disconnect = async () => {
|
|
351
|
+
try {
|
|
352
|
+
await Promise.all(stores.map(async (store) => store.disconnect()));
|
|
353
|
+
} catch (error) {
|
|
354
|
+
return Promise.reject(error);
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
const cacheId = () => _cacheId;
|
|
358
|
+
return {
|
|
359
|
+
get,
|
|
360
|
+
mget,
|
|
361
|
+
ttl,
|
|
362
|
+
set: async (key, value, ttl) => set(stores, key, value, ttl),
|
|
363
|
+
mset: async (list) => mset(stores, list),
|
|
364
|
+
del,
|
|
365
|
+
mdel,
|
|
366
|
+
clear,
|
|
367
|
+
wrap,
|
|
368
|
+
on,
|
|
369
|
+
off,
|
|
370
|
+
disconnect,
|
|
371
|
+
cacheId,
|
|
372
|
+
stores
|
|
373
|
+
};
|
|
374
374
|
};
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
createCache
|
|
379
|
-
});
|
|
380
|
-
/* v8 ignore next -- @preserve */
|
|
375
|
+
//#endregion
|
|
376
|
+
exports.KeyvAdapter = KeyvAdapter;
|
|
377
|
+
exports.createCache = createCache;
|