tricache 0.6.7 → 0.7.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.
- package/LICENSE +1 -1
- package/README.md +480 -22
- package/bin/tricache.js +8 -0
- package/dist/cache-service-AJASH5FX.js +7 -0
- package/dist/cache-service-BblHuF3i.d.ts +1476 -0
- package/dist/chunk-3XQUQBOZ.js +4583 -0
- package/dist/{chunk-FKY5R3CE.js → chunk-PFB5FMEY.js} +126 -4
- package/dist/chunk-UBSR5UYA.js +0 -0
- package/dist/cli.cjs +5181 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +130 -0
- package/dist/drizzle/index.cjs +5161 -0
- package/dist/drizzle/index.d.ts +44 -0
- package/dist/drizzle/index.js +46 -0
- package/dist/http/index.cjs +5289 -0
- package/dist/http/index.d.ts +33 -0
- package/dist/http/index.js +172 -0
- package/dist/index.cjs +1173 -194
- package/dist/index.d.ts +15 -1230
- package/dist/index.js +11 -3705
- package/dist/nestjs/index.cjs +5305 -0
- package/dist/nestjs/index.d.ts +210 -0
- package/dist/nestjs/index.js +250 -0
- package/dist/next/index.cjs +5327 -0
- package/dist/next/index.d.ts +204 -0
- package/dist/next/index.js +274 -0
- package/dist/prisma/index.cjs +122 -0
- package/dist/prisma/index.d.ts +60 -0
- package/dist/prisma/index.js +86 -0
- package/dist/serialize-worker.cjs +177 -13
- package/dist/serialize-worker.js +63 -12
- package/package.json +69 -21
- package/src/cache-service.ts +987 -190
- package/src/cli.ts +143 -0
- package/src/compression.ts +63 -0
- package/src/disk-tier.ts +71 -7
- package/src/drizzle/index.ts +86 -0
- package/src/encryption.ts +56 -5
- package/src/http/index.ts +223 -0
- package/src/index.ts +7 -0
- package/src/nestjs/decorators.ts +161 -0
- package/src/nestjs/index.ts +23 -0
- package/src/nestjs/tricache.module.ts +104 -0
- package/src/nestjs/tricache.store.ts +97 -0
- package/src/nestjs/types.ts +79 -0
- package/src/next/cache-handler.ts +244 -0
- package/src/next/index.ts +37 -0
- package/src/next/isr-handler.ts +105 -0
- package/src/next/types.ts +130 -0
- package/src/prisma/index.ts +151 -0
- package/src/serialize-worker.ts +77 -13
- package/src/smart-memory-cache.ts +22 -8
- package/src/types.ts +172 -2
- package/src/wasm/bloom-filter-wasm.ts +3 -2
- package/src/worker-pool.ts +156 -42
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { f as CacheOptions, C as CacheService, I as ILogger } from '../cache-service-BblHuF3i.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Next.js 16 modern CacheHandler value shape for `"use cache"` components.
|
|
5
|
+
*/
|
|
6
|
+
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
|
+
* Stored representation in TriCache for Next.js CacheHandler entries.
|
|
26
|
+
*/
|
|
27
|
+
interface StoredNextCacheEntry {
|
|
28
|
+
/** Serialized payload or binary buffer (for streams) */
|
|
29
|
+
data: unknown;
|
|
30
|
+
/** Indicates whether the original value was a ReadableStream */
|
|
31
|
+
isStream: boolean;
|
|
32
|
+
/** Associated tags */
|
|
33
|
+
tags: string[];
|
|
34
|
+
/** Creation timestamp */
|
|
35
|
+
timestamp: number;
|
|
36
|
+
/** Configured TTL (seconds) */
|
|
37
|
+
ttl: number;
|
|
38
|
+
/** Captured soft tag versions at write time */
|
|
39
|
+
softTagVersions?: Record<string, number>;
|
|
40
|
+
}
|
|
41
|
+
type CacheLifePreset = 'default' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'max';
|
|
42
|
+
interface CacheLifeProfile {
|
|
43
|
+
stale?: number;
|
|
44
|
+
revalidate?: number;
|
|
45
|
+
expire?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Context passed to CacheHandler methods by Next.js runtime.
|
|
49
|
+
*/
|
|
50
|
+
interface CacheHandlerContext {
|
|
51
|
+
tags?: string[];
|
|
52
|
+
softTags?: string[];
|
|
53
|
+
revalidate?: number | false;
|
|
54
|
+
cacheLife?: CacheLifePreset | CacheLifeProfile | string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Options for configuring TriCache Next.js integration.
|
|
58
|
+
*/
|
|
59
|
+
interface NextCacheHandlerOptions extends CacheOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Custom namespace for Next.js cache entries. Default: `'next'`
|
|
62
|
+
*/
|
|
63
|
+
namespace?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Explicitly force in-memory build phase mode (skips Redis socket connections).
|
|
66
|
+
* Automatically inferred when `process.env.NEXT_PHASE === 'phase-production-build'`
|
|
67
|
+
* or `process.env.NEXT_PHASE === 'phase-export'`.
|
|
68
|
+
*/
|
|
69
|
+
isBuildPhase?: boolean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Legacy Next.js IncrementalCache value shape.
|
|
73
|
+
*/
|
|
74
|
+
type IncrementalCacheValue = {
|
|
75
|
+
kind: 'PAGE';
|
|
76
|
+
html: string;
|
|
77
|
+
pageData: unknown;
|
|
78
|
+
headers?: Record<string, string | string[]>;
|
|
79
|
+
status?: number;
|
|
80
|
+
} | {
|
|
81
|
+
kind: 'FETCH';
|
|
82
|
+
data: {
|
|
83
|
+
headers: Record<string, string>;
|
|
84
|
+
body: string;
|
|
85
|
+
status?: number;
|
|
86
|
+
url?: string;
|
|
87
|
+
};
|
|
88
|
+
} | {
|
|
89
|
+
kind: 'REDIRECT';
|
|
90
|
+
props: Record<string, unknown>;
|
|
91
|
+
} | {
|
|
92
|
+
kind: 'IMAGE';
|
|
93
|
+
etag: string;
|
|
94
|
+
buffer: Buffer;
|
|
95
|
+
extension: string;
|
|
96
|
+
isAppImage?: boolean;
|
|
97
|
+
} | {
|
|
98
|
+
kind: 'ROUTE';
|
|
99
|
+
body: Buffer;
|
|
100
|
+
status: number;
|
|
101
|
+
headers: Record<string, string | string[]>;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Legacy Next.js IncrementalCache entry shape.
|
|
105
|
+
*/
|
|
106
|
+
interface IncrementalCacheEntry {
|
|
107
|
+
curRevalidate?: number | false;
|
|
108
|
+
revalidateAfter?: number | false;
|
|
109
|
+
isStale?: boolean;
|
|
110
|
+
value: IncrementalCacheValue | null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Built-in Next.js 16 cacheLife preset profile timings (in seconds).
|
|
115
|
+
*/
|
|
116
|
+
declare const PRESET_CACHE_LIFE_PROFILES: Record<CacheLifePreset, {
|
|
117
|
+
stale: number;
|
|
118
|
+
revalidate: number;
|
|
119
|
+
expire: number;
|
|
120
|
+
}>;
|
|
121
|
+
/**
|
|
122
|
+
* Resolve a Next.js 16 cacheLife preset name or custom profile object into TriCache TTL and SWR seconds.
|
|
123
|
+
*/
|
|
124
|
+
declare function resolveCacheLife(profileOrName?: CacheLifePreset | CacheLifeProfile | string): {
|
|
125
|
+
ttl: number;
|
|
126
|
+
swr: number;
|
|
127
|
+
} | null;
|
|
128
|
+
/**
|
|
129
|
+
* Modern Next.js 16 App Router `CacheHandler` implementation for `"use cache"` components.
|
|
130
|
+
*
|
|
131
|
+
* Implements the 5-method contract: `get`, `set`, `refreshTags`, `getExpiration`, `updateTags`.
|
|
132
|
+
*/
|
|
133
|
+
declare class TriCacheHandler {
|
|
134
|
+
protected readonly cache: CacheService;
|
|
135
|
+
protected readonly logger: ILogger;
|
|
136
|
+
constructor(options?: NextCacheHandlerOptions);
|
|
137
|
+
/**
|
|
138
|
+
* Get an entry from cache. Checks softTags against generational tag versions
|
|
139
|
+
* and returns a fresh unlocked ReadableStream if the stored value was a stream.
|
|
140
|
+
*/
|
|
141
|
+
get(cacheKey: string, ctx?: CacheHandlerContext): Promise<CacheHandlerValue | null>;
|
|
142
|
+
/**
|
|
143
|
+
* Write an entry into cache. Drains incoming ReadableStream into a contiguous buffer
|
|
144
|
+
* before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
|
|
145
|
+
*/
|
|
146
|
+
set(cacheKey: string, pendingEntry: Promise<CacheHandlerValue | null>, ctx?: CacheHandlerContext): Promise<void>;
|
|
147
|
+
/**
|
|
148
|
+
* Refreshes tag timestamps from shared cache. Fail-soft error boundary ensures
|
|
149
|
+
* network errors never crash incoming HTTP requests.
|
|
150
|
+
*/
|
|
151
|
+
refreshTags(): Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Returns the maximum expiration timestamp across the given tags (or 0 if not tracking).
|
|
154
|
+
*/
|
|
155
|
+
getExpiration(_tags: string[]): Promise<number>;
|
|
156
|
+
/**
|
|
157
|
+
* Update or invalidate tags cluster-wide.
|
|
158
|
+
*/
|
|
159
|
+
updateTags(tags: string[]): Promise<void>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Legacy Next.js 15/16 ISR `cacheHandler` implementation for Route Handlers and ISR pages.
|
|
164
|
+
*
|
|
165
|
+
* Implements the 4-method contract: `get`, `set`, `revalidateTag`, `resetRequestCache`.
|
|
166
|
+
*/
|
|
167
|
+
declare class TriCacheISRHandler {
|
|
168
|
+
protected readonly cache: CacheService;
|
|
169
|
+
protected readonly requestCache: Map<string, IncrementalCacheEntry | null>;
|
|
170
|
+
constructor(options?: NextCacheHandlerOptions);
|
|
171
|
+
/**
|
|
172
|
+
* Read an entry from the cache. Checks the in-memory requestCache first before querying TriCache.
|
|
173
|
+
*/
|
|
174
|
+
get(key: string): Promise<IncrementalCacheEntry | null>;
|
|
175
|
+
/**
|
|
176
|
+
* Set an entry in the cache.
|
|
177
|
+
*/
|
|
178
|
+
set(key: string, data: IncrementalCacheValue | null, ctx?: {
|
|
179
|
+
tags?: string[];
|
|
180
|
+
revalidate?: number | false;
|
|
181
|
+
}): Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* Revalidate all entries associated with a tag.
|
|
184
|
+
* Supports Next.js 16's cacheLife parameter ('max', 'hours', 'days').
|
|
185
|
+
*/
|
|
186
|
+
revalidateTag(tag: string, _cacheLife?: string): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Reset request-scoped in-memory cache at the beginning/end of each request.
|
|
189
|
+
*/
|
|
190
|
+
resetRequestCache(): void;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Factory helper for creating custom configured Next.js cache handler instances.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* // next.config.mjs
|
|
198
|
+
* export default {
|
|
199
|
+
* cacheHandler: require.resolve('tricache/next'),
|
|
200
|
+
* };
|
|
201
|
+
*/
|
|
202
|
+
declare function createNextCacheHandler(options?: NextCacheHandlerOptions): typeof TriCacheHandler;
|
|
203
|
+
|
|
204
|
+
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 };
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CacheService
|
|
3
|
+
} from "../chunk-3XQUQBOZ.js";
|
|
4
|
+
import {
|
|
5
|
+
consoleLogger
|
|
6
|
+
} from "../chunk-PFB5FMEY.js";
|
|
7
|
+
|
|
8
|
+
// src/next/cache-handler.ts
|
|
9
|
+
var PRESET_CACHE_LIFE_PROFILES = {
|
|
10
|
+
default: { stale: 300, revalidate: 900, expire: 2592e3 },
|
|
11
|
+
seconds: { stale: 0, revalidate: 1, expire: 60 },
|
|
12
|
+
minutes: { stale: 300, revalidate: 60, expire: 3600 },
|
|
13
|
+
hours: { stale: 300, revalidate: 3600, expire: 86400 },
|
|
14
|
+
days: { stale: 300, revalidate: 86400, expire: 604800 },
|
|
15
|
+
weeks: { stale: 300, revalidate: 604800, expire: 2592e3 },
|
|
16
|
+
max: { stale: 300, revalidate: 2592e3, expire: 31536e3 }
|
|
17
|
+
};
|
|
18
|
+
function resolveCacheLife(profileOrName) {
|
|
19
|
+
if (!profileOrName) return null;
|
|
20
|
+
if (typeof profileOrName === "string") {
|
|
21
|
+
const preset = PRESET_CACHE_LIFE_PROFILES[profileOrName];
|
|
22
|
+
if (preset) {
|
|
23
|
+
return {
|
|
24
|
+
ttl: preset.revalidate,
|
|
25
|
+
swr: Math.max(0, preset.expire - preset.revalidate)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
if (typeof profileOrName === "object") {
|
|
31
|
+
const revalidate = typeof profileOrName.revalidate === "number" ? profileOrName.revalidate : 900;
|
|
32
|
+
const expire = typeof profileOrName.expire === "number" ? profileOrName.expire : 2592e3;
|
|
33
|
+
return {
|
|
34
|
+
ttl: revalidate,
|
|
35
|
+
swr: Math.max(0, expire - revalidate)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
async function drainStream(stream) {
|
|
41
|
+
const reader = stream.getReader();
|
|
42
|
+
const chunks = [];
|
|
43
|
+
while (true) {
|
|
44
|
+
const { done, value } = await reader.read();
|
|
45
|
+
if (done) break;
|
|
46
|
+
if (value) chunks.push(value);
|
|
47
|
+
}
|
|
48
|
+
return Buffer.concat(chunks);
|
|
49
|
+
}
|
|
50
|
+
function createReadableStream(buffer) {
|
|
51
|
+
const uint8 = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
52
|
+
return new ReadableStream({
|
|
53
|
+
start(controller) {
|
|
54
|
+
controller.enqueue(uint8);
|
|
55
|
+
controller.close();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
var TriCacheHandler = class {
|
|
60
|
+
cache;
|
|
61
|
+
logger;
|
|
62
|
+
constructor(options = {}) {
|
|
63
|
+
this.logger = options.logger ?? consoleLogger;
|
|
64
|
+
const isBuildPhase = options.isBuildPhase ?? (typeof process !== "undefined" && (process.env.NEXT_PHASE === "phase-production-build" || process.env.NEXT_PHASE === "phase-export"));
|
|
65
|
+
this.cache = CacheService.create({
|
|
66
|
+
...options,
|
|
67
|
+
namespace: options.namespace ?? "next",
|
|
68
|
+
tagStrategy: options.tagStrategy ?? "generational",
|
|
69
|
+
cloneStrategy: options.cloneStrategy ?? "none",
|
|
70
|
+
disableRedis: isBuildPhase ? true : options.disableRedis
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get an entry from cache. Checks softTags against generational tag versions
|
|
75
|
+
* and returns a fresh unlocked ReadableStream if the stored value was a stream.
|
|
76
|
+
*/
|
|
77
|
+
async get(cacheKey, ctx) {
|
|
78
|
+
const stored = await this.cache.get(
|
|
79
|
+
cacheKey,
|
|
80
|
+
async () => null
|
|
81
|
+
);
|
|
82
|
+
if (!stored || stored.data == null) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (ctx?.softTags && ctx.softTags.length > 0) {
|
|
86
|
+
for (const softTag of ctx.softTags) {
|
|
87
|
+
const currentVer = await this.cache.getTagVersion(softTag);
|
|
88
|
+
const storedSoftVer = stored.softTagVersions?.[softTag] ?? 0;
|
|
89
|
+
if (currentVer > storedSoftVer) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
let val = stored.data;
|
|
95
|
+
if (stored.isStream && (Buffer.isBuffer(val) || val instanceof Uint8Array)) {
|
|
96
|
+
val = createReadableStream(val);
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
value: val,
|
|
100
|
+
tags: stored.tags,
|
|
101
|
+
timestamp: stored.timestamp,
|
|
102
|
+
ttl: stored.ttl
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Write an entry into cache. Drains incoming ReadableStream into a contiguous buffer
|
|
107
|
+
* before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
|
|
108
|
+
*/
|
|
109
|
+
async set(cacheKey, pendingEntry, ctx) {
|
|
110
|
+
const entry = await pendingEntry;
|
|
111
|
+
if (!entry || entry.value == null) return;
|
|
112
|
+
let dataToStore = entry.value;
|
|
113
|
+
const isStream = Boolean(
|
|
114
|
+
dataToStore && typeof dataToStore.getReader === "function"
|
|
115
|
+
);
|
|
116
|
+
if (isStream) {
|
|
117
|
+
try {
|
|
118
|
+
dataToStore = await drainStream(dataToStore);
|
|
119
|
+
} catch (streamErr) {
|
|
120
|
+
this.logger.debug("Next.js cacheHandler: stream drain failed mid-flight, skipping cache", {
|
|
121
|
+
cacheKey,
|
|
122
|
+
error: streamErr.message
|
|
123
|
+
});
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
let ttl;
|
|
128
|
+
const resolvedLife = resolveCacheLife(ctx?.cacheLife);
|
|
129
|
+
if (resolvedLife) {
|
|
130
|
+
ttl = resolvedLife.ttl;
|
|
131
|
+
} else if (typeof entry.ttl === "number") {
|
|
132
|
+
ttl = entry.ttl;
|
|
133
|
+
} else if (typeof ctx?.revalidate === "number") {
|
|
134
|
+
if (ctx.revalidate <= 0) return;
|
|
135
|
+
ttl = ctx.revalidate;
|
|
136
|
+
} else if (ctx?.revalidate === false) {
|
|
137
|
+
ttl = 31536e3;
|
|
138
|
+
} else {
|
|
139
|
+
ttl = 300;
|
|
140
|
+
}
|
|
141
|
+
const tags = Array.from(/* @__PURE__ */ new Set([...entry.tags ?? [], ...ctx?.tags ?? []]));
|
|
142
|
+
let softTagVersions;
|
|
143
|
+
if (ctx?.softTags && ctx.softTags.length > 0) {
|
|
144
|
+
softTagVersions = {};
|
|
145
|
+
for (const st of ctx.softTags) {
|
|
146
|
+
softTagVersions[st] = await this.cache.getTagVersion(st);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const stored = {
|
|
150
|
+
data: dataToStore,
|
|
151
|
+
isStream,
|
|
152
|
+
tags,
|
|
153
|
+
timestamp: entry.timestamp ?? Date.now(),
|
|
154
|
+
ttl,
|
|
155
|
+
softTagVersions
|
|
156
|
+
};
|
|
157
|
+
await this.cache.set(cacheKey, stored, ttl, void 0, tags.length > 0 ? { tags } : void 0);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Refreshes tag timestamps from shared cache. Fail-soft error boundary ensures
|
|
161
|
+
* network errors never crash incoming HTTP requests.
|
|
162
|
+
*/
|
|
163
|
+
async refreshTags() {
|
|
164
|
+
try {
|
|
165
|
+
} catch (err) {
|
|
166
|
+
this.logger.debug("Next.js cacheHandler refreshTags fail-soft catch", {
|
|
167
|
+
error: err.message
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns the maximum expiration timestamp across the given tags (or 0 if not tracking).
|
|
173
|
+
*/
|
|
174
|
+
async getExpiration(_tags) {
|
|
175
|
+
return 0;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Update or invalidate tags cluster-wide.
|
|
179
|
+
*/
|
|
180
|
+
async updateTags(tags) {
|
|
181
|
+
if (!tags || tags.length === 0) return;
|
|
182
|
+
await this.cache.invalidateTags(tags);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
var cache_handler_default = TriCacheHandler;
|
|
186
|
+
|
|
187
|
+
// src/next/isr-handler.ts
|
|
188
|
+
var TriCacheISRHandler = class {
|
|
189
|
+
cache;
|
|
190
|
+
requestCache = /* @__PURE__ */ new Map();
|
|
191
|
+
constructor(options = {}) {
|
|
192
|
+
const isBuildPhase = options.isBuildPhase ?? (typeof process !== "undefined" && (process.env.NEXT_PHASE === "phase-production-build" || process.env.NEXT_PHASE === "phase-export"));
|
|
193
|
+
this.cache = CacheService.create({
|
|
194
|
+
...options,
|
|
195
|
+
namespace: options.namespace ?? "next-isr",
|
|
196
|
+
tagStrategy: options.tagStrategy ?? "generational",
|
|
197
|
+
cloneStrategy: options.cloneStrategy ?? "none",
|
|
198
|
+
disableRedis: isBuildPhase ? true : options.disableRedis
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Read an entry from the cache. Checks the in-memory requestCache first before querying TriCache.
|
|
203
|
+
*/
|
|
204
|
+
async get(key) {
|
|
205
|
+
if (this.requestCache.has(key)) {
|
|
206
|
+
return this.requestCache.get(key) ?? null;
|
|
207
|
+
}
|
|
208
|
+
const stored = await this.cache.get(
|
|
209
|
+
key,
|
|
210
|
+
async () => null
|
|
211
|
+
);
|
|
212
|
+
if (stored) {
|
|
213
|
+
this.requestCache.set(key, stored);
|
|
214
|
+
return stored;
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Set an entry in the cache.
|
|
220
|
+
*/
|
|
221
|
+
async set(key, data, ctx) {
|
|
222
|
+
if (!data) return;
|
|
223
|
+
let ttl;
|
|
224
|
+
if (typeof ctx?.revalidate === "number") {
|
|
225
|
+
if (ctx.revalidate <= 0) return;
|
|
226
|
+
ttl = ctx.revalidate;
|
|
227
|
+
} else if (ctx?.revalidate === false) {
|
|
228
|
+
ttl = 31536e3;
|
|
229
|
+
} else {
|
|
230
|
+
ttl = 300;
|
|
231
|
+
}
|
|
232
|
+
const entry = {
|
|
233
|
+
curRevalidate: ctx?.revalidate,
|
|
234
|
+
revalidateAfter: typeof ctx?.revalidate === "number" ? Date.now() + ctx.revalidate * 1e3 : false,
|
|
235
|
+
isStale: false,
|
|
236
|
+
value: data
|
|
237
|
+
};
|
|
238
|
+
this.requestCache.set(key, entry);
|
|
239
|
+
const tags = ctx?.tags;
|
|
240
|
+
await this.cache.set(key, entry, ttl, void 0, tags?.length ? { tags } : void 0);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Revalidate all entries associated with a tag.
|
|
244
|
+
* Supports Next.js 16's cacheLife parameter ('max', 'hours', 'days').
|
|
245
|
+
*/
|
|
246
|
+
async revalidateTag(tag, _cacheLife) {
|
|
247
|
+
if (!tag) return;
|
|
248
|
+
this.requestCache.clear();
|
|
249
|
+
await this.cache.invalidateTag(tag);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Reset request-scoped in-memory cache at the beginning/end of each request.
|
|
253
|
+
*/
|
|
254
|
+
resetRequestCache() {
|
|
255
|
+
this.requestCache.clear();
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// src/next/index.ts
|
|
260
|
+
function createNextCacheHandler(options = {}) {
|
|
261
|
+
return class ConfiguredTriCacheHandler extends TriCacheHandler {
|
|
262
|
+
constructor() {
|
|
263
|
+
super(options);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
export {
|
|
268
|
+
PRESET_CACHE_LIFE_PROFILES,
|
|
269
|
+
TriCacheHandler,
|
|
270
|
+
TriCacheISRHandler,
|
|
271
|
+
createNextCacheHandler,
|
|
272
|
+
cache_handler_default as default,
|
|
273
|
+
resolveCacheLife
|
|
274
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/prisma/index.ts
|
|
31
|
+
var prisma_exports = {};
|
|
32
|
+
__export(prisma_exports, {
|
|
33
|
+
generatePrismaCacheKey: () => generatePrismaCacheKey,
|
|
34
|
+
withTriCache: () => withTriCache
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(prisma_exports);
|
|
37
|
+
var import_crypto = __toESM(require("crypto"), 1);
|
|
38
|
+
var READ_OPERATIONS = /* @__PURE__ */ new Set([
|
|
39
|
+
"findUnique",
|
|
40
|
+
"findUniqueOrThrow",
|
|
41
|
+
"findFirst",
|
|
42
|
+
"findFirstOrThrow",
|
|
43
|
+
"findMany",
|
|
44
|
+
"count",
|
|
45
|
+
"aggregate",
|
|
46
|
+
"groupBy"
|
|
47
|
+
]);
|
|
48
|
+
var MUTATION_OPERATIONS = /* @__PURE__ */ new Set([
|
|
49
|
+
"create",
|
|
50
|
+
"createMany",
|
|
51
|
+
"createManyAndReturn",
|
|
52
|
+
"update",
|
|
53
|
+
"updateMany",
|
|
54
|
+
"updateManyAndReturn",
|
|
55
|
+
"upsert",
|
|
56
|
+
"delete",
|
|
57
|
+
"deleteMany"
|
|
58
|
+
]);
|
|
59
|
+
function deterministicStringify(val) {
|
|
60
|
+
if (val === null || typeof val !== "object") {
|
|
61
|
+
return JSON.stringify(val);
|
|
62
|
+
}
|
|
63
|
+
if (Array.isArray(val)) {
|
|
64
|
+
return `[${val.map(deterministicStringify).join(",")}]`;
|
|
65
|
+
}
|
|
66
|
+
const obj = val;
|
|
67
|
+
const keys = Object.keys(obj).sort();
|
|
68
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${deterministicStringify(obj[k])}`).join(",")}}`;
|
|
69
|
+
}
|
|
70
|
+
function generatePrismaCacheKey(model, operation, args) {
|
|
71
|
+
const normalized = deterministicStringify({ model, operation, args });
|
|
72
|
+
const hash = import_crypto.default.createHash("sha256").update(normalized, "utf8").digest("hex").slice(0, 32);
|
|
73
|
+
return `prisma:${model.toLowerCase()}:${operation}:${hash}`;
|
|
74
|
+
}
|
|
75
|
+
function withTriCache(options) {
|
|
76
|
+
const { cache, defaultTtl = 300, autoInvalidate = true, autoCache = false } = options;
|
|
77
|
+
return {
|
|
78
|
+
name: "tricache-prisma-extension",
|
|
79
|
+
query: {
|
|
80
|
+
$allModels: {
|
|
81
|
+
async $allOperations({ model, operation, args, query }) {
|
|
82
|
+
const modelTag = model.toLowerCase();
|
|
83
|
+
if (autoInvalidate && MUTATION_OPERATIONS.has(operation)) {
|
|
84
|
+
const result = await query(args);
|
|
85
|
+
await cache.invalidateTag(modelTag);
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
if (READ_OPERATIONS.has(operation)) {
|
|
89
|
+
const queryArgs = args ?? {};
|
|
90
|
+
const cacheConfig = queryArgs.cache;
|
|
91
|
+
if (cacheConfig !== void 0 || autoCache) {
|
|
92
|
+
const opts = typeof cacheConfig === "object" && cacheConfig !== null ? cacheConfig : {};
|
|
93
|
+
const { cache: _c, ...cleanArgs } = queryArgs;
|
|
94
|
+
const key = opts.key ?? generatePrismaCacheKey(model, operation, cleanArgs);
|
|
95
|
+
const tags = [.../* @__PURE__ */ new Set([modelTag, ...opts.tags ?? []])];
|
|
96
|
+
return cache.wrap(
|
|
97
|
+
key,
|
|
98
|
+
() => query(cleanArgs),
|
|
99
|
+
{
|
|
100
|
+
ttl: opts.ttl ?? defaultTtl,
|
|
101
|
+
swr: opts.swr,
|
|
102
|
+
priority: opts.priority,
|
|
103
|
+
tags,
|
|
104
|
+
dependsOn: opts.dependsOn,
|
|
105
|
+
refreshAhead: opts.refreshAhead,
|
|
106
|
+
xfetchBeta: opts.xfetchBeta,
|
|
107
|
+
notFoundTtl: opts.notFoundTtl
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return query(args);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
generatePrismaCacheKey,
|
|
121
|
+
withTriCache
|
|
122
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { W as WrapOptions, C as CacheService } from '../cache-service-BblHuF3i.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* tricache/prisma — First-class Prisma Client extension for TriCache.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* import { PrismaClient } from '@prisma/client';
|
|
8
|
+
* import { withTriCache } from 'tricache/prisma';
|
|
9
|
+
*
|
|
10
|
+
* const prisma = new PrismaClient().$extends(
|
|
11
|
+
* withTriCache({
|
|
12
|
+
* cache,
|
|
13
|
+
* defaultTtl: 300,
|
|
14
|
+
* autoInvalidate: true, // Invalidate model tags on create/update/delete
|
|
15
|
+
* })
|
|
16
|
+
* );
|
|
17
|
+
*
|
|
18
|
+
* // Query with cache options:
|
|
19
|
+
* const users = await prisma.user.findMany({
|
|
20
|
+
* where: { active: true },
|
|
21
|
+
* cache: { ttl: 60, tags: ['users'], swr: 30 },
|
|
22
|
+
* });
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
interface PrismaCacheQueryOptions extends WrapOptions {
|
|
26
|
+
/** Explicit cache key. Defaults to SHA-256 hash of (model + operation + args). */
|
|
27
|
+
key?: string;
|
|
28
|
+
/** Explicit tags for invalidation. Automatically includes `[model.toLowerCase()]`. */
|
|
29
|
+
tags?: string[];
|
|
30
|
+
}
|
|
31
|
+
interface PrismaTriCacheOptions {
|
|
32
|
+
/** TriCache instance to use for caching. */
|
|
33
|
+
cache: CacheService;
|
|
34
|
+
/** Default TTL in seconds for cached queries (default: 300 s). */
|
|
35
|
+
defaultTtl?: number;
|
|
36
|
+
/** Whether to automatically invalidate model tags on write operations (default: true). */
|
|
37
|
+
autoInvalidate?: boolean;
|
|
38
|
+
/** Automatically cache all read queries even if `cache` options are not provided (default: false). */
|
|
39
|
+
autoCache?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** Generate a deterministic SHA-256 hash key for a Prisma query. */
|
|
42
|
+
declare function generatePrismaCacheKey(model: string, operation: string, args: unknown): string;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a Prisma Client Extension for TriCache.
|
|
45
|
+
*/
|
|
46
|
+
declare function withTriCache(options: PrismaTriCacheOptions): {
|
|
47
|
+
name: string;
|
|
48
|
+
query: {
|
|
49
|
+
$allModels: {
|
|
50
|
+
$allOperations({ model, operation, args, query }: {
|
|
51
|
+
model: string;
|
|
52
|
+
operation: string;
|
|
53
|
+
args: Record<string, unknown> | undefined;
|
|
54
|
+
query: (args: unknown) => Promise<unknown>;
|
|
55
|
+
}): Promise<unknown>;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export { type PrismaCacheQueryOptions, type PrismaTriCacheOptions, generatePrismaCacheKey, withTriCache };
|