tricache 1.0.1 → 2.0.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.
Files changed (127) hide show
  1. package/README.md +89 -93
  2. package/dashboards/tricache-alerts.yaml +80 -4
  3. package/dashboards/tricache-grafana.json +54 -1
  4. package/dashboards/tricache-inventory.rules.yml +21 -0
  5. package/dist/cache-service-BV5T8-20.d.cts +1064 -0
  6. package/dist/cache-service-UZXOQGEY.js +14 -0
  7. package/dist/{cache-service-CREOp7MM.d.ts → cache-service-tpyrXvlH.d.ts} +157 -24
  8. package/dist/{chunk-UBWLPKTI.js → chunk-EJXG7WO6.js} +152 -32
  9. package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
  10. package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
  11. package/dist/{chunk-2AJKEQMZ.js → chunk-KV7KISK2.js} +54 -5
  12. package/dist/{chunk-JYQFQJNJ.js → chunk-MB3DMVWN.js} +3424 -1022
  13. package/dist/chunk-RBJRTGFK.js +94 -0
  14. package/dist/chunk-S5N65I45.js +17 -0
  15. package/dist/cli.cjs +3694 -1070
  16. package/dist/cli.d.cts +12 -0
  17. package/dist/cli.js +210 -14
  18. package/dist/dashboard/index.cjs +111 -29
  19. package/dist/dashboard/index.d.cts +185 -0
  20. package/dist/dashboard/index.d.ts +5 -3
  21. package/dist/dashboard/index.js +1 -1
  22. package/dist/drizzle/index.cjs +3578 -1068
  23. package/dist/drizzle/index.d.cts +46 -0
  24. package/dist/drizzle/index.d.ts +2 -2
  25. package/dist/drizzle/index.js +6 -2
  26. package/dist/edge/index.cjs +368 -162
  27. package/dist/edge/index.d.cts +134 -0
  28. package/dist/edge/index.d.ts +11 -4
  29. package/dist/edge/index.js +314 -155
  30. package/dist/hono/index.cjs +3614 -1087
  31. package/dist/hono/index.d.cts +93 -0
  32. package/dist/hono/index.d.ts +7 -5
  33. package/dist/hono/index.js +16 -9
  34. package/dist/hono-CMv2VdQ6.d.cts +404 -0
  35. package/dist/{hono-DSV2ve66.d.ts → hono-Dvq04kX9.d.ts} +33 -7
  36. package/dist/http/index.cjs +3712 -1124
  37. package/dist/http/index.d.cts +103 -0
  38. package/dist/http/index.d.ts +13 -5
  39. package/dist/http/index.js +92 -43
  40. package/dist/index.cjs +4067 -1252
  41. package/dist/index.d.cts +1071 -0
  42. package/dist/index.d.ts +123 -27
  43. package/dist/index.js +878 -21
  44. package/dist/{murmur3-CMGw9PbV.d.ts → murmur3-DF8BFRnu.d.cts} +1 -0
  45. package/dist/murmur3-DF8BFRnu.d.ts +99 -0
  46. package/dist/nestjs/index.cjs +3635 -1112
  47. package/dist/nestjs/index.d.cts +299 -0
  48. package/dist/nestjs/index.d.ts +120 -33
  49. package/dist/nestjs/index.js +132 -43
  50. package/dist/next/index.cjs +3704 -1175
  51. package/dist/next/index.d.cts +238 -0
  52. package/dist/next/index.d.ts +39 -11
  53. package/dist/next/index.js +202 -102
  54. package/dist/prisma/index.cjs +29 -18
  55. package/dist/prisma/index.d.cts +62 -0
  56. package/dist/prisma/index.d.ts +2 -2
  57. package/dist/prisma/index.js +17 -18
  58. package/dist/serialize-worker.cjs +162 -39
  59. package/dist/serialize-worker.d.cts +2 -0
  60. package/dist/serialize-worker.js +9 -7
  61. package/dist/{types-Dyip5wAl.d.ts → types-F8vRWMrE.d.cts} +186 -54
  62. package/dist/types-F8vRWMrE.d.ts +1636 -0
  63. package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
  64. package/dist/utils-Bvzn-rKS.d.ts +29 -0
  65. package/package.json +116 -37
  66. package/src/adapters/auto-pipeliner.ts +150 -26
  67. package/src/adapters/node-redis.ts +106 -27
  68. package/src/cache-service.ts +2215 -793
  69. package/src/cli.ts +25 -14
  70. package/src/codec.ts +3 -3
  71. package/src/compression.ts +39 -16
  72. package/src/cross-region.ts +22 -4
  73. package/src/dashboard/handler.ts +26 -6
  74. package/src/dashboard/html.ts +35 -10
  75. package/src/dashboard/index.ts +35 -13
  76. package/src/dashboard/types.ts +3 -1
  77. package/src/disk-tier.ts +448 -102
  78. package/src/drizzle/index.ts +2 -1
  79. package/src/edge/adapters/cloudflare-do.ts +25 -10
  80. package/src/edge/adapters/cloudflare-kv.ts +12 -9
  81. package/src/edge/adapters/upstash.ts +27 -11
  82. package/src/edge/cache.ts +200 -110
  83. package/src/edge/hono.ts +23 -6
  84. package/src/edge/types.ts +17 -5
  85. package/src/encryption.ts +50 -18
  86. package/src/env.d.ts +1 -1
  87. package/src/errors.ts +12 -0
  88. package/src/hono/index.ts +19 -9
  89. package/src/http/express.ts +62 -17
  90. package/src/http/fastify.ts +35 -21
  91. package/src/http/utils.ts +60 -20
  92. package/src/index.ts +6 -0
  93. package/src/ipc-telemetry.ts +35 -9
  94. package/src/latency-watchdog.ts +5 -1
  95. package/src/nestjs/decorators.ts +11 -6
  96. package/src/nestjs/index.ts +2 -0
  97. package/src/nestjs/keyv-adapter.ts +68 -0
  98. package/src/nestjs/tricache.module.ts +15 -12
  99. package/src/nestjs/tricache.store.ts +69 -26
  100. package/src/nestjs/types.ts +30 -16
  101. package/src/next/cache-handler.ts +151 -53
  102. package/src/next/index.ts +1 -0
  103. package/src/next/types.ts +25 -0
  104. package/src/origin-capacity.ts +134 -0
  105. package/src/prisma/index.ts +17 -24
  106. package/src/remote-snapshot.ts +37 -27
  107. package/src/serialize-worker.ts +8 -5
  108. package/src/sigv4-snapshot-adapter.ts +37 -27
  109. package/src/smart-memory-cache.ts +121 -44
  110. package/src/types.ts +117 -37
  111. package/src/utils/cgroup.ts +12 -0
  112. package/src/utils/config-comparison.ts +29 -0
  113. package/src/utils/deadline.ts +14 -0
  114. package/src/utils/diagnostics.ts +27 -0
  115. package/src/utils/lz4-size.ts +74 -0
  116. package/src/utils/namespace-key.ts +48 -0
  117. package/src/utils/query-key.ts +13 -0
  118. package/src/utils/redis-slot.ts +13 -0
  119. package/src/utils/snapshot-transfer.ts +83 -0
  120. package/src/utils/work-limits.ts +73 -0
  121. package/src/utils/write-revision.ts +4 -0
  122. package/src/wasm/bloom-filter-wasm.ts +25 -5
  123. package/src/worker-pool.ts +44 -15
  124. package/src/wtiny-lfu.ts +82 -20
  125. package/dist/cache-service-MG7PN6MX.js +0 -12
  126. package/dist/chunk-3I5L7K6I.js +0 -704
  127. package/dist/chunk-BQRURGGD.js +0 -60
