cacheable 1.5.0 → 1.6.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/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
  [![npm](https://img.shields.io/npm/dm/cacheable.svg)](https://www.npmjs.com/package/cacheable)
10
10
  [![npm](https://img.shields.io/npm/v/cacheable)](https://www.npmjs.com/package/cacheable)
11
11
 
12
- `cacheable` is a high performance layer 1 / layer 2 caching engine that is focused on distributed caching with enterprise features such as `CacheSync`. It is built on top of the robust storage engine [Keyv](https://keyv.org) and provides a simple API to cache and retrieve data.
12
+ `cacheable` is a high performance layer 1 / layer 2 caching engine that is focused on distributed caching with enterprise features such as `CacheSync` (coming soon). It is built on top of the robust storage engine [Keyv](https://keyv.org) and provides a simple API to cache and retrieve data.
13
13
 
14
14
  * Simple to use with robust API
15
15
  * Not bloated with additional modules
@@ -25,7 +25,7 @@
25
25
 
26
26
  ## Getting Started
27
27
 
28
- `cacheable` is primarily used as an extension to you caching engine with a robust storage backend [Keyv](https://keyv.org), Memonization, Hooks, Events, and Statistics.
28
+ `cacheable` is primarily used as an extension to you caching engine with a robust storage backend [Keyv](https://keyv.org), Memonization (Wrap), Hooks, Events, and Statistics.
29
29
 
30
30
  ```bash
31
31
  npm install cacheable
@@ -115,7 +115,6 @@ const cache = new Cacheable({secondary, nonBlocking: true});
115
115
 
116
116
  `cacheable` has a feature called `CacheSync` that is coming soon. This feature will allow you to have distributed caching with Pub/Sub. This will allow you to have multiple instances of `cacheable` running and when a value is set, deleted, or cleared it will update all instances of `cacheable` with the same value. Current plan is to support the following:
117
117
 
118
- * [Google Pub/Sub](https://cloud.google.com/pubsub)
119
118
  * [AWS SQS](https://aws.amazon.com/sqs)
120
119
  * [RabbitMQ](https://www.rabbitmq.com)
121
120
  * [Nats](https://nats.io)
@@ -153,7 +152,7 @@ _This does not enable statistics for your layer 2 cache as that is a distributed
153
152
 
154
153
  ## API
155
154
 
156
- * `set(key, value, ttl? | [{string, string, ttl?}])`: Sets a value in the cache.
155
+ * `set(key, value, ttl?)`: Sets a value in the cache.
157
156
  * `setMany([{key, value, ttl?}])`: Sets multiple values in the cache.
158
157
  * `get(key)`: Gets a value from the cache.
159
158
  * `getMany([keys])`: Gets multiple values from the cache.
@@ -187,8 +186,8 @@ const options = {
187
186
  lruSize: 1000, // the size of the LRU cache (default is 0 which is unlimited)
188
187
  }
189
188
  const cache = new CacheableMemory(options);
190
- await cache.set('key', 'value');
191
- const value = await cache.get('key'); // value
189
+ cache.set('key', 'value');
190
+ const value = cache.get('key'); // value
192
191
  ```
193
192
 
194
193
  You can use `CacheableMemory` as a standalone cache or as a primary store for `cacheable`. You can also set the `useClones` property to `false` if you want to use the same reference for the values. This is useful if you are using large objects and want to save memory. The `lruSize` property is the size of the LRU cache and is set to `0` by default which is unlimited. When setting the `lruSize` property it will limit the number of keys in the cache.
@@ -213,6 +212,7 @@ By default we use lazy expiration deletion which means on `get` and `getMany` ty
213
212
  * `clear()`: Clears the cache.
214
213
  * `size()`: The number of keys in the cache.
215
214
  * `keys()`: The keys in the cache.
215
+ * `items()`: The items in the cache as `{ key, value, expiration }`.
216
216
  * `checkExpired()`: Checks for expired keys in the cache. This is used by the `checkInterval` property.
217
217
  * `startIntervalCheck()`: Starts the interval check for expired keys if `checkInterval` is above 0 ms.
218
218
  * `stopIntervalCheck()`: Stops the interval check for expired keys.
package/dist/index.cjs CHANGED
@@ -24,187 +24,13 @@ __export(src_exports, {
24
24
  CacheableEvents: () => CacheableEvents,
25
25
  CacheableHooks: () => CacheableHooks,
26
26
  CacheableMemory: () => CacheableMemory,
27
- CacheableStats: () => CacheableStats
27
+ CacheableStats: () => CacheableStats,
28
+ KeyvCacheableMemory: () => KeyvCacheableMemory
28
29
  });
29
30
  module.exports = __toCommonJS(src_exports);
30
31
  var import_keyv = require("keyv");
31
32
  var import_hookified = require("hookified");
32
33
 
33
- // src/stats.ts
34
- var CacheableStats = class {
35
- _hits = 0;
36
- _misses = 0;
37
- _gets = 0;
38
- _sets = 0;
39
- _deletes = 0;
40
- _clears = 0;
41
- _vsize = 0;
42
- _ksize = 0;
43
- _count = 0;
44
- _enabled = false;
45
- constructor(options) {
46
- if (options?.enabled) {
47
- this._enabled = options.enabled;
48
- }
49
- }
50
- get enabled() {
51
- return this._enabled;
52
- }
53
- set enabled(enabled) {
54
- this._enabled = enabled;
55
- }
56
- get hits() {
57
- return this._hits;
58
- }
59
- get misses() {
60
- return this._misses;
61
- }
62
- get gets() {
63
- return this._gets;
64
- }
65
- get sets() {
66
- return this._sets;
67
- }
68
- get deletes() {
69
- return this._deletes;
70
- }
71
- get clears() {
72
- return this._clears;
73
- }
74
- get vsize() {
75
- return this._vsize;
76
- }
77
- get ksize() {
78
- return this._ksize;
79
- }
80
- get count() {
81
- return this._count;
82
- }
83
- incrementHits() {
84
- if (!this._enabled) {
85
- return;
86
- }
87
- this._hits++;
88
- }
89
- incrementMisses() {
90
- if (!this._enabled) {
91
- return;
92
- }
93
- this._misses++;
94
- }
95
- incrementGets() {
96
- if (!this._enabled) {
97
- return;
98
- }
99
- this._gets++;
100
- }
101
- incrementSets() {
102
- if (!this._enabled) {
103
- return;
104
- }
105
- this._sets++;
106
- }
107
- incrementDeletes() {
108
- if (!this._enabled) {
109
- return;
110
- }
111
- this._deletes++;
112
- }
113
- incrementClears() {
114
- if (!this._enabled) {
115
- return;
116
- }
117
- this._clears++;
118
- }
119
- // eslint-disable-next-line @typescript-eslint/naming-convention
120
- incrementVSize(value) {
121
- if (!this._enabled) {
122
- return;
123
- }
124
- this._vsize += this.roughSizeOfObject(value);
125
- }
126
- // eslint-disable-next-line @typescript-eslint/naming-convention
127
- decreaseVSize(value) {
128
- if (!this._enabled) {
129
- return;
130
- }
131
- this._vsize -= this.roughSizeOfObject(value);
132
- }
133
- // eslint-disable-next-line @typescript-eslint/naming-convention
134
- incrementKSize(key) {
135
- if (!this._enabled) {
136
- return;
137
- }
138
- this._ksize += this.roughSizeOfString(key);
139
- }
140
- // eslint-disable-next-line @typescript-eslint/naming-convention
141
- decreaseKSize(key) {
142
- if (!this._enabled) {
143
- return;
144
- }
145
- this._ksize -= this.roughSizeOfString(key);
146
- }
147
- incrementCount() {
148
- if (!this._enabled) {
149
- return;
150
- }
151
- this._count++;
152
- }
153
- decreaseCount() {
154
- if (!this._enabled) {
155
- return;
156
- }
157
- this._count--;
158
- }
159
- setCount(count) {
160
- if (!this._enabled) {
161
- return;
162
- }
163
- this._count = count;
164
- }
165
- roughSizeOfString(value) {
166
- return value.length * 2;
167
- }
168
- roughSizeOfObject(object) {
169
- const objectList = [];
170
- const stack = [object];
171
- let bytes = 0;
172
- while (stack.length > 0) {
173
- const value = stack.pop();
174
- if (typeof value === "boolean") {
175
- bytes += 4;
176
- } else if (typeof value === "string") {
177
- bytes += value.length * 2;
178
- } else if (typeof value === "number") {
179
- bytes += 8;
180
- } else if (typeof value === "object" && value !== null && !objectList.includes(value)) {
181
- objectList.push(value);
182
- for (const key in value) {
183
- bytes += key.length * 2;
184
- stack.push(value[key]);
185
- }
186
- }
187
- }
188
- return bytes;
189
- }
190
- reset() {
191
- this._hits = 0;
192
- this._misses = 0;
193
- this._gets = 0;
194
- this._sets = 0;
195
- this._deletes = 0;
196
- this._clears = 0;
197
- this._vsize = 0;
198
- this._ksize = 0;
199
- this._count = 0;
200
- }
201
- resetStoreValues() {
202
- this._vsize = 0;
203
- this._ksize = 0;
204
- this._count = 0;
205
- }
206
- };
207
-
208
34
  // src/memory-lru.ts
209
35
  var ListNode = class {
210
36
  // eslint-disable-next-line @typescript-eslint/parameter-properties
@@ -348,6 +174,9 @@ var CacheableMemory = class {
348
174
  get keys() {
349
175
  return this.concatStores().keys();
350
176
  }
177
+ get items() {
178
+ return this.concatStores().values();
179
+ }
351
180
  get(key) {
352
181
  const store = this.getStore(key);
353
182
  const item = store.get(key);
@@ -537,6 +366,236 @@ var CacheableMemory = class {
537
366
  }
538
367
  };
539
368
 
369
+ // src/keyv-memory.ts
370
+ var KeyvCacheableMemory = class {
371
+ opts = {
372
+ ttl: 0,
373
+ useClone: true,
374
+ lruSize: 0,
375
+ checkInterval: 0
376
+ };
377
+ namespace;
378
+ _cache = new CacheableMemory();
379
+ constructor(options) {
380
+ if (options) {
381
+ this.opts = options;
382
+ this._cache = new CacheableMemory(options);
383
+ }
384
+ }
385
+ async get(key) {
386
+ const result = this._cache.get(key);
387
+ console.log("result", result);
388
+ if (result) {
389
+ return result;
390
+ }
391
+ return void 0;
392
+ }
393
+ set(key, value, ttl) {
394
+ this._cache.set(key, value, ttl);
395
+ }
396
+ async delete(key) {
397
+ this._cache.delete(key);
398
+ return true;
399
+ }
400
+ async clear() {
401
+ this._cache.clear();
402
+ }
403
+ async has(key) {
404
+ return this._cache.has(key);
405
+ }
406
+ async getMany(keys) {
407
+ const result = [];
408
+ for (const key of keys) {
409
+ result.push(this._cache.get(key));
410
+ }
411
+ return result;
412
+ }
413
+ async deleteMany(key) {
414
+ for (const k of key) {
415
+ this._cache.delete(k);
416
+ }
417
+ return true;
418
+ }
419
+ on(event, listener) {
420
+ return this;
421
+ }
422
+ };
423
+
424
+ // src/stats.ts
425
+ var CacheableStats = class {
426
+ _hits = 0;
427
+ _misses = 0;
428
+ _gets = 0;
429
+ _sets = 0;
430
+ _deletes = 0;
431
+ _clears = 0;
432
+ _vsize = 0;
433
+ _ksize = 0;
434
+ _count = 0;
435
+ _enabled = false;
436
+ constructor(options) {
437
+ if (options?.enabled) {
438
+ this._enabled = options.enabled;
439
+ }
440
+ }
441
+ get enabled() {
442
+ return this._enabled;
443
+ }
444
+ set enabled(enabled) {
445
+ this._enabled = enabled;
446
+ }
447
+ get hits() {
448
+ return this._hits;
449
+ }
450
+ get misses() {
451
+ return this._misses;
452
+ }
453
+ get gets() {
454
+ return this._gets;
455
+ }
456
+ get sets() {
457
+ return this._sets;
458
+ }
459
+ get deletes() {
460
+ return this._deletes;
461
+ }
462
+ get clears() {
463
+ return this._clears;
464
+ }
465
+ get vsize() {
466
+ return this._vsize;
467
+ }
468
+ get ksize() {
469
+ return this._ksize;
470
+ }
471
+ get count() {
472
+ return this._count;
473
+ }
474
+ incrementHits() {
475
+ if (!this._enabled) {
476
+ return;
477
+ }
478
+ this._hits++;
479
+ }
480
+ incrementMisses() {
481
+ if (!this._enabled) {
482
+ return;
483
+ }
484
+ this._misses++;
485
+ }
486
+ incrementGets() {
487
+ if (!this._enabled) {
488
+ return;
489
+ }
490
+ this._gets++;
491
+ }
492
+ incrementSets() {
493
+ if (!this._enabled) {
494
+ return;
495
+ }
496
+ this._sets++;
497
+ }
498
+ incrementDeletes() {
499
+ if (!this._enabled) {
500
+ return;
501
+ }
502
+ this._deletes++;
503
+ }
504
+ incrementClears() {
505
+ if (!this._enabled) {
506
+ return;
507
+ }
508
+ this._clears++;
509
+ }
510
+ // eslint-disable-next-line @typescript-eslint/naming-convention
511
+ incrementVSize(value) {
512
+ if (!this._enabled) {
513
+ return;
514
+ }
515
+ this._vsize += this.roughSizeOfObject(value);
516
+ }
517
+ // eslint-disable-next-line @typescript-eslint/naming-convention
518
+ decreaseVSize(value) {
519
+ if (!this._enabled) {
520
+ return;
521
+ }
522
+ this._vsize -= this.roughSizeOfObject(value);
523
+ }
524
+ // eslint-disable-next-line @typescript-eslint/naming-convention
525
+ incrementKSize(key) {
526
+ if (!this._enabled) {
527
+ return;
528
+ }
529
+ this._ksize += this.roughSizeOfString(key);
530
+ }
531
+ // eslint-disable-next-line @typescript-eslint/naming-convention
532
+ decreaseKSize(key) {
533
+ if (!this._enabled) {
534
+ return;
535
+ }
536
+ this._ksize -= this.roughSizeOfString(key);
537
+ }
538
+ incrementCount() {
539
+ if (!this._enabled) {
540
+ return;
541
+ }
542
+ this._count++;
543
+ }
544
+ decreaseCount() {
545
+ if (!this._enabled) {
546
+ return;
547
+ }
548
+ this._count--;
549
+ }
550
+ setCount(count) {
551
+ if (!this._enabled) {
552
+ return;
553
+ }
554
+ this._count = count;
555
+ }
556
+ roughSizeOfString(value) {
557
+ return value.length * 2;
558
+ }
559
+ roughSizeOfObject(object) {
560
+ const objectList = [];
561
+ const stack = [object];
562
+ let bytes = 0;
563
+ while (stack.length > 0) {
564
+ const value = stack.pop();
565
+ if (typeof value === "boolean") {
566
+ bytes += 4;
567
+ } else if (typeof value === "string") {
568
+ bytes += value.length * 2;
569
+ } else if (typeof value === "number") {
570
+ bytes += 8;
571
+ } else if (typeof value === "object" && value !== null && !objectList.includes(value)) {
572
+ objectList.push(value);
573
+ for (const key in value) {
574
+ bytes += key.length * 2;
575
+ stack.push(value[key]);
576
+ }
577
+ }
578
+ }
579
+ return bytes;
580
+ }
581
+ reset() {
582
+ this._hits = 0;
583
+ this._misses = 0;
584
+ this._gets = 0;
585
+ this._sets = 0;
586
+ this._deletes = 0;
587
+ this._clears = 0;
588
+ this._vsize = 0;
589
+ this._ksize = 0;
590
+ this._count = 0;
591
+ }
592
+ resetStoreValues() {
593
+ this._vsize = 0;
594
+ this._ksize = 0;
595
+ this._count = 0;
596
+ }
597
+ };
598
+
540
599
  // src/index.ts
541
600
  var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => {
542
601
  CacheableHooks2["BEFORE_SET"] = "BEFORE_SET";
@@ -554,7 +613,7 @@ var CacheableEvents = /* @__PURE__ */ ((CacheableEvents2) => {
554
613
  return CacheableEvents2;
555
614
  })(CacheableEvents || {});
556
615
  var Cacheable = class extends import_hookified.Hookified {
557
- _primary = new import_keyv.Keyv({ store: new CacheableMemory() });
616
+ _primary = new import_keyv.Keyv({ store: new KeyvCacheableMemory() });
558
617
  _secondary;
559
618
  _nonBlocking = false;
560
619
  _ttl;
@@ -862,5 +921,6 @@ var Cacheable = class extends import_hookified.Hookified {
862
921
  CacheableEvents,
863
922
  CacheableHooks,
864
923
  CacheableMemory,
865
- CacheableStats
924
+ CacheableStats,
925
+ KeyvCacheableMemory
866
926
  });
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { Keyv, KeyvStoreAdapter } from 'keyv';
1
+ import { KeyvStoreAdapter, StoredData, Keyv } from 'keyv';
2
2
  import { Hookified } from 'hookified';
3
3
 
4
4
  type CacheableOptions$1 = {
@@ -52,6 +52,11 @@ type CacheableMemoryOptions = {
52
52
  lruSize?: number;
53
53
  checkInterval?: number;
54
54
  };
55
+ type CacheableItem$1 = {
56
+ key: string;
57
+ value: any;
58
+ expires?: number;
59
+ };
55
60
  declare class CacheableMemory {
56
61
  private readonly _hashCache;
57
62
  private readonly _hash0;
@@ -81,6 +86,7 @@ declare class CacheableMemory {
81
86
  set checkInterval(value: number);
82
87
  get size(): number;
83
88
  get keys(): IterableIterator<string>;
89
+ get items(): IterableIterator<CacheableItem$1>;
84
90
  get<T>(key: string): any;
85
91
  set(key: string, value: any, ttl?: number): void;
86
92
  has(key: string): boolean;
@@ -100,6 +106,21 @@ declare class CacheableMemory {
100
106
  private concatStores;
101
107
  }
102
108
 
109
+ declare class KeyvCacheableMemory implements KeyvStoreAdapter {
110
+ opts: CacheableMemoryOptions;
111
+ namespace?: string | undefined;
112
+ private readonly _cache;
113
+ constructor(options?: CacheableMemoryOptions);
114
+ get<Value>(key: string): Promise<StoredData<Value> | undefined>;
115
+ set(key: string, value: any, ttl?: number): void;
116
+ delete(key: string): Promise<boolean>;
117
+ clear(): Promise<void>;
118
+ has?(key: string): Promise<boolean>;
119
+ getMany?<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
120
+ deleteMany?(key: string[]): Promise<boolean>;
121
+ on(event: string, listener: (...arguments_: any[]) => void): this;
122
+ }
123
+
103
124
  declare enum CacheableHooks {
104
125
  BEFORE_SET = "BEFORE_SET",
105
126
  AFTER_SET = "AFTER_SET",
@@ -160,4 +181,4 @@ declare class Cacheable extends Hookified {
160
181
  private hasManyKeyv;
161
182
  }
162
183
 
163
- export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats };
184
+ export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats, KeyvCacheableMemory };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Keyv, KeyvStoreAdapter } from 'keyv';
1
+ import { KeyvStoreAdapter, StoredData, Keyv } from 'keyv';
2
2
  import { Hookified } from 'hookified';
3
3
 
4
4
  type CacheableOptions$1 = {
@@ -52,6 +52,11 @@ type CacheableMemoryOptions = {
52
52
  lruSize?: number;
53
53
  checkInterval?: number;
54
54
  };
55
+ type CacheableItem$1 = {
56
+ key: string;
57
+ value: any;
58
+ expires?: number;
59
+ };
55
60
  declare class CacheableMemory {
56
61
  private readonly _hashCache;
57
62
  private readonly _hash0;
@@ -81,6 +86,7 @@ declare class CacheableMemory {
81
86
  set checkInterval(value: number);
82
87
  get size(): number;
83
88
  get keys(): IterableIterator<string>;
89
+ get items(): IterableIterator<CacheableItem$1>;
84
90
  get<T>(key: string): any;
85
91
  set(key: string, value: any, ttl?: number): void;
86
92
  has(key: string): boolean;
@@ -100,6 +106,21 @@ declare class CacheableMemory {
100
106
  private concatStores;
101
107
  }
102
108
 
109
+ declare class KeyvCacheableMemory implements KeyvStoreAdapter {
110
+ opts: CacheableMemoryOptions;
111
+ namespace?: string | undefined;
112
+ private readonly _cache;
113
+ constructor(options?: CacheableMemoryOptions);
114
+ get<Value>(key: string): Promise<StoredData<Value> | undefined>;
115
+ set(key: string, value: any, ttl?: number): void;
116
+ delete(key: string): Promise<boolean>;
117
+ clear(): Promise<void>;
118
+ has?(key: string): Promise<boolean>;
119
+ getMany?<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
120
+ deleteMany?(key: string[]): Promise<boolean>;
121
+ on(event: string, listener: (...arguments_: any[]) => void): this;
122
+ }
123
+
103
124
  declare enum CacheableHooks {
104
125
  BEFORE_SET = "BEFORE_SET",
105
126
  AFTER_SET = "AFTER_SET",
@@ -160,4 +181,4 @@ declare class Cacheable extends Hookified {
160
181
  private hasManyKeyv;
161
182
  }
162
183
 
163
- export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats };
184
+ export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats, KeyvCacheableMemory };
package/dist/index.js CHANGED
@@ -2,181 +2,6 @@
2
2
  import { Keyv } from "keyv";
3
3
  import { Hookified } from "hookified";
4
4
 
5
- // src/stats.ts
6
- var CacheableStats = class {
7
- _hits = 0;
8
- _misses = 0;
9
- _gets = 0;
10
- _sets = 0;
11
- _deletes = 0;
12
- _clears = 0;
13
- _vsize = 0;
14
- _ksize = 0;
15
- _count = 0;
16
- _enabled = false;
17
- constructor(options) {
18
- if (options?.enabled) {
19
- this._enabled = options.enabled;
20
- }
21
- }
22
- get enabled() {
23
- return this._enabled;
24
- }
25
- set enabled(enabled) {
26
- this._enabled = enabled;
27
- }
28
- get hits() {
29
- return this._hits;
30
- }
31
- get misses() {
32
- return this._misses;
33
- }
34
- get gets() {
35
- return this._gets;
36
- }
37
- get sets() {
38
- return this._sets;
39
- }
40
- get deletes() {
41
- return this._deletes;
42
- }
43
- get clears() {
44
- return this._clears;
45
- }
46
- get vsize() {
47
- return this._vsize;
48
- }
49
- get ksize() {
50
- return this._ksize;
51
- }
52
- get count() {
53
- return this._count;
54
- }
55
- incrementHits() {
56
- if (!this._enabled) {
57
- return;
58
- }
59
- this._hits++;
60
- }
61
- incrementMisses() {
62
- if (!this._enabled) {
63
- return;
64
- }
65
- this._misses++;
66
- }
67
- incrementGets() {
68
- if (!this._enabled) {
69
- return;
70
- }
71
- this._gets++;
72
- }
73
- incrementSets() {
74
- if (!this._enabled) {
75
- return;
76
- }
77
- this._sets++;
78
- }
79
- incrementDeletes() {
80
- if (!this._enabled) {
81
- return;
82
- }
83
- this._deletes++;
84
- }
85
- incrementClears() {
86
- if (!this._enabled) {
87
- return;
88
- }
89
- this._clears++;
90
- }
91
- // eslint-disable-next-line @typescript-eslint/naming-convention
92
- incrementVSize(value) {
93
- if (!this._enabled) {
94
- return;
95
- }
96
- this._vsize += this.roughSizeOfObject(value);
97
- }
98
- // eslint-disable-next-line @typescript-eslint/naming-convention
99
- decreaseVSize(value) {
100
- if (!this._enabled) {
101
- return;
102
- }
103
- this._vsize -= this.roughSizeOfObject(value);
104
- }
105
- // eslint-disable-next-line @typescript-eslint/naming-convention
106
- incrementKSize(key) {
107
- if (!this._enabled) {
108
- return;
109
- }
110
- this._ksize += this.roughSizeOfString(key);
111
- }
112
- // eslint-disable-next-line @typescript-eslint/naming-convention
113
- decreaseKSize(key) {
114
- if (!this._enabled) {
115
- return;
116
- }
117
- this._ksize -= this.roughSizeOfString(key);
118
- }
119
- incrementCount() {
120
- if (!this._enabled) {
121
- return;
122
- }
123
- this._count++;
124
- }
125
- decreaseCount() {
126
- if (!this._enabled) {
127
- return;
128
- }
129
- this._count--;
130
- }
131
- setCount(count) {
132
- if (!this._enabled) {
133
- return;
134
- }
135
- this._count = count;
136
- }
137
- roughSizeOfString(value) {
138
- return value.length * 2;
139
- }
140
- roughSizeOfObject(object) {
141
- const objectList = [];
142
- const stack = [object];
143
- let bytes = 0;
144
- while (stack.length > 0) {
145
- const value = stack.pop();
146
- if (typeof value === "boolean") {
147
- bytes += 4;
148
- } else if (typeof value === "string") {
149
- bytes += value.length * 2;
150
- } else if (typeof value === "number") {
151
- bytes += 8;
152
- } else if (typeof value === "object" && value !== null && !objectList.includes(value)) {
153
- objectList.push(value);
154
- for (const key in value) {
155
- bytes += key.length * 2;
156
- stack.push(value[key]);
157
- }
158
- }
159
- }
160
- return bytes;
161
- }
162
- reset() {
163
- this._hits = 0;
164
- this._misses = 0;
165
- this._gets = 0;
166
- this._sets = 0;
167
- this._deletes = 0;
168
- this._clears = 0;
169
- this._vsize = 0;
170
- this._ksize = 0;
171
- this._count = 0;
172
- }
173
- resetStoreValues() {
174
- this._vsize = 0;
175
- this._ksize = 0;
176
- this._count = 0;
177
- }
178
- };
179
-
180
5
  // src/memory-lru.ts
181
6
  var ListNode = class {
182
7
  // eslint-disable-next-line @typescript-eslint/parameter-properties
@@ -320,6 +145,9 @@ var CacheableMemory = class {
320
145
  get keys() {
321
146
  return this.concatStores().keys();
322
147
  }
148
+ get items() {
149
+ return this.concatStores().values();
150
+ }
323
151
  get(key) {
324
152
  const store = this.getStore(key);
325
153
  const item = store.get(key);
@@ -509,6 +337,236 @@ var CacheableMemory = class {
509
337
  }
510
338
  };
511
339
 
340
+ // src/keyv-memory.ts
341
+ var KeyvCacheableMemory = class {
342
+ opts = {
343
+ ttl: 0,
344
+ useClone: true,
345
+ lruSize: 0,
346
+ checkInterval: 0
347
+ };
348
+ namespace;
349
+ _cache = new CacheableMemory();
350
+ constructor(options) {
351
+ if (options) {
352
+ this.opts = options;
353
+ this._cache = new CacheableMemory(options);
354
+ }
355
+ }
356
+ async get(key) {
357
+ const result = this._cache.get(key);
358
+ console.log("result", result);
359
+ if (result) {
360
+ return result;
361
+ }
362
+ return void 0;
363
+ }
364
+ set(key, value, ttl) {
365
+ this._cache.set(key, value, ttl);
366
+ }
367
+ async delete(key) {
368
+ this._cache.delete(key);
369
+ return true;
370
+ }
371
+ async clear() {
372
+ this._cache.clear();
373
+ }
374
+ async has(key) {
375
+ return this._cache.has(key);
376
+ }
377
+ async getMany(keys) {
378
+ const result = [];
379
+ for (const key of keys) {
380
+ result.push(this._cache.get(key));
381
+ }
382
+ return result;
383
+ }
384
+ async deleteMany(key) {
385
+ for (const k of key) {
386
+ this._cache.delete(k);
387
+ }
388
+ return true;
389
+ }
390
+ on(event, listener) {
391
+ return this;
392
+ }
393
+ };
394
+
395
+ // src/stats.ts
396
+ var CacheableStats = class {
397
+ _hits = 0;
398
+ _misses = 0;
399
+ _gets = 0;
400
+ _sets = 0;
401
+ _deletes = 0;
402
+ _clears = 0;
403
+ _vsize = 0;
404
+ _ksize = 0;
405
+ _count = 0;
406
+ _enabled = false;
407
+ constructor(options) {
408
+ if (options?.enabled) {
409
+ this._enabled = options.enabled;
410
+ }
411
+ }
412
+ get enabled() {
413
+ return this._enabled;
414
+ }
415
+ set enabled(enabled) {
416
+ this._enabled = enabled;
417
+ }
418
+ get hits() {
419
+ return this._hits;
420
+ }
421
+ get misses() {
422
+ return this._misses;
423
+ }
424
+ get gets() {
425
+ return this._gets;
426
+ }
427
+ get sets() {
428
+ return this._sets;
429
+ }
430
+ get deletes() {
431
+ return this._deletes;
432
+ }
433
+ get clears() {
434
+ return this._clears;
435
+ }
436
+ get vsize() {
437
+ return this._vsize;
438
+ }
439
+ get ksize() {
440
+ return this._ksize;
441
+ }
442
+ get count() {
443
+ return this._count;
444
+ }
445
+ incrementHits() {
446
+ if (!this._enabled) {
447
+ return;
448
+ }
449
+ this._hits++;
450
+ }
451
+ incrementMisses() {
452
+ if (!this._enabled) {
453
+ return;
454
+ }
455
+ this._misses++;
456
+ }
457
+ incrementGets() {
458
+ if (!this._enabled) {
459
+ return;
460
+ }
461
+ this._gets++;
462
+ }
463
+ incrementSets() {
464
+ if (!this._enabled) {
465
+ return;
466
+ }
467
+ this._sets++;
468
+ }
469
+ incrementDeletes() {
470
+ if (!this._enabled) {
471
+ return;
472
+ }
473
+ this._deletes++;
474
+ }
475
+ incrementClears() {
476
+ if (!this._enabled) {
477
+ return;
478
+ }
479
+ this._clears++;
480
+ }
481
+ // eslint-disable-next-line @typescript-eslint/naming-convention
482
+ incrementVSize(value) {
483
+ if (!this._enabled) {
484
+ return;
485
+ }
486
+ this._vsize += this.roughSizeOfObject(value);
487
+ }
488
+ // eslint-disable-next-line @typescript-eslint/naming-convention
489
+ decreaseVSize(value) {
490
+ if (!this._enabled) {
491
+ return;
492
+ }
493
+ this._vsize -= this.roughSizeOfObject(value);
494
+ }
495
+ // eslint-disable-next-line @typescript-eslint/naming-convention
496
+ incrementKSize(key) {
497
+ if (!this._enabled) {
498
+ return;
499
+ }
500
+ this._ksize += this.roughSizeOfString(key);
501
+ }
502
+ // eslint-disable-next-line @typescript-eslint/naming-convention
503
+ decreaseKSize(key) {
504
+ if (!this._enabled) {
505
+ return;
506
+ }
507
+ this._ksize -= this.roughSizeOfString(key);
508
+ }
509
+ incrementCount() {
510
+ if (!this._enabled) {
511
+ return;
512
+ }
513
+ this._count++;
514
+ }
515
+ decreaseCount() {
516
+ if (!this._enabled) {
517
+ return;
518
+ }
519
+ this._count--;
520
+ }
521
+ setCount(count) {
522
+ if (!this._enabled) {
523
+ return;
524
+ }
525
+ this._count = count;
526
+ }
527
+ roughSizeOfString(value) {
528
+ return value.length * 2;
529
+ }
530
+ roughSizeOfObject(object) {
531
+ const objectList = [];
532
+ const stack = [object];
533
+ let bytes = 0;
534
+ while (stack.length > 0) {
535
+ const value = stack.pop();
536
+ if (typeof value === "boolean") {
537
+ bytes += 4;
538
+ } else if (typeof value === "string") {
539
+ bytes += value.length * 2;
540
+ } else if (typeof value === "number") {
541
+ bytes += 8;
542
+ } else if (typeof value === "object" && value !== null && !objectList.includes(value)) {
543
+ objectList.push(value);
544
+ for (const key in value) {
545
+ bytes += key.length * 2;
546
+ stack.push(value[key]);
547
+ }
548
+ }
549
+ }
550
+ return bytes;
551
+ }
552
+ reset() {
553
+ this._hits = 0;
554
+ this._misses = 0;
555
+ this._gets = 0;
556
+ this._sets = 0;
557
+ this._deletes = 0;
558
+ this._clears = 0;
559
+ this._vsize = 0;
560
+ this._ksize = 0;
561
+ this._count = 0;
562
+ }
563
+ resetStoreValues() {
564
+ this._vsize = 0;
565
+ this._ksize = 0;
566
+ this._count = 0;
567
+ }
568
+ };
569
+
512
570
  // src/index.ts
513
571
  var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => {
514
572
  CacheableHooks2["BEFORE_SET"] = "BEFORE_SET";
@@ -526,7 +584,7 @@ var CacheableEvents = /* @__PURE__ */ ((CacheableEvents2) => {
526
584
  return CacheableEvents2;
527
585
  })(CacheableEvents || {});
528
586
  var Cacheable = class extends Hookified {
529
- _primary = new Keyv({ store: new CacheableMemory() });
587
+ _primary = new Keyv({ store: new KeyvCacheableMemory() });
530
588
  _secondary;
531
589
  _nonBlocking = false;
532
590
  _ttl;
@@ -833,5 +891,6 @@ export {
833
891
  CacheableEvents,
834
892
  CacheableHooks,
835
893
  CacheableMemory,
836
- CacheableStats
894
+ CacheableStats,
895
+ KeyvCacheableMemory
837
896
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cacheable",
3
- "version": "1.5.0",
3
+ "version": "1.6.1",
4
4
  "description": "Simple Caching Engine using Keyv",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",