flat-cache 6.1.22 → 6.1.23
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/dist/index.cjs +466 -476
- package/dist/index.d.cts +215 -214
- package/dist/index.d.mts +256 -0
- package/dist/index.mjs +451 -0
- package/package.json +9 -9
- package/dist/index.d.ts +0 -255
- package/dist/index.js +0 -455
package/dist/index.cjs
CHANGED
|
@@ -1,495 +1,485 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
2
6
|
var __create = Object.create;
|
|
3
7
|
var __defProp = Object.defineProperty;
|
|
4
8
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
9
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
10
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
11
|
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
12
|
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
19
21
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var
|
|
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
|
-
|
|
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
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
* @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed
|
|
420
|
-
* @return {undefined}
|
|
421
|
-
*/
|
|
422
|
-
destroy(includeCacheDirectory = false) {
|
|
423
|
-
try {
|
|
424
|
-
this._cache.clear();
|
|
425
|
-
this.stopAutoPersist();
|
|
426
|
-
if (includeCacheDirectory) {
|
|
427
|
-
import_node_fs.default.rmSync(this.cacheDirPath, { recursive: true, force: true });
|
|
428
|
-
} else {
|
|
429
|
-
import_node_fs.default.rmSync(this.cacheFilePath, { recursive: true, force: true });
|
|
430
|
-
}
|
|
431
|
-
this._changesSinceLastSave = false;
|
|
432
|
-
this.emit("destroy" /* DESTROY */);
|
|
433
|
-
} catch (error) {
|
|
434
|
-
this.emit("error" /* ERROR */, error);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Start the auto persist interval
|
|
439
|
-
* @method startAutoPersist
|
|
440
|
-
*/
|
|
441
|
-
startAutoPersist() {
|
|
442
|
-
if (this._persistInterval > 0) {
|
|
443
|
-
if (this._persistTimer) {
|
|
444
|
-
clearInterval(this._persistTimer);
|
|
445
|
-
this._persistTimer = void 0;
|
|
446
|
-
}
|
|
447
|
-
this._persistTimer = setInterval(() => {
|
|
448
|
-
this.save();
|
|
449
|
-
}, this._persistInterval);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
/**
|
|
453
|
-
* Stop the auto persist interval
|
|
454
|
-
* @method stopAutoPersist
|
|
455
|
-
*/
|
|
456
|
-
stopAutoPersist() {
|
|
457
|
-
if (this._persistTimer) {
|
|
458
|
-
clearInterval(this._persistTimer);
|
|
459
|
-
this._persistTimer = void 0;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
//#endregion
|
|
27
|
+
let node_fs = require("node:fs");
|
|
28
|
+
node_fs = __toESM(node_fs, 1);
|
|
29
|
+
let node_path = require("node:path");
|
|
30
|
+
node_path = __toESM(node_path, 1);
|
|
31
|
+
let cacheable = require("cacheable");
|
|
32
|
+
let flatted = require("flatted");
|
|
33
|
+
let hookified = require("hookified");
|
|
34
|
+
//#region src/index.ts
|
|
35
|
+
let FlatCacheEvents = /* @__PURE__ */ function(FlatCacheEvents) {
|
|
36
|
+
FlatCacheEvents["SAVE"] = "save";
|
|
37
|
+
FlatCacheEvents["LOAD"] = "load";
|
|
38
|
+
FlatCacheEvents["DELETE"] = "delete";
|
|
39
|
+
FlatCacheEvents["CLEAR"] = "clear";
|
|
40
|
+
FlatCacheEvents["DESTROY"] = "destroy";
|
|
41
|
+
FlatCacheEvents["ERROR"] = "error";
|
|
42
|
+
FlatCacheEvents["EXPIRED"] = "expired";
|
|
43
|
+
return FlatCacheEvents;
|
|
44
|
+
}({});
|
|
45
|
+
var FlatCache = class extends hookified.Hookified {
|
|
46
|
+
_cache = new cacheable.CacheableMemory();
|
|
47
|
+
_cacheDir = ".cache";
|
|
48
|
+
_cacheId = "cache1";
|
|
49
|
+
_persistInterval = 0;
|
|
50
|
+
_persistTimer;
|
|
51
|
+
_changesSinceLastSave = false;
|
|
52
|
+
_parse = flatted.parse;
|
|
53
|
+
_stringify = flatted.stringify;
|
|
54
|
+
constructor(options) {
|
|
55
|
+
super();
|
|
56
|
+
if (options) this._cache = new cacheable.CacheableMemory({
|
|
57
|
+
ttl: options.ttl,
|
|
58
|
+
useClone: options.useClone,
|
|
59
|
+
lruSize: options.lruSize,
|
|
60
|
+
checkInterval: options.expirationInterval
|
|
61
|
+
});
|
|
62
|
+
if (options?.cacheDir) this._cacheDir = options.cacheDir;
|
|
63
|
+
if (options?.cacheId) this._cacheId = options.cacheId;
|
|
64
|
+
if (options?.persistInterval) {
|
|
65
|
+
this._persistInterval = options.persistInterval;
|
|
66
|
+
this.startAutoPersist();
|
|
67
|
+
}
|
|
68
|
+
if (options?.deserialize) this._parse = options.deserialize;
|
|
69
|
+
if (options?.serialize) this._stringify = options.serialize;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The cache object
|
|
73
|
+
* @property cache
|
|
74
|
+
* @type {CacheableMemory}
|
|
75
|
+
*/
|
|
76
|
+
get cache() {
|
|
77
|
+
return this._cache;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The cache directory
|
|
81
|
+
* @property cacheDir
|
|
82
|
+
* @type {String}
|
|
83
|
+
* @default '.cache'
|
|
84
|
+
*/
|
|
85
|
+
get cacheDir() {
|
|
86
|
+
return this._cacheDir;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Set the cache directory
|
|
90
|
+
* @property cacheDir
|
|
91
|
+
* @type {String}
|
|
92
|
+
* @default '.cache'
|
|
93
|
+
*/
|
|
94
|
+
set cacheDir(value) {
|
|
95
|
+
this._cacheDir = value;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The cache id
|
|
99
|
+
* @property cacheId
|
|
100
|
+
* @type {String}
|
|
101
|
+
* @default 'cache1'
|
|
102
|
+
*/
|
|
103
|
+
get cacheId() {
|
|
104
|
+
return this._cacheId;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Set the cache id
|
|
108
|
+
* @property cacheId
|
|
109
|
+
* @type {String}
|
|
110
|
+
* @default 'cache1'
|
|
111
|
+
*/
|
|
112
|
+
set cacheId(value) {
|
|
113
|
+
this._cacheId = value;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The flag to indicate if there are changes since the last save
|
|
117
|
+
* @property changesSinceLastSave
|
|
118
|
+
* @type {Boolean}
|
|
119
|
+
* @default false
|
|
120
|
+
*/
|
|
121
|
+
get changesSinceLastSave() {
|
|
122
|
+
return this._changesSinceLastSave;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The interval to persist the cache to disk. 0 means no timed persistence
|
|
126
|
+
* @property persistInterval
|
|
127
|
+
* @type {Number}
|
|
128
|
+
* @default 0
|
|
129
|
+
*/
|
|
130
|
+
get persistInterval() {
|
|
131
|
+
return this._persistInterval;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Set the interval to persist the cache to disk. 0 means no timed persistence
|
|
135
|
+
* @property persistInterval
|
|
136
|
+
* @type {Number}
|
|
137
|
+
* @default 0
|
|
138
|
+
*/
|
|
139
|
+
set persistInterval(value) {
|
|
140
|
+
this._persistInterval = value;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
|
144
|
+
* cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
|
|
145
|
+
* then the cache module directory `.cacheDir` will be used instead
|
|
146
|
+
*
|
|
147
|
+
* @method load
|
|
148
|
+
* @param cacheId {String} the id of the cache, would also be used as the name of the file cache
|
|
149
|
+
* @param cacheDir {String} directory for the cache entry
|
|
150
|
+
*/
|
|
151
|
+
load(cacheId, cacheDir) {
|
|
152
|
+
try {
|
|
153
|
+
const filePath = node_path.default.resolve(`${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}`);
|
|
154
|
+
this.loadFile(filePath);
|
|
155
|
+
this.emit("load");
|
|
156
|
+
} catch (error) {
|
|
157
|
+
/* v8 ignore next -- @preserve */
|
|
158
|
+
this.emit("error", error);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Load the cache from the provided file
|
|
163
|
+
* @method loadFile
|
|
164
|
+
* @param {String} pathToFile the path to the file containing the info for the cache
|
|
165
|
+
*/
|
|
166
|
+
loadFile(pathToFile) {
|
|
167
|
+
if (node_fs.default.existsSync(pathToFile)) {
|
|
168
|
+
const data = node_fs.default.readFileSync(pathToFile, "utf8");
|
|
169
|
+
const items = this._parse(data);
|
|
170
|
+
if (Array.isArray(items)) {
|
|
171
|
+
for (const item of items) if (item && typeof item === "object" && "key" in item) if (item.expires) this._cache.set(item.key, item.value, { expire: item.expires });
|
|
172
|
+
else if (item.timestamp)
|
|
173
|
+
/* v8 ignore next -- @preserve */
|
|
174
|
+
this._cache.set(item.key, item.value, { expire: item.timestamp });
|
|
175
|
+
else this._cache.set(item.key, item.value);
|
|
176
|
+
} else for (const key of Object.keys(items)) {
|
|
177
|
+
const item = items[key];
|
|
178
|
+
/* v8 ignore next -- @preserve */
|
|
179
|
+
if (item && typeof item === "object" && "key" in item) this._cache.set(item.key, item.value, { expire: item.expires });
|
|
180
|
+
else if (item && typeof item === "object" && item.timestamp)
|
|
181
|
+
/* v8 ignore next -- @preserve */
|
|
182
|
+
this._cache.set(key, item, { expire: item.timestamp });
|
|
183
|
+
else this._cache.set(key, item);
|
|
184
|
+
}
|
|
185
|
+
this._changesSinceLastSave = true;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
loadFileStream(pathToFile, onProgress, onEnd, onError) {
|
|
189
|
+
if (node_fs.default.existsSync(pathToFile)) {
|
|
190
|
+
const total = node_fs.default.statSync(pathToFile).size;
|
|
191
|
+
let loaded = 0;
|
|
192
|
+
let streamData = "";
|
|
193
|
+
const readStream = node_fs.default.createReadStream(pathToFile, { encoding: "utf8" });
|
|
194
|
+
readStream.on("data", (chunk) => {
|
|
195
|
+
loaded += chunk.length;
|
|
196
|
+
streamData += chunk;
|
|
197
|
+
onProgress(loaded, total);
|
|
198
|
+
});
|
|
199
|
+
readStream.on("end", () => {
|
|
200
|
+
const items = this._parse(streamData);
|
|
201
|
+
for (const key of Object.keys(items)) this._cache.set(items[key].key, items[key].value, { expire: items[key].expires });
|
|
202
|
+
this._changesSinceLastSave = true;
|
|
203
|
+
onEnd();
|
|
204
|
+
});
|
|
205
|
+
/* v8 ignore next -- @preserve */
|
|
206
|
+
readStream.on("error", (error) => {
|
|
207
|
+
this.emit("error", error);
|
|
208
|
+
if (onError) onError(error);
|
|
209
|
+
});
|
|
210
|
+
} else {
|
|
211
|
+
const error = /* @__PURE__ */ new Error(`Cache file ${pathToFile} does not exist`);
|
|
212
|
+
this.emit("error", error);
|
|
213
|
+
/* v8 ignore next -- @preserve */
|
|
214
|
+
if (onError) onError(error);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Returns the entire persisted object
|
|
219
|
+
* @method all
|
|
220
|
+
* @returns {*}
|
|
221
|
+
*/
|
|
222
|
+
all() {
|
|
223
|
+
const result = {};
|
|
224
|
+
const items = [...this._cache.items];
|
|
225
|
+
for (const item of items) result[item.key] = item.value;
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Returns an array with all the items in the cache { key, value, expires }
|
|
230
|
+
* @method items
|
|
231
|
+
* @returns {Array}
|
|
232
|
+
*/
|
|
233
|
+
get items() {
|
|
234
|
+
return [...this._cache.items];
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Returns the path to the file where the cache is persisted
|
|
238
|
+
* @method cacheFilePath
|
|
239
|
+
* @returns {String}
|
|
240
|
+
*/
|
|
241
|
+
get cacheFilePath() {
|
|
242
|
+
return node_path.default.resolve(`${this._cacheDir}/${this._cacheId}`);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Returns the path to the cache directory
|
|
246
|
+
* @method cacheDirPath
|
|
247
|
+
* @returns {String}
|
|
248
|
+
*/
|
|
249
|
+
get cacheDirPath() {
|
|
250
|
+
return node_path.default.resolve(this._cacheDir);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Returns an array with all the keys in the cache
|
|
254
|
+
* @method keys
|
|
255
|
+
* @returns {Array}
|
|
256
|
+
*/
|
|
257
|
+
keys() {
|
|
258
|
+
return [...this._cache.keys];
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* (Legacy) set key method. This method will be deprecated in the future
|
|
262
|
+
* @method setKey
|
|
263
|
+
* @param key {string} the key to set
|
|
264
|
+
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
265
|
+
*/
|
|
266
|
+
setKey(key, value, ttl) {
|
|
267
|
+
this.set(key, value, ttl);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Sets a key to a given value
|
|
271
|
+
* @method set
|
|
272
|
+
* @param key {string} the key to set
|
|
273
|
+
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
274
|
+
* @param [ttl] {number} the time to live in milliseconds
|
|
275
|
+
*/
|
|
276
|
+
set(key, value, ttl) {
|
|
277
|
+
this._cache.set(key, value, ttl);
|
|
278
|
+
this._changesSinceLastSave = true;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* (Legacy) Remove a given key from the cache. This method will be deprecated in the future
|
|
282
|
+
* @method removeKey
|
|
283
|
+
* @param key {String} the key to remove from the object
|
|
284
|
+
*/
|
|
285
|
+
removeKey(key) {
|
|
286
|
+
this.delete(key);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Remove a given key from the cache
|
|
290
|
+
* @method delete
|
|
291
|
+
* @param key {String} the key to remove from the object
|
|
292
|
+
*/
|
|
293
|
+
delete(key) {
|
|
294
|
+
this._cache.delete(key);
|
|
295
|
+
this._changesSinceLastSave = true;
|
|
296
|
+
this.emit("delete", key);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* (Legacy) Return the value of the provided key. This method will be deprecated in the future
|
|
300
|
+
* @method getKey<T>
|
|
301
|
+
* @param key {String} the name of the key to retrieve
|
|
302
|
+
* @returns {*} at T the value from the key
|
|
303
|
+
*/
|
|
304
|
+
getKey(key) {
|
|
305
|
+
return this.get(key);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Return the value of the provided key
|
|
309
|
+
* @method get<T>
|
|
310
|
+
* @param key {String} the name of the key to retrieve
|
|
311
|
+
* @returns {*} at T the value from the key
|
|
312
|
+
*/
|
|
313
|
+
get(key) {
|
|
314
|
+
return this._cache.get(key);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Clear the cache and save the state to disk
|
|
318
|
+
* @method clear
|
|
319
|
+
*/
|
|
320
|
+
clear() {
|
|
321
|
+
try {
|
|
322
|
+
this._cache.clear();
|
|
323
|
+
this._changesSinceLastSave = true;
|
|
324
|
+
this.save();
|
|
325
|
+
this.emit("clear");
|
|
326
|
+
} catch (error) {
|
|
327
|
+
/* v8 ignore next -- @preserve */
|
|
328
|
+
this.emit("error", error);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Save the state of the cache identified by the docId to disk
|
|
333
|
+
* as a JSON structure
|
|
334
|
+
* @method save
|
|
335
|
+
*/
|
|
336
|
+
save(force = false) {
|
|
337
|
+
try {
|
|
338
|
+
/* v8 ignore next -- @preserve */
|
|
339
|
+
if (this._changesSinceLastSave || force) {
|
|
340
|
+
const filePath = this.cacheFilePath;
|
|
341
|
+
const items = [...this._cache.items];
|
|
342
|
+
const data = this._stringify(items);
|
|
343
|
+
if (!node_fs.default.existsSync(this._cacheDir)) node_fs.default.mkdirSync(this._cacheDir, { recursive: true });
|
|
344
|
+
node_fs.default.writeFileSync(filePath, data);
|
|
345
|
+
this._changesSinceLastSave = false;
|
|
346
|
+
this.emit("save");
|
|
347
|
+
}
|
|
348
|
+
} catch (error) {
|
|
349
|
+
/* v8 ignore next -- @preserve */
|
|
350
|
+
this.emit("error", error);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Remove the file where the cache is persisted
|
|
355
|
+
* @method removeCacheFile
|
|
356
|
+
* @return {Boolean} true or false if the file was successfully deleted
|
|
357
|
+
*/
|
|
358
|
+
removeCacheFile() {
|
|
359
|
+
try {
|
|
360
|
+
if (node_fs.default.existsSync(this.cacheFilePath)) {
|
|
361
|
+
node_fs.default.rmSync(this.cacheFilePath);
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
} catch (error) {
|
|
365
|
+
/* v8 ignore next -- @preserve */
|
|
366
|
+
this.emit("error", error);
|
|
367
|
+
}
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Destroy the cache. This will remove the directory, file, and memory cache
|
|
372
|
+
* @method destroy
|
|
373
|
+
* @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed
|
|
374
|
+
* @return {undefined}
|
|
375
|
+
*/
|
|
376
|
+
destroy(includeCacheDirectory = false) {
|
|
377
|
+
try {
|
|
378
|
+
this._cache.clear();
|
|
379
|
+
this.stopAutoPersist();
|
|
380
|
+
if (includeCacheDirectory) node_fs.default.rmSync(this.cacheDirPath, {
|
|
381
|
+
recursive: true,
|
|
382
|
+
force: true
|
|
383
|
+
});
|
|
384
|
+
else node_fs.default.rmSync(this.cacheFilePath, {
|
|
385
|
+
recursive: true,
|
|
386
|
+
force: true
|
|
387
|
+
});
|
|
388
|
+
this._changesSinceLastSave = false;
|
|
389
|
+
this.emit("destroy");
|
|
390
|
+
} catch (error) {
|
|
391
|
+
/* v8 ignore next -- @preserve */
|
|
392
|
+
this.emit("error", error);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Start the auto persist interval
|
|
397
|
+
* @method startAutoPersist
|
|
398
|
+
*/
|
|
399
|
+
startAutoPersist() {
|
|
400
|
+
/* v8 ignore next -- @preserve */
|
|
401
|
+
if (this._persistInterval > 0) {
|
|
402
|
+
if (this._persistTimer) {
|
|
403
|
+
clearInterval(this._persistTimer);
|
|
404
|
+
this._persistTimer = void 0;
|
|
405
|
+
}
|
|
406
|
+
this._persistTimer = setInterval(() => {
|
|
407
|
+
this.save();
|
|
408
|
+
}, this._persistInterval);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Stop the auto persist interval
|
|
413
|
+
* @method stopAutoPersist
|
|
414
|
+
*/
|
|
415
|
+
stopAutoPersist() {
|
|
416
|
+
if (this._persistTimer) {
|
|
417
|
+
clearInterval(this._persistTimer);
|
|
418
|
+
this._persistTimer = void 0;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
462
421
|
};
|
|
463
422
|
var FlatCacheDefault = class {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
423
|
+
static create = create;
|
|
424
|
+
static createFromFile = createFromFile;
|
|
425
|
+
static clearCacheById = clearCacheById;
|
|
426
|
+
static clearAll = clearAll;
|
|
468
427
|
};
|
|
428
|
+
/**
|
|
429
|
+
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
|
430
|
+
* cache storage.
|
|
431
|
+
*
|
|
432
|
+
* @method create
|
|
433
|
+
* @param docId {String} the id of the cache, would also be used as the name of the file cache
|
|
434
|
+
* @param cacheDirectory {String} directory for the cache entry
|
|
435
|
+
* @param options {FlatCacheOptions} options for the cache
|
|
436
|
+
* @returns {cache} cache instance
|
|
437
|
+
*/
|
|
469
438
|
function create(options) {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
439
|
+
const cache = new FlatCache(options);
|
|
440
|
+
cache.load();
|
|
441
|
+
return cache;
|
|
473
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* Load a cache from the provided file
|
|
445
|
+
* @method createFromFile
|
|
446
|
+
* @param {String} filePath the path to the file containing the info for the cache
|
|
447
|
+
* @param options {FlatCacheOptions} options for the cache
|
|
448
|
+
* @returns {cache} cache instance
|
|
449
|
+
*/
|
|
474
450
|
function createFromFile(filePath, options) {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
451
|
+
const cache = new FlatCache(options);
|
|
452
|
+
cache.loadFile(filePath);
|
|
453
|
+
return cache;
|
|
478
454
|
}
|
|
455
|
+
/**
|
|
456
|
+
* Clear the cache identified by the given Id. This will only remove the cache from disk.
|
|
457
|
+
* @method clearCacheById
|
|
458
|
+
* @param cacheId {String} the id of the cache
|
|
459
|
+
* @param cacheDirectory {String} directory for the cache entry
|
|
460
|
+
*/
|
|
479
461
|
function clearCacheById(cacheId, cacheDirectory) {
|
|
480
|
-
|
|
481
|
-
|
|
462
|
+
new FlatCache({
|
|
463
|
+
cacheId,
|
|
464
|
+
cacheDir: cacheDirectory
|
|
465
|
+
}).destroy();
|
|
482
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* Clear the cache directory
|
|
469
|
+
* @method clearAll
|
|
470
|
+
* @param cacheDir {String} directory for the cache entry
|
|
471
|
+
*/
|
|
483
472
|
function clearAll(cacheDirectory) {
|
|
484
|
-
|
|
473
|
+
node_fs.default.rmSync(cacheDirectory ?? ".cache", {
|
|
474
|
+
recursive: true,
|
|
475
|
+
force: true
|
|
476
|
+
});
|
|
485
477
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
});
|
|
495
|
-
/* v8 ignore next -- @preserve */
|
|
478
|
+
//#endregion
|
|
479
|
+
exports.FlatCache = FlatCache;
|
|
480
|
+
exports.FlatCacheEvents = FlatCacheEvents;
|
|
481
|
+
exports.clearAll = clearAll;
|
|
482
|
+
exports.clearCacheById = clearCacheById;
|
|
483
|
+
exports.create = create;
|
|
484
|
+
exports.createFromFile = createFromFile;
|
|
485
|
+
exports.default = FlatCacheDefault;
|