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.
Files changed (97) hide show
  1. package/README.md +36 -28
  2. package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
  3. package/dist/cache-service-CWI4jkS7.d.cts +963 -0
  4. package/dist/{cache-service-26GCO3UU.js → cache-service-GYPZFB7O.js} +2 -2
  5. package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
  6. package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
  7. package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
  8. package/dist/chunk-OYDU3SMI.js +89 -0
  9. package/dist/{chunk-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
  10. package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
  11. package/dist/cli.cjs +1757 -777
  12. package/dist/cli.d.cts +12 -0
  13. package/dist/cli.js +27 -6
  14. package/dist/dashboard/index.cjs +111 -29
  15. package/dist/dashboard/index.d.cts +185 -0
  16. package/dist/dashboard/index.d.ts +5 -3
  17. package/dist/dashboard/index.js +1 -1
  18. package/dist/drizzle/index.cjs +1757 -773
  19. package/dist/drizzle/index.d.cts +46 -0
  20. package/dist/drizzle/index.d.ts +2 -2
  21. package/dist/drizzle/index.js +1 -1
  22. package/dist/edge/index.cjs +280 -153
  23. package/dist/edge/index.d.cts +127 -0
  24. package/dist/edge/index.d.ts +3 -3
  25. package/dist/edge/index.js +254 -147
  26. package/dist/hono/index.cjs +1805 -793
  27. package/dist/hono/index.d.cts +93 -0
  28. package/dist/hono/index.d.ts +7 -5
  29. package/dist/hono/index.js +16 -9
  30. package/dist/{hono-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
  31. package/dist/hono-C886ok3X.d.cts +391 -0
  32. package/dist/http/index.cjs +1946 -842
  33. package/dist/http/index.d.cts +103 -0
  34. package/dist/http/index.d.ts +13 -5
  35. package/dist/http/index.js +137 -57
  36. package/dist/index.cjs +2073 -937
  37. package/dist/index.d.cts +1006 -0
  38. package/dist/index.d.ts +40 -8
  39. package/dist/index.js +8 -8
  40. package/dist/murmur3-CMGw9PbV.d.cts +98 -0
  41. package/dist/nestjs/index.cjs +1849 -806
  42. package/dist/nestjs/index.d.cts +285 -0
  43. package/dist/nestjs/index.d.ts +103 -30
  44. package/dist/nestjs/index.js +115 -32
  45. package/dist/next/index.cjs +1913 -887
  46. package/dist/next/index.d.cts +232 -0
  47. package/dist/next/index.d.ts +33 -11
  48. package/dist/next/index.js +174 -107
  49. package/dist/prisma/index.cjs +11 -1
  50. package/dist/prisma/index.d.cts +62 -0
  51. package/dist/prisma/index.d.ts +2 -2
  52. package/dist/prisma/index.js +11 -1
  53. package/dist/serialize-worker.d.cts +2 -0
  54. package/dist/{types-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -9
  55. package/dist/types-BA4pz1A0.d.ts +1519 -0
  56. package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
  57. package/dist/utils-Bvzn-rKS.d.ts +29 -0
  58. package/package.json +96 -36
  59. package/src/adapters/auto-pipeliner.ts +107 -24
  60. package/src/adapters/node-redis.ts +123 -25
  61. package/src/cache-service.ts +1238 -659
  62. package/src/cli.ts +24 -6
  63. package/src/cross-region.ts +21 -3
  64. package/src/dashboard/handler.ts +26 -6
  65. package/src/dashboard/html.ts +35 -10
  66. package/src/dashboard/index.ts +35 -13
  67. package/src/dashboard/types.ts +3 -1
  68. package/src/disk-tier.ts +171 -65
  69. package/src/edge/cache.ts +202 -129
  70. package/src/edge/hono.ts +23 -6
  71. package/src/edge/types.ts +2 -0
  72. package/src/hono/index.ts +19 -9
  73. package/src/http/express.ts +110 -26
  74. package/src/http/fastify.ts +38 -28
  75. package/src/http/utils.ts +54 -20
  76. package/src/ipc-telemetry.ts +35 -9
  77. package/src/nestjs/decorators.ts +1 -1
  78. package/src/nestjs/index.ts +2 -0
  79. package/src/nestjs/keyv-adapter.ts +68 -0
  80. package/src/nestjs/tricache.module.ts +13 -11
  81. package/src/nestjs/tricache.store.ts +51 -20
  82. package/src/nestjs/types.ts +29 -15
  83. package/src/next/cache-handler.ts +132 -57
  84. package/src/next/index.ts +1 -0
  85. package/src/next/isr-handler.ts +1 -4
  86. package/src/next/types.ts +21 -0
  87. package/src/prisma/index.ts +12 -1
  88. package/src/smart-memory-cache.ts +65 -27
  89. package/src/types.ts +27 -2
  90. package/src/utils/cgroup.ts +12 -0
  91. package/src/utils/deadline.ts +14 -0
  92. package/src/utils/diagnostics.ts +27 -0
  93. package/src/utils/redis-slot.ts +13 -0
  94. package/src/wasm/bloom-filter-wasm.ts +9 -4
  95. package/src/worker-pool.ts +17 -8
  96. package/src/wtiny-lfu.ts +20 -6
  97. package/dist/chunk-BQRURGGD.js +0 -60
@@ -9,83 +9,127 @@ export type { IRedisDriver, IRedisPipeline };
9
9
  */
10
10
  export class NodeRedisPipelineAdapter implements IRedisPipeline {
11
11
  private readonly multiInstance: any;
12
+ private readonly operations: Array<() => Promise<unknown>> = [];
13
+ pttl?: (key: string) => this;
14
+ eval?: (script: string, numkeys: number, ...args: (string | number)[]) => this;
12
15
 
13
- constructor(multiInstance: any) {
16
+ constructor(multiInstance: any, private readonly transactional = true) {
14
17
  this.multiInstance = multiInstance;
18
+ if (typeof multiInstance.pTTL === 'function' || typeof multiInstance.pttl === 'function') {
19
+ this.pttl = key => { this.queueCommand(typeof multiInstance.pTTL === 'function' ? 'pTTL' : 'pttl', [key]); return this; };
20
+ }
21
+ if (typeof multiInstance.eval === 'function' || typeof multiInstance.addCommand === 'function') {
22
+ this.eval = (script, numkeys, ...args) => {
23
+ if (typeof multiInstance.eval === 'function') {
24
+ this.queueCommand('eval', [script, { keys: args.slice(0, numkeys).map(String), arguments: args.slice(numkeys).map(String) }]);
25
+ } else this.queueCommand('addCommand', [['EVAL', script, String(numkeys), ...args.map(String)]]);
26
+ return this;
27
+ };
28
+ }
29
+ }
30
+
31
+ private queueCommand(method: string, args: unknown[]): void {
32
+ if (this.transactional) this.multiInstance[method](...args);
33
+ else this.operations.push(() => this.multiInstance[method](...args));
15
34
  }
16
35
 
17
36
  get(key: string): this {
18
- this.multiInstance.get(key);
37
+ this.queueCommand('get', [key]);
19
38
  return this;
20
39
  }
21
40
 
22
41
  set(key: string, value: string, ...args: (string | number)[]): this {
23
42
  const opts = parseSetArgs(args);
24
43
  if (opts) {
25
- this.multiInstance.set(key, value, opts);
44
+ this.queueCommand('set', [key, value, opts]);
26
45
  } else {
27
- this.multiInstance.set(key, value);
46
+ this.queueCommand('set', [key, value]);
28
47
  }
29
48
  return this;
30
49
  }
31
50
 
32
51
  setex(key: string, seconds: number, value: string): this {
33
52
  if (typeof this.multiInstance.setEx === 'function') {
34
- this.multiInstance.setEx(key, seconds, value);
53
+ this.queueCommand('setEx', [key, seconds, value]);
35
54
  } else if (typeof this.multiInstance.setex === 'function') {
36
- this.multiInstance.setex(key, seconds, value);
55
+ this.queueCommand('setex', [key, seconds, value]);
37
56
  } else {
38
- this.multiInstance.set(key, value, { EX: seconds });
57
+ this.queueCommand('set', [key, value, { EX: seconds }]);
39
58
  }
40
59
  return this;
41
60
  }
42
61
 
43
62
  del(...keys: string[]): this {
44
63
  if (keys.length === 0) return this;
45
- this.multiInstance.del(keys.length === 1 ? keys[0] : keys);
64
+ this.queueCommand('del', [keys.length === 1 ? keys[0] : keys]);
46
65
  return this;
47
66
  }
48
67
 
49
68
  expire(key: string, seconds: number): this {
50
- this.multiInstance.expire(key, seconds);
69
+ this.queueCommand('expire', [key, seconds]);
51
70
  return this;
52
71
  }
53
72
 
54
73
  incr(key: string): this {
55
- this.multiInstance.incr(key);
74
+ this.queueCommand('incr', [key]);
56
75
  return this;
57
76
  }
58
77
 
59
78
  sadd(key: string, ...members: string[]): this {
60
79
  if (members.length === 0) return this;
61
80
  if (typeof this.multiInstance.sAdd === 'function') {
62
- this.multiInstance.sAdd(key, members.length === 1 ? members[0] : members);
81
+ this.queueCommand('sAdd', [key, members.length === 1 ? members[0] : members]);
63
82
  } else {
64
- this.multiInstance.sadd(key, ...members);
83
+ this.queueCommand('sadd', [key, ...members]);
65
84
  }
66
85
  return this;
67
86
  }
68
87
 
69
88
  smembers(key: string): this {
70
89
  if (typeof this.multiInstance.sMembers === 'function') {
71
- this.multiInstance.sMembers(key);
90
+ this.queueCommand('sMembers', [key]);
72
91
  } else {
73
- this.multiInstance.smembers(key);
92
+ this.queueCommand('smembers', [key]);
74
93
  }
75
94
  return this;
76
95
  }
77
96
 
78
97
  hset(key: string, value: Record<string, string>): this {
79
98
  if (typeof this.multiInstance.hSet === 'function') {
80
- this.multiInstance.hSet(key, value);
99
+ this.queueCommand('hSet', [key, value]);
81
100
  } else {
82
- this.multiInstance.hset(key, value);
101
+ this.queueCommand('hset', [key, value]);
102
+ }
103
+ return this;
104
+ }
105
+
106
+ hgetall(key: string): this {
107
+ if (typeof this.multiInstance.hGetAll === 'function') {
108
+ this.queueCommand('hGetAll', [key]);
109
+ } else {
110
+ this.queueCommand('hgetall', [key]);
83
111
  }
84
112
  return this;
85
113
  }
86
114
 
87
115
  async exec(): Promise<Array<[Error | null, any]> | null> {
88
- const raw = await this.multiInstance.exec();
116
+ let raw: any[] | null;
117
+ if (!this.transactional) {
118
+ // node-redis auto-pipelines same-tick commands. Observe every command
119
+ // separately: execAsPipeline() can reject at the first error and discard
120
+ // the successful replies, and retrying that pipeline could duplicate writes.
121
+ const results = await Promise.allSettled(this.operations.splice(0).map(operation => Promise.resolve().then(operation)));
122
+ return results.map(result => result.status === 'fulfilled' ? [null, result.value]
123
+ : [result.reason instanceof Error ? result.reason : new Error(String(result.reason)), null]);
124
+ }
125
+ try {
126
+ raw = await this.multiInstance.exec();
127
+ } catch (error) {
128
+ // node-redis rejects EXEC with MultiErrorReply, unlike ioredis tuples.
129
+ const replies = (error as { replies?: unknown[] }).replies;
130
+ if (!Array.isArray(replies)) throw error;
131
+ raw = replies;
132
+ }
89
133
  if (!raw) return null;
90
134
  return raw.map((item: any) => {
91
135
  if (item instanceof Error) {
@@ -100,14 +144,20 @@ export class NodeRedisPipelineAdapter implements IRedisPipeline {
100
144
  * Universal driver adapter wrapping `@redis/client` (node-redis) to satisfy TriCache's
101
145
  * `IRedisDriver` contract without adding runtime dependencies.
102
146
  */
103
- export class NodeRedisAdapter implements IRedisDriver {
147
+ export class NodeRedisAdapter extends EventEmitter implements IRedisDriver {
104
148
  readonly client: any;
149
+ private connecting: Promise<unknown> | null = null;
150
+ pttl?: (key: string) => Promise<number>;
105
151
 
106
152
  constructor(client: any) {
153
+ super();
107
154
  if (!client) {
108
155
  throw new Error('NodeRedisAdapter requires an active @redis/client instance');
109
156
  }
110
157
  this.client = client;
158
+ if (typeof client.pTTL === 'function' || typeof client.pttl === 'function') {
159
+ this.pttl = key => (client.pTTL ?? client.pttl).call(client, key);
160
+ }
111
161
  }
112
162
 
113
163
  get isOpen(): boolean {
@@ -115,8 +165,11 @@ export class NodeRedisAdapter implements IRedisDriver {
115
165
  }
116
166
 
117
167
  async connect(): Promise<unknown> {
168
+ if (this.connecting) return this.connecting;
118
169
  if (typeof this.client.connect === 'function' && !this.client.isOpen) {
119
- return await this.client.connect();
170
+ this.connecting = Promise.resolve(this.client.connect());
171
+ try { return await this.connecting; }
172
+ finally { this.connecting = null; }
120
173
  }
121
174
  return undefined;
122
175
  }
@@ -207,7 +260,7 @@ export class NodeRedisAdapter implements IRedisDriver {
207
260
  }
208
261
 
209
262
  pipeline(): IRedisPipeline {
210
- return this.multi();
263
+ return new NodeRedisPipelineAdapter(this.client, false);
211
264
  }
212
265
 
213
266
  async smembers(key: string): Promise<string[]> {
@@ -252,13 +305,50 @@ export class NodeRedisAdapter implements IRedisDriver {
252
305
  return undefined;
253
306
  }
254
307
 
308
+ async hgetall(key: string): Promise<Record<string, string>> {
309
+ if (typeof this.client.hGetAll === 'function') {
310
+ return (await this.client.hGetAll(key)) ?? {};
311
+ }
312
+ if (typeof this.client.hgetall === 'function') {
313
+ return (await this.client.hgetall(key)) ?? {};
314
+ }
315
+ return {};
316
+ }
317
+
255
318
  async publish(channel: string, message: string): Promise<number> {
256
319
  return await this.client.publish(channel, message);
257
320
  }
258
321
 
259
322
  async subscribe(...channels: string[]): Promise<unknown> {
260
323
  if (channels.length === 0) return undefined;
261
- return await this.client.subscribe(channels);
324
+ await this.connect();
325
+ return await this.client.subscribe(channels, (message: string, channel: string) => this.emit('message', channel, message));
326
+ }
327
+
328
+ async unsubscribe(...channels: string[]): Promise<unknown> {
329
+ if (!channels.length || !this.isOpen) return undefined;
330
+ return this.client.unsubscribe(channels);
331
+ }
332
+
333
+ async xread(...args: (string | number)[]): Promise<Array<[string, Array<[string, string[]]>]> | null> {
334
+ await this.connect();
335
+ const raw = await this.client.sendCommand(['XREAD', ...args.map(String)]);
336
+ if (raw == null) return null;
337
+ if (raw instanceof Map) return [...raw.entries()];
338
+ if (!Array.isArray(raw) && typeof raw === 'object') return Object.entries(raw);
339
+ return raw;
340
+ }
341
+
342
+ async xadd(...args: (string | number)[]): Promise<unknown> {
343
+ return this.client.sendCommand(['XADD', ...args.map(String)]);
344
+ }
345
+
346
+ async xrange(...args: (string | number)[]): Promise<Array<[string, string[]]>> {
347
+ return this.client.sendCommand(['XRANGE', ...args.map(String)]);
348
+ }
349
+
350
+ async xrevrange(...args: (string | number)[]): Promise<Array<[string, string[]]>> {
351
+ return this.client.sendCommand(['XREVRANGE', ...args.map(String)]);
262
352
  }
263
353
 
264
354
  scanStream(options?: { match?: string; count?: number }): EventEmitter {
@@ -270,8 +360,8 @@ export class NodeRedisAdapter implements IRedisDriver {
270
360
  try {
271
361
  if (typeof this.client.scanIterator === 'function') {
272
362
  const batch: string[] = [];
273
- for await (const key of this.client.scanIterator({ MATCH: pattern, COUNT: count })) {
274
- batch.push(key);
363
+ for await (const keys of this.client.scanIterator({ MATCH: pattern, COUNT: count })) {
364
+ batch.push(...(Array.isArray(keys) ? keys : [keys]));
275
365
  if (batch.length >= count) {
276
366
  ee.emit('data', [...batch]);
277
367
  batch.length = 0;
@@ -318,18 +408,26 @@ export class NodeRedisAdapter implements IRedisDriver {
318
408
  }
319
409
 
320
410
  on(event: string, listener: (...args: any[]) => void): this {
411
+ if (event === 'message' || event === 'smessage') return super.on(event, listener);
321
412
  if (typeof this.client.on === 'function') {
322
- this.client.on(event, listener);
413
+ this.client.on(event === 'close' ? 'end' : event, listener);
323
414
  }
324
415
  return this;
325
416
  }
326
417
 
327
418
  once(event: string, listener: (...args: any[]) => void): this {
419
+ if (event === 'message' || event === 'smessage') return super.once(event, listener);
328
420
  if (typeof this.client.once === 'function') {
329
- this.client.once(event, listener);
421
+ this.client.once(event === 'close' ? 'end' : event, listener);
330
422
  }
331
423
  return this;
332
424
  }
425
+
426
+ removeListener(event: string, listener: (...args: any[]) => void): this {
427
+ super.removeListener(event, listener);
428
+ this.client.removeListener?.(event === 'close' ? 'end' : event, listener);
429
+ return this;
430
+ }
333
431
  }
334
432
 
335
433
  /**