@postgresx/noredis 0.8.0 → 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.
@@ -83,8 +83,9 @@ var PgKvCache = class {
83
83
  l1Generation = 0;
84
84
  l1Hits = 0;
85
85
  l1Misses = 0;
86
+ l1Paused = false;
86
87
  invalidationListener = null;
87
- invalidationReconnectUnsubscribe = null;
88
+ invalidationUnsubscribers = [];
88
89
  constructor(options) {
89
90
  this.sql = options.sql;
90
91
  this.namespace = options.namespace || DEFAULT_NAMESPACE;
@@ -104,7 +105,8 @@ var PgKvCache = class {
104
105
  const listenerFactory = this.resolveNotifyOptions(options.notify)?.listener;
105
106
  if (listenerFactory) {
106
107
  this.startInvalidationListener(listenerFactory, {
107
- clearL1OnReconnect: this.resolveNotifyOptions(options.notify)?.clearL1OnReconnect
108
+ clearL1OnReconnect: this.resolveNotifyOptions(options.notify)?.clearL1OnReconnect,
109
+ pauseOnDisconnect: this.resolveNotifyOptions(options.notify)?.pauseOnDisconnect
108
110
  });
109
111
  }
110
112
  }
@@ -428,19 +430,45 @@ var PgKvCache = class {
428
430
  if (channel === this.notifyChannel) this.handleNotification(payload);
429
431
  }
430
432
  });
431
- 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
+ }
432
443
  this.invalidationListener = listener;
433
- 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
+ }
434
452
  return listener;
435
453
  }
436
454
  stopInvalidationListener() {
437
455
  const listener = this.invalidationListener;
438
- const reconnectUnsubscribe = this.invalidationReconnectUnsubscribe;
456
+ const unsubscribers = this.invalidationUnsubscribers;
439
457
  this.invalidationListener = null;
440
- this.invalidationReconnectUnsubscribe = null;
441
- reconnectUnsubscribe?.();
458
+ this.invalidationUnsubscribers = [];
459
+ for (const unsubscribe of unsubscribers) unsubscribe();
442
460
  listener?.close();
443
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
+ }
444
472
  handleNotification(payload) {
445
473
  const event = typeof payload === "string" ? this.parseNotification(payload) : payload;
446
474
  if (!event || event.namespace !== this.namespace || event.senderId === this.instanceId) return false;
@@ -601,7 +629,8 @@ var PgKvCache = class {
601
629
  l1Size: this.l1.size,
602
630
  l1Max: this.l1Max,
603
631
  l1Hits: this.l1Hits,
604
- l1Misses: this.l1Misses
632
+ l1Misses: this.l1Misses,
633
+ l1Paused: this.l1Paused
605
634
  };
606
635
  }
607
636
  async publish(event) {
@@ -652,7 +681,7 @@ var PgKvCache = class {
652
681
  }
653
682
  }
654
683
  getL1(key) {
655
- if (!this.l1Enabled) return { hit: false };
684
+ if (!this.l1Enabled || this.l1Paused) return { hit: false };
656
685
  const entry = this.l1.get(key);
657
686
  if (!entry) {
658
687
  this.l1Misses += 1;
@@ -669,7 +698,7 @@ var PgKvCache = class {
669
698
  return { hit: true, value: entry.value };
670
699
  }
671
700
  setL1(key, value, l2ExpiresAt, generation = this.l1Generation) {
672
- if (!this.l1Enabled || this.l1Max <= 0) return;
701
+ if (!this.l1Enabled || this.l1Paused || this.l1Max <= 0) return;
673
702
  if (generation !== this.l1Generation) return;
674
703
  const l1ExpiresAt = this.computeL1ExpiresAt(l2ExpiresAt);
675
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-6D2PPDAW.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;
@@ -66,6 +70,8 @@ interface PgKvCacheStats {
66
70
  l1Hits: number;
67
71
  /** Monotonic L1 read misses (absent or expired) since the cache was created. */
68
72
  l1Misses: number;
73
+ /** True while L1 is bypassed because the invalidation listener is unhealthy. */
74
+ l1Paused: boolean;
69
75
  }
70
76
  declare class PgKvCache {
71
77
  readonly namespace: string;
@@ -84,8 +90,9 @@ declare class PgKvCache {
84
90
  private l1Generation;
85
91
  private l1Hits;
86
92
  private l1Misses;
93
+ private l1Paused;
87
94
  private invalidationListener;
88
- private invalidationReconnectUnsubscribe;
95
+ private invalidationUnsubscribers;
89
96
  constructor(options: PgKvCacheOptions);
90
97
  ensureSchema(options?: PgKvSchemaOptions): Promise<void>;
91
98
  get<T = unknown>(key: string): Promise<T | null>;
@@ -115,6 +122,8 @@ declare class PgKvCache {
115
122
  invalidateAll(): void;
116
123
  startInvalidationListener(factory: PgKvCacheListenerFactory, options?: PgKvCacheInvalidationOptions): PgListenerHandle;
117
124
  stopInvalidationListener(): void;
125
+ private pauseL1;
126
+ private resumeL1;
118
127
  handleNotification(payload: string | PgKvNotification): boolean;
119
128
  keys(pattern?: string, limit?: number): Promise<string[]>;
120
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-6D2PPDAW.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-6D2PPDAW.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.8.0",
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",