tricache 0.6.0 → 0.6.2

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.
Files changed (42) hide show
  1. package/README.md +119 -0
  2. package/dist/chunk-F24NEZGQ.js +342 -0
  3. package/dist/index.cjs +494 -164
  4. package/dist/index.d.cts +125 -0
  5. package/dist/index.d.ts +1552 -18
  6. package/dist/index.js +3524 -25
  7. package/dist/serialize-worker.cjs +366 -0
  8. package/dist/serialize-worker.d.cts +2 -0
  9. package/dist/serialize-worker.d.ts +2 -0
  10. package/dist/serialize-worker.js +35 -0
  11. package/package.json +5 -5
  12. package/src/cache-service.ts +310 -93
  13. package/src/serialize-worker.ts +56 -0
  14. package/src/smart-memory-cache.ts +42 -1
  15. package/src/types.ts +99 -1
  16. package/src/worker-pool.ts +190 -0
  17. package/dist/cache-service.d.ts +0 -419
  18. package/dist/cache-service.d.ts.map +0 -1
  19. package/dist/cache-service.js +0 -1788
  20. package/dist/cache-service.js.map +0 -1
  21. package/dist/disk-tier.d.ts +0 -150
  22. package/dist/disk-tier.d.ts.map +0 -1
  23. package/dist/disk-tier.js +0 -808
  24. package/dist/disk-tier.js.map +0 -1
  25. package/dist/encryption.d.ts +0 -90
  26. package/dist/encryption.d.ts.map +0 -1
  27. package/dist/encryption.js +0 -393
  28. package/dist/encryption.js.map +0 -1
  29. package/dist/index.d.ts.map +0 -1
  30. package/dist/index.js.map +0 -1
  31. package/dist/smart-memory-cache.d.ts +0 -208
  32. package/dist/smart-memory-cache.d.ts.map +0 -1
  33. package/dist/smart-memory-cache.js +0 -933
  34. package/dist/smart-memory-cache.js.map +0 -1
  35. package/dist/types.d.ts +0 -537
  36. package/dist/types.d.ts.map +0 -1
  37. package/dist/types.js +0 -20
  38. package/dist/types.js.map +0 -1
  39. package/dist/wasm/bloom-filter-wasm.d.ts +0 -29
  40. package/dist/wasm/bloom-filter-wasm.d.ts.map +0 -1
  41. package/dist/wasm/bloom-filter-wasm.js +0 -83
  42. package/dist/wasm/bloom-filter-wasm.js.map +0 -1
package/dist/index.d.cts CHANGED
@@ -198,6 +198,7 @@ interface CacheMetrics {
198
198
  files: number;
199
199
  sizeKB: number;
200
200
  maxKB: number;
201
+ disabled: boolean;
201
202
  };
202
203
  /** Adaptive TTL statistics — present only when `adaptiveTtl` is enabled */
