tricache 0.7.0 → 0.8.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 (100) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +144 -1428
  3. package/bin/tricache.js +8 -8
  4. package/dashboards/tricache-alerts.yaml +54 -0
  5. package/dashboards/tricache-grafana.json +548 -0
  6. package/dist/cache-service-5MZXFIGK.js +12 -0
  7. package/dist/cache-service-CyLdpo77.d.ts +871 -0
  8. package/dist/{chunk-3XQUQBOZ.js → chunk-6PQ5MOWR.js} +2950 -523
  9. package/dist/chunk-7RV5ZVBB.js +1023 -0
  10. package/dist/{chunk-PFB5FMEY.js → chunk-LJ5QJB5O.js} +116 -20
  11. package/dist/chunk-QNU7B43J.js +687 -0
  12. package/dist/chunk-UGD4VEPD.js +138 -0
  13. package/dist/chunk-XZL45ABR.js +108 -0
  14. package/dist/chunk-ZTSWCFYO.js +135 -0
  15. package/dist/cli.cjs +2894 -518
  16. package/dist/cli.d.ts +1 -0
  17. package/dist/cli.js +103 -5
  18. package/dist/dashboard/index.cjs +1065 -0
  19. package/dist/dashboard/index.d.ts +183 -0
  20. package/dist/dashboard/index.js +22 -0
  21. package/dist/drizzle/index.cjs +2648 -508
  22. package/dist/drizzle/index.d.ts +3 -1
  23. package/dist/drizzle/index.js +1 -1
  24. package/dist/edge/index.cjs +1498 -0
  25. package/dist/edge/index.d.ts +127 -0
  26. package/dist/edge/index.js +1109 -0
  27. package/dist/hono-DQRS1Xd7.d.ts +370 -0
  28. package/dist/http/index.cjs +3038 -589
  29. package/dist/http/index.d.ts +109 -23
  30. package/dist/http/index.js +238 -62
  31. package/dist/index.cjs +6962 -2487
  32. package/dist/index.d.ts +547 -30
  33. package/dist/index.js +104 -5
  34. package/dist/murmur3-CMGw9PbV.d.ts +98 -0
  35. package/dist/nestjs/index.cjs +2586 -498
  36. package/dist/nestjs/index.d.ts +3 -1
  37. package/dist/nestjs/index.js +6 -3
  38. package/dist/next/index.cjs +2633 -519
  39. package/dist/next/index.d.ts +13 -7
  40. package/dist/next/index.js +45 -18
  41. package/dist/prisma/index.cjs +2 -1
  42. package/dist/prisma/index.d.ts +3 -1
  43. package/dist/prisma/index.js +2 -1
  44. package/dist/serialize-worker.cjs +31 -19
  45. package/dist/serialize-worker.js +1 -1
  46. package/dist/{cache-service-BblHuF3i.d.ts → types-B6krw64X.d.ts} +605 -646
  47. package/package.json +37 -7
  48. package/src/adapters/auto-pipeliner.ts +206 -0
  49. package/src/adapters/node-redis.ts +370 -0
  50. package/src/cache-service.ts +4231 -3226
  51. package/src/cli.ts +261 -143
  52. package/src/codec.ts +72 -0
  53. package/src/compression.ts +56 -63
  54. package/src/cross-region.ts +245 -0
  55. package/src/dashboard/handler.ts +392 -0
  56. package/src/dashboard/html.ts +594 -0
  57. package/src/dashboard/index.ts +195 -0
  58. package/src/dashboard/types.ts +96 -0
  59. package/src/disk-tier.ts +1223 -821
  60. package/src/drizzle/index.ts +86 -86
  61. package/src/edge/adapters/cloudflare-do.ts +130 -0
  62. package/src/edge/adapters/cloudflare-kv.ts +115 -0
  63. package/src/edge/adapters/upstash.ts +143 -0
  64. package/src/edge/cache.ts +797 -0
  65. package/src/edge/crypto.ts +182 -0
  66. package/src/edge/hono.ts +203 -0
  67. package/src/edge/index.ts +37 -0
  68. package/src/edge/types.ts +230 -0
  69. package/src/edge/utils/base64.ts +63 -0
  70. package/src/edge/utils/murmur3.ts +179 -0
  71. package/src/encryption.ts +635 -493
  72. package/src/http/express.ts +195 -0
  73. package/src/http/fastify.ts +260 -0
  74. package/src/http/index.ts +46 -223
  75. package/src/http/utils.ts +92 -0
  76. package/src/index.ts +149 -47
  77. package/src/ipc-telemetry.ts +481 -0
  78. package/src/latency-watchdog.ts +282 -0
  79. package/src/nestjs/decorators.ts +161 -161
  80. package/src/nestjs/index.ts +23 -23
  81. package/src/nestjs/tricache.module.ts +104 -104
  82. package/src/nestjs/tricache.store.ts +97 -97
  83. package/src/nestjs/types.ts +79 -79
  84. package/src/next/cache-handler.ts +271 -244
  85. package/src/next/index.ts +37 -37
  86. package/src/next/isr-handler.ts +105 -105
  87. package/src/next/types.ts +130 -130
  88. package/src/prisma/index.ts +154 -151
  89. package/src/remote-snapshot.ts +153 -0
  90. package/src/serialize-worker.ts +120 -120
  91. package/src/sigv4-snapshot-adapter.ts +260 -0
  92. package/src/smart-memory-cache.ts +1109 -1024
  93. package/src/types.ts +1199 -902
  94. package/src/utils/cgroup.ts +100 -0
  95. package/src/utils/tracing.ts +54 -0
  96. package/src/wasm/bloom-filter-wasm.ts +109 -102
  97. package/src/worker-pool.ts +299 -299
  98. package/src/wtiny-lfu.ts +624 -0
  99. package/dist/cache-service-AJASH5FX.js +0 -7
  100. package/dist/chunk-UBSR5UYA.js +0 -0
