@cacheable/memory 1.0.1 → 2.0.1

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