tricache 1.0.1 → 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-CREOp7MM.d.ts → cache-service-B108FzER.d.ts} +36 -4
- package/dist/cache-service-CWI4jkS7.d.cts +963 -0
- package/dist/{cache-service-MG7PN6MX.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-3I5L7K6I.js → chunk-VNRDL5RA.js} +102 -140
- package/dist/{chunk-JYQFQJNJ.js → chunk-WYF7NOAT.js} +1443 -609
- package/dist/cli.cjs +1367 -625
- 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 +1370 -624
- 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 +227 -123
- package/dist/edge/index.d.cts +127 -0
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +201 -117
- package/dist/hono/index.cjs +1418 -644
- 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-DSV2ve66.d.ts → hono-3rDMYVtY.d.ts} +16 -3
- package/dist/hono-C886ok3X.d.cts +391 -0
- package/dist/http/index.cjs +1514 -679
- package/dist/http/index.d.cts +103 -0
- package/dist/http/index.d.ts +13 -5
- package/dist/http/index.js +92 -43
- package/dist/index.cjs +1671 -790
- package/dist/index.d.cts +1006 -0
- package/dist/index.d.ts +39 -8
- package/dist/index.js +8 -8
- package/dist/murmur3-CMGw9PbV.d.cts +98 -0
- package/dist/nestjs/index.cjs +1454 -649
- 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 +1512 -718
- package/dist/next/index.d.cts +232 -0
- package/dist/next/index.d.ts +33 -11
- package/dist/next/index.js +172 -99
- 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-Dyip5wAl.d.ts → types-BA4pz1A0.d.cts} +24 -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 +106 -27
- package/src/cache-service.ts +802 -502
- 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 +145 -97
- 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 +62 -17
- package/src/http/fastify.ts +35 -21
- 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 +131 -53
- package/src/next/index.ts +1 -0
- package/src/next/types.ts +21 -0
- package/src/prisma/index.ts +12 -1
- package/src/smart-memory-cache.ts +63 -26
- package/src/types.ts +25 -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
|
@@ -3,6 +3,8 @@ interface KeyDerivationOptions {
|
|
|
3
3
|
keyGenerator?: (req: any) => string;
|
|
4
4
|
/** Optional list of header names (case-insensitive) to include in the cache key. */
|
|
5
5
|
headerWhitelist?: string[];
|
|
6
|
+
/** Opt in to authenticated request caching with an identity-aware key. Default: false. */
|
|
7
|
+
cacheAuthenticated?: boolean;
|
|
6
8
|
}
|
|
7
9
|
/**
|
|
8
10
|
* Generates a deterministic HTTP cache key from request method, URL path, sorted query parameters,
|
|
@@ -18,7 +20,7 @@ declare function buildDeterministicKey(req: any, options?: KeyDerivationOptions)
|
|
|
18
20
|
* Checks if request specifies Cache-Control bypass directives (no-cache, no-store)
|
|
19
21
|
* or matches a custom skipCache predicate.
|
|
20
22
|
*/
|
|
21
|
-
declare function shouldSkipCache(req: any, customSkip?: (req: any) => boolean): boolean;
|
|
23
|
+
declare function shouldSkipCache(req: any, customSkip?: (req: any) => boolean, options?: KeyDerivationOptions): boolean;
|
|
22
24
|
/**
|
|
23
25
|
* Compute a weak ETag (W/"...") from string, Buffer, or JSON-serializable object.
|
|
24
26
|
*/
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
interface KeyDerivationOptions {
|
|
2
|
+
/** Custom key derivation function. Overrides default URL and query string serialization. */
|
|
3
|
+
keyGenerator?: (req: any) => string;
|
|
4
|
+
/** Optional list of header names (case-insensitive) to include in the cache key. */
|
|
5
|
+
headerWhitelist?: string[];
|
|
6
|
+
/** Opt in to authenticated request caching with an identity-aware key. Default: false. */
|
|
7
|
+
cacheAuthenticated?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generates a deterministic HTTP cache key from request method, URL path, sorted query parameters,
|
|
11
|
+
* and optional whitelisted headers.
|
|
12
|
+
*
|
|
13
|
+
* Example:
|
|
14
|
+
* GET /api/products?sort=desc&page=2
|
|
15
|
+
* GET /api/products?page=2&sort=desc
|
|
16
|
+
* Both map to: http:GET:/api/products?page=2&sort=desc
|
|
17
|
+
*/
|
|
18
|
+
declare function buildDeterministicKey(req: any, options?: KeyDerivationOptions): string;
|
|
19
|
+
/**
|
|
20
|
+
* Checks if request specifies Cache-Control bypass directives (no-cache, no-store)
|
|
21
|
+
* or matches a custom skipCache predicate.
|
|
22
|
+
*/
|
|
23
|
+
declare function shouldSkipCache(req: any, customSkip?: (req: any) => boolean, options?: KeyDerivationOptions): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Compute a weak ETag (W/"...") from string, Buffer, or JSON-serializable object.
|
|
26
|
+
*/
|
|
27
|
+
declare function generateETag(data: unknown): string;
|
|
28
|
+
|
|
29
|
+
export { type KeyDerivationOptions as K, buildDeterministicKey as b, generateETag as g, shouldSkipCache as s };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tricache",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Three-tier Node.js cache: L1 smart RAM (adaptive LFU/LRU + Count-Min Sketch) → L1.5 NVMe disk spill → L2 Redis/Valkey. Includes AES-256-GCM at-rest encryption, WASM Bloom filter, Stale-While-Revalidate, and thundering-herd prevention.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
@@ -40,51 +40,102 @@
|
|
|
40
40
|
},
|
|
41
41
|
"exports": {
|
|
42
42
|
".": {
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/index.d.ts",
|
|
45
|
+
"default": "./dist/index.js"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/index.d.cts",
|
|
49
|
+
"default": "./dist/index.cjs"
|
|
50
|
+
}
|
|
46
51
|
},
|
|
47
52
|
"./next": {
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
|
|
53
|
+
"import": {
|
|
54
|
+
"types": "./dist/next/index.d.ts",
|
|
55
|
+
"default": "./dist/next/index.js"
|
|
56
|
+
},
|
|
57
|
+
"require": {
|
|
58
|
+
"types": "./dist/next/index.d.cts",
|
|
59
|
+
"default": "./dist/next/index.cjs"
|
|
60
|
+
}
|
|
51
61
|
},
|
|
52
62
|
"./nestjs": {
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
63
|
+
"import": {
|
|
64
|
+
"types": "./dist/nestjs/index.d.ts",
|
|
65
|
+
"default": "./dist/nestjs/index.js"
|
|
66
|
+
},
|
|
67
|
+
"require": {
|
|
68
|
+
"types": "./dist/nestjs/index.d.cts",
|
|
69
|
+
"default": "./dist/nestjs/index.cjs"
|
|
70
|
+
}
|
|
56
71
|
},
|
|
57
72
|
"./prisma": {
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
73
|
+
"import": {
|
|
74
|
+
"types": "./dist/prisma/index.d.ts",
|
|
75
|
+
"default": "./dist/prisma/index.js"
|
|
76
|
+
},
|
|
77
|
+
"require": {
|
|
78
|
+
"types": "./dist/prisma/index.d.cts",
|
|
79
|
+
"default": "./dist/prisma/index.cjs"
|
|
80
|
+
}
|
|
61
81
|
},
|
|
62
82
|
"./drizzle": {
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
83
|
+
"import": {
|
|
84
|
+
"types": "./dist/drizzle/index.d.ts",
|
|
85
|
+
"default": "./dist/drizzle/index.js"
|
|
86
|
+
},
|
|
87
|
+
"require": {
|
|
88
|
+
"types": "./dist/drizzle/index.d.cts",
|
|
89
|
+
"default": "./dist/drizzle/index.cjs"
|
|
90
|
+
}
|
|
66
91
|
},
|
|
67
92
|
"./http": {
|
|
68
|
-
"
|
|
69
|
-
|
|
70
|
-
|
|
93
|
+
"import": {
|
|
94
|
+
"types": "./dist/http/index.d.ts",
|
|
95
|
+
"default": "./dist/http/index.js"
|
|
96
|
+
},
|
|
97
|
+
"require": {
|
|
98
|
+
"types": "./dist/http/index.d.cts",
|
|
99
|
+
"default": "./dist/http/index.cjs"
|
|
100
|
+
}
|
|
71
101
|
},
|
|
72
102
|
"./hono": {
|
|
73
|
-
"
|
|
74
|
-
|
|
75
|
-
|
|
103
|
+
"import": {
|
|
104
|
+
"types": "./dist/hono/index.d.ts",
|
|
105
|
+
"default": "./dist/hono/index.js"
|
|
106
|
+
},
|
|
107
|
+
"require": {
|
|
108
|
+
"types": "./dist/hono/index.d.cts",
|
|
109
|
+
"default": "./dist/hono/index.cjs"
|
|
110
|
+
}
|
|
76
111
|
},
|
|
77
112
|
"./dashboard": {
|
|
78
|
-
"
|
|
79
|
-
|
|
80
|
-
|
|
113
|
+
"import": {
|
|
114
|
+
"types": "./dist/dashboard/index.d.ts",
|
|
115
|
+
"default": "./dist/dashboard/index.js"
|
|
116
|
+
},
|
|
117
|
+
"require": {
|
|
118
|
+
"types": "./dist/dashboard/index.d.cts",
|
|
119
|
+
"default": "./dist/dashboard/index.cjs"
|
|
120
|
+
}
|
|
81
121
|
},
|
|
82
122
|
"./edge": {
|
|
83
|
-
"
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
"
|
|
123
|
+
"edge-light": {
|
|
124
|
+
"types": "./dist/edge/index.d.ts",
|
|
125
|
+
"default": "./dist/edge/index.js"
|
|
126
|
+
},
|
|
127
|
+
"workerd": {
|
|
128
|
+
"types": "./dist/edge/index.d.ts",
|
|
129
|
+
"default": "./dist/edge/index.js"
|
|
130
|
+
},
|
|
131
|
+
"import": {
|
|
132
|
+
"types": "./dist/edge/index.d.ts",
|
|
133
|
+
"default": "./dist/edge/index.js"
|
|
134
|
+
},
|
|
135
|
+
"require": {
|
|
136
|
+
"types": "./dist/edge/index.d.cts",
|
|
137
|
+
"default": "./dist/edge/index.cjs"
|
|
138
|
+
},
|
|
88
139
|
"default": "./dist/edge/index.js"
|
|
89
140
|
}
|
|
90
141
|
},
|
|
@@ -97,8 +148,8 @@
|
|
|
97
148
|
"LICENSE"
|
|
98
149
|
],
|
|
99
150
|
"peerDependencies": {
|
|
100
|
-
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
101
|
-
"@nestjs/core": "^10.0.0 || ^11.0.0",
|
|
151
|
+
"@nestjs/common": "^10.0.0 || ^11.0.0 || ^12.0.0",
|
|
152
|
+
"@nestjs/core": "^10.0.0 || ^11.0.0 || ^12.0.0",
|
|
102
153
|
"hono": ">=3.0.0 <5.0.0"
|
|
103
154
|
},
|
|
104
155
|
"peerDependenciesMeta": {
|
|
@@ -119,13 +170,21 @@
|
|
|
119
170
|
"zstd-codec": "^0.1.5"
|
|
120
171
|
},
|
|
121
172
|
"devDependencies": {
|
|
122
|
-
"@nestjs/
|
|
123
|
-
"@nestjs/
|
|
173
|
+
"@nestjs/cache-manager": "12.0.5",
|
|
174
|
+
"@nestjs/common": "^12.5.0",
|
|
175
|
+
"@nestjs/core": "^12.5.0",
|
|
176
|
+
"@nestjs/platform-express": "^12.5.0",
|
|
124
177
|
"@types/node": "^26.6.2",
|
|
125
|
-
"
|
|
178
|
+
"@opentelemetry/api": "^1.10.0",
|
|
179
|
+
"cache-manager": "7.2.9",
|
|
180
|
+
"hono": "^4.13.16",
|
|
181
|
+
"keyv": "5.6.0",
|
|
126
182
|
"markdown-it-mathjax3": "^4.3.2",
|
|
183
|
+
"next": "16.4.6",
|
|
127
184
|
"oxlint": "^1.85.0",
|
|
128
185
|
"redis": "^6.2.1",
|
|
186
|
+
"reflect-metadata": "^0.2.2",
|
|
187
|
+
"rxjs": "^7.8.2",
|
|
129
188
|
"tsup": "^8.5.1",
|
|
130
189
|
"tsx": "^4.23.15",
|
|
131
190
|
"typescript": "^6.0.3",
|
|
@@ -138,7 +197,6 @@
|
|
|
138
197
|
},
|
|
139
198
|
"scripts": {
|
|
140
199
|
"build": "tsup src/index.ts src/cli.ts src/serialize-worker.ts src/next/index.ts src/nestjs/index.ts src/prisma/index.ts src/drizzle/index.ts src/http/index.ts src/hono/index.ts src/dashboard/index.ts src/edge/index.ts --format esm,cjs --dts --clean",
|
|
141
|
-
"postbuild": "node --input-type=module -e \"import{readdirSync,rmSync,statSync}from'fs';import{join}from'path';function clean(d){for(const f of readdirSync(d)){const p=join(d,f);if(statSync(p).isDirectory())clean(p);else if(p.endsWith('.d.cts'))rmSync(p,{force:true});}}clean('dist');\"",
|
|
142
200
|
"dev": "tsup src/index.ts src/serialize-worker.ts --format esm,cjs --dts --watch",
|
|
143
201
|
"typecheck": "tsc --noEmit",
|
|
144
202
|
"typecheck:tests": "tsc --noEmit -p tsconfig.test.json",
|
|
@@ -147,6 +205,8 @@
|
|
|
147
205
|
"test": "vitest run",
|
|
148
206
|
"test:integration": "vitest run --config vitest.integration.config.ts",
|
|
149
207
|
"test:watch": "vitest",
|
|
208
|
+
"check:package": "node scripts/check-package.mjs",
|
|
209
|
+
"check:release-version": "node scripts/check-release-version.mjs",
|
|
150
210
|
"bench": "node --expose-gc --max-old-space-size=8192 --import tsx/esm bench/benchmark.ts",
|
|
151
211
|
"bench:roi": "tsx bench/cloud-roi-simulation.ts",
|
|
152
212
|
"bench:compression": "tsx bench/compression.bench.ts",
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { IRedisDriver, IRedisPipeline } from '../types';
|
|
2
|
+
import { Cluster } from 'ioredis';
|
|
3
|
+
import { redisHashSlot } from '../utils/redis-slot';
|
|
2
4
|
|
|
3
5
|
export interface AutoPipelinerOptions {
|
|
4
6
|
/** Maximum number of operations to coalesce before forcing an immediate pipeline dispatch. Default: 100 */
|
|
@@ -23,6 +25,15 @@ interface PendingSetexOp {
|
|
|
23
25
|
reject: (err: Error) => void;
|
|
24
26
|
}
|
|
25
27
|
|
|
28
|
+
interface PendingSetpxOp {
|
|
29
|
+
type: 'setpx';
|
|
30
|
+
key: string;
|
|
31
|
+
milliseconds: number;
|
|
32
|
+
value: string;
|
|
33
|
+
resolve: () => void;
|
|
34
|
+
reject: (error: Error) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
26
37
|
interface PendingDelOp {
|
|
27
38
|
type: 'del';
|
|
28
39
|
keys: string[];
|
|
@@ -30,7 +41,39 @@ interface PendingDelOp {
|
|
|
30
41
|
reject: (err: Error) => void;
|
|
31
42
|
}
|
|
32
43
|
|
|
33
|
-
|
|
44
|
+
export interface TimedRedisValue {
|
|
45
|
+
raw: string | Record<string, string> | null;
|
|
46
|
+
ttlMs?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface PendingReadOp {
|
|
50
|
+
type: 'read';
|
|
51
|
+
key: string;
|
|
52
|
+
hash: boolean;
|
|
53
|
+
resolve: (value: TimedRedisValue) => void;
|
|
54
|
+
reject: (err: Error) => void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Read value + PTTL in one atomic, single-key transaction where the driver supports it. */
|
|
58
|
+
export async function readRedisValue(client: any, key: string, hash = false): Promise<TimedRedisValue> {
|
|
59
|
+
if (typeof client.pttl === 'function' && typeof client.multi === 'function') {
|
|
60
|
+
const transaction = client.multi();
|
|
61
|
+
if (typeof transaction.pttl === 'function' && (!hash || typeof transaction.hgetall === 'function')) {
|
|
62
|
+
if (hash) transaction.hgetall(key);
|
|
63
|
+
else transaction.get(key);
|
|
64
|
+
transaction.pttl(key);
|
|
65
|
+
const results = await transaction.exec();
|
|
66
|
+
if (!results) throw new Error('Redis read transaction aborted');
|
|
67
|
+
if (results[0]?.[0]) throw results[0][0];
|
|
68
|
+
if (results[1]?.[0]) throw results[1][0];
|
|
69
|
+
return { raw: results[0]?.[1] ?? null, ttlMs: Number(results[1]?.[1]) };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Legacy/custom drivers without an atomic TTL read retain the documented L1 fallback TTL.
|
|
73
|
+
return { raw: await (hash ? client.hgetall(key) : client.get(key)) };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type PendingOp = PendingGetOp | PendingSetexOp | PendingSetpxOp | PendingDelOp | PendingReadOp;
|
|
34
77
|
|
|
35
78
|
/**
|
|
36
79
|
* AutoPipeliner — Microtask-coalesced Redis request batching.
|
|
@@ -80,6 +123,18 @@ export class AutoPipeliner {
|
|
|
80
123
|
});
|
|
81
124
|
}
|
|
82
125
|
|
|
126
|
+
public async read(key: string, hash = false): Promise<TimedRedisValue> {
|
|
127
|
+
if (!hash && typeof this.client.pttl !== 'function') return { raw: await this.get(key) };
|
|
128
|
+
if (!this.enabled || !this.supportsPipelines() || typeof this.client.pttl !== 'function') {
|
|
129
|
+
return readRedisValue(this.client, key, hash);
|
|
130
|
+
}
|
|
131
|
+
const probe = typeof this.client.pipeline === 'function' ? this.client.pipeline() : this.client.multi();
|
|
132
|
+
if (typeof probe.pttl !== 'function' || (hash && typeof probe.hgetall !== 'function')) {
|
|
133
|
+
return readRedisValue(this.client, key, hash);
|
|
134
|
+
}
|
|
135
|
+
return new Promise<TimedRedisValue>((resolve, reject) => this.enqueue({ type: 'read', key, hash, resolve, reject }));
|
|
136
|
+
}
|
|
137
|
+
|
|
83
138
|
public async setex(key: string, seconds: number, value: string): Promise<void> {
|
|
84
139
|
if (!this.enabled || !this.supportsPipelines()) {
|
|
85
140
|
if (typeof this.client.setex === 'function') {
|
|
@@ -95,8 +150,19 @@ export class AutoPipeliner {
|
|
|
95
150
|
});
|
|
96
151
|
}
|
|
97
152
|
|
|
153
|
+
public async setpx(key: string, milliseconds: number, value: string): Promise<void> {
|
|
154
|
+
if (!this.enabled || !this.supportsPipelines()) {
|
|
155
|
+
await this.client.set(key, value, 'PX', milliseconds);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
return new Promise<void>((resolve, reject) => this.enqueue({ type: 'setpx', key, milliseconds, value, resolve, reject }));
|
|
159
|
+
}
|
|
160
|
+
|
|
98
161
|
public async del(...keys: string[]): Promise<number> {
|
|
99
162
|
if (keys.length === 0) return 0;
|
|
163
|
+
if (this.client instanceof Cluster && keys.length > 1) {
|
|
164
|
+
return (await Promise.all(keys.map(key => this.del(key)))).reduce((sum, value) => sum + value, 0);
|
|
165
|
+
}
|
|
100
166
|
if (!this.enabled || !this.supportsPipelines()) {
|
|
101
167
|
return this.client.del(...keys);
|
|
102
168
|
}
|
|
@@ -142,45 +208,58 @@ export class AutoPipeliner {
|
|
|
142
208
|
this.statsInternal.batchesDispatched++;
|
|
143
209
|
this.statsInternal.operationsBatched += batch.length;
|
|
144
210
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
case 'setex':
|
|
155
|
-
pipe.setex(op.key, op.seconds, op.value);
|
|
156
|
-
break;
|
|
157
|
-
case 'del':
|
|
158
|
-
pipe.del(...op.keys);
|
|
159
|
-
break;
|
|
211
|
+
if (this.client instanceof Cluster) {
|
|
212
|
+
const groups = new Map<string, PendingOp[]>();
|
|
213
|
+
for (const operation of batch) {
|
|
214
|
+
const key = operation.type === 'del' ? operation.keys[0] : operation.key;
|
|
215
|
+
const slot = redisHashSlot(key);
|
|
216
|
+
const master = this.client.slots[slot]?.[0] ?? String(slot);
|
|
217
|
+
const members = groups.get(master) ?? [];
|
|
218
|
+
members.push(operation);
|
|
219
|
+
groups.set(master, members);
|
|
160
220
|
}
|
|
221
|
+
await Promise.all([...groups.values()].map(members => this.dispatch(members)));
|
|
222
|
+
return;
|
|
161
223
|
}
|
|
224
|
+
await this.dispatch(batch);
|
|
225
|
+
}
|
|
162
226
|
|
|
227
|
+
private async dispatch(batch: PendingOp[]): Promise<void> {
|
|
163
228
|
try {
|
|
229
|
+
const pipe: IRedisPipeline = typeof this.client.pipeline === 'function' ? this.client.pipeline() : this.client.multi();
|
|
230
|
+
const indexes: number[] = [];
|
|
231
|
+
let commandCount = 0;
|
|
232
|
+
for (const op of batch) {
|
|
233
|
+
indexes.push(commandCount++);
|
|
234
|
+
switch (op.type) {
|
|
235
|
+
case 'get': pipe.get(op.key); break;
|
|
236
|
+
case 'setex': pipe.setex(op.key, op.seconds, op.value); break;
|
|
237
|
+
case 'setpx': pipe.set(op.key, op.value, 'PX', op.milliseconds); break;
|
|
238
|
+
case 'del': pipe.del(...op.keys); break;
|
|
239
|
+
case 'read':
|
|
240
|
+
if (op.hash) pipe.hgetall!(op.key);
|
|
241
|
+
else pipe.get(op.key);
|
|
242
|
+
pipe.pttl!(op.key);
|
|
243
|
+
commandCount++;
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
164
247
|
const results = await pipe.exec();
|
|
165
248
|
|
|
166
249
|
if (!results) {
|
|
167
250
|
// Pipeline returned null / aborted
|
|
168
251
|
for (const op of batch) {
|
|
169
|
-
|
|
170
|
-
else if (op.type === 'del') op.resolve(0);
|
|
171
|
-
else op.resolve();
|
|
252
|
+
op.reject(new Error('Redis pipeline aborted'));
|
|
172
253
|
}
|
|
173
254
|
return;
|
|
174
255
|
}
|
|
175
256
|
|
|
176
257
|
for (let i = 0; i < batch.length; i++) {
|
|
177
258
|
const op = batch[i];
|
|
178
|
-
const resTuple = results[i];
|
|
259
|
+
const resTuple = results[indexes[i]];
|
|
179
260
|
|
|
180
261
|
if (!resTuple) {
|
|
181
|
-
|
|
182
|
-
else if (op.type === 'del') op.resolve(0);
|
|
183
|
-
else op.resolve();
|
|
262
|
+
op.reject(new Error('Missing Redis pipeline result'));
|
|
184
263
|
continue;
|
|
185
264
|
}
|
|
186
265
|
|
|
@@ -188,7 +267,11 @@ export class AutoPipeliner {
|
|
|
188
267
|
if (err) {
|
|
189
268
|
op.reject(err instanceof Error ? err : new Error(String(err)));
|
|
190
269
|
} else {
|
|
191
|
-
if (op.type === '
|
|
270
|
+
if (op.type === 'read') {
|
|
271
|
+
const ttlResult = results[indexes[i] + 1];
|
|
272
|
+
if (!ttlResult || ttlResult[0]) op.reject(ttlResult?.[0] ?? new Error('Missing Redis PTTL result'));
|
|
273
|
+
else op.resolve({ raw: val, ttlMs: Number(ttlResult[1]) });
|
|
274
|
+
} else if (op.type === 'get') {
|
|
192
275
|
op.resolve(val != null ? String(val) : null);
|
|
193
276
|
} else if (op.type === 'del') {
|
|
194
277
|
op.resolve(typeof val === 'number' ? val : Number(val) || 0);
|