@maiyunnet/kebab 2.0.7 → 2.0.8
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/index.d.ts +11 -1
- package/index.js +13 -1
- package/lib/buffer.d.ts +25 -0
- package/lib/buffer.js +30 -5
- package/lib/captcha.d.ts +15 -0
- package/lib/captcha.js +20 -0
- package/lib/consistent.d.ts +51 -0
- package/lib/consistent.js +59 -0
- package/lib/core.d.ts +134 -0
- package/lib/core.js +176 -0
- package/lib/crypto.d.ts +75 -6
- package/lib/crypto.js +206 -38
- package/lib/db.d.ts +104 -0
- package/lib/db.js +126 -0
- package/lib/dns.d.ts +51 -0
- package/lib/dns.js +54 -2
- package/lib/fs.d.ts +100 -0
- package/lib/fs.js +118 -0
- package/lib/jwt.d.ts +43 -0
- package/lib/jwt.js +45 -0
- package/lib/kv.d.ts +362 -0
- package/lib/kv.js +377 -0
- package/lib/lan.d.ts +6 -0
- package/lib/lan.js +7 -0
- package/lib/net/formdata.d.ts +38 -0
- package/lib/net/formdata.js +43 -0
- package/lib/net/request.d.ts +62 -0
- package/lib/net/request.js +57 -0
- package/lib/net/response.d.ts +21 -0
- package/lib/net/response.js +16 -0
- package/lib/net.d.ts +86 -0
- package/lib/net.js +140 -0
- package/lib/s3.d.ts +52 -0
- package/lib/s3.js +51 -0
- package/lib/scan.d.ts +52 -0
- package/lib/scan.js +84 -0
- package/lib/session.d.ts +31 -0
- package/lib/session.js +52 -1
- package/lib/sql.d.ts +176 -0
- package/lib/sql.js +287 -2
- package/lib/ssh/sftp.d.ts +106 -0
- package/lib/ssh/sftp.js +106 -0
- package/lib/ssh/shell.d.ts +37 -0
- package/lib/ssh/shell.js +31 -0
- package/lib/ssh.d.ts +32 -0
- package/lib/ssh.js +32 -0
- package/lib/text.d.ts +131 -0
- package/lib/text.js +188 -0
- package/lib/time.d.ts +53 -0
- package/lib/time.js +55 -0
- package/lib/ws.d.ts +68 -0
- package/lib/ws.js +74 -0
- package/lib/zip.d.ts +53 -0
- package/lib/zip.js +73 -0
- package/lib/zlib.d.ts +76 -0
- package/lib/zlib.js +78 -0
- package/main.d.ts +6 -1
- package/main.js +11 -1
- package/package.json +1 -1
- package/sys/child.js +104 -0
- package/sys/cmd.js +28 -0
- package/sys/ctr.d.ts +166 -0
- package/sys/ctr.js +177 -0
- package/sys/master.js +63 -0
- package/sys/mod.d.ts +266 -0
- package/sys/mod.js +335 -0
- package/sys/route.d.ts +34 -0
- package/sys/route.js +164 -0
- package/www/example/ctr/test.d.ts +3 -0
- package/www/example/ctr/test.js +63 -1
- package/www/example/mod/test.js +14 -0
- package/www/example/mod/testdata.js +9 -0
- package/www/example/ws/test.js +1 -0
- package/.VSCodeCounter/2025-02-14_14-46-44/details.md +0 -82
- package/.VSCodeCounter/2025-02-14_14-46-44/diff-details.md +0 -15
- package/.VSCodeCounter/2025-02-14_14-46-44/diff.csv +0 -2
- package/.VSCodeCounter/2025-02-14_14-46-44/diff.md +0 -19
- package/.VSCodeCounter/2025-02-14_14-46-44/diff.txt +0 -22
- package/.VSCodeCounter/2025-02-14_14-46-44/results.csv +0 -69
- package/.VSCodeCounter/2025-02-14_14-46-44/results.json +0 -1
- package/.VSCodeCounter/2025-02-14_14-46-44/results.md +0 -48
- package/.VSCodeCounter/2025-02-14_14-46-44/results.txt +0 -118
- package/.vscode/tasks.json +0 -15
package/lib/kv.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Project: Kebab, User: JianSuoQiYue
|
|
4
|
+
* Date: 2019-5-30 19:25:22
|
|
5
|
+
* Last: 2020-3-28 18:54:04, 2022-09-12 23:24:45, 2022-09-22 01:06:22, 2024-2-21 13:32:56, 2024-8-21 16:59:57
|
|
6
|
+
*/
|
|
2
7
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
8
|
if (k2 === undefined) k2 = k;
|
|
4
9
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -36,22 +41,33 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
36
41
|
exports.Connection = exports.Pool = void 0;
|
|
37
42
|
exports.get = get;
|
|
38
43
|
exports.getConnectionList = getConnectionList;
|
|
44
|
+
// --- Pool 是使用时必须要一个用户创建一份的,Connection 是池子里获取的 ---
|
|
45
|
+
// --- 为啥 Pool 要独立,因为有些配置项目不能存在 Connection 是用户单独使用的,例如 pre ---
|
|
46
|
+
// --- 第三方 ---
|
|
39
47
|
const redis = __importStar(require("@litert/redis"));
|
|
48
|
+
// --- 库和定义 ---
|
|
40
49
|
const time = __importStar(require("../lib/time"));
|
|
41
50
|
const text = __importStar(require("../lib/text"));
|
|
51
|
+
/** --- 连接列表(同一个 host、port、index、auth 只有一个连接) --- */
|
|
42
52
|
const connections = [];
|
|
53
|
+
/**
|
|
54
|
+
* --- 计划任务 30 秒一次,关闭超过 3 分钟不活动的连接 ---
|
|
55
|
+
*/
|
|
43
56
|
async function checkConnection() {
|
|
44
57
|
const now = time.stamp();
|
|
45
58
|
for (let i = 0; i < connections.length; ++i) {
|
|
46
59
|
const connection = connections[i];
|
|
47
60
|
if (connection.isLost()) {
|
|
61
|
+
// --- 连接已经丢失,移除 ---
|
|
48
62
|
await connection.end();
|
|
49
63
|
connections.splice(i, 1);
|
|
50
64
|
--i;
|
|
51
65
|
continue;
|
|
52
66
|
}
|
|
53
67
|
if (connection.isUsing()) {
|
|
68
|
+
// --- 连接正在被使用,看看是否使用超过 3 分钟,超过则不是正常状态 ---
|
|
54
69
|
if (connection.getLast() <= now - 180) {
|
|
70
|
+
// --- 10 分钟之前开始的 ---
|
|
55
71
|
console.log(`[kv] [error] There is a transactional connection[${i}] that is not closed.`);
|
|
56
72
|
await connection.end();
|
|
57
73
|
connections.splice(i, 1);
|
|
@@ -59,9 +75,12 @@ async function checkConnection() {
|
|
|
59
75
|
}
|
|
60
76
|
continue;
|
|
61
77
|
}
|
|
78
|
+
// --- 检测 3 分钟内是否使用过 ---
|
|
62
79
|
if (connection.getLast() > now - 180) {
|
|
80
|
+
// --- 3 分钟内使用过,不管 ---
|
|
63
81
|
continue;
|
|
64
82
|
}
|
|
83
|
+
// --- 超 3 分钟未被使用,则关闭 ---
|
|
65
84
|
await connection.end();
|
|
66
85
|
connections.splice(i, 1);
|
|
67
86
|
--i;
|
|
@@ -83,6 +102,13 @@ class Pool {
|
|
|
83
102
|
this._etc = ctr.getPrototype('_config').kv;
|
|
84
103
|
}
|
|
85
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* --- 设定一个值 ---
|
|
107
|
+
* @param key
|
|
108
|
+
* @param val
|
|
109
|
+
* @param ttl 秒,0 为不限制
|
|
110
|
+
* @param mod 设置模式: 空,nx(key不存在才建立),xx(key存在才修改)
|
|
111
|
+
*/
|
|
86
112
|
async set(key, val, ttl = 0, mod = '') {
|
|
87
113
|
const conn = await this._getConnection();
|
|
88
114
|
if (!conn) {
|
|
@@ -92,6 +118,12 @@ class Pool {
|
|
|
92
118
|
conn.used();
|
|
93
119
|
return r;
|
|
94
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* --- 添加一个值,存在则不变 ---
|
|
123
|
+
* @param key
|
|
124
|
+
* @param val
|
|
125
|
+
* @param ttl 秒,0 为不限制
|
|
126
|
+
*/
|
|
95
127
|
async add(key, val, ttl = 0) {
|
|
96
128
|
const conn = await this._getConnection();
|
|
97
129
|
if (!conn) {
|
|
@@ -101,6 +133,12 @@ class Pool {
|
|
|
101
133
|
conn.used();
|
|
102
134
|
return r;
|
|
103
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* --- 替换一个存在的值 ---
|
|
138
|
+
* @param key
|
|
139
|
+
* @param val
|
|
140
|
+
* @param ttl 秒,0 为不限制
|
|
141
|
+
*/
|
|
104
142
|
async replace(key, val, ttl = 0) {
|
|
105
143
|
const conn = await this._getConnection();
|
|
106
144
|
if (!conn) {
|
|
@@ -110,6 +148,11 @@ class Pool {
|
|
|
110
148
|
conn.used();
|
|
111
149
|
return r;
|
|
112
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* --- 向已存在的值后追加数据 ---
|
|
153
|
+
* @param key
|
|
154
|
+
* @param val
|
|
155
|
+
*/
|
|
113
156
|
async append(key, val) {
|
|
114
157
|
const conn = await this._getConnection();
|
|
115
158
|
if (!conn) {
|
|
@@ -119,6 +162,11 @@ class Pool {
|
|
|
119
162
|
conn.used();
|
|
120
163
|
return r;
|
|
121
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* --- 向已存在的值之前追加数据 ---
|
|
167
|
+
* @param key
|
|
168
|
+
* @param val
|
|
169
|
+
*/
|
|
122
170
|
async prepend(key, val) {
|
|
123
171
|
const conn = await this._getConnection();
|
|
124
172
|
if (!conn) {
|
|
@@ -128,6 +176,10 @@ class Pool {
|
|
|
128
176
|
conn.used();
|
|
129
177
|
return r;
|
|
130
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* --- 检测 key 是否存在 ---
|
|
181
|
+
* @param keys
|
|
182
|
+
*/
|
|
131
183
|
async exists(keys) {
|
|
132
184
|
const conn = await this._getConnection();
|
|
133
185
|
if (!conn) {
|
|
@@ -137,6 +189,10 @@ class Pool {
|
|
|
137
189
|
conn.used();
|
|
138
190
|
return r;
|
|
139
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* --- 获取字符串 ---
|
|
194
|
+
* @param key
|
|
195
|
+
*/
|
|
140
196
|
async get(key) {
|
|
141
197
|
const conn = await this._getConnection();
|
|
142
198
|
if (!conn) {
|
|
@@ -146,6 +202,10 @@ class Pool {
|
|
|
146
202
|
conn.used();
|
|
147
203
|
return r;
|
|
148
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* --- 获取相应的剩余有效期秒数 ---
|
|
207
|
+
* @param key
|
|
208
|
+
*/
|
|
149
209
|
async ttl(key) {
|
|
150
210
|
const conn = await this._getConnection();
|
|
151
211
|
if (!conn) {
|
|
@@ -155,6 +215,10 @@ class Pool {
|
|
|
155
215
|
conn.used();
|
|
156
216
|
return r;
|
|
157
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* --- 获取相应的剩余有效期毫秒数 ---
|
|
220
|
+
* @param key
|
|
221
|
+
*/
|
|
158
222
|
async pttl(key) {
|
|
159
223
|
const conn = await this._getConnection();
|
|
160
224
|
if (!conn) {
|
|
@@ -164,6 +228,10 @@ class Pool {
|
|
|
164
228
|
conn.used();
|
|
165
229
|
return r;
|
|
166
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* --- 批量获取值 ---
|
|
233
|
+
* @param keys key 序列
|
|
234
|
+
*/
|
|
167
235
|
async mSet(rows) {
|
|
168
236
|
const conn = await this._getConnection();
|
|
169
237
|
if (!conn) {
|
|
@@ -173,6 +241,10 @@ class Pool {
|
|
|
173
241
|
conn.used();
|
|
174
242
|
return r;
|
|
175
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* --- 批量获取值 ---
|
|
246
|
+
* @param keys key 序列
|
|
247
|
+
*/
|
|
176
248
|
async mGet(keys) {
|
|
177
249
|
const conn = await this._getConnection();
|
|
178
250
|
if (conn) {
|
|
@@ -188,6 +260,10 @@ class Pool {
|
|
|
188
260
|
return rtn;
|
|
189
261
|
}
|
|
190
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* --- 获取 json 对象 ---
|
|
265
|
+
* @param key
|
|
266
|
+
*/
|
|
191
267
|
async getJson(key) {
|
|
192
268
|
const conn = await this._getConnection();
|
|
193
269
|
if (!conn) {
|
|
@@ -197,6 +273,10 @@ class Pool {
|
|
|
197
273
|
conn.used();
|
|
198
274
|
return r;
|
|
199
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* --- 删除已存在的值 ---
|
|
278
|
+
* @param keys
|
|
279
|
+
*/
|
|
200
280
|
async del(keys) {
|
|
201
281
|
const conn = await this._getConnection();
|
|
202
282
|
if (!conn) {
|
|
@@ -206,6 +286,11 @@ class Pool {
|
|
|
206
286
|
conn.used();
|
|
207
287
|
return r;
|
|
208
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* --- 自增 ---
|
|
291
|
+
* @param key
|
|
292
|
+
* @param num 整数或浮点正数
|
|
293
|
+
*/
|
|
209
294
|
async incr(key, num = 1) {
|
|
210
295
|
const conn = await this._getConnection();
|
|
211
296
|
if (!conn) {
|
|
@@ -215,6 +300,11 @@ class Pool {
|
|
|
215
300
|
conn.used();
|
|
216
301
|
return r;
|
|
217
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* --- 自减 ---
|
|
305
|
+
* @param key
|
|
306
|
+
* @param num 整数或浮点正数
|
|
307
|
+
*/
|
|
218
308
|
async decr(key, num = 1) {
|
|
219
309
|
const conn = await this._getConnection();
|
|
220
310
|
if (!conn) {
|
|
@@ -224,6 +314,11 @@ class Pool {
|
|
|
224
314
|
conn.used();
|
|
225
315
|
return r;
|
|
226
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* --- 仅修改过期时间不修改值 ---
|
|
319
|
+
* @param key
|
|
320
|
+
* @param ttl
|
|
321
|
+
*/
|
|
227
322
|
async expire(key, ttl) {
|
|
228
323
|
const conn = await this._getConnection();
|
|
229
324
|
if (!conn) {
|
|
@@ -233,6 +328,10 @@ class Pool {
|
|
|
233
328
|
conn.used();
|
|
234
329
|
return r;
|
|
235
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* --- 获取服务器上的所有 key 列表 ---
|
|
333
|
+
* @param pattern
|
|
334
|
+
*/
|
|
236
335
|
async keys(pattern) {
|
|
237
336
|
const conn = await this._getConnection();
|
|
238
337
|
if (!conn) {
|
|
@@ -242,6 +341,12 @@ class Pool {
|
|
|
242
341
|
conn.used();
|
|
243
342
|
return r;
|
|
244
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* --- 根据条件获取服务器上的 keys ---
|
|
346
|
+
* @param cursor
|
|
347
|
+
* @param pattern 例如 *
|
|
348
|
+
* @param count 获取的条数
|
|
349
|
+
*/
|
|
245
350
|
async scan(cursor = 0, pattern = '*', count = 10) {
|
|
246
351
|
const conn = await this._getConnection();
|
|
247
352
|
if (!conn) {
|
|
@@ -251,6 +356,9 @@ class Pool {
|
|
|
251
356
|
conn.used();
|
|
252
357
|
return r;
|
|
253
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* --- 清除当前所选数据库的所有内容 ---
|
|
361
|
+
*/
|
|
254
362
|
async flushDb() {
|
|
255
363
|
const conn = await this._getConnection();
|
|
256
364
|
if (!conn) {
|
|
@@ -260,6 +368,9 @@ class Pool {
|
|
|
260
368
|
conn.used();
|
|
261
369
|
return r;
|
|
262
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* --- 发送 ping ---
|
|
373
|
+
*/
|
|
263
374
|
async ping() {
|
|
264
375
|
const conn = await this._getConnection();
|
|
265
376
|
if (!conn) {
|
|
@@ -269,6 +380,13 @@ class Pool {
|
|
|
269
380
|
conn.used();
|
|
270
381
|
return r;
|
|
271
382
|
}
|
|
383
|
+
/**
|
|
384
|
+
* --- 设置哈希表值 ---
|
|
385
|
+
* @param key key 名
|
|
386
|
+
* @param field 字段名
|
|
387
|
+
* @param val 值
|
|
388
|
+
* @param mod 空,nx(key不存在才建立)
|
|
389
|
+
*/
|
|
272
390
|
async hSet(key, field, val, mod = '') {
|
|
273
391
|
const conn = await this._getConnection();
|
|
274
392
|
if (!conn) {
|
|
@@ -278,6 +396,11 @@ class Pool {
|
|
|
278
396
|
conn.used();
|
|
279
397
|
return r;
|
|
280
398
|
}
|
|
399
|
+
/**
|
|
400
|
+
* --- 批量设置哈希值 ---
|
|
401
|
+
* @param key key 名
|
|
402
|
+
* @param rows key / val 数组
|
|
403
|
+
*/
|
|
281
404
|
async hMSet(key, rows) {
|
|
282
405
|
const conn = await this._getConnection();
|
|
283
406
|
if (!conn) {
|
|
@@ -287,6 +410,11 @@ class Pool {
|
|
|
287
410
|
conn.used();
|
|
288
411
|
return r;
|
|
289
412
|
}
|
|
413
|
+
/**
|
|
414
|
+
* --- 获取哈希值 ---
|
|
415
|
+
* @param key
|
|
416
|
+
* @param field
|
|
417
|
+
*/
|
|
290
418
|
async hGet(key, field) {
|
|
291
419
|
const conn = await this._getConnection();
|
|
292
420
|
if (!conn) {
|
|
@@ -296,6 +424,11 @@ class Pool {
|
|
|
296
424
|
conn.used();
|
|
297
425
|
return r;
|
|
298
426
|
}
|
|
427
|
+
/**
|
|
428
|
+
* --- 获取哈希 json 对象 ---
|
|
429
|
+
* @param key
|
|
430
|
+
* @param field
|
|
431
|
+
*/
|
|
299
432
|
async hGetJson(key, field) {
|
|
300
433
|
const conn = await this._getConnection();
|
|
301
434
|
if (!conn) {
|
|
@@ -305,6 +438,11 @@ class Pool {
|
|
|
305
438
|
conn.used();
|
|
306
439
|
return r;
|
|
307
440
|
}
|
|
441
|
+
/**
|
|
442
|
+
* --- 批量获取哈希值 ---
|
|
443
|
+
* @param key
|
|
444
|
+
* @param fields
|
|
445
|
+
*/
|
|
308
446
|
async hMGet(key, fields) {
|
|
309
447
|
const conn = await this._getConnection();
|
|
310
448
|
if (conn) {
|
|
@@ -320,6 +458,10 @@ class Pool {
|
|
|
320
458
|
return rtn;
|
|
321
459
|
}
|
|
322
460
|
}
|
|
461
|
+
/**
|
|
462
|
+
* --- 批量获取哈希键值对 ---
|
|
463
|
+
* @param key
|
|
464
|
+
*/
|
|
323
465
|
async hGetAll(key) {
|
|
324
466
|
const conn = await this._getConnection();
|
|
325
467
|
if (!conn) {
|
|
@@ -329,6 +471,11 @@ class Pool {
|
|
|
329
471
|
conn.used();
|
|
330
472
|
return r;
|
|
331
473
|
}
|
|
474
|
+
/**
|
|
475
|
+
* --- 删除哈希键 ---
|
|
476
|
+
* @param key
|
|
477
|
+
* @param fields 值序列
|
|
478
|
+
*/
|
|
332
479
|
async hDel(key, fields) {
|
|
333
480
|
const conn = await this._getConnection();
|
|
334
481
|
if (!conn) {
|
|
@@ -338,6 +485,11 @@ class Pool {
|
|
|
338
485
|
conn.used();
|
|
339
486
|
return r;
|
|
340
487
|
}
|
|
488
|
+
/**
|
|
489
|
+
* --- 判断哈希字段是否存在 ---
|
|
490
|
+
* @param key
|
|
491
|
+
* @param field
|
|
492
|
+
*/
|
|
341
493
|
async hExists(key, field) {
|
|
342
494
|
const conn = await this._getConnection();
|
|
343
495
|
if (!conn) {
|
|
@@ -347,6 +499,12 @@ class Pool {
|
|
|
347
499
|
conn.used();
|
|
348
500
|
return r;
|
|
349
501
|
}
|
|
502
|
+
/**
|
|
503
|
+
* --- 设置哈希自增自减 ---
|
|
504
|
+
* @param key key
|
|
505
|
+
* @param field 字段
|
|
506
|
+
* @param increment 正数或负数,整数或浮点
|
|
507
|
+
*/
|
|
350
508
|
async hIncr(key, field, increment) {
|
|
351
509
|
const conn = await this._getConnection();
|
|
352
510
|
if (!conn) {
|
|
@@ -356,6 +514,10 @@ class Pool {
|
|
|
356
514
|
conn.used();
|
|
357
515
|
return r;
|
|
358
516
|
}
|
|
517
|
+
/**
|
|
518
|
+
* --- 获取哈希所有字段 ---
|
|
519
|
+
* @param key
|
|
520
|
+
*/
|
|
359
521
|
async hKeys(key) {
|
|
360
522
|
const conn = await this._getConnection();
|
|
361
523
|
if (!conn) {
|
|
@@ -437,6 +599,9 @@ class Pool {
|
|
|
437
599
|
conn.used();
|
|
438
600
|
return r;
|
|
439
601
|
}
|
|
602
|
+
/**
|
|
603
|
+
* --- 从连接池获取一个连接,会被自动设置为使用中,需要在执行命令后解除占用 ---
|
|
604
|
+
*/
|
|
440
605
|
async _getConnection() {
|
|
441
606
|
let conn;
|
|
442
607
|
for (const connection of connections) {
|
|
@@ -446,6 +611,7 @@ class Pool {
|
|
|
446
611
|
(etc.host !== this._etc.host) ||
|
|
447
612
|
(etc.port !== this._etc.port) ||
|
|
448
613
|
(etc.index !== this._etc.index)) {
|
|
614
|
+
// --- 配置项连接项不匹配 ---
|
|
449
615
|
continue;
|
|
450
616
|
}
|
|
451
617
|
connection.refreshLast();
|
|
@@ -454,16 +620,19 @@ class Pool {
|
|
|
454
620
|
break;
|
|
455
621
|
}
|
|
456
622
|
if (!conn) {
|
|
623
|
+
// --- 没有找到合适的连接,创建一个 ---
|
|
457
624
|
const link = redis.createCommandClient({
|
|
458
625
|
'host': this._etc.host,
|
|
459
626
|
'port': this._etc.port
|
|
460
627
|
});
|
|
628
|
+
// --- 开始连接 ---
|
|
461
629
|
try {
|
|
462
630
|
await link.connect();
|
|
463
631
|
}
|
|
464
632
|
catch {
|
|
465
633
|
return null;
|
|
466
634
|
}
|
|
635
|
+
// --- 认证 ---
|
|
467
636
|
if (this._etc.user || this._etc.pwd) {
|
|
468
637
|
try {
|
|
469
638
|
await link.auth(this._etc.user + this._etc.pwd);
|
|
@@ -478,6 +647,7 @@ class Pool {
|
|
|
478
647
|
conn.setUsing();
|
|
479
648
|
link.on('error', function (err) {
|
|
480
649
|
conn.setLost();
|
|
650
|
+
// console.log(`--- redis [${conn._etc.host}:${conn._etc.port}] error ---`);
|
|
481
651
|
console.log('[KV] [ERROR]', err);
|
|
482
652
|
}).on('close', () => {
|
|
483
653
|
conn.setLost();
|
|
@@ -490,37 +660,67 @@ class Pool {
|
|
|
490
660
|
exports.Pool = Pool;
|
|
491
661
|
class Connection {
|
|
492
662
|
constructor(etc, link) {
|
|
663
|
+
/** --- 本连接最后一次使用时间 --- */
|
|
493
664
|
this._last = 0;
|
|
665
|
+
/** --- 当前连接是否正在被独占使用 --- */
|
|
494
666
|
this._using = false;
|
|
667
|
+
/** --- 当发生断开,则需从连接池移除连接 --- */
|
|
495
668
|
this._lost = false;
|
|
496
669
|
this._etc = etc;
|
|
497
670
|
this._link = link;
|
|
498
671
|
this.refreshLast();
|
|
499
672
|
}
|
|
673
|
+
/**
|
|
674
|
+
* --- 获取连接 etc 信息 ---
|
|
675
|
+
*/
|
|
500
676
|
getEtc() {
|
|
501
677
|
return this._etc;
|
|
502
678
|
}
|
|
679
|
+
/**
|
|
680
|
+
* --- 获取最后一次获取连接的时间 ---
|
|
681
|
+
*/
|
|
503
682
|
getLast() {
|
|
504
683
|
return this._last;
|
|
505
684
|
}
|
|
685
|
+
/**
|
|
686
|
+
* --- 将本条连接设置为不可用 ---
|
|
687
|
+
*/
|
|
506
688
|
setLost() {
|
|
507
689
|
this._lost = true;
|
|
508
690
|
}
|
|
691
|
+
/**
|
|
692
|
+
* --- 是否已经丢失 ---
|
|
693
|
+
*/
|
|
509
694
|
isLost() {
|
|
510
695
|
return this._lost;
|
|
511
696
|
}
|
|
697
|
+
/**
|
|
698
|
+
* --- 将本条连接设置占用中 ---
|
|
699
|
+
*/
|
|
512
700
|
setUsing() {
|
|
513
701
|
this._using = true;
|
|
514
702
|
}
|
|
703
|
+
/**
|
|
704
|
+
* --- 获取当前状态是否正在被使用中 ---
|
|
705
|
+
*/
|
|
515
706
|
isUsing() {
|
|
516
707
|
return this._using;
|
|
517
708
|
}
|
|
709
|
+
/**
|
|
710
|
+
* --- 取消占用 ---
|
|
711
|
+
*/
|
|
518
712
|
used() {
|
|
519
713
|
this._using = false;
|
|
520
714
|
}
|
|
715
|
+
/**
|
|
716
|
+
* --- 设定最后使用时间 ---
|
|
717
|
+
*/
|
|
521
718
|
refreshLast() {
|
|
522
719
|
this._last = time.stamp();
|
|
523
720
|
}
|
|
721
|
+
/**
|
|
722
|
+
* --- 断开此连接,一般情况下不使用 ---
|
|
723
|
+
*/
|
|
524
724
|
async end() {
|
|
525
725
|
try {
|
|
526
726
|
await this._link.close();
|
|
@@ -529,6 +729,14 @@ class Connection {
|
|
|
529
729
|
return;
|
|
530
730
|
}
|
|
531
731
|
}
|
|
732
|
+
/**
|
|
733
|
+
* --- 设定一个值 ---
|
|
734
|
+
* @param key
|
|
735
|
+
* @param val
|
|
736
|
+
* @param ttl 秒,0 为不限制
|
|
737
|
+
* @param mod 设置模式: 空,nx(key不存在才建立),xx(key存在才修改)
|
|
738
|
+
* @param etc 配置项,主要用 etc.pre
|
|
739
|
+
*/
|
|
532
740
|
async set(key, val, ttl, mod, etc) {
|
|
533
741
|
this.refreshLast();
|
|
534
742
|
if (typeof val !== 'string') {
|
|
@@ -551,12 +759,32 @@ class Connection {
|
|
|
551
759
|
return false;
|
|
552
760
|
}
|
|
553
761
|
}
|
|
762
|
+
/**
|
|
763
|
+
* --- 添加一个值,存在则不变 ---
|
|
764
|
+
* @param key
|
|
765
|
+
* @param val
|
|
766
|
+
* @param ttl 秒,0 为不限制
|
|
767
|
+
* @param etc
|
|
768
|
+
*/
|
|
554
769
|
async add(key, val, ttl, etc) {
|
|
555
770
|
return this.set(key, val, ttl, 'nx', etc);
|
|
556
771
|
}
|
|
772
|
+
/**
|
|
773
|
+
* --- 替换一个存在的值 ---
|
|
774
|
+
* @param key
|
|
775
|
+
* @param val
|
|
776
|
+
* @param ttl 秒,0 为不限制
|
|
777
|
+
* @param etc
|
|
778
|
+
*/
|
|
557
779
|
async replace(key, val, ttl, etc) {
|
|
558
780
|
return this.set(key, val, ttl, 'xx', etc);
|
|
559
781
|
}
|
|
782
|
+
/**
|
|
783
|
+
* --- 向已存在的值后追加数据 ---
|
|
784
|
+
* @param key
|
|
785
|
+
* @param val
|
|
786
|
+
* @param etc
|
|
787
|
+
*/
|
|
560
788
|
async append(key, val, etc) {
|
|
561
789
|
this.refreshLast();
|
|
562
790
|
try {
|
|
@@ -566,6 +794,11 @@ class Connection {
|
|
|
566
794
|
return false;
|
|
567
795
|
}
|
|
568
796
|
}
|
|
797
|
+
/**
|
|
798
|
+
* --- 向已存在的值之前追加数据 ---
|
|
799
|
+
* @param key
|
|
800
|
+
* @param val
|
|
801
|
+
*/
|
|
569
802
|
async prepend(key, val, etc) {
|
|
570
803
|
this.refreshLast();
|
|
571
804
|
try {
|
|
@@ -592,6 +825,11 @@ end`;
|
|
|
592
825
|
return false;
|
|
593
826
|
}
|
|
594
827
|
}
|
|
828
|
+
/**
|
|
829
|
+
* --- 检测 key 是否存在 ---
|
|
830
|
+
* @param keys 单个或序列
|
|
831
|
+
* @param etc
|
|
832
|
+
*/
|
|
595
833
|
async exists(keys, etc) {
|
|
596
834
|
this.refreshLast();
|
|
597
835
|
try {
|
|
@@ -607,6 +845,11 @@ end`;
|
|
|
607
845
|
return 0;
|
|
608
846
|
}
|
|
609
847
|
}
|
|
848
|
+
/**
|
|
849
|
+
* --- 获取字符串 ---
|
|
850
|
+
* @param key
|
|
851
|
+
* @param etc
|
|
852
|
+
*/
|
|
610
853
|
async get(key, etc) {
|
|
611
854
|
this.refreshLast();
|
|
612
855
|
try {
|
|
@@ -616,6 +859,11 @@ end`;
|
|
|
616
859
|
return null;
|
|
617
860
|
}
|
|
618
861
|
}
|
|
862
|
+
/**
|
|
863
|
+
* --- 获取相应的剩余有效期秒数 ---
|
|
864
|
+
* @param key
|
|
865
|
+
* @param etc
|
|
866
|
+
*/
|
|
619
867
|
async ttl(key, etc) {
|
|
620
868
|
this.refreshLast();
|
|
621
869
|
try {
|
|
@@ -625,6 +873,11 @@ end`;
|
|
|
625
873
|
return null;
|
|
626
874
|
}
|
|
627
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* --- 获取相应的剩余有效期毫秒数 ---
|
|
878
|
+
* @param key
|
|
879
|
+
* @param etc
|
|
880
|
+
*/
|
|
628
881
|
async pttl(key, etc) {
|
|
629
882
|
this.refreshLast();
|
|
630
883
|
try {
|
|
@@ -634,6 +887,11 @@ end`;
|
|
|
634
887
|
return null;
|
|
635
888
|
}
|
|
636
889
|
}
|
|
890
|
+
/**
|
|
891
|
+
* --- 批量获取值 ---
|
|
892
|
+
* @param keys key 序列
|
|
893
|
+
* @param etc 顺序数组
|
|
894
|
+
*/
|
|
637
895
|
async mGet(keys, etc) {
|
|
638
896
|
this.refreshLast();
|
|
639
897
|
for (let k = 0; k < keys.length; ++k) {
|
|
@@ -657,6 +915,12 @@ end`;
|
|
|
657
915
|
}
|
|
658
916
|
return rtn;
|
|
659
917
|
}
|
|
918
|
+
/**
|
|
919
|
+
* --- 批量设置哈希值 ---
|
|
920
|
+
* @param key key 名
|
|
921
|
+
* @param rows key / val 数组
|
|
922
|
+
* @param etc
|
|
923
|
+
*/
|
|
660
924
|
async mSet(rows, etc) {
|
|
661
925
|
this.refreshLast();
|
|
662
926
|
try {
|
|
@@ -671,6 +935,11 @@ end`;
|
|
|
671
935
|
return false;
|
|
672
936
|
}
|
|
673
937
|
}
|
|
938
|
+
/**
|
|
939
|
+
* --- 获取 json 对象 ---
|
|
940
|
+
* @param key
|
|
941
|
+
* @param etc
|
|
942
|
+
*/
|
|
674
943
|
async getJson(key, etc) {
|
|
675
944
|
const v = await this.get(key, etc);
|
|
676
945
|
if (v === null) {
|
|
@@ -679,6 +948,11 @@ end`;
|
|
|
679
948
|
const r = text.parseJson(v);
|
|
680
949
|
return r === false ? null : r;
|
|
681
950
|
}
|
|
951
|
+
/**
|
|
952
|
+
* --- 删除已存在的值 ---
|
|
953
|
+
* @param keys
|
|
954
|
+
* @param etc
|
|
955
|
+
*/
|
|
682
956
|
async del(keys, etc) {
|
|
683
957
|
this.refreshLast();
|
|
684
958
|
if (typeof keys === 'string') {
|
|
@@ -694,6 +968,12 @@ end`;
|
|
|
694
968
|
return false;
|
|
695
969
|
}
|
|
696
970
|
}
|
|
971
|
+
/**
|
|
972
|
+
* --- 自增 ---
|
|
973
|
+
* @param key
|
|
974
|
+
* @param num 整数或浮点正数
|
|
975
|
+
* @param etc
|
|
976
|
+
*/
|
|
697
977
|
async incr(key, num, etc) {
|
|
698
978
|
this.refreshLast();
|
|
699
979
|
try {
|
|
@@ -713,6 +993,12 @@ end`;
|
|
|
713
993
|
return false;
|
|
714
994
|
}
|
|
715
995
|
}
|
|
996
|
+
/**
|
|
997
|
+
* --- 自减 ---
|
|
998
|
+
* @param key
|
|
999
|
+
* @param num 整数或浮点正数
|
|
1000
|
+
* @param etc
|
|
1001
|
+
*/
|
|
716
1002
|
async decr(key, num, etc) {
|
|
717
1003
|
this.refreshLast();
|
|
718
1004
|
try {
|
|
@@ -732,6 +1018,12 @@ end`;
|
|
|
732
1018
|
return false;
|
|
733
1019
|
}
|
|
734
1020
|
}
|
|
1021
|
+
/**
|
|
1022
|
+
* --- 仅修改过期时间不修改值 ---
|
|
1023
|
+
* @param key
|
|
1024
|
+
* @param ttl
|
|
1025
|
+
* @param etc
|
|
1026
|
+
*/
|
|
735
1027
|
async expire(key, ttl, etc) {
|
|
736
1028
|
this.refreshLast();
|
|
737
1029
|
try {
|
|
@@ -741,6 +1033,10 @@ end`;
|
|
|
741
1033
|
return false;
|
|
742
1034
|
}
|
|
743
1035
|
}
|
|
1036
|
+
/**
|
|
1037
|
+
* --- 获取服务器上的所有 key 列表 ---
|
|
1038
|
+
* @param pattern
|
|
1039
|
+
*/
|
|
744
1040
|
async keys(pattern, etc) {
|
|
745
1041
|
this.refreshLast();
|
|
746
1042
|
try {
|
|
@@ -757,6 +1053,13 @@ end`;
|
|
|
757
1053
|
return false;
|
|
758
1054
|
}
|
|
759
1055
|
}
|
|
1056
|
+
/**
|
|
1057
|
+
* --- 根据条件获取服务器上的 keys ---
|
|
1058
|
+
* @param cursor
|
|
1059
|
+
* @param pattern 例如 *
|
|
1060
|
+
* @param count 获取的条数
|
|
1061
|
+
* @param etc
|
|
1062
|
+
*/
|
|
760
1063
|
async scan(cursor, pattern, count, etc) {
|
|
761
1064
|
this.refreshLast();
|
|
762
1065
|
try {
|
|
@@ -770,6 +1073,9 @@ end`;
|
|
|
770
1073
|
return false;
|
|
771
1074
|
}
|
|
772
1075
|
}
|
|
1076
|
+
/**
|
|
1077
|
+
* --- 清除当前所选数据库的所有内容 ---
|
|
1078
|
+
*/
|
|
773
1079
|
async flushDb() {
|
|
774
1080
|
this.refreshLast();
|
|
775
1081
|
try {
|
|
@@ -780,6 +1086,9 @@ end`;
|
|
|
780
1086
|
return false;
|
|
781
1087
|
}
|
|
782
1088
|
}
|
|
1089
|
+
/**
|
|
1090
|
+
* --- 发送 ping ---
|
|
1091
|
+
*/
|
|
783
1092
|
async ping() {
|
|
784
1093
|
this.refreshLast();
|
|
785
1094
|
try {
|
|
@@ -789,6 +1098,14 @@ end`;
|
|
|
789
1098
|
return false;
|
|
790
1099
|
}
|
|
791
1100
|
}
|
|
1101
|
+
/**
|
|
1102
|
+
* --- 设置哈希表值 ---
|
|
1103
|
+
* @param key key 名
|
|
1104
|
+
* @param field 字段名
|
|
1105
|
+
* @param val 值
|
|
1106
|
+
* @param mod 空,nx(key不存在才建立)
|
|
1107
|
+
* @param etc
|
|
1108
|
+
*/
|
|
792
1109
|
async hSet(key, field, val, mod, etc) {
|
|
793
1110
|
this.refreshLast();
|
|
794
1111
|
try {
|
|
@@ -806,6 +1123,12 @@ end`;
|
|
|
806
1123
|
return false;
|
|
807
1124
|
}
|
|
808
1125
|
}
|
|
1126
|
+
/**
|
|
1127
|
+
* --- 批量设置哈希值 ---
|
|
1128
|
+
* @param key key 名
|
|
1129
|
+
* @param rows key / val 数组
|
|
1130
|
+
* @param etc
|
|
1131
|
+
*/
|
|
809
1132
|
async hMSet(key, rows, etc) {
|
|
810
1133
|
this.refreshLast();
|
|
811
1134
|
try {
|
|
@@ -822,6 +1145,12 @@ end`;
|
|
|
822
1145
|
return false;
|
|
823
1146
|
}
|
|
824
1147
|
}
|
|
1148
|
+
/**
|
|
1149
|
+
* --- 获取哈希值 ---
|
|
1150
|
+
* @param key
|
|
1151
|
+
* @param field
|
|
1152
|
+
* @param etc
|
|
1153
|
+
*/
|
|
825
1154
|
async hGet(key, field, etc) {
|
|
826
1155
|
this.refreshLast();
|
|
827
1156
|
try {
|
|
@@ -831,6 +1160,12 @@ end`;
|
|
|
831
1160
|
return null;
|
|
832
1161
|
}
|
|
833
1162
|
}
|
|
1163
|
+
/**
|
|
1164
|
+
* --- 获取哈希 json 对象 ---
|
|
1165
|
+
* @param key
|
|
1166
|
+
* @param field
|
|
1167
|
+
* @param etc
|
|
1168
|
+
*/
|
|
834
1169
|
async hGetJson(key, field, etc) {
|
|
835
1170
|
const v = await this.hGet(key, field, etc);
|
|
836
1171
|
if (v === null) {
|
|
@@ -839,6 +1174,12 @@ end`;
|
|
|
839
1174
|
const r = text.parseJson(v);
|
|
840
1175
|
return r === false ? null : v;
|
|
841
1176
|
}
|
|
1177
|
+
/**
|
|
1178
|
+
* --- 批量获取哈希值 ---
|
|
1179
|
+
* @param key
|
|
1180
|
+
* @param fields
|
|
1181
|
+
* @param etc
|
|
1182
|
+
*/
|
|
842
1183
|
async hMGet(key, fields, etc) {
|
|
843
1184
|
this.refreshLast();
|
|
844
1185
|
try {
|
|
@@ -852,6 +1193,11 @@ end`;
|
|
|
852
1193
|
return rtn;
|
|
853
1194
|
}
|
|
854
1195
|
}
|
|
1196
|
+
/**
|
|
1197
|
+
* --- 批量获取哈希键值对 ---
|
|
1198
|
+
* @param key
|
|
1199
|
+
* @param etc
|
|
1200
|
+
*/
|
|
855
1201
|
async hGetAll(key, etc) {
|
|
856
1202
|
this.refreshLast();
|
|
857
1203
|
try {
|
|
@@ -861,6 +1207,12 @@ end`;
|
|
|
861
1207
|
return null;
|
|
862
1208
|
}
|
|
863
1209
|
}
|
|
1210
|
+
/**
|
|
1211
|
+
* --- 删除哈希键 ---
|
|
1212
|
+
* @param key
|
|
1213
|
+
* @param fields 值序列
|
|
1214
|
+
* @param etc
|
|
1215
|
+
*/
|
|
864
1216
|
async hDel(key, fields, etc) {
|
|
865
1217
|
this.refreshLast();
|
|
866
1218
|
try {
|
|
@@ -870,6 +1222,12 @@ end`;
|
|
|
870
1222
|
return 0;
|
|
871
1223
|
}
|
|
872
1224
|
}
|
|
1225
|
+
/**
|
|
1226
|
+
* --- 判断哈希字段是否存在 ---
|
|
1227
|
+
* @param key
|
|
1228
|
+
* @param field
|
|
1229
|
+
* @param etc
|
|
1230
|
+
*/
|
|
873
1231
|
async hExists(key, field, etc) {
|
|
874
1232
|
this.refreshLast();
|
|
875
1233
|
try {
|
|
@@ -879,6 +1237,13 @@ end`;
|
|
|
879
1237
|
return false;
|
|
880
1238
|
}
|
|
881
1239
|
}
|
|
1240
|
+
/**
|
|
1241
|
+
* --- 设置哈希自增自减 ---
|
|
1242
|
+
* @param key key
|
|
1243
|
+
* @param field 字段
|
|
1244
|
+
* @param increment 正数或负数,整数或浮点
|
|
1245
|
+
* @param etc
|
|
1246
|
+
*/
|
|
882
1247
|
async hIncr(key, field, increment, etc) {
|
|
883
1248
|
this.refreshLast();
|
|
884
1249
|
try {
|
|
@@ -893,6 +1258,11 @@ end`;
|
|
|
893
1258
|
return 0;
|
|
894
1259
|
}
|
|
895
1260
|
}
|
|
1261
|
+
/**
|
|
1262
|
+
* --- 获取哈希所有字段 ---
|
|
1263
|
+
* @param key
|
|
1264
|
+
* @param etc
|
|
1265
|
+
*/
|
|
896
1266
|
async hKeys(key, etc) {
|
|
897
1267
|
this.refreshLast();
|
|
898
1268
|
try {
|
|
@@ -979,12 +1349,19 @@ end`;
|
|
|
979
1349
|
}
|
|
980
1350
|
}
|
|
981
1351
|
exports.Connection = Connection;
|
|
1352
|
+
/**
|
|
1353
|
+
* --- 获取 Kv Pool 对象 ---
|
|
1354
|
+
* @param etc 配置信息可留空
|
|
1355
|
+
*/
|
|
982
1356
|
function get(ctr, etc) {
|
|
983
1357
|
if (!etc) {
|
|
984
1358
|
etc = ctr.getPrototype('_config').kv;
|
|
985
1359
|
}
|
|
986
1360
|
return new Pool(ctr, etc);
|
|
987
1361
|
}
|
|
1362
|
+
/**
|
|
1363
|
+
* --- 获取当前连接池中所有连接的信息 ---
|
|
1364
|
+
*/
|
|
988
1365
|
function getConnectionList() {
|
|
989
1366
|
const list = [];
|
|
990
1367
|
for (let i = 0; i < connections.length; ++i) {
|