tricache 1.0.0 → 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 (97) hide show
  1. package/README.md +36 -28
  2. package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
  3. package/dist/cache-service-CWI4jkS7.d.cts +963 -0
  4. package/dist/{cache-service-26GCO3UU.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-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
  10. package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
  11. package/dist/cli.cjs +1757 -777
  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 +1757 -773
  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 +280 -153
  23. package/dist/edge/index.d.cts +127 -0
  24. package/dist/edge/index.d.ts +3 -3
  25. package/dist/edge/index.js +254 -147
  26. package/dist/hono/index.cjs +1805 -793
  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-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
  31. package/dist/hono-C886ok3X.d.cts +391 -0
  32. package/dist/http/index.cjs +1946 -842
  33. package/dist/http/index.d.cts +103 -0
  34. package/dist/http/index.d.ts +13 -5
  35. package/dist/http/index.js +137 -57
  36. package/dist/index.cjs +2073 -937
  37. package/dist/index.d.cts +1006 -0
  38. package/dist/index.d.ts +40 -8
  39. package/dist/index.js +8 -8
  40. package/dist/murmur3-CMGw9PbV.d.cts +98 -0
  41. package/dist/nestjs/index.cjs +1849 -806
  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 +1913 -887
  46. package/dist/next/index.d.cts +232 -0
  47. package/dist/next/index.d.ts +33 -11
  48. package/dist/next/index.js +174 -107
  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-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -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 +123 -25
  61. package/src/cache-service.ts +1238 -659
  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 +202 -129
  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 +110 -26
  74. package/src/http/fastify.ts +38 -28
  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 +132 -57
  84. package/src/next/index.ts +1 -0
  85. package/src/next/isr-handler.ts +1 -4
  86. package/src/next/types.ts +21 -0
  87. package/src/prisma/index.ts +12 -1
  88. package/src/smart-memory-cache.ts +65 -27
  89. package/src/types.ts +27 -2
  90. package/src/utils/cgroup.ts +12 -0
  91. package/src/utils/deadline.ts +14 -0
  92. package/src/utils/diagnostics.ts +27 -0
  93. package/src/utils/redis-slot.ts +13 -0
  94. package/src/wasm/bloom-filter-wasm.ts +9 -4
  95. package/src/worker-pool.ts +17 -8
  96. package/src/wtiny-lfu.ts +20 -6
  97. package/dist/chunk-BQRURGGD.js +0 -60
@@ -0,0 +1,232 @@
1
+ import { C as CacheService } from '../cache-service-CWI4jkS7.cjs';
2
+ import { C as CacheOptions, i as ILogger } from '../types-BA4pz1A0.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
+ }
93
+ /**
94
+ * Legacy Next.js IncrementalCache value shape.
95
+ */
96
+ type IncrementalCacheValue = {
97
+ kind: 'PAGE';
98
+ html: string;
99
+ pageData: unknown;
100
+ headers?: Record<string, string | string[]>;
101
+ status?: number;
102
+ } | {
103
+ kind: 'FETCH';
104
+ data: {
105
+ headers: Record<string, string>;
106
+ body: string;
107
+ status?: number;
108
+ url?: string;
109
+ };
110
+ } | {
111
+ kind: 'REDIRECT';
112
+ props: Record<string, unknown>;
113
+ } | {
114
+ kind: 'IMAGE';
115
+ etag: string;
116
+ buffer: Buffer;
117
+ extension: string;
118
+ isAppImage?: boolean;
119
+ } | {
120
+ kind: 'ROUTE';
121
+ body: Buffer;
122
+ status: number;
123
+ headers: Record<string, string | string[]>;
124
+ };
125
+ /**
126
+ * Legacy Next.js IncrementalCache entry shape.
127
+ */
128
+ interface IncrementalCacheEntry {
129
+ curRevalidate?: number | false;
130
+ revalidateAfter?: number | false;
131
+ isStale?: boolean;
132
+ value: IncrementalCacheValue | null;
133
+ }
134
+
135
+ /**
136
+ * Built-in Next.js 16 cacheLife preset profile timings (in seconds).
137
+ */
138
+ declare const PRESET_CACHE_LIFE_PROFILES: Record<CacheLifePreset, {
139
+ stale: number;
140
+ revalidate: number;
141
+ expire: number;
142
+ }>;
143
+ /**
144
+ * Resolve a Next.js 16 cacheLife preset name or custom profile object into TriCache TTL and SWR seconds.
145
+ */
146
+ declare function resolveCacheLife(profileOrName?: CacheLifePreset | CacheLifeProfile | string): {
147
+ ttl: number;
148
+ swr: number;
149
+ } | null;
150
+ /**
151
+ * Modern Next.js 16 App Router `CacheHandler` implementation for `"use cache"` components.
152
+ *
153
+ * Implements the 5-method contract: `get`, `set`, `refreshTags`, `getExpiration`, `updateTags`.
154
+ */
155
+ declare class TriCacheHandler {
156
+ protected readonly cache: CacheService;
157
+ protected readonly logger: ILogger;
158
+ private readonly maxStreamBytes;
159
+ private readonly pendingSets;
160
+ private readonly observedTags;
161
+ constructor(options?: NextCacheHandlerOptions);
162
+ /**
163
+ * Get an entry from cache. Checks softTags against generational tag versions
164
+ * and returns a fresh unlocked ReadableStream if the stored value was a stream.
165
+ */
166
+ get(cacheKey: string, softTags: string[]): Promise<NextCacheEntry | undefined>;
167
+ get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue | undefined>;
168
+ /**
169
+ * Write an entry into cache. Drains incoming ReadableStream into a contiguous buffer
170
+ * before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
171
+ */
172
+ set(cacheKey: string, pendingEntry: Promise<CacheHandlerValue | null>, ctx?: CacheHandlerContext): Promise<void>;
173
+ private observeTag;
174
+ /**
175
+ * Delegate soft-tag checking to get() using Next's Infinity sentinel.
176
+ */
177
+ getExpiration(tags: string[]): Promise<number>;
178
+ /**
179
+ * Force subsequent tag reads to re-sync without forgetting monotonic versions.
180
+ */
181
+ refreshTags(): Promise<void>;
182
+ /**
183
+ * Update or invalidate tags cluster-wide.
184
+ */
185
+ updateTags(tags: string[], durations?: {
186
+ expire?: number;
187
+ }): Promise<void>;
188
+ }
189
+
190
+ /**
191
+ * Legacy Next.js 15/16 ISR `cacheHandler` implementation for Route Handlers and ISR pages.
192
+ *
193
+ * Implements the 4-method contract: `get`, `set`, `revalidateTag`, `resetRequestCache`.
194
+ */
195
+ declare class TriCacheISRHandler {
196
+ protected readonly cache: CacheService;
197
+ protected readonly requestCache: Map<string, IncrementalCacheEntry | null>;
198
+ constructor(options?: NextCacheHandlerOptions);
199
+ /**
200
+ * Read an entry from the cache. Checks the in-memory requestCache first before querying TriCache.
201
+ */
202
+ get(key: string): Promise<IncrementalCacheEntry | null>;
203
+ /**
204
+ * Set an entry in the cache.
205
+ */
206
+ set(key: string, data: IncrementalCacheValue | null, ctx?: {
207
+ tags?: string[];
208
+ revalidate?: number | false;
209
+ }): Promise<void>;
210
+ /**
211
+ * Revalidate all entries associated with a tag.
212
+ * Supports Next.js 16's cacheLife parameter ('max', 'hours', 'days').
213
+ */
214
+ revalidateTag(tag: string, _cacheLife?: string): Promise<void>;
215
+ /**
216
+ * Reset request-scoped in-memory cache at the beginning/end of each request.
217
+ */
218
+ resetRequestCache(): void;
219
+ }
220
+
221
+ /**
222
+ * Factory helper for creating custom configured Next.js cache handler instances.
223
+ *
224
+ * @example
225
+ * // next.config.mjs
226
+ * export default {
227
+ * cacheHandler: require.resolve('tricache/next'),
228
+ * };
229
+ */
230
+ declare function createNextCacheHandler(options?: NextCacheHandlerOptions): typeof TriCacheHandler;
231
+
232
+ 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-pJ-nFcVj.js';
2
- import { C as CacheOptions, i as ILogger } from '../types-5HXqBfAf.js';
1
+ import { C as CacheService } from '../cache-service-B108FzER.js';
2
+ import { C as CacheOptions, i as ILogger } from '../types-BA4pz1A0.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,8 @@ 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;
72
92
  }
