@postgresx/noredis 0.9.0 → 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
@@ -299,6 +299,11 @@ 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
+
302
307
  ## Pub/Sub
303
308
 
304
309
  Publishing uses only the configured SQL adapter. Bun LISTEN/NOTIFY consumption
@@ -77,6 +77,7 @@ var PgKvCache = class {
77
77
  l1Enabled;
78
78
  l1Max;
79
79
  l1TtlMs;
80
+ l1NegativeTtlMs;
80
81
  notifyEnabled;
81
82
  serializer;
82
83
  singleflight;
@@ -85,6 +86,7 @@ var PgKvCache = class {
85
86
  l1Generation = 0;
86
87
  l1Hits = 0;
87
88
  l1Misses = 0;
89
+ l1NegativeHits = 0;
88
90
  l1Paused = false;
89
91
  coalescedReads = 0;
90
92
  invalidationListener = null;
@@ -99,6 +101,7 @@ var PgKvCache = class {
99
101
  this.l1Enabled = options.l1 !== false;
100
102
  this.l1Max = options.l1 && options.l1.max !== void 0 ? options.l1.max : DEFAULT_L1_MAX;
101
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;
102
105
  this.notifyEnabled = options.notify !== false && options.notify?.enabled !== false;
103
106
  this.notifyChannel = options.notify && options.notify.channel ? options.notify.channel : DEFAULT_NOTIFY_CHANNEL;
104
107
  this.serializer = options.serializer ?? {
@@ -167,7 +170,8 @@ var PgKvCache = class {
167
170
  );
168
171
  const row = rows[0];
169
172
  if (!row) {
170
- this.deleteL1(key);
173
+ if (this.l1NegativeTtlMs > 0) this.setNegativeL1(key, l1Generation);
174
+ else this.deleteL1(key);
171
175
  return null;
172
176
  }
173
177
  const value = this.deserialize(row.value);
@@ -203,7 +207,9 @@ var PgKvCache = class {
203
207
  this.setL1(row.key, value, rowExpiresAt(row), l1Generation);
204
208
  }
205
209
  for (const key of missing) {
206
- 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);
207
213
  }
208
214
  return result;
209
215
  }
@@ -650,6 +656,7 @@ var PgKvCache = class {
650
656
  l1Max: this.l1Max,
651
657
  l1Hits: this.l1Hits,
652
658
  l1Misses: this.l1Misses,
659
+ l1NegativeHits: this.l1NegativeHits,
653
660
  l1Paused: this.l1Paused,
654
661
  inflightReads: this.inflight.size,
655
662
  coalescedReads: this.coalescedReads
@@ -714,11 +721,23 @@ var PgKvCache = class {
714
721
  this.l1Misses += 1;
715
722
  return { hit: false };
716
723
  }
717
- this.l1Hits += 1;
724
+ if (entry.negative) {
725
+ this.l1NegativeHits += 1;
726
+ } else {
727
+ this.l1Hits += 1;
728
+ }
718
729
  this.l1.delete(key);
719
730
  this.l1.set(key, entry);
720
731
  return { hit: true, value: entry.value };
721
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
+ }
722
741
  setL1(key, value, l2ExpiresAt, generation = this.l1Generation) {
723
742
  if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0) return;
724
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-YB4P7SGQ.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;
@@ -75,6 +82,8 @@ interface PgKvCacheStats {
75
82
  l1Hits: number;
76
83
  /** Monotonic L1 read misses (absent or expired) since the cache was created. */
77
84
  l1Misses: number;
85
+ /** Monotonic reads served from a cached L2 miss (negative cache). */
86
+ l1NegativeHits: number;
78
87
  /** True while L1 is bypassed because the invalidation listener is unhealthy. */
79
88
  l1Paused: boolean;
80
89
  /** Reads currently coalesced into an in-flight query. */
@@ -93,6 +102,7 @@ declare class PgKvCache {
93
102
  private readonly l1Enabled;
94
103
  private readonly l1Max;
95
104
  private readonly l1TtlMs;
105
+ private readonly l1NegativeTtlMs;
96
106
  private readonly notifyEnabled;
97
107
  private readonly serializer;
98
108
  private readonly singleflight;
@@ -101,6 +111,7 @@ declare class PgKvCache {
101
111
  private l1Generation;
102
112
  private l1Hits;
103
113
  private l1Misses;
114
+ private l1NegativeHits;
104
115
  private l1Paused;
105
116
  private coalescedReads;
106
117
  private invalidationListener;
@@ -160,6 +171,7 @@ declare class PgKvCache {
160
171
  private resolveNotifyOptions;
161
172
  private parseNotification;
162
173
  private getL1;
174
+ private setNegativeL1;
163
175
  private setL1;
164
176
  private computeL1ExpiresAt;
165
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-YB4P7SGQ.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-YB4P7SGQ.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.9.0",
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",