@@ -0,0 +1,238 @@
1
+ import { C as CacheService } from '../cache-service-BV5T8-20.cjs';
2
+ import { C as CacheOptions, i as ILogger } from '../types-F8vRWMrE.cjs';
3
+ import 'crypto';
4
+
5
+ /**
6
+ * Next.js 16 modern CacheHandler value shape for `"use cache"` components.
7
+ */
8
+ interface CacheHandlerValue {
9
+ /**
10
+ * Cached value — either a ReadableStream (for RSC payloads), or a serializable JS object / string.
11
+ */
12
+ value: unknown;
13
+ /**
14
+ * Tags associated with this cache entry at write time.
15
+ */
16
+ tags?: string[];
17
+ /**
18
+ * Stored timestamp (ms).
19
+ */
20
+ timestamp?: number;
21
+ /**
22
+ * Remaining / configured TTL (seconds).
23
+ */
24
+ ttl?: number;
25
+ /** Native Next.js CacheEntry timing fields, in seconds. */
26
+ stale?: number;
27
+ revalidate?: number;
28
+ expire?: number;
29
+ }
30
+ /** Complete native Next.js CacheEntry output, structurally checked against Next in tests. */
31
+ interface NextCacheEntry {
32
+ value: ReadableStream<Uint8Array>;
33
+ tags: string[];
34
+ timestamp: number;
35
+ stale: number;
36
+ revalidate: number;
37
+ expire: number;
38
+ }
39
+ /**
40
+ * Stored representation in TriCache for Next.js CacheHandler entries.
41
+ */
42
+ interface StoredNextCacheEntry {
43
+ /** Serialized payload or binary buffer (for streams) */
44
+ data: unknown;
45
+ /** Indicates whether the original value was a ReadableStream */
46
+ isStream: boolean;
47
+ /** Associated tags */
48
+ tags: string[];
49
+ /** Creation timestamp */
50
+ timestamp: number;
51
+ /** Configured TTL (seconds) */
52
+ ttl: number;
53
+ /** Captured soft tag versions at write time */
54
+ softTagVersions?: Record<string, number>;
55
+ generations?: Record<string, number>;
56
+ stale?: number;
57
+ revalidate?: number;
58
+ expire?: number;
59
+ streamEncoding?: 'base64';
60
+ }
61
+ type CacheLifePreset = 'default' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'max';
62
+ interface CacheLifeProfile {
63
+ stale?: number;
64
+ revalidate?: number;
65
+ expire?: number;
66
+ }
67
+ /**
68
+ * Context passed to CacheHandler methods by Next.js runtime.
69
+ */
70
+ interface CacheHandlerContext {
71
+ tags?: string[];
72
+ softTags?: string[];
73
+ revalidate?: number | false;
74
+ cacheLife?: CacheLifePreset | CacheLifeProfile | string;
75
+ }
76
+ /**
77
+ * Options for configuring TriCache Next.js integration.
78
+ */
79
+ interface NextCacheHandlerOptions extends CacheOptions {
80
+ /**
81
+ * Custom namespace for Next.js cache entries. Default: `'next'`
82
+ */
83
+ namespace?: string;
84
+ /**
85
+ * Explicitly force in-memory build phase mode (skips Redis socket connections).
86
+ * Automatically inferred when `process.env.NEXT_PHASE === 'phase-production-build'`
87
+ * or `process.env.NEXT_PHASE === 'phase-export'`.
88
+ */
89
+ isBuildPhase?: boolean;
90
+ /** Maximum buffered RSC stream size. Default: 16 MiB. */
91
+ maxStreamBytes?: number;
92
+ /** Aggregate buffered input stream bytes per shared cache instance. Default: 64 MiB. Excludes encoded copies and caller-owned streams. */
93
+ maxPendingStreamBytes?: number;
94
+ /** Concurrent stream materializations per shared cache instance. Default: 16. Overflow skips caching and cancels that input stream. */
95
+ maxConcurrentStreams?: number;
96
+ }
97
+ /**
98
+ * Legacy Next.js IncrementalCache value shape.
99
+ */
100
+ type IncrementalCacheValue = {
101
+ kind: 'PAGE';
102
+ html: string;
103
+ pageData: unknown;
104
+ headers?: Record<string, string | string[]>;
105
+ status?: number;
106
+ } | {
107
+ kind: 'FETCH';
108
+ data: {
109
+ headers: Record<string, string>;
110
+ body: string;
111
+ status?: number;
112
+ url?: string;
113
+ };
114
+ } | {
115
+ kind: 'REDIRECT';
116
+ props: Record<string, unknown>;
117
+ } | {
118
+ kind: 'IMAGE';
119
+ etag: string;
120
+ buffer: Buffer;
121
+ extension: string;
122
+ isAppImage?: boolean;
123
+ } | {
124
+ kind: 'ROUTE';
125
+ body: Buffer;
126
+ status: number;
127
+ headers: Record<string, string | string[]>;
128
+ };
129
+ /**
130
+ * Legacy Next.js IncrementalCache entry shape.
131
+ */
132
+ interface IncrementalCacheEntry {
133
+ curRevalidate?: number | false;
134
+ revalidateAfter?: number | false;
135
+ isStale?: boolean;
136
+ value: IncrementalCacheValue | null;
137
+ }
138
+
139
+ /**
140
+ * Built-in Next.js 16 cacheLife preset profile timings (in seconds).
141
+ */
142
+ declare const PRESET_CACHE_LIFE_PROFILES: Record<CacheLifePreset, {
143
+ stale: number;
144
+ revalidate: number;
145
+ expire: number;
146
+ }>;
147
+ /**
148
+ * Resolve a Next.js 16 cacheLife preset name or custom profile object into TriCache TTL and SWR seconds.
149
+ */
150
+ declare function resolveCacheLife(profileOrName?: CacheLifePreset | CacheLifeProfile | string): {
151
+ ttl: number;
152
+ swr: number;
153
+ } | null;
154
+ /**
155
+ * Modern Next.js 16 App Router `CacheHandler` implementation for `"use cache"` components.
156
+ *
157
+ * Implements the 5-method contract: `get`, `set`, `refreshTags`, `getExpiration`, `updateTags`.
158
+ */
159
+ declare class TriCacheHandler {
160
+ protected readonly cache: CacheService;
161
+ protected readonly logger: ILogger;
162
+ private readonly maxStreamBytes;
163
+ private readonly streamBudget;
164
+ private static readonly streamBudgets;
165
+ private readonly pendingSets;
166
+ private readonly observedTags;
167
+ constructor(options?: NextCacheHandlerOptions);
168
+ /**
169
+ * Get an entry from cache. Checks softTags against generational tag versions
170
+ * and returns a fresh unlocked ReadableStream if the stored value was a stream.
171
+ */
172
+ get(cacheKey: string, softTags: string[]): Promise<NextCacheEntry | undefined>;
173
+ get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue | undefined>;
174
+ /**
175
+ * Write an entry into cache. Drains incoming ReadableStream into a contiguous buffer
176
+ * before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
177
+ */
178
+ set(cacheKey: string, pendingEntry: Promise<CacheHandlerValue | null>, ctx?: CacheHandlerContext): Promise<void>;
179
+ private observeTag;
180
+ /**
181
+ * Delegate soft-tag checking to get() using Next's Infinity sentinel.
182
+ */
183
+ getExpiration(tags: string[]): Promise<number>;
184
+ /**
185
+ * Force subsequent tag reads to re-sync without forgetting monotonic versions.
186
+ */
187
+ refreshTags(): Promise<void>;
188
+ /**
189
+ * Update or invalidate tags cluster-wide.
190
+ */
191
+ updateTags(tags: string[], durations?: {
192
+ expire?: number;
193
+ }): Promise<void>;
194
+ }
195
+
196
+ /**
197
+ * Legacy Next.js 15/16 ISR `cacheHandler` implementation for Route Handlers and ISR pages.
198
+ *
199
+ * Implements the 4-method contract: `get`, `set`, `revalidateTag`, `resetRequestCache`.
200
+ */
201
+ declare class TriCacheISRHandler {
202
+ protected readonly cache: CacheService;
203
+ protected readonly requestCache: Map<string, IncrementalCacheEntry | null>;
204
+ constructor(options?: NextCacheHandlerOptions);
205
+ /**
206
+ * Read an entry from the cache. Checks the in-memory requestCache first before querying TriCache.
207
+ */
208
+ get(key: string): Promise<IncrementalCacheEntry | null>;
209
+ /**
210
+ * Set an entry in the cache.
211
+ */
212
+ set(key: string, data: IncrementalCacheValue | null, ctx?: {
213
+ tags?: string[];
214
+ revalidate?: number | false;
215
+ }): Promise<void>;
216
+ /**
217
+ * Revalidate all entries associated with a tag.
218
+ * Supports Next.js 16's cacheLife parameter ('max', 'hours', 'days').
219
+ */
220
+ revalidateTag(tag: string, _cacheLife?: string): Promise<void>;
221
+ /**
222
+ * Reset request-scoped in-memory cache at the beginning/end of each request.
223
+ */
224
+ resetRequestCache(): void;
225
+ }
226
+
227
+ /**
228
+ * Factory helper for creating custom configured Next.js cache handler instances.
229
+ *
230
+ * @example
231
+ * // next.config.mjs
232
+ * export default {
233
+ * cacheHandler: require.resolve('tricache/next'),
234
+ * };
235
+ */
236
+ declare function createNextCacheHandler(options?: NextCacheHandlerOptions): typeof TriCacheHandler;
237
+
238
+ export { type CacheHandlerContext, type CacheHandlerValue, type CacheLifePreset, type CacheLifeProfile, type IncrementalCacheEntry, type IncrementalCacheValue, type NextCacheEntry, type NextCacheHandlerOptions, PRESET_CACHE_LIFE_PROFILES, type StoredNextCacheEntry, TriCacheHandler, TriCacheISRHandler, createNextCacheHandler, TriCacheHandler as default, resolveCacheLife };
@@ -1,5 +1,5 @@
1
- import { C as CacheService } from '../cache-service-CREOp7MM.js';
2
- import { C as CacheOptions, i as ILogger } from '../types-Dyip5wAl.js';
1
+ import { C as CacheService } from '../cache-service-tpyrXvlH.js';
2
+ import { C as CacheOptions, i as ILogger } from '../types-F8vRWMrE.js';
3
3
  import 'crypto';
