@postgresx/noredis 0.7.2 → 0.8.1

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
@@ -286,7 +286,11 @@ pg.cache.stopInvalidationListener();
286
286
 
287
287
  Remote notifications invalidate matching L1 entries. A listener reconnect
288
288
  clears the complete L1 cache by default so notifications missed during the
289
- disconnect cannot leave stale entries behind. `getset()` runs in a retried
289
+ disconnect cannot leave stale entries behind. By default the cache also
290
+ pauses L1 while the listener is unhealthy: `close`, `error`, and `reconnect`
291
+ events clear and bypass L1, and the listener's `connected` event clears and
292
+ resumes it. Set `notify.pauseOnDisconnect: false` to keep serving L1 through
293
+ disconnects at the cost of potentially stale reads. `getset()` runs in a retried
290
294
  serializable transaction and therefore requires an adapter with `begin()`;
291
295
  the built-in Bun and Node adapters provide it. `getdel()` uses one atomic
292
296
  `DELETE ... RETURNING` statement.
@@ -81,8 +81,11 @@ var PgKvCache = class {
81
81
  serializer;
82
82
  l1 = /* @__PURE__ */ new Map();
83
83
  l1Generation = 0;
84
+ l1Hits = 0;
85
+ l1Misses = 0;
86
+ l1Paused = false;
84
87
  invalidationListener = null;
85
- invalidationReconnectUnsubscribe = null;
88
+ invalidationUnsubscribers = [];
86
89
  constructor(options) {
87
90
  this.sql = options.sql;
88
91
  this.namespace = options.namespace || DEFAULT_NAMESPACE;
@@ -102,7 +105,8 @@ var PgKvCache = class {
102
105
  const listenerFactory = this.resolveNotifyOptions(options.notify)?.listener;
103
106
  if (listenerFactory) {
104
107
  this.startInvalidationListener(listenerFactory, {
105
- clearL1OnReconnect: this.resolveNotifyOptions(options.notify)?.clearL1OnReconnect
108
+ clearL1OnReconnect: this.resolveNotifyOptions(options.notify)?.clearL1OnReconnect,
109
+ pauseOnDisconnect: this.resolveNotifyOptions(options.notify)?.pauseOnDisconnect
106
110
  });
107
111
  }
108
112
  }
@@ -426,19 +430,45 @@ var PgKvCache = class {
426
430
  if (channel === this.notifyChannel) this.handleNotification(payload);
427
431
  }
428
432
  });
429
- const reconnectUnsubscribe = options.clearL1OnReconnect === false ? null : listener.on("reconnect", () => this.invalidateAll());
433
+ const unsubscribers = [];
434
+ if (options.pauseOnDisconnect !== false) {
435
+ unsubscribers.push(listener.on("close", () => this.pauseL1()));
436
+ unsubscribers.push(listener.on("error", () => this.pauseL1()));
437
+ unsubscribers.push(listener.on("reconnect", () => this.pauseL1()));
438
+ unsubscribers.push(listener.on("connected", () => this.resumeL1()));
439
+ }
440
+ if (options.clearL1OnReconnect !== false) {
441
+ unsubscribers.push(listener.on("reconnect", () => this.invalidateAll()));
442
+ }
430
443
  this.invalidationListener = listener;
431
- this.invalidationReconnectUnsubscribe = reconnectUnsubscribe;
444
+ this.invalidationUnsubscribers = unsubscribers;
445
+ if (options.pauseOnDisconnect !== false) {
446
+ if (listener.getHealth().connected) {
447
+ this.resumeL1();
448
+ } else {
449
+ this.pauseL1();
450
+ }
451
+ }
432
452
  return listener;
433
453
  }
434
454
  stopInvalidationListener() {
435
455
  const listener = this.invalidationListener;
436
- const reconnectUnsubscribe = this.invalidationReconnectUnsubscribe;
456
+ const unsubscribers = this.invalidationUnsubscribers;
437
457
  this.invalidationListener = null;
438
- this.invalidationReconnectUnsubscribe = null;
439
- reconnectUnsubscribe?.();
458
+ this.invalidationUnsubscribers = [];
459
+ for (const unsubscribe of unsubscribers) unsubscribe();
440
460
  listener?.close();
441
461
  }
462
+ pauseL1() {
463
+ this.l1Paused = true;
464
+ this.l1Generation += 1;
465
+ this.l1.clear();
466
+ }
467
+ resumeL1() {
468
+ this.l1Generation += 1;
469
+ this.l1.clear();
470
+ this.l1Paused = false;
471
+ }
442
472
  handleNotification(payload) {
443
473
  const event = typeof payload === "string" ? this.parseNotification(payload) : payload;
444
474
  if (!event || event.namespace !== this.namespace || event.senderId === this.instanceId) return false;
@@ -597,7 +627,10 @@ var PgKvCache = class {
597
627
  namespace: this.namespace,
598
628
  tableName: this.tableName,
599
629
  l1Size: this.l1.size,
600
- l1Max: this.l1Max
630
+ l1Max: this.l1Max,
631
+ l1Hits: this.l1Hits,
632
+ l1Misses: this.l1Misses,
633
+ l1Paused: this.l1Paused
601
634
  };
602
635
  }
