cacheable 1.1.0 → 1.3.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 +24 -11
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +24 -11
- package/package.json +29 -3
package/dist/index.cjs
CHANGED
|
@@ -518,6 +518,7 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
518
518
|
_primary = new import_keyv.Keyv({ store: new CacheableMemory() });
|
|
519
519
|
_secondary;
|
|
520
520
|
_nonBlocking = false;
|
|
521
|
+
_ttl;
|
|
521
522
|
_stats = new CacheableStats({ enabled: false });
|
|
522
523
|
constructor(options) {
|
|
523
524
|
super();
|
|
@@ -533,6 +534,9 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
533
534
|
if (options?.stats) {
|
|
534
535
|
this._stats.enabled = options.stats;
|
|
535
536
|
}
|
|
537
|
+
if (options?.ttl) {
|
|
538
|
+
this._ttl = options.ttl;
|
|
539
|
+
}
|
|
536
540
|
}
|
|
537
541
|
get stats() {
|
|
538
542
|
return this._stats;
|
|
@@ -555,6 +559,12 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
555
559
|
set nonBlocking(nonBlocking) {
|
|
556
560
|
this._nonBlocking = nonBlocking;
|
|
557
561
|
}
|
|
562
|
+
get ttl() {
|
|
563
|
+
return this._ttl;
|
|
564
|
+
}
|
|
565
|
+
set ttl(ttl) {
|
|
566
|
+
this._ttl = ttl;
|
|
567
|
+
}
|
|
558
568
|
setPrimary(primary) {
|
|
559
569
|
this._primary = primary instanceof import_keyv.Keyv ? primary : new import_keyv.Keyv(primary);
|
|
560
570
|
}
|
|
@@ -569,12 +579,12 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
569
579
|
if (!result && this._secondary) {
|
|
570
580
|
result = await this._secondary.get(key);
|
|
571
581
|
if (result) {
|
|
572
|
-
await this._primary.set(key, result);
|
|
582
|
+
await this._primary.set(key, result, this._ttl);
|
|
573
583
|
}
|
|
574
584
|
}
|
|
575
585
|
await this.hook("AFTER_GET" /* AFTER_GET */, { key, result });
|
|
576
586
|
} catch (error) {
|
|
577
|
-
|
|
587
|
+
this.emit("error" /* ERROR */, error);
|
|
578
588
|
}
|
|
579
589
|
if (this.stats.enabled) {
|
|
580
590
|
if (result) {
|
|
@@ -602,13 +612,13 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
602
612
|
for (const [i, key] of keys.entries()) {
|
|
603
613
|
if (!result[i] && secondaryResult[i]) {
|
|
604
614
|
result[i] = secondaryResult[i];
|
|
605
|
-
await this._primary.set(key, secondaryResult[i]);
|
|
615
|
+
await this._primary.set(key, secondaryResult[i], this._ttl);
|
|
606
616
|
}
|
|
607
617
|
}
|
|
608
618
|
}
|
|
609
619
|
await this.hook("AFTER_GET_MANY" /* AFTER_GET_MANY */, { keys, result });
|
|
610
620
|
} catch (error) {
|
|
611
|
-
|
|
621
|
+
this.emit("error" /* ERROR */, error);
|
|
612
622
|
}
|
|
613
623
|
if (this.stats.enabled) {
|
|
614
624
|
for (const item of result) {
|
|
@@ -624,12 +634,14 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
624
634
|
}
|
|
625
635
|
async set(key, value, ttl) {
|
|
626
636
|
let result = false;
|
|
637
|
+
const finalTtl = ttl ?? this._ttl;
|
|
627
638
|
try {
|
|
628
|
-
|
|
639
|
+
const item = { key, value, ttl: finalTtl };
|
|
640
|
+
await this.hook("BEFORE_SET" /* BEFORE_SET */, item);
|
|
629
641
|
const promises = [];
|
|
630
|
-
promises.push(this._primary.set(key, value, ttl));
|
|
642
|
+
promises.push(this._primary.set(item.key, item.value, item.ttl));
|
|
631
643
|
if (this._secondary) {
|
|
632
|
-
promises.push(this._secondary.set(key, value, ttl));
|
|
644
|
+
promises.push(this._secondary.set(item.key, item.value, item.ttl));
|
|
633
645
|
}
|
|
634
646
|
if (this._nonBlocking) {
|
|
635
647
|
result = await Promise.race(promises);
|
|
@@ -637,9 +649,9 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
637
649
|
const results = await Promise.all(promises);
|
|
638
650
|
result = results[0];
|
|
639
651
|
}
|
|
640
|
-
await this.hook("AFTER_SET" /* AFTER_SET */,
|
|
652
|
+
await this.hook("AFTER_SET" /* AFTER_SET */, item);
|
|
641
653
|
} catch (error) {
|
|
642
|
-
|
|
654
|
+
this.emit("error" /* ERROR */, error);
|
|
643
655
|
}
|
|
644
656
|
if (this.stats.enabled) {
|
|
645
657
|
this.stats.incrementKSize(key);
|
|
@@ -663,7 +675,7 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
663
675
|
}
|
|
664
676
|
await this.hook("AFTER_SET_MANY" /* AFTER_SET_MANY */, items);
|
|
665
677
|
} catch (error) {
|
|
666
|
-
|
|
678
|
+
this.emit("error" /* ERROR */, error);
|
|
667
679
|
}
|
|
668
680
|
if (this.stats.enabled) {
|
|
669
681
|
for (const item of items) {
|
|
@@ -791,7 +803,8 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
791
803
|
async setManyKeyv(keyv, items) {
|
|
792
804
|
const promises = [];
|
|
793
805
|
for (const item of items) {
|
|
794
|
-
|
|
806
|
+
const finalTtl = item.ttl ?? this._ttl;
|
|
807
|
+
promises.push(keyv.set(item.key, item.value, finalTtl));
|
|
795
808
|
}
|
|
796
809
|
await Promise.all(promises);
|
|
797
810
|
return true;
|
package/dist/index.d.cts
CHANGED
|
@@ -115,11 +115,13 @@ type CacheableOptions = {
|
|
|
115
115
|
secondary?: Keyv | KeyvStoreAdapter;
|
|
116
116
|
stats?: boolean;
|
|
117
117
|
nonBlocking?: boolean;
|
|
118
|
+
ttl?: number;
|
|
118
119
|
};
|
|
119
120
|
declare class Cacheable extends Hookified {
|
|
120
121
|
private _primary;
|
|
121
122
|
private _secondary;
|
|
122
123
|
private _nonBlocking;
|
|
124
|
+
private _ttl?;
|
|
123
125
|
private readonly _stats;
|
|
124
126
|
constructor(options?: CacheableOptions);
|
|
125
127
|
get stats(): CacheableStats;
|
|
@@ -129,6 +131,8 @@ declare class Cacheable extends Hookified {
|
|
|
129
131
|
set secondary(secondary: Keyv | undefined);
|
|
130
132
|
get nonBlocking(): boolean;
|
|
131
133
|
set nonBlocking(nonBlocking: boolean);
|
|
134
|
+
get ttl(): number | undefined;
|
|
135
|
+
set ttl(ttl: number | undefined);
|
|
132
136
|
setPrimary(primary: Keyv | KeyvStoreAdapter): void;
|
|
133
137
|
setSecondary(secondary: Keyv | KeyvStoreAdapter): void;
|
|
134
138
|
get<T>(key: string): Promise<T | undefined>;
|
package/dist/index.d.ts
CHANGED
|
@@ -115,11 +115,13 @@ type CacheableOptions = {
|
|
|
115
115
|
secondary?: Keyv | KeyvStoreAdapter;
|
|
116
116
|
stats?: boolean;
|
|
117
117
|
nonBlocking?: boolean;
|
|
118
|
+
ttl?: number;
|
|
118
119
|
};
|
|
119
120
|
declare class Cacheable extends Hookified {
|
|
120
121
|
private _primary;
|
|
121
122
|
private _secondary;
|
|
122
123
|
private _nonBlocking;
|
|
124
|
+
private _ttl?;
|
|
123
125
|
private readonly _stats;
|
|
124
126
|
constructor(options?: CacheableOptions);
|
|
125
127
|
get stats(): CacheableStats;
|
|
@@ -129,6 +131,8 @@ declare class Cacheable extends Hookified {
|
|
|
129
131
|
set secondary(secondary: Keyv | undefined);
|
|
130
132
|
get nonBlocking(): boolean;
|
|
131
133
|
set nonBlocking(nonBlocking: boolean);
|
|
134
|
+
get ttl(): number | undefined;
|
|
135
|
+
set ttl(ttl: number | undefined);
|
|
132
136
|
setPrimary(primary: Keyv | KeyvStoreAdapter): void;
|
|
133
137
|
setSecondary(secondary: Keyv | KeyvStoreAdapter): void;
|
|
134
138
|
get<T>(key: string): Promise<T | undefined>;
|
package/dist/index.js
CHANGED
|
@@ -490,6 +490,7 @@ var Cacheable = class extends Hookified {
|
|
|
490
490
|
_primary = new Keyv({ store: new CacheableMemory() });
|
|
491
491
|
_secondary;
|
|
492
492
|
_nonBlocking = false;
|
|
493
|
+
_ttl;
|
|
493
494
|
_stats = new CacheableStats({ enabled: false });
|
|
494
495
|
constructor(options) {
|
|
495
496
|
super();
|
|
@@ -505,6 +506,9 @@ var Cacheable = class extends Hookified {
|
|
|
505
506
|
if (options?.stats) {
|
|
506
507
|
this._stats.enabled = options.stats;
|
|
507
508
|
}
|
|
509
|
+
if (options?.ttl) {
|
|
510
|
+
this._ttl = options.ttl;
|
|
511
|
+
}
|
|
508
512
|
}
|
|
509
513
|
get stats() {
|
|
510
514
|
return this._stats;
|
|
@@ -527,6 +531,12 @@ var Cacheable = class extends Hookified {
|
|
|
527
531
|
set nonBlocking(nonBlocking) {
|
|
528
532
|
this._nonBlocking = nonBlocking;
|
|
529
533
|
}
|
|
534
|
+
get ttl() {
|
|
535
|
+
return this._ttl;
|
|
536
|
+
}
|
|
537
|
+
set ttl(ttl) {
|
|
538
|
+
this._ttl = ttl;
|
|
539
|
+
}
|
|
530
540
|
setPrimary(primary) {
|
|
531
541
|
this._primary = primary instanceof Keyv ? primary : new Keyv(primary);
|
|
532
542
|
}
|
|
@@ -541,12 +551,12 @@ var Cacheable = class extends Hookified {
|
|
|
541
551
|
if (!result && this._secondary) {
|
|
542
552
|
result = await this._secondary.get(key);
|
|
543
553
|
if (result) {
|
|
544
|
-
await this._primary.set(key, result);
|
|
554
|
+
await this._primary.set(key, result, this._ttl);
|
|
545
555
|
}
|
|
546
556
|
}
|
|
547
557
|
await this.hook("AFTER_GET" /* AFTER_GET */, { key, result });
|
|
548
558
|
} catch (error) {
|
|
549
|
-
|
|
559
|
+
this.emit("error" /* ERROR */, error);
|
|
550
560
|
}
|
|
551
561
|
if (this.stats.enabled) {
|
|
552
562
|
if (result) {
|
|
@@ -574,13 +584,13 @@ var Cacheable = class extends Hookified {
|
|
|
574
584
|
for (const [i, key] of keys.entries()) {
|
|
575
585
|
if (!result[i] && secondaryResult[i]) {
|
|
576
586
|
result[i] = secondaryResult[i];
|
|
577
|
-
await this._primary.set(key, secondaryResult[i]);
|
|
587
|
+
await this._primary.set(key, secondaryResult[i], this._ttl);
|
|
578
588
|
}
|
|
579
589
|
}
|
|
580
590
|
}
|
|
581
591
|
await this.hook("AFTER_GET_MANY" /* AFTER_GET_MANY */, { keys, result });
|
|
582
592
|
} catch (error) {
|
|
583
|
-
|
|
593
|
+
this.emit("error" /* ERROR */, error);
|
|
584
594
|
}
|
|
585
595
|
if (this.stats.enabled) {
|
|
586
596
|
for (const item of result) {
|
|
@@ -596,12 +606,14 @@ var Cacheable = class extends Hookified {
|
|
|
596
606
|
}
|
|
597
607
|
async set(key, value, ttl) {
|
|
598
608
|
let result = false;
|
|
609
|
+
const finalTtl = ttl ?? this._ttl;
|
|
599
610
|
try {
|
|
600
|
-
|
|
611
|
+
const item = { key, value, ttl: finalTtl };
|
|
612
|
+
await this.hook("BEFORE_SET" /* BEFORE_SET */, item);
|
|
601
613
|
const promises = [];
|
|
602
|
-
promises.push(this._primary.set(key, value, ttl));
|
|
614
|
+
promises.push(this._primary.set(item.key, item.value, item.ttl));
|
|
603
615
|
if (this._secondary) {
|
|
604
|
-
promises.push(this._secondary.set(key, value, ttl));
|
|
616
|
+
promises.push(this._secondary.set(item.key, item.value, item.ttl));
|
|
605
617
|
}
|
|
606
618
|
if (this._nonBlocking) {
|
|
607
619
|
result = await Promise.race(promises);
|
|
@@ -609,9 +621,9 @@ var Cacheable = class extends Hookified {
|
|
|
609
621
|
const results = await Promise.all(promises);
|
|
610
622
|
result = results[0];
|
|
611
623
|
}
|
|
612
|
-
await this.hook("AFTER_SET" /* AFTER_SET */,
|
|
624
|
+
await this.hook("AFTER_SET" /* AFTER_SET */, item);
|
|
613
625
|
} catch (error) {
|
|
614
|
-
|
|
626
|
+
this.emit("error" /* ERROR */, error);
|
|
615
627
|
}
|
|
616
628
|
if (this.stats.enabled) {
|
|
617
629
|
this.stats.incrementKSize(key);
|
|
@@ -635,7 +647,7 @@ var Cacheable = class extends Hookified {
|
|
|
635
647
|
}
|
|
636
648
|
await this.hook("AFTER_SET_MANY" /* AFTER_SET_MANY */, items);
|
|
637
649
|
} catch (error) {
|
|
638
|
-
|
|
650
|
+
this.emit("error" /* ERROR */, error);
|
|
639
651
|
}
|
|
640
652
|
if (this.stats.enabled) {
|
|
641
653
|
for (const item of items) {
|
|
@@ -763,7 +775,8 @@ var Cacheable = class extends Hookified {
|
|
|
763
775
|
async setManyKeyv(keyv, items) {
|
|
764
776
|
const promises = [];
|
|
765
777
|
for (const item of items) {
|
|
766
|
-
|
|
778
|
+
const finalTtl = item.ttl ?? this._ttl;
|
|
779
|
+
promises.push(keyv.set(item.key, item.value, finalTtl));
|
|
767
780
|
}
|
|
768
781
|
await Promise.all(promises);
|
|
769
782
|
return true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cacheable",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Simple Caching Engine using Keyv",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -28,9 +28,35 @@
|
|
|
28
28
|
"xo": "^0.59.3"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"hookified": "^
|
|
32
|
-
"keyv": "^5.0.
|
|
31
|
+
"hookified": "^1.1.0",
|
|
32
|
+
"keyv": "^5.0.3"
|
|
33
33
|
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"cacheable",
|
|
36
|
+
"high performance",
|
|
37
|
+
"layer 1 caching",
|
|
38
|
+
"layer 2 caching",
|
|
39
|
+
"distributed caching",
|
|
40
|
+
"Keyv storage engine",
|
|
41
|
+
"memory caching",
|
|
42
|
+
"LRU cache",
|
|
43
|
+
"expiration",
|
|
44
|
+
"CacheableMemory",
|
|
45
|
+
"offline support",
|
|
46
|
+
"distributed sync",
|
|
47
|
+
"secondary store",
|
|
48
|
+
"primary store",
|
|
49
|
+
"non-blocking operations",
|
|
50
|
+
"cache statistics",
|
|
51
|
+
"layered caching",
|
|
52
|
+
"fault tolerant",
|
|
53
|
+
"scalable cache",
|
|
54
|
+
"in-memory cache",
|
|
55
|
+
"distributed cache",
|
|
56
|
+
"lruSize",
|
|
57
|
+
"lru",
|
|
58
|
+
"multi-tier cache"
|
|
59
|
+
],
|
|
34
60
|
"files": [
|
|
35
61
|
"dist",
|
|
36
62
|
"LICENSE"
|