@postgresx/noredis 0.8.1 → 0.10.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,15 @@ 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
+
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
+
298
307
  ## Pub/Sub
299
308
 
300
309
  Publishing uses only the configured SQL adapter. Bun LISTEN/NOTIFY consumption
@@ -77,13 +77,18 @@ var PgKvCache = class {
77
77
  l1Enabled;
78
78
  l1Max;
79
79
  l1TtlMs;
80
+ l1NegativeTtlMs;
80
81
  notifyEnabled;
81
82
  serializer;
83
+ singleflight;
82
84
  l1 = /* @__PURE__ */ new Map();
85
+ inflight = /* @__PURE__ */ new Map();
83
86
  l1Generation = 0;
84
87
  l1Hits = 0;
85
88
  l1Misses = 0;
89
+ l1NegativeHits = 0;
86
90
  l1Paused = false;
91
+ coalescedReads = 0;
87
92
  invalidationListener = null;
88
93
  invalidationUnsubscribers = [];
89
94
  constructor(options) {
@@ -96,12 +101,14 @@ var PgKvCache = class {
96
101
  this.l1Enabled = options.l1 !== false;
97
102
  this.l1Max = options.l1 && options.l1.max !== void 0 ? options.l1.max : DEFAULT_L1_MAX;
98
103
  this.l1TtlMs = options.l1 && options.l1.ttlMs !== void 0 ? options.l1.ttlMs : DEFAULT_L1_TTL_MS;
104
+ this.l1NegativeTtlMs = options.l1 && options.l1.negativeTtlMs !== void 0 && options.l1.negativeTtlMs > 0 ? options.l1.negativeTtlMs : 0;
99
105
  this.notifyEnabled = options.notify !== false && options.notify?.enabled !== false;
100
106
  this.notifyChannel = options.notify && options.notify.channel ? options.notify.channel : DEFAULT_NOTIFY_CHANNEL;
101
107
  this.serializer = options.serializer ?? {
102
108
  serialize: (value) => value,
103
109
  deserialize: (value) => value
104
110
  };
111
+ this.singleflight = options.singleflight !== false;
105
112
  const listenerFactory = this.resolveNotifyOptions(options.notify)?.listener;
106
113
  if (listenerFactory) {
107
114
  this.startInvalidationListener(listenerFactory, {
@@ -135,6 +142,22 @@ var PgKvCache = class {
135
142
  async get(key) {
136
143
  const cached = this.getL1(key);
137
144
  if (cached.hit) return cached.value;
145
+ return this.loadKey(key);
146
+ }
147
+ loadKey(key) {
148
+ if (!this.singleflight) return this.readKey(key);
149
+ const existing = this.inflight.get(key);
150
+ if (existing) {
151
+ this.coalescedReads += 1;
152
+ return existing;
153
+ }
154
+ const pending = this.readKey(key).finally(() => {
155
+ if (this.inflight.get(key) === pending) this.inflight.delete(key);
156
+ });
157
+ this.inflight.set(key, pending);
158
+ return pending;
159
+ }
160
+ async readKey(key) {
138
161
  const l1Generation = this.l1Generation;
139
162
  const rows = await this.sql.unsafe(
140
163
  `SELECT value, expires_at
@@ -147,7 +170,8 @@ var PgKvCache = class {
147
170
  );
148
171
  const row = rows[0];
149
172
  if (!row) {
150
- this.deleteL1(key);
173
+ if (this.l1NegativeTtlMs > 0) this.setNegativeL1(key, l1Generation);
174
+ else this.deleteL1(key);
151
175
  return null;
152
176
  }
153
177
  const value = this.deserialize(row.value);
@@ -183,7 +207,9 @@ var PgKvCache = class {
183
207
  this.setL1(row.key, value, rowExpiresAt(row), l1Generation);
184
208
  }
185
209
  for (const key of missing) {
186
- if (!result.has(key)) this.deleteL1(key);
210
+ if (result.has(key)) continue;
211
+ if (this.l1NegativeTtlMs > 0) this.setNegativeL1(key, l1Generation);
212
+ else this.deleteL1(key);
187
213
  }
188
214
  return result;
189
215
  }
@@ -630,7 +656,10 @@ var PgKvCache = class {
630
656
  l1Max: this.l1Max,
631
657
  l1Hits: this.l1Hits,
632
658
  l1Misses: this.l1Misses,
633
- l1Paused: this.l1Paused
659
+ l1NegativeHits: this.l1NegativeHits,
660
+ l1Paused: this.l1Paused,
661
+ inflightReads: this.inflight.size,
662
+ coalescedReads: this.coalescedReads
634
663
  };
635
664
  }
636
665
  async publish(event) {
@@ -692,11 +721,23 @@ var PgKvCache = class {
692
721
  this.l1Misses += 1;
693
722
  return { hit: false };
694
723
  }
695
- this.l1Hits += 1;
724
+ if (entry.negative) {
725
+ this.l1NegativeHits += 1;
726
+ } else {
727
+ this.l1Hits += 1;
728
+ }
696
729
  this.l1.delete(key);
697
730
  this.l1.set(key, entry);
698
731
  return { hit: true, value: entry.value };
699
732
  }
733
+ setNegativeL1(key, generation = this.l1Generation) {
734
+ if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0 || this.l1NegativeTtlMs <= 0) return;
735
+ if (generation !== this.l1Generation) return;
736
+ const expiresAt = this.now() + this.l1NegativeTtlMs;
737
+ this.l1.delete(key);
738
+ this.l1.set(key, { value: null, expiresAt, negative: true });
739
+ this.enforceL1Max();
740
+ }
700
741
  setL1(key, value, l2ExpiresAt, generation = this.l1Generation) {
701
742
  if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0) return;
702
743
  if (generation !== this.l1Generation) return;
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-7T5O55TM.js";
13
13
  import {
14
14
  PgList,
15
15
  createPgList
@@ -8,6 +8,13 @@ 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;
11
18
  }
12
19
  interface PgKvCacheNotifyOptions {
13
20
  channel?: string;
@@ -36,6 +43,11 @@ interface PgKvCacheOptions {
36
43
  instanceId?: string;
37
44
  now?: () => number;
38
45
  serializer?: PgKvSerializer;
46
+ /**
47
+ * Coalesce concurrent reads of the same key into a single Postgres query.
48
+ * Defaults to true; set to false to issue one query per caller.
49
+ */
50
+ singleflight?: boolean;
39
51
  }
40
52
  interface PgKvSetOptions {
41
53
  ttlMs?: number | null;
@@ -70,8 +82,14 @@ interface PgKvCacheStats {
70
82
  l1Hits: number;
71
83
  /** Monotonic L1 read misses (absent or expired) since the cache was created. */
72
84
  l1Misses: number;
85
+ /** Monotonic reads served from a cached L2 miss (negative cache). */
86
+ l1NegativeHits: number;
73
87
  /** True while L1 is bypassed because the invalidation listener is unhealthy. */
74
88
  l1Paused: boolean;
89
+ /** Reads currently coalesced into an in-flight query. */
90
+ inflightReads: number;
91
+ /** Monotonic count of reads that joined an in-flight query for the same key. */
92
+ coalescedReads: number;
75
93
  }
76
94
  declare class PgKvCache {
77
95
  readonly namespace: string;
@@ -84,18 +102,25 @@ declare class PgKvCache {
84
102
  private readonly l1Enabled;
85
103
  private readonly l1Max;
86
104
  private readonly l1TtlMs;
105
+ private readonly l1NegativeTtlMs;
87
106
  private readonly notifyEnabled;
88
107
  private readonly serializer;
108
+ private readonly singleflight;
89
109
  private readonly l1;
110
+ private readonly inflight;
90
111
  private l1Generation;
91
112
  private l1Hits;
92
113
  private l1Misses;
114
+ private l1NegativeHits;
93
115
  private l1Paused;
116
+ private coalescedReads;
94
117
  private invalidationListener;
95
118
  private invalidationUnsubscribers;
96
119
  constructor(options: PgKvCacheOptions);
97
120
  ensureSchema(options?: PgKvSchemaOptions): Promise<void>;
98
121
  get<T = unknown>(key: string): Promise<T | null>;
122
+ private loadKey;
123
+ private readKey;
99
124
  mget<T = unknown>(keys: readonly string[]): Promise<Map<string, T>>;
100
125
  set<T = unknown>(key: string, value: T, options?: PgKvSetOptions): Promise<boolean>;
101
126
  mset<T = unknown>(entries: Iterable<readonly [string, T]>, options?: PgKvSetOptions): Promise<void>;
@@ -146,6 +171,7 @@ declare class PgKvCache {
146
171
  private resolveNotifyOptions;
147
172
  private parseNotification;
148
173
  private getL1;
174
+ private setNegativeL1;
149
175
  private setL1;
150
176
  private computeL1ExpiresAt;
151
177
  private deleteL1;
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-7T5O55TM.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-7T5O55TM.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.10.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",