tricache 0.7.1 → 0.9.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 +21 -21
- package/README.md +89 -1371
- package/dashboards/prometheus-scrape.yml +18 -0
- package/dashboards/tricache-alerts.yaml +54 -0
- package/dashboards/tricache-grafana.json +548 -0
- package/dist/cache-service-DVvroyqA.d.ts +886 -0
- package/dist/cache-service-KI5ACF3F.js +12 -0
- package/dist/chunk-7RV5ZVBB.js +1023 -0
- package/dist/chunk-BQRURGGD.js +60 -0
- package/dist/chunk-IITRFWHY.js +196 -0
- package/dist/{chunk-X5AXHPVD.js → chunk-LJ5QJB5O.js} +90 -5
- package/dist/{chunk-EUGYR7KR.js → chunk-PL5J3ZNT.js} +3184 -514
- package/dist/chunk-QNU7B43J.js +687 -0
- package/dist/chunk-UGD4VEPD.js +138 -0
- package/dist/chunk-XZL45ABR.js +108 -0
- package/dist/cli.cjs +3122 -495
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +121 -5
- package/dist/dashboard/index.cjs +1065 -0
- package/dist/dashboard/index.d.ts +183 -0
- package/dist/dashboard/index.js +22 -0
- package/dist/drizzle/index.cjs +2846 -494
- package/dist/drizzle/index.d.ts +3 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +1559 -0
- package/dist/edge/index.d.ts +127 -0
- package/dist/edge/index.js +1109 -0
- package/dist/hono/index.cjs +7760 -0
- package/dist/hono/index.d.ts +91 -0
- package/dist/hono/index.js +185 -0
- package/dist/hono-VA9A1m0I.d.ts +371 -0
- package/dist/http/index.cjs +3281 -587
- package/dist/http/index.d.ts +85 -23
- package/dist/http/index.js +176 -78
- package/dist/index.cjs +7212 -2505
- package/dist/index.d.ts +547 -30
- package/dist/index.js +104 -5
- package/dist/murmur3-CMGw9PbV.d.ts +98 -0
- package/dist/nestjs/index.cjs +2787 -487
- package/dist/nestjs/index.d.ts +3 -1
- package/dist/nestjs/index.js +6 -3
- package/dist/next/index.cjs +2804 -501
- package/dist/next/index.d.ts +4 -2
- package/dist/next/index.js +14 -10
- package/dist/prisma/index.d.ts +3 -1
- package/dist/serialize-worker.cjs +5 -4
- package/dist/serialize-worker.js +1 -1
- package/dist/{cache-service-D8tIXmzb.d.ts → types-B_QkVwJ7.d.ts} +622 -662
- package/dist/utils-CEsU_Z0s.d.ts +27 -0
- package/package.json +48 -10
- package/src/adapters/auto-pipeliner.ts +206 -0
- package/src/adapters/node-redis.ts +370 -0
- package/src/cache-service.ts +1619 -415
- package/src/cli.ts +137 -2
- package/src/codec.ts +72 -0
- package/src/cross-region.ts +245 -0
- package/src/dashboard/handler.ts +392 -0
- package/src/dashboard/html.ts +594 -0
- package/src/dashboard/index.ts +195 -0
- package/src/dashboard/types.ts +96 -0
- package/src/disk-tier.ts +449 -47
- package/src/drizzle/index.ts +86 -86
- package/src/edge/adapters/cloudflare-do.ts +130 -0
- package/src/edge/adapters/cloudflare-kv.ts +115 -0
- package/src/edge/adapters/upstash.ts +143 -0
- package/src/edge/cache.ts +797 -0
- package/src/edge/crypto.ts +182 -0
- package/src/edge/hono.ts +284 -0
- package/src/edge/index.ts +37 -0
- package/src/edge/types.ts +230 -0
- package/src/edge/utils/base64.ts +63 -0
- package/src/edge/utils/murmur3.ts +179 -0
- package/src/encryption.ts +127 -5
- package/src/hono/index.ts +316 -0
- package/src/http/express.ts +195 -0
- package/src/http/fastify.ts +260 -0
- package/src/http/index.ts +44 -258
- package/src/http/utils.ts +92 -0
- package/src/index.ts +149 -47
- package/src/ipc-telemetry.ts +509 -0
- package/src/latency-watchdog.ts +282 -0
- package/src/nestjs/decorators.ts +161 -161
- package/src/nestjs/index.ts +23 -23
- package/src/nestjs/tricache.module.ts +104 -104
- package/src/nestjs/tricache.store.ts +97 -97
- package/src/nestjs/types.ts +79 -79
- package/src/next/cache-handler.ts +12 -9
- package/src/next/index.ts +37 -37
- package/src/next/isr-handler.ts +105 -105
- package/src/next/types.ts +130 -130
- package/src/remote-snapshot.ts +153 -0
- package/src/serialize-worker.ts +120 -120
- package/src/sigv4-snapshot-adapter.ts +260 -0
- package/src/smart-memory-cache.ts +91 -8
- package/src/types.ts +315 -2
- package/src/utils/cgroup.ts +100 -0
- package/src/utils/tracing.ts +54 -0
- package/src/wasm/bloom-filter-wasm.ts +109 -102
- package/src/worker-pool.ts +299 -299
- package/src/wtiny-lfu.ts +624 -0
- package/dist/cache-service-5GATUYAK.js +0 -7
- package/dist/chunk-UBSR5UYA.js +0 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import type { IRedisDriver, IRedisPipeline } from '../types';
|
|
3
|
+
|
|
4
|
+
export type { IRedisDriver, IRedisPipeline };
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Pipeline adapter converting `@redis/client` (node-redis v4/v5/v6) chained multi calls
|
|
8
|
+
* into the tuple response format (`Array<[Error | null, any]>`) expected by TriCache.
|
|
9
|
+
*/
|
|
10
|
+
export class NodeRedisPipelineAdapter implements IRedisPipeline {
|
|
11
|
+
private readonly multiInstance: any;
|
|
12
|
+
|
|
13
|
+
constructor(multiInstance: any) {
|
|
14
|
+
this.multiInstance = multiInstance;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get(key: string): this {
|
|
18
|
+
this.multiInstance.get(key);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
set(key: string, value: string, ...args: (string | number)[]): this {
|
|
23
|
+
const opts = parseSetArgs(args);
|
|
24
|
+
if (opts) {
|
|
25
|
+
this.multiInstance.set(key, value, opts);
|
|
26
|
+
} else {
|
|
27
|
+
this.multiInstance.set(key, value);
|
|
28
|
+
}
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setex(key: string, seconds: number, value: string): this {
|
|
33
|
+
if (typeof this.multiInstance.setEx === 'function') {
|
|
34
|
+
this.multiInstance.setEx(key, seconds, value);
|
|
35
|
+
} else if (typeof this.multiInstance.setex === 'function') {
|
|
36
|
+
this.multiInstance.setex(key, seconds, value);
|
|
37
|
+
} else {
|
|
38
|
+
this.multiInstance.set(key, value, { EX: seconds });
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
del(...keys: string[]): this {
|
|
44
|
+
if (keys.length === 0) return this;
|
|
45
|
+
this.multiInstance.del(keys.length === 1 ? keys[0] : keys);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
expire(key: string, seconds: number): this {
|
|
50
|
+
this.multiInstance.expire(key, seconds);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
incr(key: string): this {
|
|
55
|
+
this.multiInstance.incr(key);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
sadd(key: string, ...members: string[]): this {
|
|
60
|
+
if (members.length === 0) return this;
|
|
61
|
+
if (typeof this.multiInstance.sAdd === 'function') {
|
|
62
|
+
this.multiInstance.sAdd(key, members.length === 1 ? members[0] : members);
|
|
63
|
+
} else {
|
|
64
|
+
this.multiInstance.sadd(key, ...members);
|
|
65
|
+
}
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
smembers(key: string): this {
|
|
70
|
+
if (typeof this.multiInstance.sMembers === 'function') {
|
|
71
|
+
this.multiInstance.sMembers(key);
|
|
72
|
+
} else {
|
|
73
|
+
this.multiInstance.smembers(key);
|
|
74
|
+
}
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
hset(key: string, value: Record<string, string>): this {
|
|
79
|
+
if (typeof this.multiInstance.hSet === 'function') {
|
|
80
|
+
this.multiInstance.hSet(key, value);
|
|
81
|
+
} else {
|
|
82
|
+
this.multiInstance.hset(key, value);
|
|
83
|
+
}
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async exec(): Promise<Array<[Error | null, any]> | null> {
|
|
88
|
+
const raw = await this.multiInstance.exec();
|
|
89
|
+
if (!raw) return null;
|
|
90
|
+
return raw.map((item: any) => {
|
|
91
|
+
if (item instanceof Error) {
|
|
92
|
+
return [item, null];
|
|
93
|
+
}
|
|
94
|
+
return [null, item];
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Universal driver adapter wrapping `@redis/client` (node-redis) to satisfy TriCache's
|
|
101
|
+
* `IRedisDriver` contract without adding runtime dependencies.
|
|
102
|
+
*/
|
|
103
|
+
export class NodeRedisAdapter implements IRedisDriver {
|
|
104
|
+
readonly client: any;
|
|
105
|
+
|
|
106
|
+
constructor(client: any) {
|
|
107
|
+
if (!client) {
|
|
108
|
+
throw new Error('NodeRedisAdapter requires an active @redis/client instance');
|
|
109
|
+
}
|
|
110
|
+
this.client = client;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get isOpen(): boolean {
|
|
114
|
+
return this.client.isOpen !== false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async connect(): Promise<unknown> {
|
|
118
|
+
if (typeof this.client.connect === 'function' && !this.client.isOpen) {
|
|
119
|
+
return await this.client.connect();
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async disconnect(): Promise<void> {
|
|
125
|
+
if (typeof this.client.disconnect === 'function' && this.client.isOpen !== false) {
|
|
126
|
+
try {
|
|
127
|
+
await this.client.disconnect();
|
|
128
|
+
} catch {
|
|
129
|
+
/* ok */
|
|
130
|
+
}
|
|
131
|
+
} else if (typeof this.client.quit === 'function') {
|
|
132
|
+
try {
|
|
133
|
+
await this.client.quit();
|
|
134
|
+
} catch {
|
|
135
|
+
/* ok */
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async quit(): Promise<void> {
|
|
141
|
+
return this.disconnect();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async get(key: string): Promise<string | null> {
|
|
145
|
+
return (await this.client.get(key)) ?? null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async set(key: string, value: string, ...args: (string | number)[]): Promise<unknown> {
|
|
149
|
+
const opts = parseSetArgs(args);
|
|
150
|
+
if (opts) {
|
|
151
|
+
return await this.client.set(key, value, opts);
|
|
152
|
+
}
|
|
153
|
+
return await this.client.set(key, value);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async setex(key: string, seconds: number, value: string): Promise<unknown> {
|
|
157
|
+
if (typeof this.client.setEx === 'function') {
|
|
158
|
+
return await this.client.setEx(key, seconds, value);
|
|
159
|
+
}
|
|
160
|
+
if (typeof this.client.setex === 'function') {
|
|
161
|
+
return await this.client.setex(key, seconds, value);
|
|
162
|
+
}
|
|
163
|
+
return await this.client.set(key, value, { EX: seconds });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async del(...keys: string[]): Promise<number> {
|
|
167
|
+
if (keys.length === 0) return 0;
|
|
168
|
+
const res = await this.client.del(keys.length === 1 ? keys[0] : keys);
|
|
169
|
+
return typeof res === 'number' ? res : 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async ping(): Promise<string> {
|
|
173
|
+
return await this.client.ping();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async expire(key: string, seconds: number): Promise<number | boolean> {
|
|
177
|
+
return await this.client.expire(key, seconds);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async incr(key: string): Promise<number> {
|
|
181
|
+
return await this.client.incr(key);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async eval(script: string, numkeys: number, ...args: (string | number)[]): Promise<unknown> {
|
|
185
|
+
if (typeof this.client.sendCommand === 'function') {
|
|
186
|
+
return await this.client.sendCommand([
|
|
187
|
+
'EVAL',
|
|
188
|
+
script,
|
|
189
|
+
String(numkeys),
|
|
190
|
+
...args.map(String),
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
if (typeof this.client.eval === 'function') {
|
|
194
|
+
const keys = args.slice(0, numkeys).map(String);
|
|
195
|
+
const scriptArgs = args.slice(numkeys).map(String);
|
|
196
|
+
return await this.client.eval(script, {
|
|
197
|
+
keys,
|
|
198
|
+
arguments: scriptArgs,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
throw new Error('Redis client does not support EVAL or sendCommand');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
multi(): IRedisPipeline {
|
|
205
|
+
const multiInstance = this.client.multi();
|
|
206
|
+
return new NodeRedisPipelineAdapter(multiInstance);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
pipeline(): IRedisPipeline {
|
|
210
|
+
return this.multi();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async smembers(key: string): Promise<string[]> {
|
|
214
|
+
if (typeof this.client.sMembers === 'function') {
|
|
215
|
+
return await this.client.sMembers(key);
|
|
216
|
+
}
|
|
217
|
+
if (typeof this.client.smembers === 'function') {
|
|
218
|
+
return await this.client.smembers(key);
|
|
219
|
+
}
|
|
220
|
+
return [];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async sadd(key: string, ...members: string[]): Promise<number> {
|
|
224
|
+
if (members.length === 0) return 0;
|
|
225
|
+
if (typeof this.client.sAdd === 'function') {
|
|
226
|
+
return await this.client.sAdd(key, members.length === 1 ? members[0] : members);
|
|
227
|
+
}
|
|
228
|
+
if (typeof this.client.sadd === 'function') {
|
|
229
|
+
return await this.client.sadd(key, ...members);
|
|
230
|
+
}
|
|
231
|
+
return 0;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async mget(keys: string[]): Promise<(string | null)[]> {
|
|
235
|
+
if (keys.length === 0) return [];
|
|
236
|
+
if (typeof this.client.mGet === 'function') {
|
|
237
|
+
return await this.client.mGet(keys);
|
|
238
|
+
}
|
|
239
|
+
if (typeof this.client.mget === 'function') {
|
|
240
|
+
return await this.client.mget(keys);
|
|
241
|
+
}
|
|
242
|
+
return [];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async hset(key: string, value: Record<string, string>): Promise<unknown> {
|
|
246
|
+
if (typeof this.client.hSet === 'function') {
|
|
247
|
+
return await this.client.hSet(key, value);
|
|
248
|
+
}
|
|
249
|
+
if (typeof this.client.hset === 'function') {
|
|
250
|
+
return await this.client.hset(key, value);
|
|
251
|
+
}
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async publish(channel: string, message: string): Promise<number> {
|
|
256
|
+
return await this.client.publish(channel, message);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async subscribe(...channels: string[]): Promise<unknown> {
|
|
260
|
+
if (channels.length === 0) return undefined;
|
|
261
|
+
return await this.client.subscribe(channels);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
scanStream(options?: { match?: string; count?: number }): EventEmitter {
|
|
265
|
+
const ee = new EventEmitter();
|
|
266
|
+
const pattern = options?.match ?? '*';
|
|
267
|
+
const count = options?.count ?? 100;
|
|
268
|
+
|
|
269
|
+
queueMicrotask(async () => {
|
|
270
|
+
try {
|
|
271
|
+
if (typeof this.client.scanIterator === 'function') {
|
|
272
|
+
const batch: string[] = [];
|
|
273
|
+
for await (const key of this.client.scanIterator({ MATCH: pattern, COUNT: count })) {
|
|
274
|
+
batch.push(key);
|
|
275
|
+
if (batch.length >= count) {
|
|
276
|
+
ee.emit('data', [...batch]);
|
|
277
|
+
batch.length = 0;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (batch.length > 0) {
|
|
281
|
+
ee.emit('data', batch);
|
|
282
|
+
}
|
|
283
|
+
ee.emit('end');
|
|
284
|
+
} else if (typeof this.client.sendCommand === 'function') {
|
|
285
|
+
let cursor = '0';
|
|
286
|
+
do {
|
|
287
|
+
const raw = await this.client.sendCommand([
|
|
288
|
+
'SCAN',
|
|
289
|
+
cursor,
|
|
290
|
+
'MATCH',
|
|
291
|
+
pattern,
|
|
292
|
+
'COUNT',
|
|
293
|
+
String(count),
|
|
294
|
+
]);
|
|
295
|
+
cursor = String(raw[0]);
|
|
296
|
+
const batch = raw[1] as string[];
|
|
297
|
+
if (batch?.length) {
|
|
298
|
+
ee.emit('data', batch);
|
|
299
|
+
}
|
|
300
|
+
} while (cursor !== '0');
|
|
301
|
+
ee.emit('end');
|
|
302
|
+
} else {
|
|
303
|
+
ee.emit('end');
|
|
304
|
+
}
|
|
305
|
+
} catch (err) {
|
|
306
|
+
ee.emit('error', err);
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
return ee;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
duplicate(): IRedisDriver {
|
|
314
|
+
if (typeof this.client.duplicate === 'function') {
|
|
315
|
+
return new NodeRedisAdapter(this.client.duplicate());
|
|
316
|
+
}
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
on(event: string, listener: (...args: any[]) => void): this {
|
|
321
|
+
if (typeof this.client.on === 'function') {
|
|
322
|
+
this.client.on(event, listener);
|
|
323
|
+
}
|
|
324
|
+
return this;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
once(event: string, listener: (...args: any[]) => void): this {
|
|
328
|
+
if (typeof this.client.once === 'function') {
|
|
329
|
+
this.client.once(event, listener);
|
|
330
|
+
}
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Creates a pluggable `IRedisDriver` adapter wrapping an existing `@redis/client` instance.
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* import { createClient } from 'redis';
|
|
340
|
+
* import { CacheService, createNodeRedisAdapter } from 'tricache';
|
|
341
|
+
*
|
|
342
|
+
* const client = createClient({ url: 'redis://localhost:6379' });
|
|
343
|
+
* await client.connect();
|
|
344
|
+
*
|
|
345
|
+
* const cache = CacheService.create({
|
|
346
|
+
* redisClient: createNodeRedisAdapter(client),
|
|
347
|
+
* });
|
|
348
|
+
*/
|
|
349
|
+
export function createNodeRedisAdapter(client: any): IRedisDriver {
|
|
350
|
+
if (client instanceof NodeRedisAdapter) return client;
|
|
351
|
+
return new NodeRedisAdapter(client);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function parseSetArgs(args: (string | number)[]): Record<string, any> | undefined {
|
|
355
|
+
if (args.length === 0) return undefined;
|
|
356
|
+
const opts: Record<string, any> = {};
|
|
357
|
+
for (let i = 0; i < args.length; i++) {
|
|
358
|
+
const a = String(args[i]).toUpperCase();
|
|
359
|
+
if (a === 'EX' && i + 1 < args.length) {
|
|
360
|
+
opts.EX = Number(args[++i]);
|
|
361
|
+
} else if (a === 'PX' && i + 1 < args.length) {
|
|
362
|
+
opts.PX = Number(args[++i]);
|
|
363
|
+
} else if (a === 'NX') {
|
|
364
|
+
opts.NX = true;
|
|
365
|
+
} else if (a === 'XX') {
|
|
366
|
+
opts.XX = true;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return Object.keys(opts).length > 0 ? opts : undefined;
|
|
370
|
+
}
|