cacheable 1.1.0 → 1.2.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 +19 -7
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +19 -7
- package/package.json +1 -1
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,7 +579,7 @@ 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 });
|
|
@@ -602,7 +612,7 @@ 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
|
}
|
|
@@ -624,12 +634,13 @@ 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
|
-
await this.hook("BEFORE_SET" /* BEFORE_SET */, { key, value,
|
|
639
|
+
await this.hook("BEFORE_SET" /* BEFORE_SET */, { key, value, finalTtl });
|
|
629
640
|
const promises = [];
|
|
630
|
-
promises.push(this._primary.set(key, value,
|
|
641
|
+
promises.push(this._primary.set(key, value, finalTtl));
|
|
631
642
|
if (this._secondary) {
|
|
632
|
-
promises.push(this._secondary.set(key, value,
|
|
643
|
+
promises.push(this._secondary.set(key, value, finalTtl));
|
|
633
644
|
}
|
|
634
645
|
if (this._nonBlocking) {
|
|
635
646
|
result = await Promise.race(promises);
|
|
@@ -637,7 +648,7 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
637
648
|
const results = await Promise.all(promises);
|
|
638
649
|
result = results[0];
|
|
639
650
|
}
|
|
640
|
-
await this.hook("AFTER_SET" /* AFTER_SET */, { key, value,
|
|
651
|
+
await this.hook("AFTER_SET" /* AFTER_SET */, { key, value, finalTtl });
|
|
641
652
|
} catch (error) {
|
|
642
653
|
await this.emit("error" /* ERROR */, error);
|
|
643
654
|
}
|
|
@@ -791,7 +802,8 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
791
802
|
async setManyKeyv(keyv, items) {
|
|
792
803
|
const promises = [];
|
|
793
804
|
for (const item of items) {
|
|
794
|
-
|
|
805
|
+
const finalTtl = item.ttl ?? this._ttl;
|
|
806
|
+
promises.push(keyv.set(item.key, item.value, finalTtl));
|
|
795
807
|
}
|
|
796
808
|
await Promise.all(promises);
|
|
797
809
|
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,7 +551,7 @@ 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 });
|
|
@@ -574,7 +584,7 @@ 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
|
}
|
|
@@ -596,12 +606,13 @@ 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
|
-
await this.hook("BEFORE_SET" /* BEFORE_SET */, { key, value,
|
|
611
|
+
await this.hook("BEFORE_SET" /* BEFORE_SET */, { key, value, finalTtl });
|
|
601
612
|
const promises = [];
|
|
602
|
-
promises.push(this._primary.set(key, value,
|
|
613
|
+
promises.push(this._primary.set(key, value, finalTtl));
|
|
603
614
|
if (this._secondary) {
|
|
604
|
-
promises.push(this._secondary.set(key, value,
|
|
615
|
+
promises.push(this._secondary.set(key, value, finalTtl));
|
|
605
616
|
}
|
|
606
617
|
if (this._nonBlocking) {
|
|
607
618
|
result = await Promise.race(promises);
|
|
@@ -609,7 +620,7 @@ var Cacheable = class extends Hookified {
|
|
|
609
620
|
const results = await Promise.all(promises);
|
|
610
621
|
result = results[0];
|
|
611
622
|
}
|
|
612
|
-
await this.hook("AFTER_SET" /* AFTER_SET */, { key, value,
|
|
623
|
+
await this.hook("AFTER_SET" /* AFTER_SET */, { key, value, finalTtl });
|
|
613
624
|
} catch (error) {
|
|
614
625
|
await this.emit("error" /* ERROR */, error);
|
|
615
626
|
}
|
|
@@ -763,7 +774,8 @@ var Cacheable = class extends Hookified {
|
|
|
763
774
|
async setManyKeyv(keyv, items) {
|
|
764
775
|
const promises = [];
|
|
765
776
|
for (const item of items) {
|
|
766
|
-
|
|
777
|
+
const finalTtl = item.ttl ?? this._ttl;
|
|
778
|
+
promises.push(keyv.set(item.key, item.value, finalTtl));
|
|
767
779
|
}
|
|
768
780
|
await Promise.all(promises);
|
|
769
781
|
return true;
|