203
204
  adaptiveTtl?: {
@@ -533,6 +534,101 @@ interface CacheOptions {
533
534
  * Default: `5000`.
534
535
  */
535
536
  adaptiveTtlMaxKeys?: number;
537
+ /**
538
+ * When `true`, tricache spawns a pool of worker threads and offloads AES-GCM
539
+ * encryption and decryption for large payloads to those threads, keeping the
540
+ * V8 main event loop free for concurrent HTTP requests.
541
+ *
542
+ * Only takes effect when `encryptionKey` is set — plaintext payloads are never
543
+ * serialized off-thread because `JSON.stringify/parse` is already fast.
544
+ *
545
+ * Default: `false`. Enable in long-running servers that write multi-megabyte
546
+ * encrypted payloads and observe event-loop latency spikes.
547
+ */
548
+ workerThreads?: boolean;
549
+ /**
550
+ * Byte threshold above which an L2 payload is offloaded to a worker thread
551
+ * for encryption / decryption. Payloads smaller than this are processed
552
+ * synchronously (IPC overhead would exceed the savings).
553
+ * Default: `131072` (128 KB).
554
+ */
555
+ workerThresholdBytes?: number;
556
+ /**
557
+ * Number of worker threads in the pool.
558
+ * Default: `Math.min(4, os.availableParallelism())`.
559
+ */
560
+ workerPoolSize?: number;
561
+ /**
562
+ * Maximum milliseconds the Redis Pub/Sub subscriber is allowed to be
563
+ * disconnected before tricache considers its L1 entries potentially stale.
564
+ *
565
+ * When the subscriber reconnects after a gap longer than this value, all L1
566
+ * entries that were written *before* the disconnect started are evicted.
567
+ * This prevents the instance from silently serving data that peers may have
568
+ * invalidated while the backplane connection was down.
569
+ *
570
+ * Set to `0` to disable the fence entirely (accept eventual-consistency drift).
571
+ * Default: `5000` (5 seconds).
572
+ */
573
+ backplaneMaxStalenessMs?: number;
574
+ /**
575
+ * Explicitly disable the L1.5 disk spill tier and cold-start snapshots.
576
+ *
577
+ * Tricache auto-detects common serverless / ephemeral-filesystem runtimes
578
+ * (AWS Lambda, Google Cloud Run, Google Cloud Functions, Azure Functions,
579
+ * Fly.io) and disables the disk tier automatically. Set this to `false` to
580
+ * override that detection and force the disk tier on regardless of runtime.
581
+ *
582
+ * When disabled:
583
+ * - L1 evictions are dropped instead of being spilled to disk.
584
+ * - Cold-start snapshot persistence is skipped.
585
+ * - The `disk` metrics field still appears but shows zeroed counters.
586
+ *
587
+ * Default: `undefined` (auto-detect).
588
+ */
589
+ disableDisk?: boolean;
590
+ /**
591
+ * Redis Cluster node list. When provided, tricache connects using ioredis
592
+ * `Cluster` mode with automatic slot routing and multi-node connection
593
+ * pooling — no external proxy required.
594
+ *
595
+ * Takes precedence over `redisHost`/`redisPort`.
596
+ *
597
+ * @example
598
+ * redisClusterNodes: [
599
+ * { host: 'redis-1.internal', port: 6379 },
600
+ * { host: 'redis-2.internal', port: 6379 },
601
+ * { host: 'redis-3.internal', port: 6379 },
602
+ * ]
603
+ */
604
+ redisClusterNodes?: Array<{
605
+ host: string;
606
+ port: number;
607
+ }>;
608
+ /**
609
+ * Redis Sentinel configuration for high-availability setups.
610
+ * When provided, tricache connects through Sentinel for automatic
611
+ * primary failover — no static `redisHost` / `redisPort` needed.
612
+ *
613
+ * Takes precedence over `redisHost`/`redisPort` (but not over `redisClusterNodes`).
614
+ *
615
+ * @example
616
+ * redisSentinel: {
617
+ * name: 'mymaster',
618
+ * sentinels: [
619
+ * { host: 'sentinel-1.internal', port: 26379 },
620
+ * { host: 'sentinel-2.internal', port: 26379 },
621
+ * ],
622
+ * }
623
+ */
624
+ redisSentinel?: {
625
+ /** Logical name of the monitored master. Must match the Sentinel `monitor` config. */
626
+ name: string;
627
+ sentinels: Array<{
628
+ host: string;
629
+ port: number;
630
+ }>;
631
+ };
536
632
  }
537
633
 
538
634
  /**
@@ -558,6 +654,10 @@ declare class CacheService {
558
654
  private readonly enc;
559
655
  private readonly l1;
560
656
  private readonly disk;
657
+ /** When true, all disk-tier and snapshot operations are skipped. */
658
+ private _diskDisabled;
659
+ /** Worker pool for off-main-thread AES-GCM (null when workerThreads is false or unavailable). */
660
+ private _workerPool;
561
661
  private readonly opts;
562
662
  /** Pre-computed once — opts.namespace never changes after construction. */
563
663
  private readonly _namespace;
@@ -587,6 +687,8 @@ declare class CacheService {
587
687
  private readonly instanceId;
588
688
  private readonly backplaneChannel;
589
689
  private subClient;
690
+ /** Timestamp (Date.now()) when the backplane subscriber last lost its connection. */
691
+ private _subDisconnectedAt;
590
692
  private readonly counters;
591
693
  constructor(options?: CacheOptions);
592
694
  /**
@@ -630,6 +732,11 @@ declare class CacheService {
630
732
  /** @internal Exposed for testing. Applies a raw backplane JSON message to local state. */
631
733
  _handleBackplaneMessage(message: string): void;
632
734
  private publishInvalidation;
735
+ /**
736
+ * Scan all keys matching `pattern` across single-node or Cluster clients.
737
+ * For Cluster, iterates every master node independently (SCAN is node-local).
738
+ */
739
+ private _scanKeys;
633
740
  private getRedis;
634
741
  writeSnapshot(altPath?: string): void;
635
742
  loadSnapshot(): void;
@@ -1145,6 +1252,24 @@ declare class SmartMemoryCache {
1145
1252
  */
1146
1253
  evictPercentage(pct: number): number;
1147
1254
  cleanup(): number;
1255
+ /**
1256
+ * Evict all non-CRITICAL L1 entries that were written before `cutoffMs`.
1257
+ *
1258
+ * Called by the backplane staleness fence when the Pub/Sub subscriber
1259
+ * reconnects after a disconnect gap that exceeded `backplaneMaxStalenessMs`.
1260
+ * Entries written before the disconnect started may have been invalidated by
1261
+ * peers while the backplane was down; evicting them forces a controlled
1262
+ * cold-start re-fetch from L2 rather than serving silently stale data.
1263
+ *
1264
+ * Write-time is approximated as `entry.expiresAt - entry.ttlMs`.
1265
+ * Entries with no stored `ttlMs` (e.g. legacy snapshot imports) are evicted
1266
+ * conservatively.
1267
+ *
1268
+ * @param cutoffMs - Unix timestamp in milliseconds; entries written before
1269
+ * this time are evicted.
1270
+ * @returns Number of entries evicted.
1271
+ */
1272
+ evictSetBefore(cutoffMs: number): number;
1148
1273
  exportEntries(forbiddenPrefixes: readonly string[]): Array<{
1149
1274
  key: string;
1150
1275
  entry: SmartCacheEntry;