603
636
  async publish(event) {
@@ -648,19 +681,24 @@ var PgKvCache = class {
648
681
  }
649
682
  }
650
683
  getL1(key) {
651
- if (!this.l1Enabled) return { hit: false };
684
+ if (!this.l1Enabled || this.l1Paused) return { hit: false };
652
685
  const entry = this.l1.get(key);
653
- if (!entry) return { hit: false };
686
+ if (!entry) {
687
+ this.l1Misses += 1;
688
+ return { hit: false };
689
+ }
654
690
  if (entry.expiresAt !== null && entry.expiresAt <= this.now()) {
655
691
  this.l1.delete(key);
692
+ this.l1Misses += 1;
656
693
  return { hit: false };
657
694
  }
695
+ this.l1Hits += 1;
658
696
  this.l1.delete(key);
659
697
  this.l1.set(key, entry);
660
698
  return { hit: true, value: entry.value };
661
699
  }
662
700
  setL1(key, value, l2ExpiresAt, generation = this.l1Generation) {
663
- if (!this.l1Enabled || this.l1Max <= 0) return;
701
+ if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0) return;
664
702
  if (generation !== this.l1Generation) return;
665
703
  const l1ExpiresAt = this.computeL1ExpiresAt(l2ExpiresAt);
666
704
  if (l1ExpiresAt !== null && l1ExpiresAt <= this.now()) {
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  import {
10
10
  PgKvCache,
11
11
  createPgKvCache
12
- } from "./chunk-YSQCYFKW.js";
12
+ } from "./chunk-HTTJHU3O.js";
13
13
  import {
14
14
  PgList,
15
15
  createPgList
@@ -14,6 +14,8 @@ interface PgKvCacheNotifyOptions {
14
14
  enabled?: boolean;
15
15
  listener?: PgKvCacheListenerFactory;
16
16
  clearL1OnReconnect?: boolean;
17
+ /** Pause L1 reads and fills while the invalidation listener is unhealthy. Defaults to true. */
18
+ pauseOnDisconnect?: boolean;
17
19
  }
18
20
  interface PgKvCacheListenerFactoryOptions {
19
21
  channels: string[];
@@ -22,6 +24,8 @@ interface PgKvCacheListenerFactoryOptions {
22
24
  type PgKvCacheListenerFactory = (options: PgKvCacheListenerFactoryOptions) => PgListenerHandle;
23
25
  interface PgKvCacheInvalidationOptions {
24
26
  clearL1OnReconnect?: boolean;
27
+ /** Pause L1 reads and fills while the invalidation listener is unhealthy. Defaults to true. */
28
+ pauseOnDisconnect?: boolean;
25
29
  }
26
30
  interface PgKvCacheOptions {
27
31
  sql: BunSqlLike;
@@ -62,6 +66,12 @@ interface PgKvCacheStats {
62
66
  tableName: string;
63
67
  l1Size: number;
64
68
  l1Max: number;
69
+ /** Monotonic L1 read hits since the cache was created. */
70
+ l1Hits: number;
71
+ /** Monotonic L1 read misses (absent or expired) since the cache was created. */
72
+ l1Misses: number;
73
+ /** True while L1 is bypassed because the invalidation listener is unhealthy. */
74
+ l1Paused: boolean;
65
75
  }
66
76
  declare class PgKvCache {
67
77
  readonly namespace: string;
@@ -78,8 +88,11 @@ declare class PgKvCache {
78
88
  private readonly serializer;
79
89
  private readonly l1;
80
90
  private l1Generation;
91
+ private l1Hits;
92
+ private l1Misses;
93
+ private l1Paused;
81
94
  private invalidationListener;
82
- private invalidationReconnectUnsubscribe;
95
+ private invalidationUnsubscribers;
83
96
  constructor(options: PgKvCacheOptions);
84
97
  ensureSchema(options?: PgKvSchemaOptions): Promise<void>;
85
98
  get<T = unknown>(key: string): Promise<T | null>;
@@ -109,6 +122,8 @@ declare class PgKvCache {
109
122
  invalidateAll(): void;
110
123
  startInvalidationListener(factory: PgKvCacheListenerFactory, options?: PgKvCacheInvalidationOptions): PgListenerHandle;
111
124
  stopInvalidationListener(): void;
125
+ private pauseL1;
126
+ private resumeL1;
112
127
  handleNotification(payload: string | PgKvNotification): boolean;
113
128
  keys(pattern?: string, limit?: number): Promise<string[]>;
114
129
  scan(cursor?: string | null, count?: number, pattern?: string): Promise<{
package/dist/kv-cache.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PgKvCache,
3
3
  createPgKvCache
4
- } from "./chunk-YSQCYFKW.js";
4
+ } from "./chunk-HTTJHU3O.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-YSQCYFKW.js";
4
+ } from "./chunk-HTTJHU3O.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.7.2",
3
+ "version": "0.8.1",
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",