4
4
 
5
5
  /**
@@ -22,6 +22,19 @@ interface CacheHandlerValue {
22
22
  * Remaining / configured TTL (seconds).
23
23
  */
24
24
  ttl?: number;
25
+ /** Native Next.js CacheEntry timing fields, in seconds. */
26
+ stale?: number;
27
+ revalidate?: number;
28
+ expire?: number;
29
+ }
30
+ /** Complete native Next.js CacheEntry output, structurally checked against Next in tests. */
31
+ interface NextCacheEntry {
32
+ value: ReadableStream<Uint8Array>;
33
+ tags: string[];
34
+ timestamp: number;
35
+ stale: number;
36
+ revalidate: number;
37
+ expire: number;
25
38
  }
26
39
  /**
27
40
  * Stored representation in TriCache for Next.js CacheHandler entries.
@@ -39,6 +52,11 @@ interface StoredNextCacheEntry {
39
52
  ttl: number;
40
53
  /** Captured soft tag versions at write time */
41
54
  softTagVersions?: Record<string, number>;
55
+ generations?: Record<string, number>;
56
+ stale?: number;
57
+ revalidate?: number;
58
+ expire?: number;
59
+ streamEncoding?: 'base64';
42
60
  }
43
61
  type CacheLifePreset = 'default' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'max';
