tricache 0.7.1 → 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 (95) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +87 -1371
  3. package/dashboards/tricache-alerts.yaml +54 -0
  4. package/dashboards/tricache-grafana.json +548 -0
  5. package/dist/cache-service-5MZXFIGK.js +12 -0
  6. package/dist/cache-service-CyLdpo77.d.ts +871 -0
  7. package/dist/{chunk-EUGYR7KR.js → chunk-6PQ5MOWR.js} +2871 -490
  8. package/dist/chunk-7RV5ZVBB.js +1023 -0
  9. package/dist/{chunk-X5AXHPVD.js → chunk-LJ5QJB5O.js} +90 -5
  10. package/dist/chunk-QNU7B43J.js +687 -0
  11. package/dist/chunk-UGD4VEPD.js +138 -0
  12. package/dist/chunk-XZL45ABR.js +108 -0
  13. package/dist/chunk-ZTSWCFYO.js +135 -0
  14. package/dist/cli.cjs +2790 -471
  15. package/dist/cli.d.ts +1 -0
  16. package/dist/cli.js +103 -5
  17. package/dist/dashboard/index.cjs +1065 -0
  18. package/dist/dashboard/index.d.ts +183 -0
  19. package/dist/dashboard/index.js +22 -0
  20. package/dist/drizzle/index.cjs +2553 -470
  21. package/dist/drizzle/index.d.ts +3 -1
  22. package/dist/drizzle/index.js +1 -1
  23. package/dist/edge/index.cjs +1498 -0
  24. package/dist/edge/index.d.ts +127 -0
  25. package/dist/edge/index.js +1109 -0
  26. package/dist/hono-DQRS1Xd7.d.ts +370 -0
  27. package/dist/http/index.cjs +2926 -562
  28. package/dist/http/index.d.ts +109 -23
  29. package/dist/http/index.js +220 -72
  30. package/dist/index.cjs +6936 -2518
  31. package/dist/index.d.ts +547 -30
  32. package/dist/index.js +104 -5
  33. package/dist/murmur3-CMGw9PbV.d.ts +98 -0
  34. package/dist/nestjs/index.cjs +2494 -463
  35. package/dist/nestjs/index.d.ts +3 -1
  36. package/dist/nestjs/index.js +6 -3
  37. package/dist/next/index.cjs +2517 -483
  38. package/dist/next/index.d.ts +4 -2
  39. package/dist/next/index.js +14 -10
  40. package/dist/prisma/index.d.ts +3 -1
  41. package/dist/serialize-worker.cjs +5 -4
  42. package/dist/serialize-worker.js +1 -1
  43. package/dist/{cache-service-D8tIXmzb.d.ts → types-B6krw64X.d.ts} +596 -659
  44. package/package.json +34 -6
  45. package/src/adapters/auto-pipeliner.ts +206 -0
  46. package/src/adapters/node-redis.ts +370 -0
  47. package/src/cache-service.ts +1339 -402
  48. package/src/cli.ts +120 -2
  49. package/src/codec.ts +72 -0
  50. package/src/cross-region.ts +245 -0
  51. package/src/dashboard/handler.ts +392 -0
  52. package/src/dashboard/html.ts +594 -0
  53. package/src/dashboard/index.ts +195 -0
  54. package/src/dashboard/types.ts +96 -0
  55. package/src/disk-tier.ts +449 -47
  56. package/src/drizzle/index.ts +86 -86
  57. package/src/edge/adapters/cloudflare-do.ts +130 -0
  58. package/src/edge/adapters/cloudflare-kv.ts +115 -0
  59. package/src/edge/adapters/upstash.ts +143 -0
  60. package/src/edge/cache.ts +797 -0
  61. package/src/edge/crypto.ts +182 -0
  62. package/src/edge/hono.ts +203 -0
  63. package/src/edge/index.ts +37 -0
  64. package/src/edge/types.ts +230 -0
  65. package/src/edge/utils/base64.ts +63 -0
  66. package/src/edge/utils/murmur3.ts +179 -0
  67. package/src/encryption.ts +127 -5
  68. package/src/http/express.ts +195 -0
  69. package/src/http/fastify.ts +260 -0
  70. package/src/http/index.ts +39 -258
  71. package/src/http/utils.ts +92 -0
  72. package/src/index.ts +149 -47
  73. package/src/ipc-telemetry.ts +481 -0
  74. package/src/latency-watchdog.ts +282 -0
  75. package/src/nestjs/decorators.ts +161 -161
  76. package/src/nestjs/index.ts +23 -23
  77. package/src/nestjs/tricache.module.ts +104 -104
  78. package/src/nestjs/tricache.store.ts +97 -97
  79. package/src/nestjs/types.ts +79 -79
  80. package/src/next/cache-handler.ts +12 -9
  81. package/src/next/index.ts +37 -37
  82. package/src/next/isr-handler.ts +105 -105
  83. package/src/next/types.ts +130 -130
  84. package/src/remote-snapshot.ts +153 -0
  85. package/src/serialize-worker.ts +120 -120
  86. package/src/sigv4-snapshot-adapter.ts +260 -0
  87. package/src/smart-memory-cache.ts +91 -8
  88. package/src/types.ts +290 -2
  89. package/src/utils/cgroup.ts +100 -0
  90. package/src/utils/tracing.ts +54 -0
  91. package/src/wasm/bloom-filter-wasm.ts +109 -102
  92. package/src/worker-pool.ts +299 -299
  93. package/src/wtiny-lfu.ts +624 -0
  94. package/dist/cache-service-5GATUYAK.js +0 -7
  95. package/dist/chunk-UBSR5UYA.js +0 -0
