@postgresx/noredis 0.10.0 → 0.11.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/README.md +5 -0
- package/dist/{chunk-7T5O55TM.js → chunk-QIWUTOLC.js} +50 -15
- package/dist/index.js +1 -1
- package/dist/kv-cache.d.ts +20 -0
- package/dist/kv-cache.js +1 -1
- package/dist/kv.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -304,6 +304,11 @@ short negative cache. Set `l1: { negativeTtlMs: 250 }` to remember a miss for
|
|
|
304
304
|
at most 250ms; `set()` and delete notifications clear the entry immediately,
|
|
305
305
|
and `stats()` counts those reads separately as `l1NegativeHits`.
|
|
306
306
|
|
|
307
|
+
L1 eviction is by entry count (`l1.max`) and, when configured, by approximate
|
|
308
|
+
bytes. Set `l1: { maxBytes: 8_388_608, maxEntryBytes: 65_536 }` to cap the
|
|
309
|
+
cache and refuse oversized single values; `stats()` reports `l1Bytes` and
|
|
310
|
+
`l1MaxBytes`.
|
|
311
|
+
|
|
307
312
|
## Pub/Sub
|
|
308
313
|
|
|
309
314
|
Publishing uses only the configured SQL adapter. Bun LISTEN/NOTIFY consumption
|
|
@@ -4,6 +4,15 @@ var DEFAULT_NAMESPACE = "default";
|
|
|
4
4
|
var DEFAULT_L1_MAX = 1e4;
|
|
5
5
|
var DEFAULT_L1_TTL_MS = 6e4;
|
|
6
6
|
var DEFAULT_NOTIFY_CHANNEL = "pg_kv_cache_invalidate";
|
|
7
|
+
var textEncoder = new TextEncoder();
|
|
8
|
+
function estimateValueBytes(value) {
|
|
9
|
+
try {
|
|
10
|
+
const json = JSON.stringify(value);
|
|
11
|
+
return json === void 0 ? 0 : textEncoder.encode(json).length;
|
|
12
|
+
} catch {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
7
16
|
function randomId() {
|
|
8
17
|
const cryptoApi = globalThis.crypto;
|
|
9
18
|
if (cryptoApi?.randomUUID) return cryptoApi.randomUUID();
|
|
@@ -78,6 +87,8 @@ var PgKvCache = class {
|
|
|
78
87
|
l1Max;
|
|
79
88
|
l1TtlMs;
|
|
80
89
|
l1NegativeTtlMs;
|
|
90
|
+
l1MaxBytes;
|
|
91
|
+
l1MaxEntryBytes;
|
|
81
92
|
notifyEnabled;
|
|
82
93
|
serializer;
|
|
83
94
|
singleflight;
|
|
@@ -88,6 +99,7 @@ var PgKvCache = class {
|
|
|
88
99
|
l1Misses = 0;
|
|
89
100
|
l1NegativeHits = 0;
|
|
90
101
|
l1Paused = false;
|
|
102
|
+
l1Bytes = 0;
|
|
91
103
|
coalescedReads = 0;
|
|
92
104
|
invalidationListener = null;
|
|
93
105
|
invalidationUnsubscribers = [];
|
|
@@ -102,6 +114,8 @@ var PgKvCache = class {
|
|
|
102
114
|
this.l1Max = options.l1 && options.l1.max !== void 0 ? options.l1.max : DEFAULT_L1_MAX;
|
|
103
115
|
this.l1TtlMs = options.l1 && options.l1.ttlMs !== void 0 ? options.l1.ttlMs : DEFAULT_L1_TTL_MS;
|
|
104
116
|
this.l1NegativeTtlMs = options.l1 && options.l1.negativeTtlMs !== void 0 && options.l1.negativeTtlMs > 0 ? options.l1.negativeTtlMs : 0;
|
|
117
|
+
this.l1MaxBytes = options.l1 && options.l1.maxBytes !== void 0 && options.l1.maxBytes > 0 ? options.l1.maxBytes : 0;
|
|
118
|
+
this.l1MaxEntryBytes = options.l1 && options.l1.maxEntryBytes !== void 0 && options.l1.maxEntryBytes > 0 ? options.l1.maxEntryBytes : 0;
|
|
105
119
|
this.notifyEnabled = options.notify !== false && options.notify?.enabled !== false;
|
|
106
120
|
this.notifyChannel = options.notify && options.notify.channel ? options.notify.channel : DEFAULT_NOTIFY_CHANNEL;
|
|
107
121
|
this.serializer = options.serializer ?? {
|
|
@@ -446,7 +460,7 @@ var PgKvCache = class {
|
|
|
446
460
|
}
|
|
447
461
|
invalidateAll() {
|
|
448
462
|
this.l1Generation += 1;
|
|
449
|
-
this.
|
|
463
|
+
this.clearL1();
|
|
450
464
|
}
|
|
451
465
|
startInvalidationListener(factory, options = {}) {
|
|
452
466
|
this.stopInvalidationListener();
|
|
@@ -488,13 +502,17 @@ var PgKvCache = class {
|
|
|
488
502
|
pauseL1() {
|
|
489
503
|
this.l1Paused = true;
|
|
490
504
|
this.l1Generation += 1;
|
|
491
|
-
this.
|
|
505
|
+
this.clearL1();
|
|
492
506
|
}
|
|
493
507
|
resumeL1() {
|
|
494
508
|
this.l1Generation += 1;
|
|
495
|
-
this.
|
|
509
|
+
this.clearL1();
|
|
496
510
|
this.l1Paused = false;
|
|
497
511
|
}
|
|
512
|
+
clearL1() {
|
|
513
|
+
this.l1.clear();
|
|
514
|
+
this.l1Bytes = 0;
|
|
515
|
+
}
|
|
498
516
|
handleNotification(payload) {
|
|
499
517
|
const event = typeof payload === "string" ? this.parseNotification(payload) : payload;
|
|
500
518
|
if (!event || event.namespace !== this.namespace || event.senderId === this.instanceId) return false;
|
|
@@ -506,7 +524,7 @@ var PgKvCache = class {
|
|
|
506
524
|
if (!event.prefix) return false;
|
|
507
525
|
this.l1Generation += 1;
|
|
508
526
|
for (const key of this.l1.keys()) {
|
|
509
|
-
if (key.startsWith(event.prefix)) this.
|
|
527
|
+
if (key.startsWith(event.prefix)) this.removeEntry(key);
|
|
510
528
|
}
|
|
511
529
|
return true;
|
|
512
530
|
}
|
|
@@ -659,7 +677,9 @@ var PgKvCache = class {
|
|
|
659
677
|
l1NegativeHits: this.l1NegativeHits,
|
|
660
678
|
l1Paused: this.l1Paused,
|
|
661
679
|
inflightReads: this.inflight.size,
|
|
662
|
-
coalescedReads: this.coalescedReads
|
|
680
|
+
coalescedReads: this.coalescedReads,
|
|
681
|
+
l1Bytes: this.l1Bytes,
|
|
682
|
+
l1MaxBytes: this.l1MaxBytes
|
|
663
683
|
};
|
|
664
684
|
}
|
|
665
685
|
async publish(event) {
|
|
@@ -717,7 +737,7 @@ var PgKvCache = class {
|
|
|
717
737
|
return { hit: false };
|
|
718
738
|
}
|
|
719
739
|
if (entry.expiresAt !== null && entry.expiresAt <= this.now()) {
|
|
720
|
-
this.
|
|
740
|
+
this.removeEntry(key);
|
|
721
741
|
this.l1Misses += 1;
|
|
722
742
|
return { hit: false };
|
|
723
743
|
}
|
|
@@ -730,12 +750,21 @@ var PgKvCache = class {
|
|
|
730
750
|
this.l1.set(key, entry);
|
|
731
751
|
return { hit: true, value: entry.value };
|
|
732
752
|
}
|
|
753
|
+
removeEntry(key) {
|
|
754
|
+
const entry = this.l1.get(key);
|
|
755
|
+
if (!entry) return;
|
|
756
|
+
this.l1.delete(key);
|
|
757
|
+
this.l1Bytes -= entry.bytes ?? 0;
|
|
758
|
+
}
|
|
759
|
+
tracksBytes() {
|
|
760
|
+
return this.l1MaxBytes > 0 || this.l1MaxEntryBytes > 0;
|
|
761
|
+
}
|
|
733
762
|
setNegativeL1(key, generation = this.l1Generation) {
|
|
734
763
|
if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0 || this.l1NegativeTtlMs <= 0) return;
|
|
735
764
|
if (generation !== this.l1Generation) return;
|
|
736
765
|
const expiresAt = this.now() + this.l1NegativeTtlMs;
|
|
737
|
-
this.
|
|
738
|
-
this.l1.set(key, { value: null, expiresAt, negative: true });
|
|
766
|
+
this.removeEntry(key);
|
|
767
|
+
this.l1.set(key, { value: null, expiresAt, negative: true, bytes: 0 });
|
|
739
768
|
this.enforceL1Max();
|
|
740
769
|
}
|
|
741
770
|
setL1(key, value, l2ExpiresAt, generation = this.l1Generation) {
|
|
@@ -743,11 +772,17 @@ var PgKvCache = class {
|
|
|
743
772
|
if (generation !== this.l1Generation) return;
|
|
744
773
|
const l1ExpiresAt = this.computeL1ExpiresAt(l2ExpiresAt);
|
|
745
774
|
if (l1ExpiresAt !== null && l1ExpiresAt <= this.now()) {
|
|
746
|
-
this.
|
|
775
|
+
this.removeEntry(key);
|
|
747
776
|
return;
|
|
748
777
|
}
|
|
749
|
-
this.
|
|
750
|
-
this.
|
|
778
|
+
const bytes = this.tracksBytes() ? estimateValueBytes(value) : 0;
|
|
779
|
+
if (this.l1MaxEntryBytes > 0 && bytes > this.l1MaxEntryBytes) {
|
|
780
|
+
this.removeEntry(key);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
this.removeEntry(key);
|
|
784
|
+
this.l1.set(key, { value, expiresAt: l1ExpiresAt, bytes });
|
|
785
|
+
this.l1Bytes += bytes;
|
|
751
786
|
this.enforceL1Max();
|
|
752
787
|
}
|
|
753
788
|
computeL1ExpiresAt(l2ExpiresAt) {
|
|
@@ -759,20 +794,20 @@ var PgKvCache = class {
|
|
|
759
794
|
deleteL1(key) {
|
|
760
795
|
if (!this.l1Enabled) return;
|
|
761
796
|
this.l1Generation += 1;
|
|
762
|
-
this.
|
|
797
|
+
this.removeEntry(key);
|
|
763
798
|
}
|
|
764
799
|
cleanupL1() {
|
|
765
800
|
if (!this.l1Enabled) return;
|
|
766
801
|
const now = this.now();
|
|
767
802
|
for (const [key, entry] of this.l1.entries()) {
|
|
768
|
-
if (entry.expiresAt !== null && entry.expiresAt <= now) this.
|
|
803
|
+
if (entry.expiresAt !== null && entry.expiresAt <= now) this.removeEntry(key);
|
|
769
804
|
}
|
|
770
805
|
}
|
|
771
806
|
enforceL1Max() {
|
|
772
|
-
while (this.l1.size > this.l1Max) {
|
|
807
|
+
while (this.l1.size > this.l1Max || this.l1MaxBytes > 0 && this.l1Bytes > this.l1MaxBytes) {
|
|
773
808
|
const oldest = this.l1.keys().next();
|
|
774
809
|
if (oldest.done) return;
|
|
775
|
-
this.
|
|
810
|
+
this.removeEntry(oldest.value);
|
|
776
811
|
}
|
|
777
812
|
}
|
|
778
813
|
};
|
package/dist/index.js
CHANGED
package/dist/kv-cache.d.ts
CHANGED
|
@@ -15,6 +15,16 @@ interface PgKvCacheL1Options {
|
|
|
15
15
|
* set/delete notification clears the entry immediately.
|
|
16
16
|
*/
|
|
17
17
|
negativeTtlMs?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Evict least-recently-used entries once the approximate cache size exceeds
|
|
20
|
+
* this many bytes. Defaults to 0 (no byte cap; only `max` entries apply).
|
|
21
|
+
*/
|
|
22
|
+
maxBytes?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Refuse to cache any single value larger than this many bytes. Defaults to
|
|
25
|
+
* 0 (no per-value limit).
|
|
26
|
+
*/
|
|
27
|
+
maxEntryBytes?: number;
|
|
18
28
|
}
|
|
19
29
|
interface PgKvCacheNotifyOptions {
|
|
20
30
|
channel?: string;
|
|
@@ -90,6 +100,10 @@ interface PgKvCacheStats {
|
|
|
90
100
|
inflightReads: number;
|
|
91
101
|
/** Monotonic count of reads that joined an in-flight query for the same key. */
|
|
92
102
|
coalescedReads: number;
|
|
103
|
+
/** Approximate bytes currently held by L1 (0 when byte accounting is off). */
|
|
104
|
+
l1Bytes: number;
|
|
105
|
+
/** Configured byte cap, or 0 when unlimited. */
|
|
106
|
+
l1MaxBytes: number;
|
|
93
107
|
}
|
|
94
108
|
declare class PgKvCache {
|
|
95
109
|
readonly namespace: string;
|
|
@@ -103,6 +117,8 @@ declare class PgKvCache {
|
|
|
103
117
|
private readonly l1Max;
|
|
104
118
|
private readonly l1TtlMs;
|
|
105
119
|
private readonly l1NegativeTtlMs;
|
|
120
|
+
private readonly l1MaxBytes;
|
|
121
|
+
private readonly l1MaxEntryBytes;
|
|
106
122
|
private readonly notifyEnabled;
|
|
107
123
|
private readonly serializer;
|
|
108
124
|
private readonly singleflight;
|
|
@@ -113,6 +129,7 @@ declare class PgKvCache {
|
|
|
113
129
|
private l1Misses;
|
|
114
130
|
private l1NegativeHits;
|
|
115
131
|
private l1Paused;
|
|
132
|
+
private l1Bytes;
|
|
116
133
|
private coalescedReads;
|
|
117
134
|
private invalidationListener;
|
|
118
135
|
private invalidationUnsubscribers;
|
|
@@ -149,6 +166,7 @@ declare class PgKvCache {
|
|
|
149
166
|
stopInvalidationListener(): void;
|
|
150
167
|
private pauseL1;
|
|
151
168
|
private resumeL1;
|
|
169
|
+
private clearL1;
|
|
152
170
|
handleNotification(payload: string | PgKvNotification): boolean;
|
|
153
171
|
keys(pattern?: string, limit?: number): Promise<string[]>;
|
|
154
172
|
scan(cursor?: string | null, count?: number, pattern?: string): Promise<{
|
|
@@ -171,6 +189,8 @@ declare class PgKvCache {
|
|
|
171
189
|
private resolveNotifyOptions;
|
|
172
190
|
private parseNotification;
|
|
173
191
|
private getL1;
|
|
192
|
+
private removeEntry;
|
|
193
|
+
private tracksBytes;
|
|
174
194
|
private setNegativeL1;
|
|
175
195
|
private setL1;
|
|
176
196
|
private computeL1ExpiresAt;
|
package/dist/kv-cache.js
CHANGED
package/dist/kv.js
CHANGED
package/package.json
CHANGED