44
62
  interface CacheLifeProfile {
@@ -69,6 +87,12 @@ interface NextCacheHandlerOptions extends CacheOptions {
69
87
  * or `process.env.NEXT_PHASE === 'phase-export'`.
70
88
  */
71
89
  isBuildPhase?: boolean;
90
+ /** Maximum buffered RSC stream size. Default: 16 MiB. */
91
+ maxStreamBytes?: number;
92
+ /** Aggregate buffered input stream bytes per shared cache instance. Default: 64 MiB. Excludes encoded copies and caller-owned streams. */
93
+ maxPendingStreamBytes?: number;
94
+ /** Concurrent stream materializations per shared cache instance. Default: 16. Overflow skips caching and cancels that input stream. */
95
+ maxConcurrentStreams?: number;
72
96
  }
73
97
  /**
74
98
  * Legacy Next.js IncrementalCache value shape.
@@ -135,34 +159,38 @@ declare function resolveCacheLife(profileOrName?: CacheLifePreset | CacheLifePro
135
159
  declare class TriCacheHandler {
136
160
  protected readonly cache: CacheService;
137
161
  protected readonly logger: ILogger;
162
+ private readonly maxStreamBytes;
163
+ private readonly streamBudget;
164
+ private static readonly streamBudgets;
165
+ private readonly pendingSets;
166
+ private readonly observedTags;
138
167
  constructor(options?: NextCacheHandlerOptions);
139
168
  /**
140
169
  * Get an entry from cache. Checks softTags against generational tag versions
141
170
  * and returns a fresh unlocked ReadableStream if the stored value was a stream.
142
171
  */
172
+ get(cacheKey: string, softTags: string[]): Promise<NextCacheEntry | undefined>;
143
173
  get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue | undefined>;
144
174
  /**
145
175
  * Write an entry into cache. Drains incoming ReadableStream into a contiguous buffer
146
176
  * before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
147
177
  */
148
178
  set(cacheKey: string, pendingEntry: Promise<CacheHandlerValue | null>, ctx?: CacheHandlerContext): Promise<void>;
179
+ private observeTag;
149
180
  /**
150
- * Returns the expiration timestamp (ms since epoch) of the most relevant
151
- * cached entry for the given tags, or 0 when nothing is tracked. Next.js
152
- * uses this to decide whether a revalidation is due — a hardcoded 0 means
153
- * "always expired", so this reads the real stored entries instead.
181
+ * Delegate soft-tag checking to get() using Next's Infinity sentinel.
154
182
  */
155
183
  getExpiration(tags: string[]): Promise<number>;
156
184
  /**
157
- * Reconcile local generational tag knowledge against shared state: drops the
158
- * in-memory tag-version cache so every subsequent read re-syncs versions
159
- * from Redis. The previous implementation was an empty try/catch.
185
+ * Force subsequent tag reads to re-sync without forgetting monotonic versions.
160
186
  */
161
187
  refreshTags(): Promise<void>;
162
188
  /**
163
189
  * Update or invalidate tags cluster-wide.
164
190
  */
165
- updateTags(tags: string[]): Promise<void>;
191
+ updateTags(tags: string[], durations?: {
192
+ expire?: number;
193
+ }): Promise<void>;
166
194
  }
167
195
 
168
196
  /**
@@ -207,4 +235,4 @@ declare class TriCacheISRHandler {
207
235
  */
208
236
  declare function createNextCacheHandler(options?: NextCacheHandlerOptions): typeof TriCacheHandler;
209
237
 
210
- export { type CacheHandlerContext, type CacheHandlerValue, type CacheLifePreset, type CacheLifeProfile, type IncrementalCacheEntry, type IncrementalCacheValue, type NextCacheHandlerOptions, PRESET_CACHE_LIFE_PROFILES, type StoredNextCacheEntry, TriCacheHandler, TriCacheISRHandler, createNextCacheHandler, TriCacheHandler as default, resolveCacheLife };
238
+ export { type CacheHandlerContext, type CacheHandlerValue, type CacheLifePreset, type CacheLifeProfile, type IncrementalCacheEntry, type IncrementalCacheValue, type NextCacheEntry, type NextCacheHandlerOptions, PRESET_CACHE_LIFE_PROFILES, type StoredNextCacheEntry, TriCacheHandler, TriCacheISRHandler, createNextCacheHandler, TriCacheHandler as default, resolveCacheLife };