@@ -1,105 +1,105 @@
1
- import { CacheService } from '../cache-service';
2
- import type {
3
- IncrementalCacheEntry,
4
- IncrementalCacheValue,
5
- NextCacheHandlerOptions,
6
- } from './types';
7
-
8
- /**
9
- * Legacy Next.js 15/16 ISR `cacheHandler` implementation for Route Handlers and ISR pages.
10
- *
11
- * Implements the 4-method contract: `get`, `set`, `revalidateTag`, `resetRequestCache`.
12
- */
13
- export class TriCacheISRHandler {
14
- protected readonly cache: CacheService;
15
- protected readonly requestCache = new Map<string, IncrementalCacheEntry | null>();
16
-
17
- constructor(options: NextCacheHandlerOptions = {}) {
18
- const isBuildPhase =
19
- options.isBuildPhase ??
20
- (typeof process !== 'undefined' &&
21
- (process.env.NEXT_PHASE === 'phase-production-build' ||
22
- process.env.NEXT_PHASE === 'phase-export'));
23
-
24
- this.cache = CacheService.create({
25
- ...options,
26
- namespace: options.namespace ?? 'next-isr',
27
- tagStrategy: options.tagStrategy ?? 'generational',
28
- cloneStrategy: options.cloneStrategy ?? 'none',
29
- disableRedis: isBuildPhase ? true : options.disableRedis,
30
- });
31
- }
32
-
33
- /**
34
- * Read an entry from the cache. Checks the in-memory requestCache first before querying TriCache.
35
- */
36
- async get(key: string): Promise<IncrementalCacheEntry | null> {
37
- if (this.requestCache.has(key)) {
38
- return this.requestCache.get(key) ?? null;
39
- }
40
-
41
- const stored = await this.cache.get<IncrementalCacheEntry | null>(
42
- key,
43
- async () => null,
44
- );
45
-
46
- if (stored) {
47
- this.requestCache.set(key, stored);
48
- return stored;
49
- }
50
-
51
- return null;
52
- }
53
-
54
- /**
55
- * Set an entry in the cache.
56
- */
57
- async set(
58
- key: string,
59
- data: IncrementalCacheValue | null,
60
- ctx?: { tags?: string[]; revalidate?: number | false },
61
- ): Promise<void> {
62
- if (!data) return;
63
-
64
- let ttl: number;
65
- if (typeof ctx?.revalidate === 'number') {
66
- if (ctx.revalidate <= 0) return; // Do not cache dynamic 0s responses
67
- ttl = ctx.revalidate;
68
- } else if (ctx?.revalidate === false) {
69
- ttl = 31_536_000; // 1 year for static indefinite assets
70
- } else {
71
- ttl = 300; // fallback default
72
- }
73
-
74
- const entry: IncrementalCacheEntry = {
75
- curRevalidate: ctx?.revalidate,
76
- revalidateAfter: typeof ctx?.revalidate === 'number' ? Date.now() + ctx.revalidate * 1000 : false,
77
- isStale: false,
78
- value: data,
79
- };
80
-
81
- this.requestCache.set(key, entry);
82
-
83
- const tags = ctx?.tags;
84
- await this.cache.set(key, entry, ttl, undefined, tags?.length ? { tags } : undefined);
85
- }
86
-
87
- /**
88
- * Revalidate all entries associated with a tag.
89
- * Supports Next.js 16's cacheLife parameter ('max', 'hours', 'days').
90
- */
91
- async revalidateTag(tag: string, _cacheLife?: string): Promise<void> {
92
- if (!tag) return;
93
- this.requestCache.clear();
94
- await this.cache.invalidateTag(tag);
95
- }
96
-
97
- /**
98
- * Reset request-scoped in-memory cache at the beginning/end of each request.
99
- */
100
- resetRequestCache(): void {
101
- this.requestCache.clear();
102
- }
103
- }
104
-
105
- export default TriCacheISRHandler;
1
+ import { CacheService } from '../cache-service';
2
+ import type {
3
+ IncrementalCacheEntry,
4
+ IncrementalCacheValue,
5
+ NextCacheHandlerOptions,
6
+ } from './types';
7
+
8
+ /**
9
+ * Legacy Next.js 15/16 ISR `cacheHandler` implementation for Route Handlers and ISR pages.
10
+ *
11
+ * Implements the 4-method contract: `get`, `set`, `revalidateTag`, `resetRequestCache`.
12
+ */
13
+ export class TriCacheISRHandler {
14
+ protected readonly cache: CacheService;
15
+ protected readonly requestCache = new Map<string, IncrementalCacheEntry | null>();
16
+
17
+ constructor(options: NextCacheHandlerOptions = {}) {
18
+ const isBuildPhase =
19
+ options.isBuildPhase ??
20
+ (typeof process !== 'undefined' &&
21
+ (process.env.NEXT_PHASE === 'phase-production-build' ||
22
+ process.env.NEXT_PHASE === 'phase-export'));
23
+
24
+ this.cache = CacheService.create({
25
+ ...options,
26
+ namespace: options.namespace ?? 'next-isr',
27
+ tagStrategy: options.tagStrategy ?? 'generational',
28
+ cloneStrategy: options.cloneStrategy ?? 'none',
29
+ disableRedis: isBuildPhase ? true : options.disableRedis,
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Read an entry from the cache. Checks the in-memory requestCache first before querying TriCache.
35
+ */
36
+ async get(key: string): Promise<IncrementalCacheEntry | null> {
37
+ if (this.requestCache.has(key)) {
38
+ return this.requestCache.get(key) ?? null;
39
+ }
40
+
41
+ const stored = await this.cache.get<IncrementalCacheEntry | null>(
42
+ key,
43
+ async () => null,
44
+ );
45
+
46
+ if (stored) {
47
+ this.requestCache.set(key, stored);
48
+ return stored;
49
+ }
50
+
51
+ return null;
52
+ }
53
+
54
+ /**
55
+ * Set an entry in the cache.
56
+ */
57
+ async set(
58
+ key: string,
59
+ data: IncrementalCacheValue | null,
60
+ ctx?: { tags?: string[]; revalidate?: number | false },
61
+ ): Promise<void> {
62
+ if (!data) return;
63
+
64
+ let ttl: number;
65
+ if (typeof ctx?.revalidate === 'number') {
66
+ if (ctx.revalidate <= 0) return; // Do not cache dynamic 0s responses
67
+ ttl = ctx.revalidate;
68
+ } else if (ctx?.revalidate === false) {
69
+ ttl = 31_536_000; // 1 year for static indefinite assets
70
+ } else {
71
+ ttl = 300; // fallback default
72
+ }
73
+
74
+ const entry: IncrementalCacheEntry = {
75
+ curRevalidate: ctx?.revalidate,
76
+ revalidateAfter: typeof ctx?.revalidate === 'number' ? Date.now() + ctx.revalidate * 1000 : false,
77
+ isStale: false,
78
+ value: data,
79
+ };
80
+
81
+ this.requestCache.set(key, entry);
82
+
83
+ const tags = ctx?.tags;
84
+ await this.cache.set(key, entry, ttl, undefined, tags?.length ? { tags } : undefined);
85
+ }
86
+
87
+ /**
88
+ * Revalidate all entries associated with a tag.
89
+ * Supports Next.js 16's cacheLife parameter ('max', 'hours', 'days').
90
+ */
91
+ async revalidateTag(tag: string, _cacheLife?: string): Promise<void> {
92
+ if (!tag) return;
93
+ this.requestCache.clear();
94
+ await this.cache.invalidateTag(tag);
95
+ }
96
+
97
+ /**
98
+ * Reset request-scoped in-memory cache at the beginning/end of each request.
99
+ */
100
+ resetRequestCache(): void {
101
+ this.requestCache.clear();
102
+ }
103
+ }
104
+
105
+ export default TriCacheISRHandler;
package/src/next/types.ts CHANGED
@@ -1,130 +1,130 @@
1
- import type { CacheOptions } from '../types';
2
-
3
- /**
4
- * Next.js 16 modern CacheHandler value shape for `"use cache"` components.
5
- */
6
- export interface CacheHandlerValue {
7
- /**
8
- * Cached value — either a ReadableStream (for RSC payloads), or a serializable JS object / string.
9
- */
10
- value: unknown;
11
- /**
12
- * Tags associated with this cache entry at write time.
13
- */
14
- tags?: string[];
15
- /**
16
- * Stored timestamp (ms).
17
- */
18
- timestamp?: number;
19
- /**
20
- * Remaining / configured TTL (seconds).
21
- */
22
- ttl?: number;
23
- }
24
-
25
- /**
26
- * Stored representation in TriCache for Next.js CacheHandler entries.
27
- */
28
- export interface StoredNextCacheEntry {
29
- /** Serialized payload or binary buffer (for streams) */
30
- data: unknown;
31
- /** Indicates whether the original value was a ReadableStream */
32
- isStream: boolean;
33
- /** Associated tags */
34
- tags: string[];
35
- /** Creation timestamp */
36
- timestamp: number;
37
- /** Configured TTL (seconds) */
38
- ttl: number;
39
- /** Captured soft tag versions at write time */
40
- softTagVersions?: Record<string, number>;
41
- }
42
-
43
- export type CacheLifePreset =
44
- | 'default'
45
- | 'seconds'
46
- | 'minutes'
47
- | 'hours'
48
- | 'days'
49
- | 'weeks'
50
- | 'max';
51
-
52
- export interface CacheLifeProfile {
53
- stale?: number;
54
- revalidate?: number;
55
- expire?: number;
56
- }
57
-
58
- /**
59
- * Context passed to CacheHandler methods by Next.js runtime.
60
- */
61
- export interface CacheHandlerContext {
62
- tags?: string[];
63
- softTags?: string[];
64
- revalidate?: number | false;
65
- cacheLife?: CacheLifePreset | CacheLifeProfile | string;
66
- }
67
-
68
- /**
69
- * Options for configuring TriCache Next.js integration.
70
- */
71
- export interface NextCacheHandlerOptions extends CacheOptions {
72
- /**
73
- * Custom namespace for Next.js cache entries. Default: `'next'`
74
- */
75
- namespace?: string;
76
- /**
77
- * Explicitly force in-memory build phase mode (skips Redis socket connections).
78
- * Automatically inferred when `process.env.NEXT_PHASE === 'phase-production-build'`
79
- * or `process.env.NEXT_PHASE === 'phase-export'`.
80
- */
81
- isBuildPhase?: boolean;
82
- }
83
-
84
- /**
85
- * Legacy Next.js IncrementalCache value shape.
86
- */
87
- export type IncrementalCacheValue =
88
- | {
89
- kind: 'PAGE';
90
- html: string;
91
- pageData: unknown;
92
- headers?: Record<string, string | string[]>;
93
- status?: number;
94
- }
95
- | {
96
- kind: 'FETCH';
97
- data: {
98
- headers: Record<string, string>;
99
- body: string;
100
- status?: number;
101
- url?: string;
102
- };
103
- }
104
- | {
105
- kind: 'REDIRECT';
106
- props: Record<string, unknown>;
107
- }
108
- | {
109
- kind: 'IMAGE';
110
- etag: string;
111
- buffer: Buffer;
112
- extension: string;
113
- isAppImage?: boolean;
114
- }
115
- | {
116
- kind: 'ROUTE';
117
- body: Buffer;
118
- status: number;
119
- headers: Record<string, string | string[]>;
120
- };
121
-
122
- /**
123
- * Legacy Next.js IncrementalCache entry shape.
124
- */
125
- export interface IncrementalCacheEntry {
126
- curRevalidate?: number | false;
127
- revalidateAfter?: number | false;
128
- isStale?: boolean;
129
- value: IncrementalCacheValue | null;
130
- }
1
+ import type { CacheOptions } from '../types';
2
+
3
+ /**
4
+ * Next.js 16 modern CacheHandler value shape for `"use cache"` components.
5
+ */
6
+ export interface CacheHandlerValue {
7
+ /**
8
+ * Cached value — either a ReadableStream (for RSC payloads), or a serializable JS object / string.
9
+ */
10
+ value: unknown;
11
+ /**
12
+ * Tags associated with this cache entry at write time.
13
+ */
14
+ tags?: string[];
15
+ /**
16
+ * Stored timestamp (ms).
17
+ */
18
+ timestamp?: number;
19
+ /**
20
+ * Remaining / configured TTL (seconds).
21
+ */
22
+ ttl?: number;
23
+ }
24
+
25
+ /**
26
+ * Stored representation in TriCache for Next.js CacheHandler entries.
27
+ */
28
+ export interface StoredNextCacheEntry {
29
+ /** Serialized payload or binary buffer (for streams) */
30
+ data: unknown;
31
+ /** Indicates whether the original value was a ReadableStream */
32
+ isStream: boolean;
33
+ /** Associated tags */
34
+ tags: string[];
35
+ /** Creation timestamp */
36
+ timestamp: number;
37
+ /** Configured TTL (seconds) */
38
+ ttl: number;
39
+ /** Captured soft tag versions at write time */
40
+ softTagVersions?: Record<string, number>;
41
+ }
42
+
43
+ export type CacheLifePreset =
44
+ | 'default'
45
+ | 'seconds'
46
+ | 'minutes'
47
+ | 'hours'
48
+ | 'days'
49
+ | 'weeks'
50
+ | 'max';
51
+
52
+ export interface CacheLifeProfile {
53
+ stale?: number;
54
+ revalidate?: number;
55
+ expire?: number;
56
+ }
57
+
58
+ /**
59
+ * Context passed to CacheHandler methods by Next.js runtime.
60
+ */
61
+ export interface CacheHandlerContext {
62
+ tags?: string[];
63
+ softTags?: string[];
64
+ revalidate?: number | false;
65
+ cacheLife?: CacheLifePreset | CacheLifeProfile | string;
66
+ }
67
+
68
+ /**
69
+ * Options for configuring TriCache Next.js integration.
70
+ */
71
+ export interface NextCacheHandlerOptions extends CacheOptions {
72
+ /**
73
+ * Custom namespace for Next.js cache entries. Default: `'next'`
74
+ */
75
+ namespace?: string;
76
+ /**
77
+ * Explicitly force in-memory build phase mode (skips Redis socket connections).
78
+ * Automatically inferred when `process.env.NEXT_PHASE === 'phase-production-build'`
79
+ * or `process.env.NEXT_PHASE === 'phase-export'`.
80
+ */
81
+ isBuildPhase?: boolean;
82
+ }
83
+
84
+ /**
85
+ * Legacy Next.js IncrementalCache value shape.
86
+ */
87
+ export type IncrementalCacheValue =
88
+ | {
89
+ kind: 'PAGE';
90
+ html: string;
91
+ pageData: unknown;
92
+ headers?: Record<string, string | string[]>;
93
+ status?: number;
94
+ }
95
+ | {
96
+ kind: 'FETCH';
97
+ data: {
98
+ headers: Record<string, string>;
99
+ body: string;
100
+ status?: number;
101
+ url?: string;
102
+ };
103
+ }
104
+ | {
105
+ kind: 'REDIRECT';
106
+ props: Record<string, unknown>;
107
+ }
108
+ | {
109
+ kind: 'IMAGE';
110
+ etag: string;
111
+ buffer: Buffer;
112
+ extension: string;
113
+ isAppImage?: boolean;
114
+ }
115
+ | {
116
+ kind: 'ROUTE';
117
+ body: Buffer;
118
+ status: number;
119
+ headers: Record<string, string | string[]>;
120
+ };
121
+
122
+ /**
123
+ * Legacy Next.js IncrementalCache entry shape.
124
+ */
125
+ export interface IncrementalCacheEntry {
126
+ curRevalidate?: number | false;
127
+ revalidateAfter?: number | false;
128
+ isStale?: boolean;
129
+ value: IncrementalCacheValue | null;
130
+ }