@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 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
@@ -9,7 +9,7 @@ import {
9
9
  import {
10
10
  PgKvCache,
11
11
  createPgKvCache
12
- } from "./chunk-HTTJHU3O.js";
12
+ } from "./chunk-YB4P7SGQ.js";
13
13
  import {
14
14
  PgList,
15
15
  createPgList
@@ -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
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PgKvCache,
3
3
  createPgKvCache
4
- } from "./chunk-HTTJHU3O.js";
4
+ } from "./chunk-YB4P7SGQ.js";
5
5
  export {
6
6
  PgKvCache,
7
7
  createPgKvCache
package/dist/kv.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PgKvCache,
3
3
  createPgKvCache
4
- } from "./chunk-HTTJHU3O.js";
4
+ } from "./chunk-YB4P7SGQ.js";
5
5
  export {
6
6
  PgKvCache,
7
7
  createPgKvCache
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postgresx/noredis",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "PostgreSQL-backed Redis feature replacement toolkit: KV/TTL cache, pub/sub, advisory locks, rate limiting, and pg-boss queues",
5
5
  "type": "module",
6
6
  "license": "MIT",