tricache 1.0.1 → 1.0.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 (96) hide show
  1. package/README.md +36 -28
  2. package/dist/{cache-service-CREOp7MM.d.ts → cache-service-B108FzER.d.ts} +36 -4
  3. package/dist/cache-service-CWI4jkS7.d.cts +963 -0
  4. package/dist/{cache-service-MG7PN6MX.js → cache-service-GYPZFB7O.js} +2 -2
  5. package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
  6. package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
  7. package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
  8. package/dist/chunk-OYDU3SMI.js +89 -0
  9. package/dist/{chunk-3I5L7K6I.js → chunk-VNRDL5RA.js} +102 -140
  10. package/dist/{chunk-JYQFQJNJ.js → chunk-WYF7NOAT.js} +1443 -609
  11. package/dist/cli.cjs +1367 -625
  12. package/dist/cli.d.cts +12 -0
  13. package/dist/cli.js +27 -6
  14. package/dist/dashboard/index.cjs +111 -29
  15. package/dist/dashboard/index.d.cts +185 -0
  16. package/dist/dashboard/index.d.ts +5 -3
  17. package/dist/dashboard/index.js +1 -1
  18. package/dist/drizzle/index.cjs +1370 -624
  19. package/dist/drizzle/index.d.cts +46 -0
  20. package/dist/drizzle/index.d.ts +2 -2
  21. package/dist/drizzle/index.js +1 -1
  22. package/dist/edge/index.cjs +227 -123
  23. package/dist/edge/index.d.cts +127 -0
  24. package/dist/edge/index.d.ts +3 -3
  25. package/dist/edge/index.js +201 -117
  26. package/dist/hono/index.cjs +1418 -644
  27. package/dist/hono/index.d.cts +93 -0
  28. package/dist/hono/index.d.ts +7 -5
  29. package/dist/hono/index.js +16 -9
  30. package/dist/{hono-DSV2ve66.d.ts → hono-3rDMYVtY.d.ts} +16 -3
  31. package/dist/hono-C886ok3X.d.cts +391 -0
  32. package/dist/http/index.cjs +1514 -679
  33. package/dist/http/index.d.cts +103 -0
  34. package/dist/http/index.d.ts +13 -5
  35. package/dist/http/index.js +92 -43
  36. package/dist/index.cjs +1671 -790
  37. package/dist/index.d.cts +1006 -0
  38. package/dist/index.d.ts +39 -8
  39. package/dist/index.js +8 -8
  40. package/dist/murmur3-CMGw9PbV.d.cts +98 -0
  41. package/dist/nestjs/index.cjs +1454 -649
  42. package/dist/nestjs/index.d.cts +285 -0
  43. package/dist/nestjs/index.d.ts +103 -30
  44. package/dist/nestjs/index.js +115 -32
  45. package/dist/next/index.cjs +1512 -718
  46. package/dist/next/index.d.cts +232 -0
  47. package/dist/next/index.d.ts +33 -11
  48. package/dist/next/index.js +172 -99
  49. package/dist/prisma/index.cjs +11 -1
  50. package/dist/prisma/index.d.cts +62 -0
  51. package/dist/prisma/index.d.ts +2 -2
  52. package/dist/prisma/index.js +11 -1
  53. package/dist/serialize-worker.d.cts +2 -0
  54. package/dist/{types-Dyip5wAl.d.ts → types-BA4pz1A0.d.cts} +24 -9
  55. package/dist/types-BA4pz1A0.d.ts +1519 -0
  56. package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
  57. package/dist/utils-Bvzn-rKS.d.ts +29 -0
  58. package/package.json +96 -36
  59. package/src/adapters/auto-pipeliner.ts +107 -24
  60. package/src/adapters/node-redis.ts +106 -27
  61. package/src/cache-service.ts +802 -502
  62. package/src/cli.ts +24 -6
  63. package/src/cross-region.ts +21 -3
  64. package/src/dashboard/handler.ts +26 -6
  65. package/src/dashboard/html.ts +35 -10
  66. package/src/dashboard/index.ts +35 -13
  67. package/src/dashboard/types.ts +3 -1
  68. package/src/disk-tier.ts +171 -65
  69. package/src/edge/cache.ts +145 -97
  70. package/src/edge/hono.ts +23 -6
  71. package/src/edge/types.ts +2 -0
  72. package/src/hono/index.ts +19 -9
  73. package/src/http/express.ts +62 -17
  74. package/src/http/fastify.ts +35 -21
  75. package/src/http/utils.ts +54 -20
  76. package/src/ipc-telemetry.ts +35 -9
  77. package/src/nestjs/decorators.ts +1 -1
  78. package/src/nestjs/index.ts +2 -0
  79. package/src/nestjs/keyv-adapter.ts +68 -0
  80. package/src/nestjs/tricache.module.ts +13 -11
  81. package/src/nestjs/tricache.store.ts +51 -20
  82. package/src/nestjs/types.ts +29 -15
  83. package/src/next/cache-handler.ts +131 -53
  84. package/src/next/index.ts +1 -0
  85. package/src/next/types.ts +21 -0
  86. package/src/prisma/index.ts +12 -1
  87. package/src/smart-memory-cache.ts +63 -26
  88. package/src/types.ts +25 -2
  89. package/src/utils/cgroup.ts +12 -0
  90. package/src/utils/deadline.ts +14 -0
  91. package/src/utils/diagnostics.ts +27 -0
  92. package/src/utils/redis-slot.ts +13 -0
  93. package/src/wasm/bloom-filter-wasm.ts +9 -4
  94. package/src/worker-pool.ts +17 -8
  95. package/src/wtiny-lfu.ts +20 -6
  96. package/dist/chunk-BQRURGGD.js +0 -60
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export { C as CacheService, I as IIpcCacheProvider, a as IpcMetricsPayload, b as IpcTelemetryClient, c as IpcTelemetryServer, L as LatencyWatchdogOptions, d as LatencyWatchdogTelemetry, P as ProcessTerminationBus, T as TierLatencyWatchdog, e as deepMergeOptions, f as findActiveSockets, g as formatBytes, h as formatDuration, i as formatNumber, r as renderProgressBar, j as renderTopDashboard, k as resolveIpcSocketPath } from './cache-service-CREOp7MM.js';
2
- import { j as IRemoteSnapshotAdapter, k as CategoryLimit, S as SmartCacheEntry, l as EvictionReason, i as ILogger, m as CacheHit, f as CachePriority, h as WTinyLfuStats$1, n as CacheEncryption, o as CompressionAlgorithm, p as DiskCacheEntry, D as DiskBackpressureStats, q as IRedisDriver, r as IRedisPipeline } from './types-Dyip5wAl.js';
3
- export { c as CacheHealthStatus, b as CacheMetrics, C as CacheOptions, g as CachePingResult, d as CachePresetType, e as CrossRegionInvalidationEvent, s as CrossRegionRelayOptions, t as CustomCompressor, E as EncryptionMode, u as EnvelopeEncryption, v as EnvelopeEncryptionOptions, H as HttpMeshRelayOptions, w as HttpSnapshotAdapterOptions, x as ICacheBatchObservableCallback, y as ICacheCounter, z as ICacheMeter, A as ICacheObservableGauge, B as ICacheSpan, F as ICacheSpanLink, I as ICacheTracer, G as ICrossRegionRelay, a as InvalidateTagOptions, L as LockOptions, R as RemoteSnapshotOptions, J as WebhookRequestLike, K as WebhookResponseLike, W as WrapOptions, M as consoleLogger, N as createCrossRegionWebhookHandler, O as createCustomCrossRegionRelay, P as createCustomSnapshotAdapter, Q as createHttpMeshRelay, T as createHttpSnapshotAdapter, U as createMemoryCrossRegionRelay, V as createMemorySnapshotAdapter, X as initZstd } from './types-Dyip5wAl.js';
1
+ export { C as CacheService, I as IIpcCacheProvider, a as IpcMetricsPayload, b as IpcTelemetryClient, c as IpcTelemetryServer, L as LatencyWatchdogOptions, d as LatencyWatchdogTelemetry, P as ProcessTerminationBus, T as TierLatencyWatchdog, e as deepMergeOptions, f as findActiveSockets, g as formatBytes, h as formatDuration, i as formatNumber, r as renderProgressBar, j as renderTopDashboard, k as resolveIpcSocketPath } from './cache-service-B108FzER.js';
2
+ import { j as IRemoteSnapshotAdapter, k as CategoryLimit, S as SmartCacheEntry, l as EvictionReason, i as ILogger, m as CacheHit, f as CachePriority, h as WTinyLfuStats$1, n as CacheEncryption, o as CompressionAlgorithm, p as DiskCacheEntry, D as DiskBackpressureStats, q as IRedisDriver, r as IRedisPipeline } from './types-BA4pz1A0.js';
3
+ export { c as CacheHealthStatus, b as CacheMetrics, C as CacheOptions, g as CachePingResult, d as CachePresetType, e as CrossRegionInvalidationEvent, s as CrossRegionRelayOptions, t as CustomCompressor, E as EncryptionMode, u as EnvelopeEncryption, v as EnvelopeEncryptionOptions, H as HttpMeshRelayOptions, w as HttpSnapshotAdapterOptions, x as ICacheBatchObservableCallback, y as ICacheCounter, z as ICacheMeter, A as ICacheObservableGauge, B as ICacheSpan, F as ICacheSpanLink, I as ICacheTracer, G as ICrossRegionRelay, a as InvalidateTagOptions, L as LockOptions, R as RemoteSnapshotOptions, J as WebhookRequestLike, K as WebhookResponseLike, W as WrapOptions, M as consoleLogger, N as createCrossRegionWebhookHandler, O as createCustomCrossRegionRelay, P as createCustomSnapshotAdapter, Q as createHttpMeshRelay, T as createHttpSnapshotAdapter, U as createMemoryCrossRegionRelay, V as createMemorySnapshotAdapter, X as initZstd } from './types-BA4pz1A0.js';
4
4
  export { B as BloomFilterStats, M as Murmur3BloomFilter, W as WasmBloomFilter, m as murmur3_32 } from './murmur3-CMGw9PbV.js';
