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.
- package/README.md +36 -28
- package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
- package/dist/cache-service-CWI4jkS7.d.cts +963 -0
- package/dist/{cache-service-26GCO3UU.js → cache-service-GYPZFB7O.js} +2 -2
- package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
- package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
- package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
- package/dist/chunk-OYDU3SMI.js +89 -0
- package/dist/{chunk-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
- package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
- package/dist/cli.cjs +1757 -777
- package/dist/cli.d.cts +12 -0
- package/dist/cli.js +27 -6
- package/dist/dashboard/index.cjs +111 -29
- package/dist/dashboard/index.d.cts +185 -0
- package/dist/dashboard/index.d.ts +5 -3
- package/dist/dashboard/index.js +1 -1
- package/dist/drizzle/index.cjs +1757 -773
- package/dist/drizzle/index.d.cts +46 -0
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +280 -153
- package/dist/edge/index.d.cts +127 -0
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +254 -147
- package/dist/hono/index.cjs +1805 -793
- package/dist/hono/index.d.cts +93 -0
- package/dist/hono/index.d.ts +7 -5
- package/dist/hono/index.js +16 -9
- package/dist/{hono-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
- package/dist/hono-C886ok3X.d.cts +391 -0
- package/dist/http/index.cjs +1946 -842
- package/dist/http/index.d.cts +103 -0
- package/dist/http/index.d.ts +13 -5
- package/dist/http/index.js +137 -57
- package/dist/index.cjs +2073 -937
- package/dist/index.d.cts +1006 -0
- package/dist/index.d.ts +40 -8
- package/dist/index.js +8 -8
- package/dist/murmur3-CMGw9PbV.d.cts +98 -0
- package/dist/nestjs/index.cjs +1849 -806
- package/dist/nestjs/index.d.cts +285 -0
- package/dist/nestjs/index.d.ts +103 -30
- package/dist/nestjs/index.js +115 -32
- package/dist/next/index.cjs +1913 -887
- package/dist/next/index.d.cts +232 -0
- package/dist/next/index.d.ts +33 -11
- package/dist/next/index.js +174 -107
- package/dist/prisma/index.cjs +11 -1
- package/dist/prisma/index.d.cts +62 -0
- package/dist/prisma/index.d.ts +2 -2
- package/dist/prisma/index.js +11 -1
- package/dist/serialize-worker.d.cts +2 -0
- package/dist/{types-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -9
- package/dist/types-BA4pz1A0.d.ts +1519 -0
- package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
- package/dist/utils-Bvzn-rKS.d.ts +29 -0
- package/package.json +96 -36
- package/src/adapters/auto-pipeliner.ts +107 -24
- package/src/adapters/node-redis.ts +123 -25
- package/src/cache-service.ts +1238 -659
- package/src/cli.ts +24 -6
- package/src/cross-region.ts +21 -3
- package/src/dashboard/handler.ts +26 -6
- package/src/dashboard/html.ts +35 -10
- package/src/dashboard/index.ts +35 -13
- package/src/dashboard/types.ts +3 -1
- package/src/disk-tier.ts +171 -65
- package/src/edge/cache.ts +202 -129
- package/src/edge/hono.ts +23 -6
- package/src/edge/types.ts +2 -0
- package/src/hono/index.ts +19 -9
- package/src/http/express.ts +110 -26
- package/src/http/fastify.ts +38 -28
- package/src/http/utils.ts +54 -20
- package/src/ipc-telemetry.ts +35 -9
- package/src/nestjs/decorators.ts +1 -1
- package/src/nestjs/index.ts +2 -0
- package/src/nestjs/keyv-adapter.ts +68 -0
- package/src/nestjs/tricache.module.ts +13 -11
- package/src/nestjs/tricache.store.ts +51 -20
- package/src/nestjs/types.ts +29 -15
- package/src/next/cache-handler.ts +132 -57
- package/src/next/index.ts +1 -0
- package/src/next/isr-handler.ts +1 -4
- package/src/next/types.ts +21 -0
- package/src/prisma/index.ts +12 -1
- package/src/smart-memory-cache.ts +65 -27
- package/src/types.ts +27 -2
- package/src/utils/cgroup.ts +12 -0
- package/src/utils/deadline.ts +14 -0
- package/src/utils/diagnostics.ts +27 -0
- package/src/utils/redis-slot.ts +13 -0
- package/src/wasm/bloom-filter-wasm.ts +9 -4
- package/src/worker-pool.ts +17 -8
- package/src/wtiny-lfu.ts +20 -6
- package/dist/chunk-BQRURGGD.js +0 -60
package/dist/next/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CacheService
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-WYF7NOAT.js";
|
|
4
4
|
import {
|
|
5
5
|
consoleLogger
|
|
6
6
|
} from "../chunk-UBWLPKTI.js";
|
|
7
|
-
import "../chunk-
|
|
7
|
+
import "../chunk-H4GFLQ26.js";
|
|
8
8
|
|
|
9
9
|
// src/next/cache-handler.ts
|
|
10
10
|
var PRESET_CACHE_LIFE_PROFILES = {
|
|
@@ -38,18 +38,31 @@ function resolveCacheLife(profileOrName) {
|
|
|
38
38
|
}
|
|
39
39
|
return null;
|
|
40
40
|
}
|
|
41
|
-
async function drainStream(stream) {
|
|
41
|
+
async function drainStream(stream, maxBytes) {
|
|
42
42
|
const reader = stream.getReader();
|
|
43
43
|
const chunks = [];
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
let size = 0;
|
|
45
|
+
try {
|
|
46
|
+
while (true) {
|
|
47
|
+
const { done, value } = await reader.read();
|
|
48
|
+
if (done) break;
|
|
49
|
+
if (value) {
|
|
50
|
+
size += value.byteLength;
|
|
51
|
+
if (size > maxBytes) throw new Error("RSC stream exceeds maxStreamBytes");
|
|
52
|
+
chunks.push(Uint8Array.from(value));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return Buffer.concat(chunks);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
void reader.cancel(error).catch(() => {
|
|
58
|
+
});
|
|
59
|
+
throw error;
|
|
60
|
+
} finally {
|
|
61
|
+
reader.releaseLock();
|
|
48
62
|
}
|
|
49
|
-
return Buffer.concat(chunks);
|
|
50
63
|
}
|
|
51
64
|
function createReadableStream(buffer) {
|
|
52
|
-
const uint8 =
|
|
65
|
+
const uint8 = Uint8Array.from(buffer);
|
|
53
66
|
return new ReadableStream({
|
|
54
67
|
start(controller) {
|
|
55
68
|
controller.enqueue(uint8);
|
|
@@ -60,8 +73,14 @@ function createReadableStream(buffer) {
|
|
|
60
73
|
var TriCacheHandler = class {
|
|
61
74
|
cache;
|
|
62
75
|
logger;
|
|
76
|
+
maxStreamBytes;
|
|
77
|
+
pendingSets = /* @__PURE__ */ new Map();
|
|
78
|
+
observedTags = /* @__PURE__ */ new Map();
|
|
63
79
|
constructor(options = {}) {
|
|
64
80
|
this.logger = options.logger ?? consoleLogger;
|
|
81
|
+
this.maxStreamBytes = options.maxStreamBytes ?? 16 * 1024 * 1024;
|
|
82
|
+
if (!Number.isFinite(this.maxStreamBytes) || this.maxStreamBytes <= 0) throw new Error("maxStreamBytes must be finite and positive");
|
|
83
|
+
if (options.tagStrategy && options.tagStrategy !== "generational") throw new Error("Next.js cache handlers require tagStrategy: generational");
|
|
65
84
|
const isBuildPhase = options.isBuildPhase ?? (typeof process !== "undefined" && (process.env.NEXT_PHASE === "phase-production-build" || process.env.NEXT_PHASE === "phase-export"));
|
|
66
85
|
this.cache = CacheService.create({
|
|
67
86
|
...options,
|
|
@@ -71,37 +90,62 @@ var TriCacheHandler = class {
|
|
|
71
90
|
disableRedis: isBuildPhase ? true : options.disableRedis
|
|
72
91
|
});
|
|
73
92
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
);
|
|
93
|
+
async get(cacheKey, context) {
|
|
94
|
+
let pending = this.pendingSets.get(cacheKey);
|
|
95
|
+
while (pending) {
|
|
96
|
+
await pending.completion;
|
|
97
|
+
pending = this.pendingSets.get(cacheKey);
|
|
98
|
+
}
|
|
99
|
+
const native = Array.isArray(context);
|
|
100
|
+
const ctx = native ? { softTags: context } : context;
|
|
101
|
+
const stored = await this.cache.peek(cacheKey);
|
|
83
102
|
if (!stored || stored.data == null) {
|
|
84
103
|
return void 0;
|
|
85
104
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
105
|
+
const expire = stored.expire ?? stored.ttl;
|
|
106
|
+
if (!Number.isFinite(stored.timestamp) || !Number.isFinite(expire) || Date.now() >= stored.timestamp + expire * 1e3) return void 0;
|
|
107
|
+
const tags = [.../* @__PURE__ */ new Set([...stored.tags, ...ctx?.softTags ?? []])];
|
|
108
|
+
const versions = await Promise.all(tags.map((tag) => this.cache.getTagVersion(tag)));
|
|
109
|
+
const states = await this.cache.mget(tags.map((tag) => `_next_tag_state_:${tag}`));
|
|
110
|
+
let revalidate = stored.revalidate ?? stored.ttl;
|
|
111
|
+
for (let index = 0; index < tags.length; index++) {
|
|
112
|
+
const tag = tags[index];
|
|
113
|
+
const state = states[index];
|
|
114
|
+
const snapshot = stored.generations?.[tag] ?? stored.softTagVersions?.[tag];
|
|
115
|
+
if (state && (state.revalidatedAt > stored.timestamp || state.revalidatedAt === stored.timestamp && snapshot !== versions[index])) {
|
|
116
|
+
if (state.expiredAt !== void 0 && state.expiredAt <= Date.now()) return void 0;
|
|
117
|
+
revalidate = -1;
|
|
118
|
+
} else if (!state && versions[index] > 0) {
|
|
119
|
+
if (snapshot !== void 0 && versions[index] > snapshot) return void 0;
|
|
120
|
+
if (snapshot === void 0) {
|
|
121
|
+
const observed = this.observeTag(tag, versions[index]);
|
|
122
|
+
if (stored.timestamp <= observed) return void 0;
|
|
93
123
|
}
|
|
94
124
|
}
|
|
95
125
|
}
|
|
96
126
|
let val = stored.data;
|
|
97
|
-
if (stored.isStream
|
|
98
|
-
|
|
127
|
+
if (stored.isStream) {
|
|
128
|
+
let bytes;
|
|
129
|
+
if (stored.streamEncoding === "base64" && typeof val === "string") {
|
|
130
|
+
if (val.length > Math.ceil(this.maxStreamBytes / 3) * 4) return void 0;
|
|
131
|
+
bytes = Buffer.from(val, "base64");
|
|
132
|
+
} else if (Buffer.isBuffer(val) || val instanceof Uint8Array) bytes = val;
|
|
133
|
+
else if (val && typeof val === "object" && val.type === "Buffer" && Array.isArray(val.data)) {
|
|
134
|
+
bytes = Buffer.from(val.data);
|
|
135
|
+
} else return void 0;
|
|
136
|
+
if (bytes.length > this.maxStreamBytes) return void 0;
|
|
137
|
+
val = createReadableStream(bytes);
|
|
138
|
+
} else if (native) {
|
|
139
|
+
return void 0;
|
|
99
140
|
}
|
|
100
141
|
return {
|
|
101
142
|
value: val,
|
|
102
143
|
tags: stored.tags,
|
|
103
144
|
timestamp: stored.timestamp,
|
|
104
|
-
ttl: stored.ttl
|
|
145
|
+
ttl: stored.ttl,
|
|
146
|
+
stale: stored.stale ?? 300,
|
|
147
|
+
revalidate,
|
|
148
|
+
expire
|
|
105
149
|
};
|
|
106
150
|
}
|
|
107
151
|
/**
|
|
@@ -109,92 +153,111 @@ var TriCacheHandler = class {
|
|
|
109
153
|
* before storing. Catches mid-flight stream aborts to avoid crashing Next.js.
|
|
110
154
|
*/
|
|
111
155
|
async set(cacheKey, pendingEntry, ctx) {
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
156
|
+
const token = Symbol(cacheKey);
|
|
157
|
+
let finish;
|
|
158
|
+
const completion = new Promise((resolve) => {
|
|
159
|
+
finish = resolve;
|
|
160
|
+
});
|
|
161
|
+
this.pendingSets.set(cacheKey, { token, completion });
|
|
162
|
+
try {
|
|
163
|
+
const canCommit = this.cache.captureMutationFence(cacheKey);
|
|
164
|
+
const entry = await pendingEntry;
|
|
165
|
+
if (!entry || entry.value == null) return;
|
|
166
|
+
const tags = Array.from(/* @__PURE__ */ new Set([...entry.tags ?? [], ...ctx?.tags ?? []]));
|
|
167
|
+
const allTags = [.../* @__PURE__ */ new Set([...tags, ...ctx?.softTags ?? []])];
|
|
168
|
+
const initialVersions = Object.fromEntries(await Promise.all(allTags.map(async (tag) => [tag, await this.cache.getTagVersion(tag)])));
|
|
169
|
+
let dataToStore = entry.value;
|
|
170
|
+
const isStream = Boolean(
|
|
171
|
+
dataToStore && typeof dataToStore.getReader === "function"
|
|
172
|
+
);
|
|
173
|
+
if (isStream) {
|
|
174
|
+
try {
|
|
175
|
+
dataToStore = (await drainStream(dataToStore, this.maxStreamBytes)).toString("base64");
|
|
176
|
+
} catch (streamErr) {
|
|
177
|
+
this.logger.debug("Next.js cacheHandler: stream drain failed mid-flight, skipping cache", {
|
|
178
|
+
cacheKey,
|
|
179
|
+
error: streamErr.message
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
127
183
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (ctx
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const tags = Array.from(/* @__PURE__ */ new Set([...entry.tags ?? [], ...ctx?.tags ?? []]));
|
|
144
|
-
let softTagVersions;
|
|
145
|
-
if (ctx?.softTags && ctx.softTags.length > 0) {
|
|
146
|
-
const softTags = ctx.softTags;
|
|
147
|
-
const vers = await Promise.all(softTags.map((st) => this.cache.getTagVersion(st)));
|
|
148
|
-
softTagVersions = {};
|
|
149
|
-
for (let i = 0; i < softTags.length; i++) {
|
|
150
|
-
softTagVersions[softTags[i]] = vers[i];
|
|
184
|
+
let ttl;
|
|
185
|
+
const resolvedLife = resolveCacheLife(ctx?.cacheLife);
|
|
186
|
+
if (resolvedLife) {
|
|
187
|
+
ttl = resolvedLife.ttl;
|
|
188
|
+
} else if (typeof entry.revalidate === "number") {
|
|
189
|
+
ttl = entry.revalidate;
|
|
190
|
+
} else if (typeof entry.ttl === "number") {
|
|
191
|
+
ttl = entry.ttl;
|
|
192
|
+
} else if (typeof ctx?.revalidate === "number") {
|
|
193
|
+
if (ctx.revalidate <= 0) return;
|
|
194
|
+
ttl = ctx.revalidate;
|
|
195
|
+
} else if (ctx?.revalidate === false) {
|
|
196
|
+
ttl = 31536e3;
|
|
197
|
+
} else {
|
|
198
|
+
ttl = 300;
|
|
151
199
|
}
|
|
200
|
+
const expire = entry.expire ?? (resolvedLife ? resolvedLife.ttl + resolvedLife.swr : ttl);
|
|
201
|
+
const timestamp = entry.timestamp ?? Date.now();
|
|
202
|
+
if (!Number.isFinite(expire) || expire <= 0 || !Number.isFinite(timestamp) || timestamp > Date.now() + (this.cache.options.clockSkewToleranceMs ?? 250)) return;
|
|
203
|
+
const remaining = timestamp + expire * 1e3 - Date.now();
|
|
204
|
+
if (remaining <= 0 || !canCommit() || this.pendingSets.get(cacheKey)?.token !== token) return;
|
|
205
|
+
const finalVersions = await Promise.all(allTags.map((tag) => this.cache.getTagVersion(tag)));
|
|
206
|
+
if (allTags.some((tag, index) => finalVersions[index] !== initialVersions[tag])) return;
|
|
207
|
+
const tagStates = await this.cache.mget(allTags.map((tag) => `_next_tag_state_:${tag}`));
|
|
208
|
+
if (tagStates.some((state) => state && state.revalidatedAt > timestamp)) return;
|
|
209
|
+
let softTagVersions;
|
|
210
|
+
if (ctx?.softTags && ctx.softTags.length > 0) {
|
|
211
|
+
const softTags = ctx.softTags;
|
|
212
|
+
const vers = await Promise.all(softTags.map((st) => this.cache.getTagVersion(st)));
|
|
213
|
+
softTagVersions = {};
|
|
214
|
+
for (let i = 0; i < softTags.length; i++) {
|
|
215
|
+
softTagVersions[softTags[i]] = vers[i];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const stored = {
|
|
219
|
+
data: dataToStore,
|
|
220
|
+
isStream,
|
|
221
|
+
tags,
|
|
222
|
+
timestamp,
|
|
223
|
+
ttl,
|
|
224
|
+
softTagVersions,
|
|
225
|
+
generations: initialVersions,
|
|
226
|
+
stale: entry.stale ?? (typeof ctx?.cacheLife === "string" ? PRESET_CACHE_LIFE_PROFILES[ctx.cacheLife]?.stale : typeof ctx?.cacheLife === "object" ? ctx.cacheLife.stale : void 0) ?? 300,
|
|
227
|
+
revalidate: ttl,
|
|
228
|
+
expire,
|
|
229
|
+
streamEncoding: isStream ? "base64" : void 0
|
|
230
|
+
};
|
|
231
|
+
if (!canCommit() || this.pendingSets.get(cacheKey)?.token !== token) return;
|
|
232
|
+
await this.cache.set(cacheKey, stored, remaining / 1e3, void 0, { tags: [] });
|
|
233
|
+
} finally {
|
|
234
|
+
finish();
|
|
235
|
+
if (this.pendingSets.get(cacheKey)?.token === token) this.pendingSets.delete(cacheKey);
|
|
152
236
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
237
|
+
}
|
|
238
|
+
observeTag(tag, version) {
|
|
239
|
+
const known = this.observedTags.get(tag);
|
|
240
|
+
if (known?.version === version) return known.observedAt;
|
|
241
|
+
if (this.observedTags.size >= 1e4) {
|
|
242
|
+
const oldest = this.observedTags.keys().next().value;
|
|
243
|
+
if (oldest !== void 0) this.observedTags.delete(oldest);
|
|
244
|
+
}
|
|
245
|
+
const observedAt = Date.now();
|
|
246
|
+
this.observedTags.set(tag, { version, observedAt });
|
|
247
|
+
return observedAt;
|
|
162
248
|
}
|
|
163
249
|
/**
|
|
164
|
-
*
|
|
165
|
-
* cached entry for the given tags, or 0 when nothing is tracked. Next.js
|
|
166
|
-
* uses this to decide whether a revalidation is due — a hardcoded 0 means
|
|
167
|
-
* "always expired", so this reads the real stored entries instead.
|
|
250
|
+
* Delegate soft-tag checking to get() using Next's Infinity sentinel.
|
|
168
251
|
*/
|
|
169
252
|
async getExpiration(tags) {
|
|
170
|
-
|
|
171
|
-
let maxExpiration = 0;
|
|
172
|
-
const tagSet = new Set(tags);
|
|
173
|
-
const l1 = this.cache.l1;
|
|
174
|
-
l1.scan((key, entry) => {
|
|
175
|
-
if (!entry.tagVersions) return;
|
|
176
|
-
for (const tag of Object.keys(entry.tagVersions)) {
|
|
177
|
-
if (!tagSet.has(tag)) continue;
|
|
178
|
-
const exp = entry.expiresAt ?? 0;
|
|
179
|
-
if (exp > maxExpiration) maxExpiration = exp;
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
}, 0);
|
|
183
|
-
return maxExpiration;
|
|
184
|
-
} catch (err) {
|
|
185
|
-
this.logger.debug("Next.js cacheHandler getExpiration fail-soft", { error: err.message });
|
|
186
|
-
return 0;
|
|
187
|
-
}
|
|
253
|
+
return tags.length ? Infinity : 0;
|
|
188
254
|
}
|
|
189
255
|
/**
|
|
190
|
-
*
|
|
191
|
-
* in-memory tag-version cache so every subsequent read re-syncs versions
|
|
192
|
-
* from Redis. The previous implementation was an empty try/catch.
|
|
256
|
+
* Force subsequent tag reads to re-sync without forgetting monotonic versions.
|
|
193
257
|
*/
|
|
194
258
|
async refreshTags() {
|
|
195
259
|
try {
|
|
196
|
-
|
|
197
|
-
cache.tagVersions?.clear();
|
|
260
|
+
this.cache.refreshTagVersions();
|
|
198
261
|
} catch (err) {
|
|
199
262
|
this.logger.debug("Next.js cacheHandler refreshTags fail-soft catch", {
|
|
200
263
|
error: err.message
|
|
@@ -204,8 +267,15 @@ var TriCacheHandler = class {
|
|
|
204
267
|
/**
|
|
205
268
|
* Update or invalidate tags cluster-wide.
|
|
206
269
|
*/
|
|
207
|
-
async updateTags(tags) {
|
|
270
|
+
async updateTags(tags, durations) {
|
|
208
271
|
if (!tags || tags.length === 0) return;
|
|
272
|
+
const now = Date.now();
|
|
273
|
+
const delay = durations && durations.expire === void 0 ? void 0 : Math.max(0, durations?.expire ?? 0) * 1e3;
|
|
274
|
+
if (delay !== void 0 && !Number.isFinite(delay)) throw new Error("Tag expiry duration must be finite");
|
|
275
|
+
await Promise.all(tags.map((tag) => this.cache.set(`_next_tag_state_:${tag}`, {
|
|
276
|
+
revalidatedAt: now,
|
|
277
|
+
expiredAt: delay === void 0 ? void 0 : now + delay
|
|
278
|
+
}, 0, 3 /* HIGH */)));
|
|
209
279
|
await this.cache.invalidateTags(tags);
|
|
210
280
|
}
|
|
211
281
|
};
|
|
@@ -232,10 +302,7 @@ var TriCacheISRHandler = class {
|
|
|
232
302
|
if (this.requestCache.has(key)) {
|
|
233
303
|
return this.requestCache.get(key) ?? null;
|
|
234
304
|
}
|
|
235
|
-
const stored = await this.cache.
|
|
236
|
-
key,
|
|
237
|
-
async () => null
|
|
238
|
-
);
|
|
305
|
+
const stored = await this.cache.peek(key);
|
|
239
306
|
if (stored) {
|
|
240
307
|
this.requestCache.set(key, stored);
|
|
241
308
|
return stored;
|
package/dist/prisma/index.cjs
CHANGED
|
@@ -57,8 +57,14 @@ var MUTATION_OPERATIONS = /* @__PURE__ */ new Set([
|
|
|
57
57
|
"deleteMany"
|
|
58
58
|
]);
|
|
59
59
|
function deterministicStringify(val) {
|
|
60
|
+
if (val instanceof Date) return `date:${JSON.stringify(val.toISOString())}`;
|
|
61
|
+
if (typeof val === "bigint") return `bigint:${val}`;
|
|
62
|
+
if (val instanceof Uint8Array) return `binary:${Buffer.from(val.buffer, val.byteOffset, val.byteLength).toString("base64")}`;
|
|
63
|
+
if (val && typeof val === "object" && typeof val.toJSON === "function") {
|
|
64
|
+
return deterministicStringify(val.toJSON());
|
|
65
|
+
}
|
|
60
66
|
if (val === null || typeof val !== "object") {
|
|
61
|
-
return JSON.stringify(val);
|
|
67
|
+
return JSON.stringify(val) ?? "undefined";
|
|
62
68
|
}
|
|
63
69
|
if (Array.isArray(val)) {
|
|
64
70
|
return `[${val.map(deterministicStringify).join(",")}]`;
|
|
@@ -89,6 +95,10 @@ function withTriCache(options) {
|
|
|
89
95
|
if (READ_OPERATIONS.has(operation)) {
|
|
90
96
|
const queryArgs = args ?? {};
|
|
91
97
|
const cacheConfig = queryArgs.cache;
|
|
98
|
+
if (cacheConfig === false) {
|
|
99
|
+
const { cache: _c, ...cleanArgs } = queryArgs;
|
|
100
|
+
return query(cleanArgs);
|
|
101
|
+
}
|
|
92
102
|
if (cacheConfig !== void 0 || autoCache) {
|
|
93
103
|
const opts = typeof cacheConfig === "object" && cacheConfig !== null ? cacheConfig : {};
|
|
94
104
|
const { cache: _c, ...cleanArgs } = queryArgs;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { C as CacheService } from '../cache-service-CWI4jkS7.cjs';
|
|
2
|
+
import { W as WrapOptions } from '../types-BA4pz1A0.cjs';
|
|
3
|
+
import 'crypto';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* tricache/prisma — First-class Prisma Client extension for TriCache.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { PrismaClient } from '@prisma/client';
|
|
10
|
+
* import { withTriCache } from 'tricache/prisma';
|
|
11
|
+
*
|
|
12
|
+
* const prisma = new PrismaClient().$extends(
|
|
13
|
+
* withTriCache({
|
|
14
|
+
* cache,
|
|
15
|
+
* defaultTtl: 300,
|
|
16
|
+
* autoInvalidate: true, // Invalidate model tags on create/update/delete
|
|
17
|
+
* })
|
|
18
|
+
* );
|
|
19
|
+
*
|
|
20
|
+
* // Query with cache options:
|
|
21
|
+
* const users = await prisma.user.findMany({
|
|
22
|
+
* where: { active: true },
|
|
23
|
+
* cache: { ttl: 60, tags: ['users'], swr: 30 },
|
|
24
|
+
* });
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
interface PrismaCacheQueryOptions extends WrapOptions {
|
|
28
|
+
/** Explicit cache key. Defaults to SHA-256 hash of (model + operation + args). */
|
|
29
|
+
key?: string;
|
|
30
|
+
/** Explicit tags for invalidation. Automatically includes `[model.toLowerCase()]`. */
|
|
31
|
+
tags?: string[];
|
|
32
|
+
}
|
|
33
|
+
interface PrismaTriCacheOptions {
|
|
34
|
+
/** TriCache instance to use for caching. */
|
|
35
|
+
cache: CacheService;
|
|
36
|
+
/** Default TTL in seconds for cached queries (default: 300 s). */
|
|
37
|
+
defaultTtl?: number;
|
|
38
|
+
/** Whether to automatically invalidate model tags on write operations (default: true). */
|
|
39
|
+
autoInvalidate?: boolean;
|
|
40
|
+
/** Automatically cache all read queries even if `cache` options are not provided (default: false). */
|
|
41
|
+
autoCache?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Generate a deterministic SHA-256 hash key for a Prisma query. */
|
|
44
|
+
declare function generatePrismaCacheKey(model: string, operation: string, args: unknown): string;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a Prisma Client Extension for TriCache.
|
|
47
|
+
*/
|
|
48
|
+
declare function withTriCache(options: PrismaTriCacheOptions): {
|
|
49
|
+
name: string;
|
|
50
|
+
query: {
|
|
51
|
+
$allModels: {
|
|
52
|
+
$allOperations({ model, operation, args, query }: {
|
|
53
|
+
model: string;
|
|
54
|
+
operation: string;
|
|
55
|
+
args: Record<string, unknown> | undefined;
|
|
56
|
+
query: (args: unknown) => Promise<unknown>;
|
|
57
|
+
}): Promise<unknown>;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { type PrismaCacheQueryOptions, type PrismaTriCacheOptions, generatePrismaCacheKey, withTriCache };
|
package/dist/prisma/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as CacheService } from '../cache-service-
|
|
2
|
-
import { W as WrapOptions } from '../types-
|
|
1
|
+
import { C as CacheService } from '../cache-service-B108FzER.js';
|
|
2
|
+
import { W as WrapOptions } from '../types-BA4pz1A0.js';
|
|
3
3
|
import 'crypto';
|
|
4
4
|
|
|
5
5
|
/**
|
package/dist/prisma/index.js
CHANGED
|
@@ -22,8 +22,14 @@ var MUTATION_OPERATIONS = /* @__PURE__ */ new Set([
|
|
|
22
22
|
"deleteMany"
|
|
23
23
|
]);
|
|
24
24
|
function deterministicStringify(val) {
|
|
25
|
+
if (val instanceof Date) return `date:${JSON.stringify(val.toISOString())}`;
|
|
26
|
+
if (typeof val === "bigint") return `bigint:${val}`;
|
|
27
|
+
if (val instanceof Uint8Array) return `binary:${Buffer.from(val.buffer, val.byteOffset, val.byteLength).toString("base64")}`;
|
|
28
|
+
if (val && typeof val === "object" && typeof val.toJSON === "function") {
|
|
29
|
+
return deterministicStringify(val.toJSON());
|
|
30
|
+
}
|
|
25
31
|
if (val === null || typeof val !== "object") {
|
|
26
|
-
return JSON.stringify(val);
|
|
32
|
+
return JSON.stringify(val) ?? "undefined";
|
|
27
33
|
}
|
|
28
34
|
if (Array.isArray(val)) {
|
|
29
35
|
return `[${val.map(deterministicStringify).join(",")}]`;
|
|
@@ -54,6 +60,10 @@ function withTriCache(options) {
|
|
|
54
60
|
if (READ_OPERATIONS.has(operation)) {
|
|
55
61
|
const queryArgs = args ?? {};
|
|
56
62
|
const cacheConfig = queryArgs.cache;
|
|
63
|
+
if (cacheConfig === false) {
|
|
64
|
+
const { cache: _c, ...cleanArgs } = queryArgs;
|
|
65
|
+
return query(cleanArgs);
|
|
66
|
+
}
|
|
57
67
|
if (cacheConfig !== void 0 || autoCache) {
|
|
58
68
|
const opts = typeof cacheConfig === "object" && cacheConfig !== null ? cacheConfig : {};
|
|
59
69
|
const { cache: _c, ...cleanArgs } = queryArgs;
|
|
@@ -215,13 +215,6 @@ declare function createMemorySnapshotAdapter(): IRemoteSnapshotAdapter & {
|
|
|
215
215
|
clear(): void;
|
|
216
216
|
};
|
|
217
217
|
|
|
218
|
-
/**
|
|
219
|
-
* tricache — Multi-Cluster / Geo-Distributed Cross-Region Invalidation Relay
|
|
220
|
-
*
|
|
221
|
-
* Enables multi-region architectures (e.g. us-east-1 + eu-central-1) with independent
|
|
222
|
-
* Redis clusters to synchronize invalidations across regions without expensive
|
|
223
|
-
* active-active Redis Enterprise licenses.
|
|
224
|
-
*/
|
|
225
218
|
interface CrossRegionInvalidationEvent {
|
|
226
219
|
/** Unique ID of this invalidation event (UUIDv4) */
|
|
227
220
|
id: string;
|
|
@@ -362,7 +355,7 @@ interface ICacheSpan {
|
|
|
362
355
|
code: 0 | 1 | 2;
|
|
363
356
|
message?: string;
|
|
364
357
|
}): this;
|
|
365
|
-
recordException?(err:
|
|
358
|
+
recordException?(err: unknown): void;
|
|
366
359
|
end(): void;
|
|
367
360
|
}
|
|
368
361
|
/**
|
|
@@ -446,6 +439,9 @@ interface SmartCacheEntry {
|
|
|
446
439
|
setAt?: number;
|
|
447
440
|
/** Active tag versions at write time (used when tagStrategy === 'generational') */
|
|
448
441
|
tagVersions?: Record<string, number>;
|
|
442
|
+
/** Set-based tags and dependency patterns preserved through disk/snapshot promotion. */
|
|
443
|
+
tags?: string[];
|
|
444
|
+
dependsOn?: string[];
|
|
449
445
|
}
|
|
450
446
|
/** Returned by SmartMemoryCache.get() — distinguishes "cached null" from a real miss */
|
|
451
447
|
interface CacheHit {
|
|
@@ -466,6 +462,8 @@ interface CacheHit {
|
|
|
466
462
|
tagVersions?: Record<string, number>;
|
|
467
463
|
/** Timestamp (ms) when this entry was written into memory */
|
|
468
464
|
setAt?: number;
|
|
465
|
+
tags?: string[];
|
|
466
|
+
dependsOn?: string[];
|
|
469
467
|
}
|
|
470
468
|
/** On-disk entry shape (mirrors SmartCacheEntry — Uint8Array accepted for msgpack round-trips;
|
|
471
469
|
* legacy disk files written by older versions may have a JSON string in data) */
|
|
@@ -480,6 +478,12 @@ interface DiskCacheEntry {
|
|
|
480
478
|
priority: number;
|
|
481
479
|
/** Active tag versions at write time (used when tagStrategy === 'generational') */
|
|
482
480
|
tagVersions?: Record<string, number>;
|
|
481
|
+
/** Original write time and TTL, preserved on spill and promotion. */
|
|
482
|
+
setAt?: number;
|
|
483
|
+
ttlMs?: number;
|
|
484
|
+
delta?: number;
|
|
485
|
+
tags?: string[];
|
|
486
|
+
dependsOn?: string[];
|
|
483
487
|
}
|
|
484
488
|
/**
|
|
485
489
|
* Snapshot of runtime metrics for a CacheService instance.
|
|
@@ -1404,8 +1408,12 @@ interface LockOptions {
|
|
|
1404
1408
|
interface ICacheCounter {
|
|
1405
1409
|
add(value: number, attributes?: Record<string, string | number | boolean>): void;
|
|
1406
1410
|
}
|
|
1411
|
+
type ICacheObservableCallback = (result: {
|
|
1412
|
+
observe(value: number, attributes?: Record<string, string | number | boolean>): void;
|
|
1413
|
+
}) => void | Promise<void>;
|
|
1407
1414
|
interface ICacheObservableGauge {
|
|
1408
|
-
|
|
1415
|
+
addCallback(callback: ICacheObservableCallback): void;
|
|
1416
|
+
removeCallback(callback: ICacheObservableCallback): void;
|
|
1409
1417
|
}
|
|
1410
1418
|
interface ICacheBatchObservableCallback {
|
|
1411
1419
|
(observableResult: {
|
|
@@ -1422,6 +1430,7 @@ interface ICacheMeter {
|
|
|
1422
1430
|
unit?: string;
|
|
1423
1431
|
}): ICacheObservableGauge;
|
|
1424
1432
|
addBatchObservableCallback(callback: ICacheBatchObservableCallback, observables: ICacheObservableGauge[]): void;
|
|
1433
|
+
removeBatchObservableCallback?(callback: ICacheBatchObservableCallback, observables: ICacheObservableGauge[]): void;
|
|
1425
1434
|
}
|
|
1426
1435
|
/**
|
|
1427
1436
|
* Common pipeline / transaction interface matching ioredis and adapted drivers.
|
|
@@ -1436,6 +1445,10 @@ interface IRedisPipeline {
|
|
|
1436
1445
|
sadd(key: string, ...members: string[]): this;
|
|
1437
1446
|
smembers(key: string): this;
|
|
1438
1447
|
hset(key: string, value: Record<string, string>): this;
|
|
1448
|
+
hgetall?(key: string): this;
|
|
1449
|
+
/** Remaining TTL in milliseconds; enables atomic value + expiry reads. */
|
|
1450
|
+
pttl?(key: string): this;
|
|
1451
|
+
eval?(script: string, numkeys: number, ...args: (string | number)[]): this;
|
|
1439
1452
|
exec(): Promise<Array<[Error | null, any]> | null>;
|
|
1440
1453
|
}
|
|
1441
1454
|
/**
|
|
@@ -1448,6 +1461,7 @@ interface IRedisDriver {
|
|
|
1448
1461
|
setex?(key: string, seconds: number, value: string): Promise<unknown>;
|
|
1449
1462
|
del(...keys: string[]): Promise<number>;
|
|
1450
1463
|
ping(): Promise<string>;
|
|
1464
|
+
pttl?(key: string): Promise<number>;
|
|
1451
1465
|
expire(key: string, seconds: number): Promise<number | boolean>;
|
|
1452
1466
|
incr(key: string): Promise<number>;
|
|
1453
1467
|
eval(script: string, numkeys: number, ...args: (string | number)[]): Promise<unknown>;
|
|
@@ -1461,8 +1475,11 @@ interface IRedisDriver {
|
|
|
1461
1475
|
count?: number;
|
|
1462
1476
|
}): any;
|
|
1463
1477
|
hset?(key: string, value: Record<string, string>): Promise<unknown>;
|
|
1478
|
+
hgetall?(key: string): Promise<Record<string, string>>;
|
|
1464
1479
|
publish?(channel: string, message: string): Promise<number>;
|
|
1465
1480
|
subscribe?(...channels: string[]): Promise<unknown>;
|
|
1481
|
+
unsubscribe?(...channels: string[]): Promise<unknown>;
|
|
1482
|
+
removeListener?(event: string, listener: (...args: any[]) => void): this | unknown;
|
|
1466
1483
|
disconnect(): Promise<void> | void;
|
|
1467
1484
|
quit?(): Promise<void> | void;
|
|
1468
1485
|
duplicate?(): IRedisDriver;
|