package/src/next/index.ts CHANGED
@@ -1,37 +1,37 @@
1
- export {
2
- TriCacheHandler,
3
- PRESET_CACHE_LIFE_PROFILES,
4
- resolveCacheLife,
5
- default,
6
- } from './cache-handler';
7
- export { TriCacheISRHandler } from './isr-handler';
8
- export type {
9
- CacheHandlerValue,
10
- StoredNextCacheEntry,
11
- CacheHandlerContext,
12
- NextCacheHandlerOptions,
13
- IncrementalCacheValue,
14
- IncrementalCacheEntry,
15
- CacheLifePreset,
16
- CacheLifeProfile,
17
- } from './types';
18
-
19
- import { TriCacheHandler } from './cache-handler';
20
- import type { NextCacheHandlerOptions } from './types';
21
-
22
- /**
23
- * Factory helper for creating custom configured Next.js cache handler instances.
24
- *
25
- * @example
26
- * // next.config.mjs
27
- * export default {
28
- * cacheHandler: require.resolve('tricache/next'),
29
- * };
30
- */
31
- export function createNextCacheHandler(options: NextCacheHandlerOptions = {}): typeof TriCacheHandler {
32
- return class ConfiguredTriCacheHandler extends TriCacheHandler {
33
- constructor() {
34
- super(options);
35
- }
36
- };
37
- }
1
+ export {
2
+ TriCacheHandler,
3
+ PRESET_CACHE_LIFE_PROFILES,
4
+ resolveCacheLife,
5
+ default,
6
+ } from './cache-handler';
7
+ export { TriCacheISRHandler } from './isr-handler';
8
+ export type {
9
+ CacheHandlerValue,
10
+ StoredNextCacheEntry,
11
+ CacheHandlerContext,
12
+ NextCacheHandlerOptions,
13
+ IncrementalCacheValue,
14
+ IncrementalCacheEntry,
15
+ CacheLifePreset,
16
+ CacheLifeProfile,
17
+ } from './types';
18
+
19
+ import { TriCacheHandler } from './cache-handler';
20
+ import type { NextCacheHandlerOptions } from './types';
21
+
22
+ /**
23
+ * Factory helper for creating custom configured Next.js cache handler instances.
24
+ *
25
+ * @example
26
+ * // next.config.mjs
27
+ * export default {
28
+ * cacheHandler: require.resolve('tricache/next'),
29
+ * };
30
+ */
31
+ export function createNextCacheHandler(options: NextCacheHandlerOptions = {}): typeof TriCacheHandler {
32
+ return class ConfiguredTriCacheHandler extends TriCacheHandler {
33
+ constructor() {
34
+ super(options);
35
+ }
36
+ };
37
+ }
@@ -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
+ }