73
93
  /**
74
94
  * Legacy Next.js IncrementalCache value shape.
@@ -135,34 +155,36 @@ declare function resolveCacheLife(profileOrName?: CacheLifePreset | CacheLifePro
135
155
  declare class TriCacheHandler {
136
156
  protected readonly cache: CacheService;
137
157
  protected readonly logger: ILogger;
158
+ private readonly maxStreamBytes;
159
+ private readonly pendingSets;
160
+ private readonly observedTags;
138
161
  constructor(options?: NextCacheHandlerOptions);
139
162
  /**
140
163
  * Get an entry from cache. Checks softTags against generational tag versions
141
164
  * and returns a fresh unlocked ReadableStream if the stored value was a stream.
142
165
  */
166
+ get(cacheKey: string, softTags: string[]): Promise<NextCacheEntry | undefined>;
143
167
  get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue | undefined>;
144
168
  /**
145
169
  * Write an entry into cache. Drains incoming ReadableStream into a contiguous buffer
146
170
  * before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
147
171
  */
148
172
  set(cacheKey: string, pendingEntry: Promise<CacheHandlerValue | null>, ctx?: CacheHandlerContext): Promise<void>;
173
+ private observeTag;
149
174
  /**
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.
175
+ * Delegate soft-tag checking to get() using Next's Infinity sentinel.
154
176
  */
155
177
  getExpiration(tags: string[]): Promise<number>;
156
178
  /**
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.
179
+ * Force subsequent tag reads to re-sync without forgetting monotonic versions.
160
180
  */
161
181
  refreshTags(): Promise<void>;
162
182
  /**
163
183
  * Update or invalidate tags cluster-wide.
164
184
  */
165
- updateTags(tags: string[]): Promise<void>;
185
+ updateTags(tags: string[], durations?: {
186
+ expire?: number;
187
+ }): Promise<void>;
166
188
  }
167
189
 
168
190
  /**
@@ -207,4 +229,4 @@ declare class TriCacheISRHandler {
207
229
  */
208
230
  declare function createNextCacheHandler(options?: NextCacheHandlerOptions): typeof TriCacheHandler;
209
231
 
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 };
232
+ 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 };