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