@postgresx/noredis 0.9.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 +10 -0
- package/dist/{chunk-YB4P7SGQ.js → chunk-QIWUTOLC.js} +70 -16
- package/dist/index.js +1 -1
- package/dist/kv-cache.d.ts +32 -0
- package/dist/kv-cache.js +1 -1
- package/dist/kv.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -299,6 +299,16 @@ Concurrent reads of the same key are coalesced into a single Postgres
|
|
|
299
299
|
round-trip by default. Disable it with `singleflight: false`; `stats()`
|
|
300
300
|
reports `inflightReads` and the monotonic `coalescedReads` counter.
|
|
301
301
|
|
|
302
|
+
Repeated reads of an absent key each reach Postgres unless you opt into a
|
|
303
|
+
short negative cache. Set `l1: { negativeTtlMs: 250 }` to remember a miss for
|
|
304
|
+
at most 250ms; `set()` and delete notifications clear the entry immediately,
|
|
305
|
+
and `stats()` counts those reads separately as `l1NegativeHits`.
|
|
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
|
+
|
|
302
312
|
## Pub/Sub
|
|
303
313
|
|
|
304
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();
|
|
@@ -77,6 +86,9 @@ var PgKvCache = class {
|
|
|
77
86
|
l1Enabled;
|
|
78
87
|
l1Max;
|
|
79
88
|
l1TtlMs;
|
|
89
|
+
l1NegativeTtlMs;
|
|
90
|
+
l1MaxBytes;
|
|
91
|
+
l1MaxEntryBytes;
|
|
80
92
|
notifyEnabled;
|
|
81
93
|
serializer;
|
|
82
94
|
singleflight;
|
|
@@ -85,7 +97,9 @@ var PgKvCache = class {
|
|
|
85
97
|
l1Generation = 0;
|
|
86
98
|
l1Hits = 0;
|
|
87
99
|
l1Misses = 0;
|
|
100
|
+
l1NegativeHits = 0;
|
|
88
101
|
l1Paused = false;
|
|
102
|
+
l1Bytes = 0;
|
|
89
103
|
coalescedReads = 0;
|
|
90
104
|
invalidationListener = null;
|
|
91
105
|
invalidationUnsubscribers = [];
|
|
@@ -99,6 +113,9 @@ var PgKvCache = class {
|
|
|
99
113
|
this.l1Enabled = options.l1 !== false;
|
|
100
114
|
this.l1Max = options.l1 && options.l1.max !== void 0 ? options.l1.max : DEFAULT_L1_MAX;
|
|
101
115
|
this.l1TtlMs = options.l1 && options.l1.ttlMs !== void 0 ? options.l1.ttlMs : DEFAULT_L1_TTL_MS;
|
|
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;
|
|
102
119
|
this.notifyEnabled = options.notify !== false && options.notify?.enabled !== false;
|
|
103
120
|
this.notifyChannel = options.notify && options.notify.channel ? options.notify.channel : DEFAULT_NOTIFY_CHANNEL;
|
|
104
121
|
this.serializer = options.serializer ?? {
|
|
@@ -167,7 +184,8 @@ var PgKvCache = class {
|
|
|
167
184
|
);
|
|
168
185
|
const row = rows[0];
|
|
169
186
|
if (!row) {
|
|
170
|
-
this.
|
|
187
|
+
if (this.l1NegativeTtlMs > 0) this.setNegativeL1(key, l1Generation);
|
|
188
|
+
else this.deleteL1(key);
|
|
171
189
|
return null;
|
|
172
190
|
}
|
|
173
191
|
const value = this.deserialize(row.value);
|
|
@@ -203,7 +221,9 @@ var PgKvCache = class {
|
|
|
203
221
|
this.setL1(row.key, value, rowExpiresAt(row), l1Generation);
|
|
204
222
|
}
|
|
205
223
|
for (const key of missing) {
|
|
206
|
-
if (
|
|
224
|
+
if (result.has(key)) continue;
|
|
225
|
+
if (this.l1NegativeTtlMs > 0) this.setNegativeL1(key, l1Generation);
|
|
226
|
+
else this.deleteL1(key);
|
|
207
227
|
}
|
|
208
228
|
return result;
|
|
209
229
|
}
|
|
@@ -440,7 +460,7 @@ var PgKvCache = class {
|
|
|
440
460
|
}
|
|
441
461
|
invalidateAll() {
|
|
442
462
|
this.l1Generation += 1;
|
|
443
|
-
this.
|
|
463
|
+
this.clearL1();
|
|
444
464
|
}
|
|
445
465
|
startInvalidationListener(factory, options = {}) {
|
|
446
466
|
this.stopInvalidationListener();
|
|
@@ -482,13 +502,17 @@ var PgKvCache = class {
|
|
|
482
502
|
pauseL1() {
|
|
483
503
|
this.l1Paused = true;
|
|
484
504
|
this.l1Generation += 1;
|
|
485
|
-
this.
|
|
505
|
+
this.clearL1();
|
|
486
506
|
}
|
|
487
507
|
resumeL1() {
|
|
488
508
|
this.l1Generation += 1;
|
|
489
|
-
this.
|
|
509
|
+
this.clearL1();
|
|
490
510
|
this.l1Paused = false;
|
|
491
511
|
}
|
|
512
|
+
clearL1() {
|
|
513
|
+
this.l1.clear();
|
|
514
|
+
this.l1Bytes = 0;
|
|
515
|
+
}
|
|
492
516
|
handleNotification(payload) {
|
|
493
517
|
const event = typeof payload === "string" ? this.parseNotification(payload) : payload;
|
|
494
518
|
if (!event || event.namespace !== this.namespace || event.senderId === this.instanceId) return false;
|
|
@@ -500,7 +524,7 @@ var PgKvCache = class {
|
|
|
500
524
|
if (!event.prefix) return false;
|
|
501
525
|
this.l1Generation += 1;
|
|
502
526
|
for (const key of this.l1.keys()) {
|
|
503
|
-
if (key.startsWith(event.prefix)) this.
|
|
527
|
+
if (key.startsWith(event.prefix)) this.removeEntry(key);
|
|
504
528
|
}
|
|
505
529
|
return true;
|
|
506
530
|
}
|
|
@@ -650,9 +674,12 @@ var PgKvCache = class {
|
|
|
650
674
|
l1Max: this.l1Max,
|
|
651
675
|
l1Hits: this.l1Hits,
|
|
652
676
|
l1Misses: this.l1Misses,
|
|
677
|
+
l1NegativeHits: this.l1NegativeHits,
|
|
653
678
|
l1Paused: this.l1Paused,
|
|
654
679
|
inflightReads: this.inflight.size,
|
|
655
|
-
coalescedReads: this.coalescedReads
|
|
680
|
+
coalescedReads: this.coalescedReads,
|
|
681
|
+
l1Bytes: this.l1Bytes,
|
|
682
|
+
l1MaxBytes: this.l1MaxBytes
|
|
656
683
|
};
|
|
657
684
|
}
|
|
658
685
|
async publish(event) {
|
|
@@ -710,25 +737,52 @@ var PgKvCache = class {
|
|
|
710
737
|
return { hit: false };
|
|
711
738
|
}
|
|
712
739
|
if (entry.expiresAt !== null && entry.expiresAt <= this.now()) {
|
|
713
|
-
this.
|
|
740
|
+
this.removeEntry(key);
|
|
714
741
|
this.l1Misses += 1;
|
|
715
742
|
return { hit: false };
|
|
716
743
|
}
|
|
717
|
-
|
|
744
|
+
if (entry.negative) {
|
|
745
|
+
this.l1NegativeHits += 1;
|
|
746
|
+
} else {
|
|
747
|
+
this.l1Hits += 1;
|
|
748
|
+
}
|
|
718
749
|
this.l1.delete(key);
|
|
719
750
|
this.l1.set(key, entry);
|
|
720
751
|
return { hit: true, value: entry.value };
|
|
721
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
|
+
}
|
|
762
|
+
setNegativeL1(key, generation = this.l1Generation) {
|
|
763
|
+
if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0 || this.l1NegativeTtlMs <= 0) return;
|
|
764
|
+
if (generation !== this.l1Generation) return;
|
|
765
|
+
const expiresAt = this.now() + this.l1NegativeTtlMs;
|
|
766
|
+
this.removeEntry(key);
|
|
767
|
+
this.l1.set(key, { value: null, expiresAt, negative: true, bytes: 0 });
|
|
768
|
+
this.enforceL1Max();
|
|
769
|
+
}
|
|
722
770
|
setL1(key, value, l2ExpiresAt, generation = this.l1Generation) {
|
|
723
771
|
if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0) return;
|
|
724
772
|
if (generation !== this.l1Generation) return;
|
|
725
773
|
const l1ExpiresAt = this.computeL1ExpiresAt(l2ExpiresAt);
|
|
726
774
|
if (l1ExpiresAt !== null && l1ExpiresAt <= this.now()) {
|
|
727
|
-
this.
|
|
775
|
+
this.removeEntry(key);
|
|
728
776
|
return;
|
|
729
777
|
}
|
|
730
|
-
this.
|
|
731
|
-
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;
|
|
732
786
|
this.enforceL1Max();
|
|
733
787
|
}
|
|
734
788
|
computeL1ExpiresAt(l2ExpiresAt) {
|
|
@@ -740,20 +794,20 @@ var PgKvCache = class {
|
|
|
740
794
|
deleteL1(key) {
|
|
741
795
|
if (!this.l1Enabled) return;
|
|
742
796
|
this.l1Generation += 1;
|
|
743
|
-
this.
|
|
797
|
+
this.removeEntry(key);
|
|
744
798
|
}
|
|
745
799
|
cleanupL1() {
|
|
746
800
|
if (!this.l1Enabled) return;
|
|
747
801
|
const now = this.now();
|
|
748
802
|
for (const [key, entry] of this.l1.entries()) {
|
|
749
|
-
if (entry.expiresAt !== null && entry.expiresAt <= now) this.
|
|
803
|
+
if (entry.expiresAt !== null && entry.expiresAt <= now) this.removeEntry(key);
|
|
750
804
|
}
|
|
751
805
|
}
|
|
752
806
|
enforceL1Max() {
|
|
753
|
-
while (this.l1.size > this.l1Max) {
|
|
807
|
+
while (this.l1.size > this.l1Max || this.l1MaxBytes > 0 && this.l1Bytes > this.l1MaxBytes) {
|
|
754
808
|
const oldest = this.l1.keys().next();
|
|
755
809
|
if (oldest.done) return;
|
|
756
|
-
this.
|
|
810
|
+
this.removeEntry(oldest.value);
|
|
757
811
|
}
|
|
758
812
|
}
|
|
759
813
|
};
|
package/dist/index.js
CHANGED
package/dist/kv-cache.d.ts
CHANGED
|
@@ -8,6 +8,23 @@ interface BunSqlLike {
|
|
|
8
8
|
interface PgKvCacheL1Options {
|
|
9
9
|
max?: number;
|
|
10
10
|
ttlMs?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Cache L2 misses for this many milliseconds so repeated reads of an absent
|
|
13
|
+
* key do not each reach Postgres. Defaults to 0 (disabled). Keep it short
|
|
14
|
+
* (for example 250ms) to bound how long a concurrent write stays hidden; a
|
|
15
|
+
* set/delete notification clears the entry immediately.
|
|
16
|
+
*/
|
|
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;
|
|
11
28
|
}
|
|
12
29
|
interface PgKvCacheNotifyOptions {
|
|
13
30
|
channel?: string;
|
|
@@ -75,12 +92,18 @@ interface PgKvCacheStats {
|
|
|
75
92
|
l1Hits: number;
|
|
76
93
|
/** Monotonic L1 read misses (absent or expired) since the cache was created. */
|
|
77
94
|
l1Misses: number;
|
|
95
|
+
/** Monotonic reads served from a cached L2 miss (negative cache). */
|
|
96
|
+
l1NegativeHits: number;
|
|
78
97
|
/** True while L1 is bypassed because the invalidation listener is unhealthy. */
|
|
79
98
|
l1Paused: boolean;
|
|
80
99
|
/** Reads currently coalesced into an in-flight query. */
|
|
81
100
|
inflightReads: number;
|
|
82
101
|
/** Monotonic count of reads that joined an in-flight query for the same key. */
|
|
83
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;
|
|
84
107
|
}
|
|
85
108
|
declare class PgKvCache {
|
|
86
109
|
readonly namespace: string;
|
|
@@ -93,6 +116,9 @@ declare class PgKvCache {
|
|
|
93
116
|
private readonly l1Enabled;
|
|
94
117
|
private readonly l1Max;
|
|
95
118
|
private readonly l1TtlMs;
|
|
119
|
+
private readonly l1NegativeTtlMs;
|
|
120
|
+
private readonly l1MaxBytes;
|
|
121
|
+
private readonly l1MaxEntryBytes;
|
|
96
122
|
private readonly notifyEnabled;
|
|
97
123
|
private readonly serializer;
|
|
98
124
|
private readonly singleflight;
|
|
@@ -101,7 +127,9 @@ declare class PgKvCache {
|
|
|
101
127
|
private l1Generation;
|
|
102
128
|
private l1Hits;
|
|
103
129
|
private l1Misses;
|
|
130
|
+
private l1NegativeHits;
|
|
104
131
|
private l1Paused;
|
|
132
|
+
private l1Bytes;
|
|
105
133
|
private coalescedReads;
|
|
106
134
|
private invalidationListener;
|
|
107
135
|
private invalidationUnsubscribers;
|
|
@@ -138,6 +166,7 @@ declare class PgKvCache {
|
|
|
138
166
|
stopInvalidationListener(): void;
|
|
139
167
|
private pauseL1;
|
|
140
168
|
private resumeL1;
|
|
169
|
+
private clearL1;
|
|
141
170
|
handleNotification(payload: string | PgKvNotification): boolean;
|
|
142
171
|
keys(pattern?: string, limit?: number): Promise<string[]>;
|
|
143
172
|
scan(cursor?: string | null, count?: number, pattern?: string): Promise<{
|
|
@@ -160,6 +189,9 @@ declare class PgKvCache {
|
|
|
160
189
|
private resolveNotifyOptions;
|
|
161
190
|
private parseNotification;
|
|
162
191
|
private getL1;
|
|
192
|
+
private removeEntry;
|
|
193
|
+
private tracksBytes;
|
|
194
|
+
private setNegativeL1;
|
|
163
195
|
private setL1;
|
|
164
196
|
private computeL1ExpiresAt;
|
|
165
197
|
private deleteL1;
|
package/dist/kv-cache.js
CHANGED
package/dist/kv.js
CHANGED
package/package.json
CHANGED