cacheable 1.10.2 → 1.10.4
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 +184 -141
- package/dist/index.d.cts +50 -50
- package/dist/index.d.ts +50 -50
- package/dist/index.js +185 -147
- package/package.json +12 -13
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
|
-
import { KeyvStoreAdapter, StoredData, Keyv, StoredDataRaw } from 'keyv';
|
|
2
|
-
export { Keyv, KeyvHooks, KeyvOptions, KeyvStoreAdapter } from 'keyv';
|
|
3
1
|
import { Hookified } from 'hookified';
|
|
2
|
+
import { Keyv, KeyvStoreAdapter, StoredData, StoredDataRaw } from 'keyv';
|
|
3
|
+
export { Keyv, KeyvHooks, KeyvOptions, KeyvStoreAdapter } from 'keyv';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* CacheableItem
|
|
7
|
+
* @typedef {Object} CacheableItem
|
|
8
|
+
* @property {string} key - The key of the cacheable item
|
|
9
|
+
* @property {any} value - The value of the cacheable item
|
|
10
|
+
* @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
|
|
11
|
+
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
|
|
12
|
+
* undefined then it will not have a time-to-live.
|
|
13
|
+
*/
|
|
14
|
+
type CacheableItem = {
|
|
15
|
+
key: string;
|
|
16
|
+
value: any;
|
|
17
|
+
ttl?: number | string;
|
|
18
|
+
};
|
|
19
|
+
type CacheableStoreItem = {
|
|
20
|
+
key: string;
|
|
21
|
+
value: any;
|
|
22
|
+
expires?: number;
|
|
23
|
+
};
|
|
4
24
|
|
|
5
25
|
type CacheableOptions$1 = {
|
|
6
26
|
enabled?: boolean;
|
|
@@ -89,26 +109,6 @@ declare class CacheableStats {
|
|
|
89
109
|
resetStoreValues(): void;
|
|
90
110
|
}
|
|
91
111
|
|
|
92
|
-
/**
|
|
93
|
-
* CacheableItem
|
|
94
|
-
* @typedef {Object} CacheableItem
|
|
95
|
-
* @property {string} key - The key of the cacheable item
|
|
96
|
-
* @property {any} value - The value of the cacheable item
|
|
97
|
-
* @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
|
|
98
|
-
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
|
|
99
|
-
* undefined then it will not have a time-to-live.
|
|
100
|
-
*/
|
|
101
|
-
type CacheableItem = {
|
|
102
|
-
key: string;
|
|
103
|
-
value: any;
|
|
104
|
-
ttl?: number | string;
|
|
105
|
-
};
|
|
106
|
-
type CacheableStoreItem = {
|
|
107
|
-
key: string;
|
|
108
|
-
value: any;
|
|
109
|
-
expires?: number;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
112
|
type GetOrSetKey = string | ((options?: GetOrSetOptions) => string);
|
|
113
113
|
type GetOrSetFunctionOptions = {
|
|
114
114
|
ttl?: number | string;
|
|
@@ -144,7 +144,7 @@ declare enum StoreHashAlgorithm {
|
|
|
144
144
|
MD5 = "md5",
|
|
145
145
|
djb2Hash = "djb2Hash"
|
|
146
146
|
}
|
|
147
|
-
type StoreHashAlgorithmFunction = (
|
|
147
|
+
type StoreHashAlgorithmFunction = (key: string, storeHashSize: number) => number;
|
|
148
148
|
/**
|
|
149
149
|
* @typedef {Object} CacheableMemoryOptions
|
|
150
150
|
* @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
|
|
@@ -623,19 +623,19 @@ declare class Cacheable extends Hookified {
|
|
|
623
623
|
isKeyvInstance(keyv: any): boolean;
|
|
624
624
|
getNameSpace(): string | undefined;
|
|
625
625
|
/**
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
626
|
+
* Retrieves an entry from the cache, with an optional “raw” mode.
|
|
627
|
+
*
|
|
628
|
+
* Checks the primary store first; if not found and a secondary store is configured,
|
|
629
|
+
* it will fetch from the secondary, repopulate the primary, and return the result.
|
|
630
|
+
*
|
|
631
|
+
* @typeParam T - The expected type of the stored value.
|
|
632
|
+
* @param {string} key - The cache key to retrieve.
|
|
633
|
+
* @param {{ raw?: boolean }} [opts] - Options for retrieval.
|
|
634
|
+
* @param {boolean} [opts.raw=false] - If `true`, returns the full raw data object
|
|
635
|
+
* (`StoredDataRaw<T>`); otherwise returns just the value.
|
|
636
|
+
* @returns {Promise<T | StoredDataRaw<T> | undefined>}
|
|
637
|
+
* A promise that resolves to the cached value (or raw data) if found, or `undefined`.
|
|
638
|
+
*/
|
|
639
639
|
get<T>(key: string, options?: {
|
|
640
640
|
raw?: false;
|
|
641
641
|
}): Promise<T | undefined>;
|
|
@@ -643,20 +643,20 @@ declare class Cacheable extends Hookified {
|
|
|
643
643
|
raw: true;
|
|
644
644
|
}): Promise<StoredDataRaw<T>>;
|
|
645
645
|
/**
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
646
|
+
* Retrieves multiple entries from the cache.
|
|
647
|
+
* Checks the primary store for each key; if a key is missing and a secondary store is configured,
|
|
648
|
+
* it will fetch from the secondary store, repopulate the primary store, and return the results.
|
|
649
|
+
*
|
|
650
|
+
* @typeParam T - The expected type of the stored values.
|
|
651
|
+
* @param {string[]} keys - The cache keys to retrieve.
|
|
652
|
+
* @param {{ raw?: boolean }} [options] - Options for retrieval.
|
|
653
|
+
* @param {boolean} [options.raw=false] - When `true`, returns an array of raw data objects (`StoredDataRaw<T>`);
|
|
654
|
+
* when `false`, returns an array of unwrapped values (`T`) or `undefined` for misses.
|
|
655
|
+
* @returns {Promise<Array<T | undefined>> | Promise<Array<StoredDataRaw<T>>>}
|
|
656
|
+
* A promise that resolves to:
|
|
657
|
+
* - `Array<T | undefined>` if `raw` is `false` (default).
|
|
658
|
+
* - `Array<StoredDataRaw<T>>` if `raw` is `true`.
|
|
659
|
+
*/
|
|
660
660
|
getMany<T>(keys: string[], options?: {
|
|
661
661
|
raw?: false;
|
|
662
662
|
}): Promise<Array<T | undefined>>;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,116 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { Keyv as Keyv2 } from "keyv";
|
|
3
2
|
import { Hookified as Hookified2 } from "hookified";
|
|
3
|
+
import { Keyv as Keyv2 } from "keyv";
|
|
4
|
+
|
|
5
|
+
// src/hash.ts
|
|
6
|
+
import * as crypto from "crypto";
|
|
7
|
+
function hash(object, algorithm = "sha256") {
|
|
8
|
+
const objectString = JSON.stringify(object);
|
|
9
|
+
if (!crypto.getHashes().includes(algorithm)) {
|
|
10
|
+
throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
|
|
11
|
+
}
|
|
12
|
+
const hasher = crypto.createHash(algorithm);
|
|
13
|
+
hasher.update(objectString);
|
|
14
|
+
return hasher.digest("hex");
|
|
15
|
+
}
|
|
16
|
+
function hashToNumber(object, min = 0, max = 10, algorithm = "sha256") {
|
|
17
|
+
const objectString = JSON.stringify(object);
|
|
18
|
+
if (!crypto.getHashes().includes(algorithm)) {
|
|
19
|
+
throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
|
|
20
|
+
}
|
|
21
|
+
const hasher = crypto.createHash(algorithm);
|
|
22
|
+
hasher.update(objectString);
|
|
23
|
+
const hashHex = hasher.digest("hex");
|
|
24
|
+
const hashNumber = Number.parseInt(hashHex, 16);
|
|
25
|
+
const range = max - min + 1;
|
|
26
|
+
return min + hashNumber % range;
|
|
27
|
+
}
|
|
28
|
+
function djb2Hash(string_, min = 0, max = 10) {
|
|
29
|
+
let hash2 = 5381;
|
|
30
|
+
for (let i = 0; i < string_.length; i++) {
|
|
31
|
+
hash2 = hash2 * 33 ^ string_.charCodeAt(i);
|
|
32
|
+
}
|
|
33
|
+
const range = max - min + 1;
|
|
34
|
+
return min + Math.abs(hash2) % range;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/keyv-memory.ts
|
|
38
|
+
import { Keyv } from "keyv";
|
|
39
|
+
|
|
40
|
+
// src/memory.ts
|
|
41
|
+
import { Hookified } from "hookified";
|
|
42
|
+
|
|
43
|
+
// src/memory-lru.ts
|
|
44
|
+
var ListNode = class {
|
|
45
|
+
value;
|
|
46
|
+
prev = void 0;
|
|
47
|
+
next = void 0;
|
|
48
|
+
constructor(value) {
|
|
49
|
+
this.value = value;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var DoublyLinkedList = class {
|
|
53
|
+
head = void 0;
|
|
54
|
+
tail = void 0;
|
|
55
|
+
nodesMap = /* @__PURE__ */ new Map();
|
|
56
|
+
// Add a new node to the front (most recently used)
|
|
57
|
+
addToFront(value) {
|
|
58
|
+
const newNode = new ListNode(value);
|
|
59
|
+
if (this.head) {
|
|
60
|
+
newNode.next = this.head;
|
|
61
|
+
this.head.prev = newNode;
|
|
62
|
+
this.head = newNode;
|
|
63
|
+
} else {
|
|
64
|
+
this.head = this.tail = newNode;
|
|
65
|
+
}
|
|
66
|
+
this.nodesMap.set(value, newNode);
|
|
67
|
+
}
|
|
68
|
+
// Move an existing node to the front (most recently used)
|
|
69
|
+
moveToFront(value) {
|
|
70
|
+
const node = this.nodesMap.get(value);
|
|
71
|
+
if (!node || this.head === node) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (node.prev) {
|
|
75
|
+
node.prev.next = node.next;
|
|
76
|
+
}
|
|
77
|
+
if (node.next) {
|
|
78
|
+
node.next.prev = node.prev;
|
|
79
|
+
}
|
|
80
|
+
if (node === this.tail) {
|
|
81
|
+
this.tail = node.prev;
|
|
82
|
+
}
|
|
83
|
+
node.prev = void 0;
|
|
84
|
+
node.next = this.head;
|
|
85
|
+
if (this.head) {
|
|
86
|
+
this.head.prev = node;
|
|
87
|
+
}
|
|
88
|
+
this.head = node;
|
|
89
|
+
this.tail ??= node;
|
|
90
|
+
}
|
|
91
|
+
// Get the oldest node (tail)
|
|
92
|
+
getOldest() {
|
|
93
|
+
return this.tail ? this.tail.value : void 0;
|
|
94
|
+
}
|
|
95
|
+
// Remove the oldest node (tail)
|
|
96
|
+
removeOldest() {
|
|
97
|
+
if (!this.tail) {
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
const oldValue = this.tail.value;
|
|
101
|
+
if (this.tail.prev) {
|
|
102
|
+
this.tail = this.tail.prev;
|
|
103
|
+
this.tail.next = void 0;
|
|
104
|
+
} else {
|
|
105
|
+
this.head = this.tail = void 0;
|
|
106
|
+
}
|
|
107
|
+
this.nodesMap.delete(oldValue);
|
|
108
|
+
return oldValue;
|
|
109
|
+
}
|
|
110
|
+
get size() {
|
|
111
|
+
return this.nodesMap.size;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
4
114
|
|
|
5
115
|
// src/shorthand-time.ts
|
|
6
116
|
var shorthandToMilliseconds = (shorthand) => {
|
|
@@ -15,7 +125,9 @@ var shorthandToMilliseconds = (shorthand) => {
|
|
|
15
125
|
if (Number.isNaN(Number(shorthand))) {
|
|
16
126
|
const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand);
|
|
17
127
|
if (!match) {
|
|
18
|
-
throw new Error(
|
|
128
|
+
throw new Error(
|
|
129
|
+
`Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`
|
|
130
|
+
);
|
|
19
131
|
}
|
|
20
132
|
const [, value, unit] = match;
|
|
21
133
|
const numericValue = Number.parseFloat(value);
|
|
@@ -67,46 +179,6 @@ var shorthandToTime = (shorthand, fromDate) => {
|
|
|
67
179
|
return fromDate.getTime() + milliseconds;
|
|
68
180
|
};
|
|
69
181
|
|
|
70
|
-
// src/keyv-memory.ts
|
|
71
|
-
import {
|
|
72
|
-
Keyv
|
|
73
|
-
} from "keyv";
|
|
74
|
-
|
|
75
|
-
// src/memory.ts
|
|
76
|
-
import { Hookified } from "hookified";
|
|
77
|
-
|
|
78
|
-
// src/hash.ts
|
|
79
|
-
import * as crypto from "crypto";
|
|
80
|
-
function hash(object, algorithm = "sha256") {
|
|
81
|
-
const objectString = JSON.stringify(object);
|
|
82
|
-
if (!crypto.getHashes().includes(algorithm)) {
|
|
83
|
-
throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
|
|
84
|
-
}
|
|
85
|
-
const hasher = crypto.createHash(algorithm);
|
|
86
|
-
hasher.update(objectString);
|
|
87
|
-
return hasher.digest("hex");
|
|
88
|
-
}
|
|
89
|
-
function hashToNumber(object, min = 0, max = 10, algorithm = "sha256") {
|
|
90
|
-
const objectString = JSON.stringify(object);
|
|
91
|
-
if (!crypto.getHashes().includes(algorithm)) {
|
|
92
|
-
throw new Error(`Unsupported hash algorithm: '${algorithm}'`);
|
|
93
|
-
}
|
|
94
|
-
const hasher = crypto.createHash(algorithm);
|
|
95
|
-
hasher.update(objectString);
|
|
96
|
-
const hashHex = hasher.digest("hex");
|
|
97
|
-
const hashNumber = Number.parseInt(hashHex, 16);
|
|
98
|
-
const range = max - min + 1;
|
|
99
|
-
return min + hashNumber % range;
|
|
100
|
-
}
|
|
101
|
-
function djb2Hash(string_, min = 0, max = 10) {
|
|
102
|
-
let hash2 = 5381;
|
|
103
|
-
for (let i = 0; i < string_.length; i++) {
|
|
104
|
-
hash2 = hash2 * 33 ^ string_.charCodeAt(i);
|
|
105
|
-
}
|
|
106
|
-
const range = max - min + 1;
|
|
107
|
-
return min + Math.abs(hash2) % range;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
182
|
// src/coalesce-async.ts
|
|
111
183
|
var callbacks = /* @__PURE__ */ new Map();
|
|
112
184
|
function hasKey(key) {
|
|
@@ -165,7 +237,7 @@ async function coalesceAsync(key, fnc) {
|
|
|
165
237
|
// src/wrap.ts
|
|
166
238
|
function wrapSync(function_, options) {
|
|
167
239
|
const { ttl, keyPrefix, cache } = options;
|
|
168
|
-
return
|
|
240
|
+
return (...arguments_) => {
|
|
169
241
|
let cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
170
242
|
if (options.createKey) {
|
|
171
243
|
cacheKey = options.createKey(function_, arguments_, options);
|
|
@@ -211,12 +283,16 @@ async function getOrSet(key, function_, options) {
|
|
|
211
283
|
}
|
|
212
284
|
function wrap(function_, options) {
|
|
213
285
|
const { keyPrefix, cache } = options;
|
|
214
|
-
return async
|
|
286
|
+
return async (...arguments_) => {
|
|
215
287
|
let cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
216
288
|
if (options.createKey) {
|
|
217
289
|
cacheKey = options.createKey(function_, arguments_, options);
|
|
218
290
|
}
|
|
219
|
-
return cache.getOrSet(
|
|
291
|
+
return cache.getOrSet(
|
|
292
|
+
cacheKey,
|
|
293
|
+
async () => function_(...arguments_),
|
|
294
|
+
options
|
|
295
|
+
);
|
|
220
296
|
};
|
|
221
297
|
}
|
|
222
298
|
function createWrapKey(function_, arguments_, keyPrefix) {
|
|
@@ -226,79 +302,6 @@ function createWrapKey(function_, arguments_, keyPrefix) {
|
|
|
226
302
|
return `${keyPrefix}::${function_.name}::${hash(arguments_)}`;
|
|
227
303
|
}
|
|
228
304
|
|
|
229
|
-
// src/memory-lru.ts
|
|
230
|
-
var ListNode = class {
|
|
231
|
-
// eslint-disable-next-line @typescript-eslint/parameter-properties
|
|
232
|
-
value;
|
|
233
|
-
prev = void 0;
|
|
234
|
-
next = void 0;
|
|
235
|
-
constructor(value) {
|
|
236
|
-
this.value = value;
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
var DoublyLinkedList = class {
|
|
240
|
-
head = void 0;
|
|
241
|
-
tail = void 0;
|
|
242
|
-
nodesMap = /* @__PURE__ */ new Map();
|
|
243
|
-
// Add a new node to the front (most recently used)
|
|
244
|
-
addToFront(value) {
|
|
245
|
-
const newNode = new ListNode(value);
|
|
246
|
-
if (this.head) {
|
|
247
|
-
newNode.next = this.head;
|
|
248
|
-
this.head.prev = newNode;
|
|
249
|
-
this.head = newNode;
|
|
250
|
-
} else {
|
|
251
|
-
this.head = this.tail = newNode;
|
|
252
|
-
}
|
|
253
|
-
this.nodesMap.set(value, newNode);
|
|
254
|
-
}
|
|
255
|
-
// Move an existing node to the front (most recently used)
|
|
256
|
-
moveToFront(value) {
|
|
257
|
-
const node = this.nodesMap.get(value);
|
|
258
|
-
if (!node || this.head === node) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
if (node.prev) {
|
|
262
|
-
node.prev.next = node.next;
|
|
263
|
-
}
|
|
264
|
-
if (node.next) {
|
|
265
|
-
node.next.prev = node.prev;
|
|
266
|
-
}
|
|
267
|
-
if (node === this.tail) {
|
|
268
|
-
this.tail = node.prev;
|
|
269
|
-
}
|
|
270
|
-
node.prev = void 0;
|
|
271
|
-
node.next = this.head;
|
|
272
|
-
if (this.head) {
|
|
273
|
-
this.head.prev = node;
|
|
274
|
-
}
|
|
275
|
-
this.head = node;
|
|
276
|
-
this.tail ??= node;
|
|
277
|
-
}
|
|
278
|
-
// Get the oldest node (tail)
|
|
279
|
-
getOldest() {
|
|
280
|
-
return this.tail ? this.tail.value : void 0;
|
|
281
|
-
}
|
|
282
|
-
// Remove the oldest node (tail)
|
|
283
|
-
removeOldest() {
|
|
284
|
-
if (!this.tail) {
|
|
285
|
-
return void 0;
|
|
286
|
-
}
|
|
287
|
-
const oldValue = this.tail.value;
|
|
288
|
-
if (this.tail.prev) {
|
|
289
|
-
this.tail = this.tail.prev;
|
|
290
|
-
this.tail.next = void 0;
|
|
291
|
-
} else {
|
|
292
|
-
this.head = this.tail = void 0;
|
|
293
|
-
}
|
|
294
|
-
this.nodesMap.delete(oldValue);
|
|
295
|
-
return oldValue;
|
|
296
|
-
}
|
|
297
|
-
get size() {
|
|
298
|
-
return this.nodesMap.size;
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
|
|
302
305
|
// src/memory.ts
|
|
303
306
|
var defaultStoreHashSize = 16;
|
|
304
307
|
var maximumMapSize = 16777216;
|
|
@@ -307,7 +310,10 @@ var CacheableMemory = class extends Hookified {
|
|
|
307
310
|
_storeHashSize = defaultStoreHashSize;
|
|
308
311
|
_storeHashAlgorithm = "djb2Hash" /* djb2Hash */;
|
|
309
312
|
// Default is djb2Hash
|
|
310
|
-
_store = Array.from(
|
|
313
|
+
_store = Array.from(
|
|
314
|
+
{ length: this._storeHashSize },
|
|
315
|
+
() => /* @__PURE__ */ new Map()
|
|
316
|
+
);
|
|
311
317
|
_ttl;
|
|
312
318
|
// Turned off by default
|
|
313
319
|
_useClone = true;
|
|
@@ -335,7 +341,12 @@ var CacheableMemory = class extends Hookified {
|
|
|
335
341
|
}
|
|
336
342
|
if (options?.lruSize) {
|
|
337
343
|
if (options.lruSize > maximumMapSize) {
|
|
338
|
-
this.emit(
|
|
344
|
+
this.emit(
|
|
345
|
+
"error",
|
|
346
|
+
new Error(
|
|
347
|
+
`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
|
|
348
|
+
)
|
|
349
|
+
);
|
|
339
350
|
} else {
|
|
340
351
|
this._lruSize = options.lruSize;
|
|
341
352
|
}
|
|
@@ -346,7 +357,10 @@ var CacheableMemory = class extends Hookified {
|
|
|
346
357
|
if (options?.storeHashAlgorithm) {
|
|
347
358
|
this._storeHashAlgorithm = options.storeHashAlgorithm;
|
|
348
359
|
}
|
|
349
|
-
this._store = Array.from(
|
|
360
|
+
this._store = Array.from(
|
|
361
|
+
{ length: this._storeHashSize },
|
|
362
|
+
() => /* @__PURE__ */ new Map()
|
|
363
|
+
);
|
|
350
364
|
this.startIntervalCheck();
|
|
351
365
|
}
|
|
352
366
|
/**
|
|
@@ -390,7 +404,12 @@ var CacheableMemory = class extends Hookified {
|
|
|
390
404
|
*/
|
|
391
405
|
set lruSize(value) {
|
|
392
406
|
if (value > maximumMapSize) {
|
|
393
|
-
this.emit(
|
|
407
|
+
this.emit(
|
|
408
|
+
"error",
|
|
409
|
+
new Error(
|
|
410
|
+
`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
|
|
411
|
+
)
|
|
412
|
+
);
|
|
394
413
|
return;
|
|
395
414
|
}
|
|
396
415
|
this._lruSize = value;
|
|
@@ -441,7 +460,10 @@ var CacheableMemory = class extends Hookified {
|
|
|
441
460
|
return;
|
|
442
461
|
}
|
|
443
462
|
this._storeHashSize = value;
|
|
444
|
-
this._store = Array.from(
|
|
463
|
+
this._store = Array.from(
|
|
464
|
+
{ length: this._storeHashSize },
|
|
465
|
+
() => /* @__PURE__ */ new Map()
|
|
466
|
+
);
|
|
445
467
|
}
|
|
446
468
|
/**
|
|
447
469
|
* Gets the store hash algorithm
|
|
@@ -462,7 +484,7 @@ var CacheableMemory = class extends Hookified {
|
|
|
462
484
|
* @returns {IterableIterator<string>} - The keys
|
|
463
485
|
*/
|
|
464
486
|
get keys() {
|
|
465
|
-
const keys =
|
|
487
|
+
const keys = [];
|
|
466
488
|
for (const store of this._store) {
|
|
467
489
|
for (const key of store.keys()) {
|
|
468
490
|
const item = store.get(key);
|
|
@@ -480,7 +502,7 @@ var CacheableMemory = class extends Hookified {
|
|
|
480
502
|
* @returns {IterableIterator<CacheableStoreItem>} - The items
|
|
481
503
|
*/
|
|
482
504
|
get items() {
|
|
483
|
-
const items =
|
|
505
|
+
const items = [];
|
|
484
506
|
for (const store of this._store) {
|
|
485
507
|
for (const item of store.values()) {
|
|
486
508
|
if (this.hasExpired(item)) {
|
|
@@ -526,7 +548,7 @@ var CacheableMemory = class extends Hookified {
|
|
|
526
548
|
* @returns {T[]} - The values of the keys
|
|
527
549
|
*/
|
|
528
550
|
getMany(keys) {
|
|
529
|
-
const result =
|
|
551
|
+
const result = [];
|
|
530
552
|
for (const key of keys) {
|
|
531
553
|
result.push(this.get(key));
|
|
532
554
|
}
|
|
@@ -556,7 +578,7 @@ var CacheableMemory = class extends Hookified {
|
|
|
556
578
|
* @returns {CacheableStoreItem[]} - The raw values of the keys
|
|
557
579
|
*/
|
|
558
580
|
getManyRaw(keys) {
|
|
559
|
-
const result =
|
|
581
|
+
const result = [];
|
|
560
582
|
for (const key of keys) {
|
|
561
583
|
result.push(this.getRaw(key));
|
|
562
584
|
}
|
|
@@ -607,10 +629,7 @@ var CacheableMemory = class extends Hookified {
|
|
|
607
629
|
}
|
|
608
630
|
}
|
|
609
631
|
const item = { key, value, expires };
|
|
610
|
-
store.set(
|
|
611
|
-
key,
|
|
612
|
-
item
|
|
613
|
-
);
|
|
632
|
+
store.set(key, item);
|
|
614
633
|
}
|
|
615
634
|
/**
|
|
616
635
|
* Sets the values of the keys
|
|
@@ -637,7 +656,7 @@ var CacheableMemory = class extends Hookified {
|
|
|
637
656
|
* @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
|
|
638
657
|
*/
|
|
639
658
|
hasMany(keys) {
|
|
640
|
-
const result =
|
|
659
|
+
const result = [];
|
|
641
660
|
for (const key of keys) {
|
|
642
661
|
const item = this.get(key);
|
|
643
662
|
result.push(Boolean(item));
|
|
@@ -663,7 +682,7 @@ var CacheableMemory = class extends Hookified {
|
|
|
663
682
|
* @returns {T[]} - The values of the keys
|
|
664
683
|
*/
|
|
665
684
|
takeMany(keys) {
|
|
666
|
-
const result =
|
|
685
|
+
const result = [];
|
|
667
686
|
for (const key of keys) {
|
|
668
687
|
result.push(this.take(key));
|
|
669
688
|
}
|
|
@@ -693,7 +712,10 @@ var CacheableMemory = class extends Hookified {
|
|
|
693
712
|
* @returns {void}
|
|
694
713
|
*/
|
|
695
714
|
clear() {
|
|
696
|
-
this._store = Array.from(
|
|
715
|
+
this._store = Array.from(
|
|
716
|
+
{ length: this._storeHashSize },
|
|
717
|
+
() => /* @__PURE__ */ new Map()
|
|
718
|
+
);
|
|
697
719
|
this._lru = new DoublyLinkedList();
|
|
698
720
|
}
|
|
699
721
|
/**
|
|
@@ -1065,28 +1087,24 @@ var CacheableStats = class {
|
|
|
1065
1087
|
}
|
|
1066
1088
|
this._clears++;
|
|
1067
1089
|
}
|
|
1068
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1069
1090
|
incrementVSize(value) {
|
|
1070
1091
|
if (!this._enabled) {
|
|
1071
1092
|
return;
|
|
1072
1093
|
}
|
|
1073
1094
|
this._vsize += this.roughSizeOfObject(value);
|
|
1074
1095
|
}
|
|
1075
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1076
1096
|
decreaseVSize(value) {
|
|
1077
1097
|
if (!this._enabled) {
|
|
1078
1098
|
return;
|
|
1079
1099
|
}
|
|
1080
1100
|
this._vsize -= this.roughSizeOfObject(value);
|
|
1081
1101
|
}
|
|
1082
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1083
1102
|
incrementKSize(key) {
|
|
1084
1103
|
if (!this._enabled) {
|
|
1085
1104
|
return;
|
|
1086
1105
|
}
|
|
1087
1106
|
this._ksize += this.roughSizeOfString(key);
|
|
1088
1107
|
}
|
|
1089
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1090
1108
|
decreaseKSize(key) {
|
|
1091
1109
|
if (!this._enabled) {
|
|
1092
1110
|
return;
|
|
@@ -1184,10 +1202,7 @@ function calculateTtlFromExpiration(ttl, expires) {
|
|
|
1184
1202
|
}
|
|
1185
1203
|
|
|
1186
1204
|
// src/index.ts
|
|
1187
|
-
import {
|
|
1188
|
-
KeyvHooks,
|
|
1189
|
-
Keyv as Keyv3
|
|
1190
|
-
} from "keyv";
|
|
1205
|
+
import { Keyv as Keyv3, KeyvHooks } from "keyv";
|
|
1191
1206
|
var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => {
|
|
1192
1207
|
CacheableHooks2["BEFORE_SET"] = "BEFORE_SET";
|
|
1193
1208
|
CacheableHooks2["AFTER_SET"] = "AFTER_SET";
|
|
@@ -1408,11 +1423,26 @@ var Cacheable = class extends Hookified2 {
|
|
|
1408
1423
|
this.emit("error" /* ERROR */, error);
|
|
1409
1424
|
});
|
|
1410
1425
|
}
|
|
1426
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
1411
1427
|
isKeyvInstance(keyv) {
|
|
1412
1428
|
if (keyv instanceof Keyv2) {
|
|
1413
1429
|
return true;
|
|
1414
1430
|
}
|
|
1415
|
-
const keyvMethods = [
|
|
1431
|
+
const keyvMethods = [
|
|
1432
|
+
"generateIterator",
|
|
1433
|
+
"get",
|
|
1434
|
+
"getMany",
|
|
1435
|
+
"set",
|
|
1436
|
+
"setMany",
|
|
1437
|
+
"delete",
|
|
1438
|
+
"deleteMany",
|
|
1439
|
+
"has",
|
|
1440
|
+
"hasMany",
|
|
1441
|
+
"clear",
|
|
1442
|
+
"disconnect",
|
|
1443
|
+
"serialize",
|
|
1444
|
+
"deserialize"
|
|
1445
|
+
];
|
|
1416
1446
|
return keyvMethods.every((method) => typeof keyv[method] === "function");
|
|
1417
1447
|
}
|
|
1418
1448
|
getNameSpace() {
|
|
@@ -1436,7 +1466,10 @@ var Cacheable = class extends Hookified2 {
|
|
|
1436
1466
|
const expires = secondaryResult.expires ?? void 0;
|
|
1437
1467
|
ttl = calculateTtlFromExpiration(cascadeTtl, expires);
|
|
1438
1468
|
const setItem = { key, value: result.value, ttl };
|
|
1439
|
-
await this.hook(
|
|
1469
|
+
await this.hook(
|
|
1470
|
+
"BEFORE_SECONDARY_SETS_PRIMARY" /* BEFORE_SECONDARY_SETS_PRIMARY */,
|
|
1471
|
+
setItem
|
|
1472
|
+
);
|
|
1440
1473
|
await this._primary.set(setItem.key, setItem.value, setItem.ttl);
|
|
1441
1474
|
}
|
|
1442
1475
|
}
|
|
@@ -1478,7 +1511,10 @@ var Cacheable = class extends Hookified2 {
|
|
|
1478
1511
|
}
|
|
1479
1512
|
const ttl = calculateTtlFromExpiration(cascadeTtl, expires);
|
|
1480
1513
|
const setItem = { key, value: result[i].value, ttl };
|
|
1481
|
-
await this.hook(
|
|
1514
|
+
await this.hook(
|
|
1515
|
+
"BEFORE_SECONDARY_SETS_PRIMARY" /* BEFORE_SECONDARY_SETS_PRIMARY */,
|
|
1516
|
+
setItem
|
|
1517
|
+
);
|
|
1482
1518
|
await this._primary.set(setItem.key, setItem.value, setItem.ttl);
|
|
1483
1519
|
}
|
|
1484
1520
|
}
|
|
@@ -1620,7 +1656,7 @@ var Cacheable = class extends Hookified2 {
|
|
|
1620
1656
|
}
|
|
1621
1657
|
if (missingKeys.length > 0 && this._secondary) {
|
|
1622
1658
|
const secondary = await this.hasManyKeyv(this._secondary, keys);
|
|
1623
|
-
for (const [i,
|
|
1659
|
+
for (const [i, _key] of keys.entries()) {
|
|
1624
1660
|
if (!result[i] && secondary[i]) {
|
|
1625
1661
|
result[i] = secondary[i];
|
|
1626
1662
|
}
|
|
@@ -1718,6 +1754,7 @@ var Cacheable = class extends Hookified2 {
|
|
|
1718
1754
|
* @param {WrapOptions} [options] The options for the wrap function
|
|
1719
1755
|
* @returns {Function} The wrapped function
|
|
1720
1756
|
*/
|
|
1757
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
1721
1758
|
wrap(function_, options) {
|
|
1722
1759
|
const wrapOptions = {
|
|
1723
1760
|
ttl: options?.ttl ?? this._ttl,
|
|
@@ -1753,6 +1790,7 @@ var Cacheable = class extends Hookified2 {
|
|
|
1753
1790
|
* @param {string} algorithm the hash algorithm to use. The default is 'sha256'
|
|
1754
1791
|
* @returns {string} the hash of the object
|
|
1755
1792
|
*/
|
|
1793
|
+
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
1756
1794
|
hash(object, algorithm = "sha256") {
|
|
1757
1795
|
return hash(object, algorithm);
|
|
1758
1796
|
}
|
|
@@ -1764,7 +1802,7 @@ var Cacheable = class extends Hookified2 {
|
|
|
1764
1802
|
return result;
|
|
1765
1803
|
}
|
|
1766
1804
|
async getManySecondaryRawResults(keys) {
|
|
1767
|
-
let result =
|
|
1805
|
+
let result = [];
|
|
1768
1806
|
if (this._secondary) {
|
|
1769
1807
|
result = await this._secondary.get(keys, { raw: true });
|
|
1770
1808
|
}
|