@cacheable/memory 1.0.1 → 2.0.0

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 CHANGED
@@ -1,687 +1 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- CacheableMemory: () => CacheableMemory,
24
- HashAlgorithm: () => import_utils2.HashAlgorithm,
25
- defaultStoreHashSize: () => defaultStoreHashSize,
26
- hash: () => import_utils2.hash,
27
- hashToNumber: () => import_utils2.hashToNumber,
28
- maximumMapSize: () => maximumMapSize
29
- });
30
- module.exports = __toCommonJS(index_exports);
31
- var import_memoize = require("@cacheable/memoize");
32
- var import_utils = require("@cacheable/utils");
33
- var import_hookified = require("hookified");
34
-
35
- // src/memory-lru.ts
36
- var ListNode = class {
37
- value;
38
- prev = void 0;
39
- next = void 0;
40
- constructor(value) {
41
- this.value = value;
42
- }
43
- };
44
- var DoublyLinkedList = class {
45
- head = void 0;
46
- tail = void 0;
47
- nodesMap = /* @__PURE__ */ new Map();
48
- // Add a new node to the front (most recently used)
49
- addToFront(value) {
50
- const newNode = new ListNode(value);
51
- if (this.head) {
52
- newNode.next = this.head;
53
- this.head.prev = newNode;
54
- this.head = newNode;
55
- } else {
56
- this.head = this.tail = newNode;
57
- }
58
- this.nodesMap.set(value, newNode);
59
- }
60
- // Move an existing node to the front (most recently used)
61
- moveToFront(value) {
62
- const node = this.nodesMap.get(value);
63
- if (!node || this.head === node) {
64
- return;
65
- }
66
- if (node.prev) {
67
- node.prev.next = node.next;
68
- }
69
- if (node.next) {
70
- node.next.prev = node.prev;
71
- }
72
- if (node === this.tail) {
73
- this.tail = node.prev;
74
- }
75
- node.prev = void 0;
76
- node.next = this.head;
77
- if (this.head) {
78
- this.head.prev = node;
79
- }
80
- this.head = node;
81
- this.tail ??= node;
82
- }
83
- // Get the oldest node (tail)
84
- getOldest() {
85
- return this.tail ? this.tail.value : void 0;
86
- }
87
- // Remove the oldest node (tail)
88
- removeOldest() {
89
- if (!this.tail) {
90
- return void 0;
91
- }
92
- const oldValue = this.tail.value;
93
- if (this.tail.prev) {
94
- this.tail = this.tail.prev;
95
- this.tail.next = void 0;
96
- } else {
97
- this.head = this.tail = void 0;
98
- }
99
- this.nodesMap.delete(oldValue);
100
- return oldValue;
101
- }
102
- get size() {
103
- return this.nodesMap.size;
104
- }
105
- };
106
-
107
- // src/index.ts
108
- var import_utils2 = require("@cacheable/utils");
109
- var defaultStoreHashSize = 16;
110
- var maximumMapSize = 16777216;
111
- var CacheableMemory = class extends import_hookified.Hookified {
112
- _lru = new DoublyLinkedList();
113
- _storeHashSize = defaultStoreHashSize;
114
- _storeHashAlgorithm = import_utils.HashAlgorithm.DJB2;
115
- // Default is djb2Hash
116
- _store = Array.from(
117
- { length: this._storeHashSize },
118
- () => /* @__PURE__ */ new Map()
119
- );
120
- _ttl;
121
- // Turned off by default
122
- _useClone = true;
123
- // Turned on by default
124
- _lruSize = 0;
125
- // Turned off by default
126
- _checkInterval = 0;
127
- // Turned off by default
128
- _interval = 0;
129
- // Turned off by default
130
- /**
131
- * @constructor
132
- * @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
133
- */
134
- constructor(options) {
135
- super();
136
- if (options?.ttl) {
137
- this.setTtl(options.ttl);
138
- }
139
- if (options?.useClone !== void 0) {
140
- this._useClone = options.useClone;
141
- }
142
- if (options?.storeHashSize && options.storeHashSize > 0) {
143
- this._storeHashSize = options.storeHashSize;
144
- }
145
- if (options?.lruSize) {
146
- if (options.lruSize > maximumMapSize) {
147
- this.emit(
148
- "error",
149
- new Error(
150
- `LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
151
- )
152
- );
153
- } else {
154
- this._lruSize = options.lruSize;
155
- }
156
- }
157
- if (options?.checkInterval) {
158
- this._checkInterval = options.checkInterval;
159
- }
160
- if (options?.storeHashAlgorithm) {
161
- this._storeHashAlgorithm = options.storeHashAlgorithm;
162
- }
163
- this._store = Array.from(
164
- { length: this._storeHashSize },
165
- () => /* @__PURE__ */ new Map()
166
- );
167
- this.startIntervalCheck();
168
- }
169
- /**
170
- * Gets the time-to-live
171
- * @returns {number|string|undefined} - The time-to-live in miliseconds or a human-readable format. If undefined, it will not have a time-to-live.
172
- */
173
- get ttl() {
174
- return this._ttl;
175
- }
176
- /**
177
- * Sets the time-to-live
178
- * @param {number|string|undefined} value - The time-to-live in miliseconds or a human-readable format (example '1s' = 1 second, '1h' = 1 hour). If undefined, it will not have a time-to-live.
179
- */
180
- set ttl(value) {
181
- this.setTtl(value);
182
- }
183
- /**
184
- * Gets whether to use clone
185
- * @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
186
- */
187
- get useClone() {
188
- return this._useClone;
189
- }
190
- /**
191
- * Sets whether to use clone
192
- * @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
193
- */
194
- set useClone(value) {
195
- this._useClone = value;
196
- }
197
- /**
198
- * Gets the size of the LRU cache
199
- * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
200
- */
201
- get lruSize() {
202
- return this._lruSize;
203
- }
204
- /**
205
- * Sets the size of the LRU cache
206
- * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
207
- */
208
- set lruSize(value) {
209
- if (value > maximumMapSize) {
210
- this.emit(
211
- "error",
212
- new Error(
213
- `LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
214
- )
215
- );
216
- return;
217
- }
218
- this._lruSize = value;
219
- if (this._lruSize === 0) {
220
- this._lru = new DoublyLinkedList();
221
- return;
222
- }
223
- this.lruResize();
224
- }
225
- /**
226
- * Gets the check interval
227
- * @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
228
- */
229
- get checkInterval() {
230
- return this._checkInterval;
231
- }
232
- /**
233
- * Sets the check interval
234
- * @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
235
- */
236
- set checkInterval(value) {
237
- this._checkInterval = value;
238
- }
239
- /**
240
- * Gets the size of the cache
241
- * @returns {number} - The size of the cache
242
- */
243
- get size() {
244
- let size = 0;
245
- for (const store of this._store) {
246
- size += store.size;
247
- }
248
- return size;
249
- }
250
- /**
251
- * Gets the number of hash stores
252
- * @returns {number} - The number of hash stores
253
- */
254
- get storeHashSize() {
255
- return this._storeHashSize;
256
- }
257
- /**
258
- * Sets the number of hash stores. This will recreate the store and all data will be cleared
259
- * @param {number} value - The number of hash stores
260
- */
261
- set storeHashSize(value) {
262
- if (value === this._storeHashSize) {
263
- return;
264
- }
265
- this._storeHashSize = value;
266
- this._store = Array.from(
267
- { length: this._storeHashSize },
268
- () => /* @__PURE__ */ new Map()
269
- );
270
- }
271
- /**
272
- * Gets the store hash algorithm
273
- * @returns {HashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
274
- */
275
- get storeHashAlgorithm() {
276
- return this._storeHashAlgorithm;
277
- }
278
- /**
279
- * Sets the store hash algorithm. This will recreate the store and all data will be cleared
280
- * @param {HashAlgorithm | HashAlgorithmFunction} value - The store hash algorithm
281
- */
282
- set storeHashAlgorithm(value) {
283
- this._storeHashAlgorithm = value;
284
- }
285
- /**
286
- * Gets the keys
287
- * @returns {IterableIterator<string>} - The keys
288
- */
289
- get keys() {
290
- const keys = [];
291
- for (const store of this._store) {
292
- for (const key of store.keys()) {
293
- const item = store.get(key);
294
- if (item && this.hasExpired(item)) {
295
- store.delete(key);
296
- continue;
297
- }
298
- keys.push(key);
299
- }
300
- }
301
- return keys.values();
302
- }
303
- /**
304
- * Gets the items
305
- * @returns {IterableIterator<CacheableStoreItem>} - The items
306
- */
307
- get items() {
308
- const items = [];
309
- for (const store of this._store) {
310
- for (const item of store.values()) {
311
- if (this.hasExpired(item)) {
312
- store.delete(item.key);
313
- continue;
314
- }
315
- items.push(item);
316
- }
317
- }
318
- return items.values();
319
- }
320
- /**
321
- * Gets the store
322
- * @returns {Array<Map<string, CacheableStoreItem>>} - The store
323
- */
324
- get store() {
325
- return this._store;
326
- }
327
- /**
328
- * Gets the value of the key
329
- * @param {string} key - The key to get the value
330
- * @returns {T | undefined} - The value of the key
331
- */
332
- get(key) {
333
- const store = this.getStore(key);
334
- const item = store.get(key);
335
- if (!item) {
336
- return void 0;
337
- }
338
- if (item.expires && Date.now() > item.expires) {
339
- store.delete(key);
340
- return void 0;
341
- }
342
- this.lruMoveToFront(key);
343
- if (!this._useClone) {
344
- return item.value;
345
- }
346
- return this.clone(item.value);
347
- }
348
- /**
349
- * Gets the values of the keys
350
- * @param {string[]} keys - The keys to get the values
351
- * @returns {T[]} - The values of the keys
352
- */
353
- getMany(keys) {
354
- const result = [];
355
- for (const key of keys) {
356
- result.push(this.get(key));
357
- }
358
- return result;
359
- }
360
- /**
361
- * Gets the raw value of the key
362
- * @param {string} key - The key to get the value
363
- * @returns {CacheableStoreItem | undefined} - The raw value of the key
364
- */
365
- getRaw(key) {
366
- const store = this.getStore(key);
367
- const item = store.get(key);
368
- if (!item) {
369
- return void 0;
370
- }
371
- if (item.expires && item.expires && Date.now() > item.expires) {
372
- store.delete(key);
373
- return void 0;
374
- }
375
- this.lruMoveToFront(key);
376
- return item;
377
- }
378
- /**
379
- * Gets the raw values of the keys
380
- * @param {string[]} keys - The keys to get the values
381
- * @returns {CacheableStoreItem[]} - The raw values of the keys
382
- */
383
- getManyRaw(keys) {
384
- const result = [];
385
- for (const key of keys) {
386
- result.push(this.getRaw(key));
387
- }
388
- return result;
389
- }
390
- /**
391
- * Sets the value of the key
392
- * @param {string} key - The key to set the value
393
- * @param {any} value - The value to set
394
- * @param {number|string|SetOptions} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable.
395
- * If you want to set expire directly you can do that by setting the expire property in the SetOptions.
396
- * If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live.
397
- * @returns {void}
398
- */
399
- set(key, value, ttl) {
400
- const store = this.getStore(key);
401
- let expires;
402
- if (ttl !== void 0 || this._ttl !== void 0) {
403
- if (typeof ttl === "object") {
404
- if (ttl.expire) {
405
- expires = typeof ttl.expire === "number" ? ttl.expire : ttl.expire.getTime();
406
- }
407
- if (ttl.ttl) {
408
- const finalTtl = (0, import_utils.shorthandToTime)(ttl.ttl);
409
- if (finalTtl !== void 0) {
410
- expires = finalTtl;
411
- }
412
- }
413
- } else {
414
- const finalTtl = (0, import_utils.shorthandToTime)(ttl ?? this._ttl);
415
- if (finalTtl !== void 0) {
416
- expires = finalTtl;
417
- }
418
- }
419
- }
420
- if (this._lruSize > 0) {
421
- if (store.has(key)) {
422
- this.lruMoveToFront(key);
423
- } else {
424
- this.lruAddToFront(key);
425
- if (this._lru.size > this._lruSize) {
426
- const oldestKey = this._lru.getOldest();
427
- if (oldestKey) {
428
- this._lru.removeOldest();
429
- this.delete(oldestKey);
430
- }
431
- }
432
- }
433
- }
434
- const item = { key, value, expires };
435
- store.set(key, item);
436
- }
437
- /**
438
- * Sets the values of the keys
439
- * @param {CacheableItem[]} items - The items to set
440
- * @returns {void}
441
- */
442
- setMany(items) {
443
- for (const item of items) {
444
- this.set(item.key, item.value, item.ttl);
445
- }
446
- }
447
- /**
448
- * Checks if the key exists
449
- * @param {string} key - The key to check
450
- * @returns {boolean} - If true, the key exists. If false, the key does not exist.
451
- */
452
- has(key) {
453
- const item = this.get(key);
454
- return Boolean(item);
455
- }
456
- /**
457
- * @function hasMany
458
- * @param {string[]} keys - The keys to check
459
- * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
460
- */
461
- hasMany(keys) {
462
- const result = [];
463
- for (const key of keys) {
464
- const item = this.get(key);
465
- result.push(Boolean(item));
466
- }
467
- return result;
468
- }
469
- /**
470
- * Take will get the key and delete the entry from cache
471
- * @param {string} key - The key to take
472
- * @returns {T | undefined} - The value of the key
473
- */
474
- take(key) {
475
- const item = this.get(key);
476
- if (!item) {
477
- return void 0;
478
- }
479
- this.delete(key);
480
- return item;
481
- }
482
- /**
483
- * TakeMany will get the keys and delete the entries from cache
484
- * @param {string[]} keys - The keys to take
485
- * @returns {T[]} - The values of the keys
486
- */
487
- takeMany(keys) {
488
- const result = [];
489
- for (const key of keys) {
490
- result.push(this.take(key));
491
- }
492
- return result;
493
- }
494
- /**
495
- * Delete the key
496
- * @param {string} key - The key to delete
497
- * @returns {void}
498
- */
499
- delete(key) {
500
- const store = this.getStore(key);
501
- store.delete(key);
502
- }
503
- /**
504
- * Delete the keys
505
- * @param {string[]} keys - The keys to delete
506
- * @returns {void}
507
- */
508
- deleteMany(keys) {
509
- for (const key of keys) {
510
- this.delete(key);
511
- }
512
- }
513
- /**
514
- * Clear the cache
515
- * @returns {void}
516
- */
517
- clear() {
518
- this._store = Array.from(
519
- { length: this._storeHashSize },
520
- () => /* @__PURE__ */ new Map()
521
- );
522
- this._lru = new DoublyLinkedList();
523
- }
524
- /**
525
- * Get the store based on the key (internal use)
526
- * @param {string} key - The key to get the store
527
- * @returns {CacheableHashStore} - The store
528
- */
529
- getStore(key) {
530
- const hash2 = this.getKeyStoreHash(key);
531
- this._store[hash2] ||= /* @__PURE__ */ new Map();
532
- return this._store[hash2];
533
- }
534
- /**
535
- * Hash the key for which store to go to (internal use)
536
- * @param {string} key - The key to hash
537
- * Available algorithms are: SHA256, SHA1, MD5, and djb2Hash.
538
- * @returns {number} - The hashed key as a number
539
- */
540
- getKeyStoreHash(key) {
541
- if (this._store.length === 1) {
542
- return 0;
543
- }
544
- if (typeof this._storeHashAlgorithm === "function") {
545
- return this._storeHashAlgorithm(key, this._storeHashSize);
546
- }
547
- const storeHashSize = this._storeHashSize - 1;
548
- const hash2 = (0, import_utils.hashToNumber)(key, 0, storeHashSize, this._storeHashAlgorithm);
549
- return hash2;
550
- }
551
- /**
552
- * Clone the value. This is for internal use
553
- * @param {any} value - The value to clone
554
- * @returns {any} - The cloned value
555
- */
556
- // biome-ignore lint/suspicious/noExplicitAny: type format
557
- clone(value) {
558
- if (this.isPrimitive(value)) {
559
- return value;
560
- }
561
- return structuredClone(value);
562
- }
563
- /**
564
- * Add to the front of the LRU cache. This is for internal use
565
- * @param {string} key - The key to add to the front
566
- * @returns {void}
567
- */
568
- lruAddToFront(key) {
569
- if (this._lruSize === 0) {
570
- return;
571
- }
572
- this._lru.addToFront(key);
573
- }
574
- /**
575
- * Move to the front of the LRU cache. This is for internal use
576
- * @param {string} key - The key to move to the front
577
- * @returns {void}
578
- */
579
- lruMoveToFront(key) {
580
- if (this._lruSize === 0) {
581
- return;
582
- }
583
- this._lru.moveToFront(key);
584
- }
585
- /**
586
- * Resize the LRU cache. This is for internal use.
587
- * @returns {void}
588
- */
589
- lruResize() {
590
- while (this._lru.size > this._lruSize) {
591
- const oldestKey = this._lru.getOldest();
592
- if (oldestKey) {
593
- this._lru.removeOldest();
594
- this.delete(oldestKey);
595
- }
596
- }
597
- }
598
- /**
599
- * Check for expiration. This is for internal use
600
- * @returns {void}
601
- */
602
- checkExpiration() {
603
- for (const store of this._store) {
604
- for (const item of store.values()) {
605
- if (item.expires && Date.now() > item.expires) {
606
- store.delete(item.key);
607
- }
608
- }
609
- }
610
- }
611
- /**
612
- * Start the interval check. This is for internal use
613
- * @returns {void}
614
- */
615
- startIntervalCheck() {
616
- if (this._checkInterval > 0) {
617
- if (this._interval) {
618
- clearInterval(this._interval);
619
- }
620
- this._interval = setInterval(() => {
621
- this.checkExpiration();
622
- }, this._checkInterval).unref();
623
- }
624
- }
625
- /**
626
- * Stop the interval check. This is for internal use
627
- * @returns {void}
628
- */
629
- stopIntervalCheck() {
630
- if (this._interval) {
631
- clearInterval(this._interval);
632
- }
633
- this._interval = 0;
634
- this._checkInterval = 0;
635
- }
636
- /**
637
- * Wrap the function for caching
638
- * @param {Function} function_ - The function to wrap
639
- * @param {Object} [options] - The options to wrap
640
- * @returns {Function} - The wrapped function
641
- */
642
- // biome-ignore lint/suspicious/noExplicitAny: type format
643
- wrap(function_, options) {
644
- const wrapOptions = {
645
- ttl: options?.ttl ?? this._ttl,
646
- keyPrefix: options?.keyPrefix,
647
- createKey: options?.createKey,
648
- cache: this
649
- };
650
- return (0, import_memoize.wrapSync)(function_, wrapOptions);
651
- }
652
- // biome-ignore lint/suspicious/noExplicitAny: type format
653
- isPrimitive(value) {
654
- const result = false;
655
- if (value === null || value === void 0) {
656
- return true;
657
- }
658
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
659
- return true;
660
- }
661
- return result;
662
- }
663
- setTtl(ttl) {
664
- if (typeof ttl === "string" || ttl === void 0) {
665
- this._ttl = ttl;
666
- } else if (ttl > 0) {
667
- this._ttl = ttl;
668
- } else {
669
- this._ttl = void 0;
670
- }
671
- }
672
- hasExpired(item) {
673
- if (item.expires && Date.now() > item.expires) {
674
- return true;
675
- }
676
- return false;
677
- }
678
- };
679
- // Annotate the CommonJS export names for ESM import in node:
680
- 0 && (module.exports = {
681
- CacheableMemory,
682
- HashAlgorithm,
683
- defaultStoreHashSize,
684
- hash,
685
- hashToNumber,
686
- maximumMapSize
687
- });
1
+ "use strict";var p=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var z=(i,e)=>{for(var t in e)p(i,t,{get:e[t],enumerable:!0})},x=(i,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of C(e))!T.call(i,s)&&s!==t&&p(i,s,{get:()=>e[s],enumerable:!(r=S(e,s))||r.enumerable});return i};var H=i=>x(p({},"__esModule",{value:!0}),i);var M={};z(M,{CacheableMemory:()=>a,HashAlgorithm:()=>l.HashAlgorithm,KeyvCacheableMemory:()=>u,createKeyv:()=>g,defaultStoreHashSize:()=>y,hash:()=>l.hash,hashToNumber:()=>l.hashToNumber,maximumMapSize:()=>c});module.exports=H(M);var b=require("@cacheable/memoize"),o=require("@cacheable/utils"),_=require("hookified");var m=class{value;prev=void 0;next=void 0;constructor(e){this.value=e}},h=class{head=void 0;tail=void 0;nodesMap=new Map;addToFront(e){let t=new m(e);this.head?(t.next=this.head,this.head.prev=t,this.head=t):this.head=this.tail=t,this.nodesMap.set(e,t)}moveToFront(e){let t=this.nodesMap.get(e);!t||this.head===t||(t.prev&&(t.prev.next=t.next),t.next&&(t.next.prev=t.prev),t===this.tail&&(this.tail=t.prev),t.prev=void 0,t.next=this.head,this.head&&(this.head.prev=t),this.head=t,this.tail??=t)}getOldest(){return this.tail?this.tail.value:void 0}removeOldest(){if(!this.tail)return;let e=this.tail.value;return this.tail.prev?(this.tail=this.tail.prev,this.tail.next=void 0):this.head=this.tail=void 0,this.nodesMap.delete(e),e}get size(){return this.nodesMap.size}};var l=require("@cacheable/utils");var f=require("keyv");var u=class{opts={ttl:0,useClone:!0,lruSize:0,checkInterval:0};_defaultCache=new a;_nCache=new Map;_namespace;constructor(e){e&&(this.opts=e,this._defaultCache=new a(e),e.namespace&&(this._namespace=e.namespace,this._nCache.set(this._namespace,new a(e))))}get namespace(){return this._namespace}set namespace(e){this._namespace=e}get store(){return this.getStore(this._namespace)}async get(e){let t=this.getStore(this._namespace).get(e);if(t)return t}async getMany(e){return this.getStore(this._namespace).getMany(e)}async set(e,t,r){this.getStore(this._namespace).set(e,t,r)}async setMany(e){this.getStore(this._namespace).setMany(e)}async delete(e){return this.getStore(this._namespace).delete(e),!0}async deleteMany(e){return this.getStore(this._namespace).deleteMany(e),!0}async clear(){this.getStore(this._namespace).clear()}async has(e){return this.getStore(this._namespace).has(e)}on(e,t){return this.getStore(this._namespace).on(e,t),this}getStore(e){return e?(this._nCache.has(e)||this._nCache.set(e,new a(this.opts)),this._nCache.get(e)):this._defaultCache}};function g(i){let e=new u(i),t=i?.namespace,r;i?.ttl&&Number.isInteger(i.ttl)&&(r=i?.ttl);let s=new f.Keyv({store:e,namespace:t,ttl:r});return s.serialize=void 0,s.deserialize=void 0,s}var y=16,c=16777216,a=class extends _.Hookified{_lru=new h;_storeHashSize=y;_storeHashAlgorithm=o.HashAlgorithm.DJB2;_store=Array.from({length:this._storeHashSize},()=>new Map);_ttl;_useClone=!0;_lruSize=0;_checkInterval=0;_interval=0;constructor(e){super(),e?.ttl&&this.setTtl(e.ttl),e?.useClone!==void 0&&(this._useClone=e.useClone),e?.storeHashSize&&e.storeHashSize>0&&(this._storeHashSize=e.storeHashSize),e?.lruSize&&(e.lruSize>c?this.emit("error",new Error(`LRU size cannot be larger than ${c} due to Map limitations.`)):this._lruSize=e.lruSize),e?.checkInterval&&(this._checkInterval=e.checkInterval),e?.storeHashAlgorithm&&(this._storeHashAlgorithm=e.storeHashAlgorithm),this._store=Array.from({length:this._storeHashSize},()=>new Map),this.startIntervalCheck()}get ttl(){return this._ttl}set ttl(e){this.setTtl(e)}get useClone(){return this._useClone}set useClone(e){this._useClone=e}get lruSize(){return this._lruSize}set lruSize(e){if(e>c){this.emit("error",new Error(`LRU size cannot be larger than ${c} due to Map limitations.`));return}if(this._lruSize=e,this._lruSize===0){this._lru=new h;return}this.lruResize()}get checkInterval(){return this._checkInterval}set checkInterval(e){this._checkInterval=e}get size(){let e=0;for(let t of this._store)e+=t.size;return e}get storeHashSize(){return this._storeHashSize}set storeHashSize(e){e!==this._storeHashSize&&(this._storeHashSize=e,this._store=Array.from({length:this._storeHashSize},()=>new Map))}get storeHashAlgorithm(){return this._storeHashAlgorithm}set storeHashAlgorithm(e){this._storeHashAlgorithm=e}get keys(){let e=[];for(let t of this._store)for(let r of t.keys()){let s=t.get(r);if(s&&this.hasExpired(s)){t.delete(r);continue}e.push(r)}return e.values()}get items(){let e=[];for(let t of this._store)for(let r of t.values()){if(this.hasExpired(r)){t.delete(r.key);continue}e.push(r)}return e.values()}get store(){return this._store}get(e){let t=this.getStore(e),r=t.get(e);if(r){if(r.expires&&Date.now()>r.expires){t.delete(e);return}return this.lruMoveToFront(e),this._useClone?this.clone(r.value):r.value}}getMany(e){let t=[];for(let r of e)t.push(this.get(r));return t}getRaw(e){let t=this.getStore(e),r=t.get(e);if(r){if(r.expires&&r.expires&&Date.now()>r.expires){t.delete(e);return}return this.lruMoveToFront(e),r}}getManyRaw(e){let t=[];for(let r of e)t.push(this.getRaw(r));return t}set(e,t,r){let s=this.getStore(e),d;if(r!==void 0||this._ttl!==void 0)if(typeof r=="object"){if(r.expire&&(d=typeof r.expire=="number"?r.expire:r.expire.getTime()),r.ttl){let n=(0,o.shorthandToTime)(r.ttl);n!==void 0&&(d=n)}}else{let n=(0,o.shorthandToTime)(r??this._ttl);n!==void 0&&(d=n)}if(this._lruSize>0){if(s.has(e))this.lruMoveToFront(e);else if(this.lruAddToFront(e),this._lru.size>this._lruSize){let n=this._lru.getOldest();n&&(this._lru.removeOldest(),this.delete(n))}}let v={key:e,value:t,expires:d};s.set(e,v)}setMany(e){for(let t of e)this.set(t.key,t.value,t.ttl)}has(e){return!!this.get(e)}hasMany(e){let t=[];for(let r of e){let s=this.get(r);t.push(!!s)}return t}take(e){let t=this.get(e);if(t)return this.delete(e),t}takeMany(e){let t=[];for(let r of e)t.push(this.take(r));return t}delete(e){this.getStore(e).delete(e)}deleteMany(e){for(let t of e)this.delete(t)}clear(){this._store=Array.from({length:this._storeHashSize},()=>new Map),this._lru=new h}getStore(e){let t=this.getKeyStoreHash(e);return this._store[t]||=new Map,this._store[t]}getKeyStoreHash(e){if(this._store.length===1)return 0;if(typeof this._storeHashAlgorithm=="function")return this._storeHashAlgorithm(e,this._storeHashSize);let t=this._storeHashSize-1;return(0,o.hashToNumber)(e,{min:0,max:t,algorithm:this._storeHashAlgorithm})}clone(e){return this.isPrimitive(e)?e:structuredClone(e)}lruAddToFront(e){this._lruSize!==0&&this._lru.addToFront(e)}lruMoveToFront(e){this._lruSize!==0&&this._lru.moveToFront(e)}lruResize(){for(;this._lru.size>this._lruSize;){let e=this._lru.getOldest();e&&(this._lru.removeOldest(),this.delete(e))}}checkExpiration(){for(let e of this._store)for(let t of e.values())t.expires&&Date.now()>t.expires&&e.delete(t.key)}startIntervalCheck(){this._checkInterval>0&&(this._interval&&clearInterval(this._interval),this._interval=setInterval(()=>{this.checkExpiration()},this._checkInterval).unref())}stopIntervalCheck(){this._interval&&clearInterval(this._interval),this._interval=0,this._checkInterval=0}wrap(e,t){let r={ttl:t?.ttl??this._ttl,keyPrefix:t?.keyPrefix,createKey:t?.createKey,cache:this};return(0,b.wrapSync)(e,r)}isPrimitive(e){return e==null||typeof e=="string"||typeof e=="number"||typeof e=="boolean"}setTtl(e){typeof e=="string"||e===void 0?this._ttl=e:e>0?this._ttl=e:this._ttl=void 0}hasExpired(e){return!!(e.expires&&Date.now()>e.expires)}};0&&(module.exports={CacheableMemory,HashAlgorithm,KeyvCacheableMemory,createKeyv,defaultStoreHashSize,hash,hashToNumber,maximumMapSize});