@postgresx/noredis 0.8.1 → 0.9.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 +4 -0
- package/dist/{chunk-HTTJHU3O.js → chunk-YB4P7SGQ.js} +23 -1
- package/dist/index.js +1 -1
- package/dist/kv-cache.d.ts +14 -0
- package/dist/kv-cache.js +1 -1
- package/dist/kv.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -295,6 +295,10 @@ serializable transaction and therefore requires an adapter with `begin()`;
|
|
|
295
295
|
the built-in Bun and Node adapters provide it. `getdel()` uses one atomic
|
|
296
296
|
`DELETE ... RETURNING` statement.
|
|
297
297
|
|
|
298
|
+
Concurrent reads of the same key are coalesced into a single Postgres
|
|
299
|
+
round-trip by default. Disable it with `singleflight: false`; `stats()`
|
|
300
|
+
reports `inflightReads` and the monotonic `coalescedReads` counter.
|
|
301
|
+
|
|
298
302
|
## Pub/Sub
|
|
299
303
|
|
|
300
304
|
Publishing uses only the configured SQL adapter. Bun LISTEN/NOTIFY consumption
|
|
@@ -79,11 +79,14 @@ var PgKvCache = class {
|
|
|
79
79
|
l1TtlMs;
|
|
80
80
|
notifyEnabled;
|
|
81
81
|
serializer;
|
|
82
|
+
singleflight;
|
|
82
83
|
l1 = /* @__PURE__ */ new Map();
|
|
84
|
+
inflight = /* @__PURE__ */ new Map();
|
|
83
85
|
l1Generation = 0;
|
|
84
86
|
l1Hits = 0;
|
|
85
87
|
l1Misses = 0;
|
|
86
88
|
l1Paused = false;
|
|
89
|
+
coalescedReads = 0;
|
|
87
90
|
invalidationListener = null;
|
|
88
91
|
invalidationUnsubscribers = [];
|
|
89
92
|
constructor(options) {
|
|
@@ -102,6 +105,7 @@ var PgKvCache = class {
|
|
|
102
105
|
serialize: (value) => value,
|
|
103
106
|
deserialize: (value) => value
|
|
104
107
|
};
|
|
108
|
+
this.singleflight = options.singleflight !== false;
|
|
105
109
|
const listenerFactory = this.resolveNotifyOptions(options.notify)?.listener;
|
|
106
110
|
if (listenerFactory) {
|
|
107
111
|
this.startInvalidationListener(listenerFactory, {
|
|
@@ -135,6 +139,22 @@ var PgKvCache = class {
|
|
|
135
139
|
async get(key) {
|
|
136
140
|
const cached = this.getL1(key);
|
|
137
141
|
if (cached.hit) return cached.value;
|
|
142
|
+
return this.loadKey(key);
|
|
143
|
+
}
|
|
144
|
+
loadKey(key) {
|
|
145
|
+
if (!this.singleflight) return this.readKey(key);
|
|
146
|
+
const existing = this.inflight.get(key);
|
|
147
|
+
if (existing) {
|
|
148
|
+
this.coalescedReads += 1;
|
|
149
|
+
return existing;
|
|
150
|
+
}
|
|
151
|
+
const pending = this.readKey(key).finally(() => {
|
|
152
|
+
if (this.inflight.get(key) === pending) this.inflight.delete(key);
|
|
153
|
+
});
|
|
154
|
+
this.inflight.set(key, pending);
|
|
155
|
+
return pending;
|
|
156
|
+
}
|
|
157
|
+
async readKey(key) {
|
|
138
158
|
const l1Generation = this.l1Generation;
|
|
139
159
|
const rows = await this.sql.unsafe(
|
|
140
160
|
`SELECT value, expires_at
|
|
@@ -630,7 +650,9 @@ var PgKvCache = class {
|
|
|
630
650
|
l1Max: this.l1Max,
|
|
631
651
|
l1Hits: this.l1Hits,
|
|
632
652
|
l1Misses: this.l1Misses,
|
|
633
|
-
l1Paused: this.l1Paused
|
|
653
|
+
l1Paused: this.l1Paused,
|
|
654
|
+
inflightReads: this.inflight.size,
|
|
655
|
+
coalescedReads: this.coalescedReads
|
|
634
656
|
};
|
|
635
657
|
}
|
|
636
658
|
async publish(event) {
|
package/dist/index.js
CHANGED
package/dist/kv-cache.d.ts
CHANGED
|
@@ -36,6 +36,11 @@ interface PgKvCacheOptions {
|
|
|
36
36
|
instanceId?: string;
|
|
37
37
|
now?: () => number;
|
|
38
38
|
serializer?: PgKvSerializer;
|
|
39
|
+
/**
|
|
40
|
+
* Coalesce concurrent reads of the same key into a single Postgres query.
|
|
41
|
+
* Defaults to true; set to false to issue one query per caller.
|
|
42
|
+
*/
|
|
43
|
+
singleflight?: boolean;
|
|
39
44
|
}
|
|
40
45
|
interface PgKvSetOptions {
|
|
41
46
|
ttlMs?: number | null;
|
|
@@ -72,6 +77,10 @@ interface PgKvCacheStats {
|
|
|
72
77
|
l1Misses: number;
|
|
73
78
|
/** True while L1 is bypassed because the invalidation listener is unhealthy. */
|
|
74
79
|
l1Paused: boolean;
|
|
80
|
+
/** Reads currently coalesced into an in-flight query. */
|
|
81
|
+
inflightReads: number;
|
|
82
|
+
/** Monotonic count of reads that joined an in-flight query for the same key. */
|
|
83
|
+
coalescedReads: number;
|
|
75
84
|
}
|
|
76
85
|
declare class PgKvCache {
|
|
77
86
|
readonly namespace: string;
|
|
@@ -86,16 +95,21 @@ declare class PgKvCache {
|
|
|
86
95
|
private readonly l1TtlMs;
|
|
87
96
|
private readonly notifyEnabled;
|
|
88
97
|
private readonly serializer;
|
|
98
|
+
private readonly singleflight;
|
|
89
99
|
private readonly l1;
|
|
100
|
+
private readonly inflight;
|
|
90
101
|
private l1Generation;
|
|
91
102
|
private l1Hits;
|
|
92
103
|
private l1Misses;
|
|
93
104
|
private l1Paused;
|
|
105
|
+
private coalescedReads;
|
|
94
106
|
private invalidationListener;
|
|
95
107
|
private invalidationUnsubscribers;
|
|
96
108
|
constructor(options: PgKvCacheOptions);
|
|
97
109
|
ensureSchema(options?: PgKvSchemaOptions): Promise<void>;
|
|
98
110
|
get<T = unknown>(key: string): Promise<T | null>;
|
|
111
|
+
private loadKey;
|
|
112
|
+
private readKey;
|
|
99
113
|
mget<T = unknown>(keys: readonly string[]): Promise<Map<string, T>>;
|
|
100
114
|
set<T = unknown>(key: string, value: T, options?: PgKvSetOptions): Promise<boolean>;
|
|
101
115
|
mset<T = unknown>(entries: Iterable<readonly [string, T]>, options?: PgKvSetOptions): Promise<void>;
|
package/dist/kv-cache.js
CHANGED
package/dist/kv.js
CHANGED
package/package.json
CHANGED