5
5
  import { EventEmitter } from 'node:events';
6
6
  export { DashboardActionEvent, DashboardOptions, DashboardPeerInstance, StandaloneDashboardOptions, createNextDashboardHandlers, handleDashboardRequest, startDashboardServer, tricacheDashboard } from './dashboard/index.js';
@@ -203,7 +203,8 @@ declare class SmartMemoryCache {
203
203
  private maybeRebuildBloom;
204
204
  private smartEvict;
205
205
  get(key: string): CacheHit | null;
206
- set(key: string, data: unknown, ttlMs: number, priority?: CachePriority, staleAt?: number, delta?: number, tagVersions?: Record<string, number>, setAt?: number): void;
206
+ set(key: string, data: unknown, ttlMs: number, priority?: CachePriority, staleAt?: number, delta?: number, tagVersions?: Record<string, number>, setAt?: number, tags?: string[], dependsOn?: string[]): boolean;
207
+ private storePacked;
207
208
  delete(key: string): boolean;
208
209
  private keysMatchingPattern;
209
210
  deletePattern(pattern: string): number;
@@ -528,6 +529,7 @@ interface WTinyLfuAdmissionDecision {
528
529
  */
529
530
  declare class WTinyLfuPolicy {
530
531
  private readonly sketch;
532
+ private readonly canEvict?;
531
533
  private readonly maxTotal;
532
534
  private readonly maxWindow;
533
535
  private readonly maxProtected;
@@ -544,7 +546,7 @@ declare class WTinyLfuPolicy {
544
546
  constructor(capacity: number, sketch: {
545
547
  estimate(key: string): number;
546
548
  increment(key: string): void;
547
- }, windowPercent?: number, protectedPercent?: number);
549
+ }, windowPercent?: number, protectedPercent?: number, canEvict?: ((key: string) => boolean) | undefined);
548
550
  get windowSize(): number;
549
551
  get probationSize(): number;
550
552
  get protectedSize(): number;
@@ -593,6 +595,8 @@ declare class BoundedDiskQueue {
593
595
  private circuitState;
594
596
  private lastStateChangeMonotonic;
595
597
  constructor(maxConcurrent?: number, maxPending?: number, writeTimeoutMs?: number, failureThreshold?: number, cooldownMs?: number);
598
+ get isSaturated(): boolean;
599
+ private releaseSlot;
596
600
  schedule<T>(task: () => Promise<T>): Promise<T | null>;
597
601
  getStats(): DiskBackpressureStats;
598
602
  reset(): void;
@@ -638,6 +642,13 @@ declare class DiskTier {
638
642
  private diskUsageBytes;
639
643
  private usageCounted;
640
644
  private fileCount;
645
+ private reservedBytes;
646
+ private reservedFiles;
647
+ private saveSequence;
648
+ private epoch;
649
+ private readonly pendingSaves;
650
+ private closed;
651
+ private readonly invalidatedKeys;
641
652
  /** Next bucket index (0–255) for the staggered janitor wheel. */
642
653
  private _nextJanitorBucket;
643
654
  private _isPruning;
@@ -688,6 +699,8 @@ declare class DiskTier {
688
699
  * 12 hex digits ≈ 6 bytes ≈ year 10889 max. Padded so length is always 77 chars.
689
700
  */
690
701
  private hashToWritePath;
702
+ private findFilePaths;
703
+ private fileWrittenAt;
691
704
  /**
692
705
  * Find the on-disk file for a key hash, handling all filename generations:
693
706
  * V3 (current): "{hash}_{expiresHex12}" — 77 chars
@@ -695,6 +708,7 @@ declare class DiskTier {
695
708
  * Returns null when the bucket dir does not exist or the key is absent.
696
709
  */
697
710
  private findFilePath;
711
+ private removeFile;
698
712
  /**
699
713
  * Recursively list all cache file paths across the 2-level sharded subdirs.
700
714
  * Top-level entries that are not directories are skipped (e.g. stale flat
@@ -734,8 +748,10 @@ declare class DiskTier {
734
748
  save(key: string, entry: DiskCacheEntry): Promise<void>;
735
749
  /** Load a key from disk, or return null on miss/expiry/corruption. */
736
750
  load(key: string): DiskCacheEntry | null;
737
- /** Explicitly delete a key from disk (cache invalidation). */
738
- delete(key: string): void;
751
+ /** Synchronously fence queued/running saves while leaving filesystem cleanup deferred. */
752
+ invalidate(key: string): void;
753
+ /** Delete all versions; deferred cleanup must not cancel a newer save. */
754
+ delete(key: string, invalidatedOnly?: boolean): void;
739
755
  /** Purge all expired entries. Returns number deleted. */
740
756
  purgeExpired(): number;
741
757
  /**
@@ -803,8 +819,10 @@ declare class DiskTier {
803
819
  * Universal driver adapter wrapping `@redis/client` (node-redis) to satisfy TriCache's
804
820
  * `IRedisDriver` contract without adding runtime dependencies.
805
821
  */
806
- declare class NodeRedisAdapter implements IRedisDriver {
822
+ declare class NodeRedisAdapter extends EventEmitter implements IRedisDriver {
807
823
  readonly client: any;
824
+ private connecting;
825
+ pttl?: (key: string) => Promise<number>;
808
826
  constructor(client: any);
809
827
  get isOpen(): boolean;
810
828
  connect(): Promise<unknown>;
@@ -827,6 +845,11 @@ declare class NodeRedisAdapter implements IRedisDriver {
827
845
  hgetall(key: string): Promise<Record<string, string>>;
828
846
  publish(channel: string, message: string): Promise<number>;
829
847
  subscribe(...channels: string[]): Promise<unknown>;
848
+ unsubscribe(...channels: string[]): Promise<unknown>;
849
+ xread(...args: (string | number)[]): Promise<Array<[string, Array<[string, string[]]>]> | null>;
850
+ xadd(...args: (string | number)[]): Promise<unknown>;
851
+ xrange(...args: (string | number)[]): Promise<Array<[string, string[]]>>;
852
+ xrevrange(...args: (string | number)[]): Promise<Array<[string, string[]]>>;
830
853
  scanStream(options?: {
831
854
  match?: string;
832
855
  count?: number;
@@ -834,6 +857,7 @@ declare class NodeRedisAdapter implements IRedisDriver {
834
857
  duplicate(): IRedisDriver;
835
858
  on(event: string, listener: (...args: any[]) => void): this;
836
859
  once(event: string, listener: (...args: any[]) => void): this;
860
+ removeListener(event: string, listener: (...args: any[]) => void): this;
837
861
  }
838
862
  /**
839
863
  * Creates a pluggable `IRedisDriver` adapter wrapping an existing `@redis/client` instance.
@@ -857,6 +881,10 @@ interface AutoPipelinerOptions {
857
881
  /** Whether auto-pipelining is enabled. Default: true */
858
882
  enabled?: boolean;
859
883
  }
884
+ interface TimedRedisValue {
885
+ raw: string | Record<string, string> | null;
886
+ ttlMs?: number;
887
+ }
860
888
  /**
861
889
  * AutoPipeliner — Microtask-coalesced Redis request batching.
862
890
  *
@@ -881,11 +909,14 @@ declare class AutoPipeliner {
881
909
  pendingQueueSize: number;
882
910
  };
883
911
  get(key: string): Promise<string | null>;
912
+ read(key: string, hash?: boolean): Promise<TimedRedisValue>;
884
913
  setex(key: string, seconds: number, value: string): Promise<void>;
914
+ setpx(key: string, milliseconds: number, value: string): Promise<void>;
885
915
  del(...keys: string[]): Promise<number>;
886
916
  private supportsPipelines;
887
917
  private enqueue;
888
918
  flush(): Promise<void>;
919
+ private dispatch;
889
920
  }
890
921
 
891
922
  /** Default safety ceiling for autonomous L1 memory allocations: 512 MB */
package/dist/index.js CHANGED
@@ -1,12 +1,8 @@
1
1
  import {
2
2
  NodeRedisAdapter,
3
3
  SigV4SnapshotAdapter,
4
- createCrossRegionWebhookHandler,
5
- createCustomCrossRegionRelay,
6
4
  createCustomSnapshotAdapter,
7
- createHttpMeshRelay,
8
5
  createHttpSnapshotAdapter,
9
- createMemoryCrossRegionRelay,
10
6
  createMemorySnapshotAdapter,
11
7
  createNodeRedisAdapter,
12
8
  createR2SnapshotAdapter,
@@ -14,7 +10,7 @@ import {
14
10
  createSigV4SnapshotAdapter,
15
11
  formatTraceParent,
16
12
  parseTraceParent
17
- } from "./chunk-3I5L7K6I.js";
13
+ } from "./chunk-VNRDL5RA.js";
18
14
  import {
19
15
  AutoPipeliner,
20
16
  CacheCodec,
@@ -30,6 +26,10 @@ import {
30
26
  TinyLfuSketch,
31
27
  WTinyLfuCache,
32
28
  WTinyLfuPolicy,
29
+ createCrossRegionWebhookHandler,
30
+ createCustomCrossRegionRelay,
31
+ createHttpMeshRelay,
32
+ createMemoryCrossRegionRelay,
33
33
  deepMergeOptions,
34
34
  defaultCodec,
35
35
  findActiveSockets,
@@ -43,7 +43,7 @@ import {
43
43
  resolveAutonomousL1MaxBytes,
44
44
  resolveDefaultDiskDir,
45
45
  resolveIpcSocketPath
46
- } from "./chunk-JYQFQJNJ.js";
46
+ } from "./chunk-WYF7NOAT.js";
47
47
  import {
48
48
  CacheEncryption,
49
49
  CachePriority,
@@ -56,7 +56,7 @@ import {
56
56
  handleDashboardRequest,
57
57
  startDashboardServer,
58
58
  tricacheDashboard
59
- } from "./chunk-7RV5ZVBB.js";
59
+ } from "./chunk-JNSLMK4Q.js";
60
60
  import {
61
61
  Murmur3BloomFilter,
62
62
  murmur3_32
@@ -67,7 +67,7 @@ import {
67
67
  isChildTag,
68
68
  isWildcardTag,
69
69
  matchesTagPattern
70
- } from "./chunk-2AJKEQMZ.js";
70
+ } from "./chunk-H4GFLQ26.js";
71
71
  export {
72
72
  AutoPipeliner,
73
73
  CacheCodec,
@@ -0,0 +1,98 @@
1
+ declare class WasmBloomFilter {
2
+ private readonly exports;
3
+ private readonly mem;
4
+ private readonly stagingTarget;
5
+ private readonly encoder;
6
+ private readonly numBits;
7
+ /** Pre-computed — k and numBits are fixed constants; no need to recalculate on every probe. */
8
+ private readonly _maxCapacity;
9
+ /** Tracks total add() calls so we know when phantom bits have saturated the filter. */
10
+ private _insertionCount;
11
+ constructor();
12
+ private writeKey;
13
+ add(key: string): void;
14
+ mightContain(key: string): boolean;
15
+ reset(): void;
16
+ rebuild(keys: Iterable<string>): void;
17
+ get stats(): {
18
+ bitsSet: number;
19
+ fillFactor: number;
20
+ };
21
+ /** Number of add() calls since last reset/rebuild. */
22
+ get insertions(): number;
23
+ /**
24
+ * Maximum safe insertions before false-positive rate exceeds ~1 %.
25
+ * Exact formula for k=7 hash rounds, m=100 000 bits, p=1%:
26
+ * n_max = -m * ln(1 - p^(1/k)) / k ≈ 10 434
27
+ */
28
+ get maxCapacity(): number;
29
+ }
30
+
31
+ /**
32
+ * Standard 32-bit MurmurHash3 (x86_32) implementation.
33
+ * Pure TypeScript, zero external dependencies, zero Node.js Buffer dependencies.
34
+ *
35
+ * Fully compatible with V8 Edge Isolates (Cloudflare Workers, Fastly Compute,
36
+ * Vercel Edge, Deno, Bun, and modern browsers).
37
+ */
38
+ /**
39
+ * Computes the 32-bit MurmurHash3 of a string with an optional seed.
40
+ * Uses 32-bit unsigned arithmetic (>>> 0).
41
+ */
42
+ declare function murmur3_32(key: string, seed?: number): number;
43
+ interface BloomFilterStats {
44
+ bitsSet: number;
45
+ fillFactor: number;
46
+ }
47
+ /**
48
+ * Pure TypeScript Bloom filter backed by a Uint8Array bitset.
49
+ * Uses Murmur3 double-hashing (Kirsch-Mitzenmacher technique):
50
+ * h_i(x) = (h1(x) + i * h2(x)) % m
51
+ *
52
+ * Zero Buffer dependency — runs identically in Node.js, Edge Isolates, and Browsers.
53
+ */
54
+ declare class Murmur3BloomFilter {
55
+ private readonly bits;
56
+ readonly numBits: number;
57
+ readonly k: number;
58
+ private readonly _maxCapacity;
59
+ private _insertionCount;
60
+ /**
61
+ * @param numBits Total bits in the filter (default: 100,000 bits = 12.5 KB)
62
+ * @param k Number of hash probes per key (default: 7, targeting ~1% FPR at 2,000 items)
63
+ */
64
+ constructor(numBits?: number, k?: number);
65
+ private setBit;
66
+ private getBit;
67
+ /**
68
+ * Adds a key to the Bloom filter.
69
+ */
70
+ add(key: string): void;
71
+ /**
72
+ * Tests whether a key might be in the set.
73
+ * Returns `false` for guaranteed misses, `true` for possible hits.
74
+ */
75
+ mightContain(key: string): boolean;
76
+ /**
77
+ * Clears all set bits in the filter.
78
+ */
79
+ reset(): void;
80
+ /**
81
+ * Rebuilds the filter from a collection of keys.
82
+ */
83
+ rebuild(keys: Iterable<string>): void;
84
+ /**
85
+ * Total add() calls since creation or last reset.
86
+ */
87
+ get insertions(): number;
88
+ /**
89
+ * Maximum safe insertions before false positive rate exceeds target ~1%.
90
+ */
91
+ get maxCapacity(): number;
92
+ /**
93
+ * Returns current bit saturation metrics.
94
+ */
95
+ get stats(): BloomFilterStats;
96
+ }
97
+
98
+ export { type BloomFilterStats as B, Murmur3BloomFilter as M, WasmBloomFilter as W, murmur3_32 as m };