mm_redis 2.0.7 → 2.0.9

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 (4) hide show
  1. package/README.md +102 -39
  2. package/README_EN.md +433 -0
  3. package/index.js +430 -490
  4. package/package.json +1 -1
package/index.js CHANGED
@@ -1,18 +1,13 @@
1
- /**
2
- * @fileOverview redis缓存帮助类函数
3
- * @author <a href="http://qww.elins.cn">邱文武</a>
4
- * @version 1.5.0
5
- */
6
1
  const {
7
2
  createClient
8
3
  } = require('redis');
9
- const {Event} = require('mm_expand');
4
+ const { Base } = require('mm_expand');
10
5
 
11
- // Redis缓存帮助类函数
12
-
13
- /* 创建Redis帮助类函数 */
14
- /// 构造函数
15
- class Redis extends Event {
6
+ /**
7
+ * @class Redis
8
+ * @description Redis缓存帮助类函数
9
+ */
10
+ class Redis extends Base {
16
11
  /**
17
12
  * @description 创建Redis帮助类函数 (构造函数)
18
13
  * @param {Object} config 配置参数
@@ -39,12 +34,10 @@ class Redis extends Event {
39
34
  // 重试间隔(ms)
40
35
  "retry_delay": 1000,
41
36
  // 连接池配置
42
- "pool": {
43
- "max_clients": 100, // 最大连接数
44
- "min_idle": 5, // 最小空闲连接数
45
- "max_idle": 20, // 最大空闲连接数
46
- "idle_timeout": 30000 // 空闲超时时间(ms)
47
- }
37
+ "max_clients": 100, // 最大连接数
38
+ "min_idle": 5, // 最小空闲连接数
39
+ "max_idle": 20, // 最大空闲连接数
40
+ "idle_timeout": 30000 // 空闲超时时间(ms)
48
41
  };
49
42
  this.setConfig(config);
50
43
  // 初始化连接池
@@ -66,8 +59,9 @@ class Redis extends Event {
66
59
  * 应用键前缀
67
60
  * @param {String} key 键
68
61
  * @return {String} 处理后的键
62
+ * @private
69
63
  */
70
- Redis.prototype.applyPrefix = function(key) {
64
+ Redis.prototype._addPrefix = function (key) {
71
65
  return this.config.prefix ? this.config.prefix + key : key;
72
66
  };
73
67
 
@@ -75,8 +69,9 @@ Redis.prototype.applyPrefix = function(key) {
75
69
  * 移除键前缀
76
70
  * @param {String} key 带前缀的键
77
71
  * @return {String} 处理后的键
72
+ * @private
78
73
  */
79
- Redis.prototype.removePrefix = function(key) {
74
+ Redis.prototype._delPrefix = function (key) {
80
75
  if (this.config.prefix && key.startsWith(this.config.prefix)) {
81
76
  return key.slice(this.config.prefix.length);
82
77
  }
@@ -89,13 +84,21 @@ Redis.prototype.removePrefix = function(key) {
89
84
  * @param {Function} func 操作回调函数,可判断成功与否
90
85
  * @return {Promise<Boolean>} 订阅结果
91
86
  */
92
- Redis.prototype.subscribe = async function(channel, func) {
87
+ Redis.prototype.subscribe = async function (channel, func) {
88
+ // 参数验证
89
+ if (!channel || typeof channel !== 'string') {
90
+ throw new TypeError('channel必须是非空字符串');
91
+ }
92
+ if (typeof func !== 'function') {
93
+ throw new TypeError('func必须是函数');
94
+ }
95
+
93
96
  try {
94
- await this.conn.subscribe(this.applyPrefix(channel));
97
+ await this.conn.subscribe(this._addPrefix(channel));
95
98
  this.func_list[channel] = func;
96
99
  return true;
97
100
  } catch (err) {
98
- this.logger('error',`订阅频道 ${channel} 失败:`, err);
101
+ this.logger('error', `订阅频道 ${channel} 失败:`, err);
99
102
  return false;
100
103
  }
101
104
  };
@@ -105,13 +108,18 @@ Redis.prototype.subscribe = async function(channel, func) {
105
108
  * @param {String} channel 订阅的频道
106
109
  * @return {Promise<Boolean>} 取消订阅结果
107
110
  */
108
- Redis.prototype.unsubscribe = async function(channel) {
111
+ Redis.prototype.unsubscribe = async function (channel) {
112
+ // 参数验证
113
+ if (!channel || typeof channel !== 'string') {
114
+ throw new TypeError('channel必须是非空字符串');
115
+ }
116
+
109
117
  try {
110
- await this.conn.unsubscribe(this.applyPrefix(channel));
118
+ await this.conn.unsubscribe(this._addPrefix(channel));
111
119
  delete this.func_list[channel];
112
120
  return true;
113
121
  } catch (err) {
114
- this.logger('error',`取消订阅频道 ${channel} 失败:`, err);
122
+ this.logger('error', `取消订阅频道 ${channel} 失败:`, err);
115
123
  return false;
116
124
  }
117
125
  };
@@ -122,11 +130,19 @@ Redis.prototype.unsubscribe = async function(channel) {
122
130
  * @param {String} message 订阅的消息
123
131
  * @return {Promise<Number>} 接收消息的客户端数量
124
132
  */
125
- Redis.prototype.publish = async function(channel, message) {
133
+ Redis.prototype.publish = async function (channel, message) {
134
+ // 参数验证
135
+ if (!channel || typeof channel !== 'string') {
136
+ throw new TypeError('channel必须是非空字符串');
137
+ }
138
+ if (message === undefined || message === null) {
139
+ throw new TypeError('message不能为undefined或null');
140
+ }
141
+
126
142
  try {
127
- return await this.conn.publish(this.applyPrefix(channel), message);
143
+ return await this.conn.publish(this._addPrefix(channel), message);
128
144
  } catch (err) {
129
- this.logger('error',`发布消息到频道 ${channel} 失败:`, err);
145
+ this.logger('error', `发布消息到频道 ${channel} 失败:`, err);
130
146
  return false;
131
147
  }
132
148
  };
@@ -136,9 +152,9 @@ Redis.prototype.publish = async function(channel, message) {
136
152
  * @param {String} channel
137
153
  * @param {String} message
138
154
  */
139
- Redis.prototype.main = async function(channel, message) {
155
+ Redis.prototype.main = async function (channel, message) {
140
156
  // 默认实现,可由子类重写
141
- this.logger('debug',`收到未处理的消息 [${channel}]:`, message);
157
+ this.logger('debug', `收到未处理的消息 [${channel}]:`, message);
142
158
  };
143
159
 
144
160
  /**
@@ -146,15 +162,15 @@ Redis.prototype.main = async function(channel, message) {
146
162
  * @param {String} channel 频道
147
163
  * @param {String} message 消息
148
164
  */
149
- Redis.prototype.message = function(channel, message) {
165
+ Redis.prototype.message = function (channel, message) {
150
166
  // 移除前缀
151
- channel = this.removePrefix(channel);
167
+ channel = this._delPrefix(channel);
152
168
  var func = this.func_list[channel];
153
169
  if (func) {
154
170
  try {
155
171
  func(message);
156
172
  } catch (err) {
157
- this.logger('error',`处理频道 ${channel} 的消息时出错:`, err);
173
+ this.logger('error', `处理频道 ${channel} 的消息时出错:`, err);
158
174
  }
159
175
  } else {
160
176
  this.main(channel, message);
@@ -165,55 +181,17 @@ Redis.prototype.message = function(channel, message) {
165
181
  * @description 设置配置
166
182
  * @param {Object} config 配置对象或配置路径
167
183
  */
168
- Redis.prototype.setConfig = function(config) {
184
+ Redis.prototype.setConfig = function (config) {
169
185
  let obj = {};
170
186
  if (typeof config === 'string') {
171
187
  obj = config.loadJson(__dirname);
172
188
  } else if (config) {
173
189
  // 合并配置参数
174
- if (config.pool) {
175
- // 确保连接池配置的合并不会覆盖所有默认值
176
- if (!this.config.pool) {
177
- this.config.pool = {};
178
- }
179
- Object.assign(this.config.pool, config.pool);
180
- // 删除config中的pool属性,避免后续Object.assign重复处理
181
- const configCopy = {
182
- ...config
183
- };
184
- delete configCopy.pool;
185
- Object.assign(this.config, configCopy);
186
- } else {
187
- Object.assign(this.config, config);
188
- }
190
+ Object.assign(this.config, config);
189
191
  } else {
190
192
  return;
191
193
  }
192
194
 
193
- // 确保连接池配置存在且包含所有必要字段
194
- if (!this.config.pool) {
195
- this.config.pool = {};
196
- }
197
-
198
- // 设置默认连接池配置,只覆盖未定义的字段
199
- const defaultPoolConfig = {
200
- max_clients: 100,
201
- min_idle: 5,
202
- max_idle: 20,
203
- idle_timeout: 30000
204
- };
205
-
206
- for (const key in defaultPoolConfig) {
207
- if (this.config.pool[key] === undefined) {
208
- this.config.pool[key] = defaultPoolConfig[key];
209
- }
210
- }
211
-
212
- // 如果连接池已经初始化,更新其配置
213
- if (this.connection_pool) {
214
- this.connection_pool.config = this.config.pool;
215
- }
216
-
217
195
  // 初始化连接池
218
196
  this._initPool();
219
197
  // this.logger('debug','配置已更新');
@@ -222,33 +200,25 @@ Redis.prototype.setConfig = function(config) {
222
200
  };
223
201
 
224
202
  // 添加连接池初始化方法
225
- Redis.prototype._initPool = function() {
203
+ Redis.prototype._initPool = function () {
226
204
  // 确保连接池配置存在
227
- if (!this.config.pool) {
228
- this.config.pool = {
229
- max_clients: 100,
230
- min_idle: 5,
231
- max_idle: 20,
232
- idle_timeout: 30000
233
- };
234
- }
235
205
 
236
206
  // 初始化连接池相关属性
237
207
  this.connection_pool = {
238
208
  available: [],
239
209
  in_use: new Set(),
240
- config: this.config.pool // 直接引用配置对象,确保同步更新
210
+ config: this.config // 直接引用配置对象,确保同步更新
241
211
  };
242
212
 
243
213
  // 记录连接池初始化
244
- // this.logger('debug','连接池已初始化,配置:', this.config.pool);
214
+ // this.logger('debug','连接池已初始化,配置:', this.config);
245
215
  };
246
216
 
247
217
  /**
248
218
  * @description 连接Redis数据库
249
219
  * @return {Promise<Boolean>} 连接结果
250
220
  */
251
- Redis.prototype.open = async function() {
221
+ Redis.prototype.open = async function () {
252
222
  if (this.conn && this.connected) {
253
223
  return true;
254
224
  }
@@ -270,7 +240,7 @@ Redis.prototype.open = async function() {
270
240
 
271
241
  // 监听错误事件
272
242
  this.conn.on('error', (err) => {
273
- this.logger('error','Redis连接错误:', err);
243
+ this.logger('error', 'Redis连接错误:', err);
274
244
  this.connected = false;
275
245
  // 尝试重连
276
246
  this._reconnect();
@@ -291,12 +261,12 @@ Redis.prototype.open = async function() {
291
261
 
292
262
  // 监听订阅成功事件
293
263
  this.conn.on('subscribe', (channel, count) => {
294
- this.logger('debug',`频道${this.removePrefix(channel)}订阅成功, 当前总计订阅数:${count}`);
264
+ this.logger('debug', `频道${this._delPrefix(channel)}订阅成功, 当前总计订阅数:${count}`);
295
265
  });
296
266
 
297
267
  // 监听取消订阅事件
298
268
  this.conn.on('unsubscribe', (channel, count) => {
299
- this.logger('debug',`频道${this.removePrefix(channel)}取消订阅, 当前总计订阅数:${count}`);
269
+ this.logger('debug', `频道${this._delPrefix(channel)}取消订阅, 当前总计订阅数:${count}`);
300
270
  });
301
271
 
302
272
  // 收到消息后执行回调
@@ -309,7 +279,7 @@ Redis.prototype.open = async function() {
309
279
  this.connected = true;
310
280
  return true;
311
281
  } catch (err) {
312
- this.logger('error','Redis连接失败:', err);
282
+ this.logger('error', 'Redis连接失败:', err);
313
283
  // 尝试重连
314
284
  this._reconnect();
315
285
  return false;
@@ -320,20 +290,20 @@ Redis.prototype.open = async function() {
320
290
  * 内部重连方法
321
291
  * @private
322
292
  */
323
- Redis.prototype._reconnect = function() {
293
+ Redis.prototype._reconnect = function () {
324
294
  if (this.retry_count >= this.config.retry_count) {
325
- this.logger('error',`达到最大重连次数 ${this.config.retry_count},停止重连`);
295
+ this.logger('error', `达到最大重连次数 ${this.config.retry_count},停止重连`);
326
296
  return;
327
297
  }
328
298
 
329
299
  this.retry_count++;
330
- this.logger('debug',`尝试第 ${this.retry_count} 次重连...`);
300
+ this.logger('debug', `尝试第 ${this.retry_count} 次重连...`);
331
301
 
332
302
  setTimeout(async () => {
333
303
  try {
334
304
  await this.open();
335
305
  } catch (err) {
336
- this.logger('error',`重连失败:`, err);
306
+ this.logger('error', `重连失败:`, err);
337
307
  }
338
308
  }, this.config.retry_delay);
339
309
  };
@@ -343,7 +313,7 @@ Redis.prototype._reconnect = function() {
343
313
  * @return {Promise<Boolean>} 关闭结果
344
314
  */
345
315
  // 修复close方法,正确处理连接池
346
- Redis.prototype.close = function() {
316
+ Redis.prototype.close = function () {
347
317
  try {
348
318
  // this.logger('debug','关闭所有连接...');
349
319
 
@@ -353,7 +323,7 @@ Redis.prototype.close = function() {
353
323
  try {
354
324
  connection.close();
355
325
  } catch (e) {
356
- this.logger('error','关闭连接失败:', e);
326
+ this.logger('error', '关闭连接失败:', e);
357
327
  }
358
328
  }
359
329
  this.connection_pool.in_use.clear();
@@ -370,68 +340,68 @@ Redis.prototype.close = function() {
370
340
  // 某些Redis客户端可能使用end方法
371
341
  connection.end();
372
342
  } else {
373
- this.logger('debug','连接对象没有可用的关闭方法');
343
+ this.logger('debug', '连接对象没有可用的关闭方法');
374
344
  }
375
345
  } catch (e) {
376
- this.logger('error','关闭连接失败:', e);
346
+ this.logger('error', '关闭连接失败:', e);
377
347
  }
378
348
  }
379
349
  this.connection_pool.available = [];
380
350
  }
381
351
 
382
352
  this.connected = false;
383
- this.logger('debug','Redis连接已断开');
353
+ this.logger('debug', 'Redis连接已断开');
384
354
  return Promise.resolve(true);
385
355
  } catch (error) {
386
- this.logger('error','关闭连接时发生错误:', error);
356
+ this.logger('error', '关闭连接时发生错误:', error);
387
357
  return Promise.resolve(false);
388
358
  }
389
359
  };
390
360
 
391
361
  // 添加Pipeline支持
392
- Redis.prototype.pipeline = function() {
362
+ Redis.prototype.pipeline = function () {
393
363
  const redisInstance = this;
394
364
  const commands = [];
395
365
 
396
366
  return {
397
367
  // 设置值
398
- set: function(key, value, seconds) {
368
+ set: function (key, value, seconds) {
399
369
  commands.push(['set', key, value, seconds]);
400
370
  return this;
401
371
  },
402
372
 
403
373
  // 获取值
404
- get: function(key) {
374
+ get: function (key) {
405
375
  commands.push(['get', key]);
406
376
  return this;
407
377
  },
408
378
 
409
379
  // 删除
410
- del: function(key) {
380
+ del: function (key) {
411
381
  commands.push(['del', key]);
412
382
  return this;
413
383
  },
414
384
 
415
385
  // 判断存在
416
- has: function(key) {
386
+ has: function (key) {
417
387
  commands.push(['has', key]);
418
388
  return this;
419
389
  },
420
390
 
421
391
  // 设置哈希字段
422
- hset: function(key, field, value) {
392
+ hset: function (key, field, value) {
423
393
  commands.push(['hset', key, field, value]);
424
394
  return this;
425
395
  },
426
396
 
427
397
  // 过期时间设置
428
- expire: function(key, seconds) {
398
+ expire: function (key, seconds) {
429
399
  commands.push(['expire', key, seconds]);
430
400
  return this;
431
401
  },
432
402
 
433
403
  // 执行所有命令
434
- exec: async function() {
404
+ exec: async function () {
435
405
  try {
436
406
  // 使用Redis实例的方法获取连接
437
407
  const connection = await redisInstance._getConnection();
@@ -448,7 +418,7 @@ Redis.prototype.pipeline = function() {
448
418
  case 'set': {
449
419
  // 键前缀处理和值序列化
450
420
  const [key, value, seconds] = args;
451
- const prefixedKey = redisInstance.applyPrefix(key);
421
+ const prefixedKey = redisInstance._addPrefix(key);
452
422
  let processedValue = value;
453
423
  if (typeof value === 'object' && value !== null) {
454
424
  processedValue = JSON.stringify(value);
@@ -462,7 +432,7 @@ Redis.prototype.pipeline = function() {
462
432
  }
463
433
  case 'get': {
464
434
  const [key] = args;
465
- const prefixedKey = redisInstance.applyPrefix(key);
435
+ const prefixedKey = redisInstance._addPrefix(key);
466
436
  result = await connection.get(prefixedKey);
467
437
  // 尝试解析JSON
468
438
  if (typeof result === 'string') {
@@ -479,25 +449,25 @@ Redis.prototype.pipeline = function() {
479
449
  }
480
450
  case 'del': {
481
451
  const [key] = args;
482
- const prefixedKey = redisInstance.applyPrefix(key);
452
+ const prefixedKey = redisInstance._addPrefix(key);
483
453
  result = await connection.del(prefixedKey);
484
454
  break;
485
455
  }
486
456
  case 'has': {
487
457
  const [key] = args;
488
- const prefixedKey = redisInstance.applyPrefix(key);
458
+ const prefixedKey = redisInstance._addPrefix(key);
489
459
  result = await connection.exists(prefixedKey) > 0;
490
460
  break;
491
461
  }
492
462
  case 'hset': {
493
463
  const [key, field, value] = args;
494
- const prefixedKey = redisInstance.applyPrefix(key);
464
+ const prefixedKey = redisInstance._addPrefix(key);
495
465
  result = await connection.hSet(prefixedKey, field, value);
496
466
  break;
497
467
  }
498
468
  case 'expire': {
499
469
  const [key, seconds] = args;
500
- const prefixedKey = redisInstance.applyPrefix(key);
470
+ const prefixedKey = redisInstance._addPrefix(key);
501
471
  result = await connection.expire(prefixedKey, seconds);
502
472
  break;
503
473
  }
@@ -510,7 +480,7 @@ Redis.prototype.pipeline = function() {
510
480
  // 直接返回结果值,符合测试期望的格式
511
481
  results.push(result);
512
482
  } catch (err) {
513
- this.logger('error',`Pipeline命令执行失败 [${cmd[0]}]:`, err);
483
+ this.logger('error', `Pipeline命令执行失败 [${cmd[0]}]:`, err);
514
484
  results.push(null);
515
485
  }
516
486
  }
@@ -519,7 +489,7 @@ Redis.prototype.pipeline = function() {
519
489
  redisInstance._recycleConnection(connection);
520
490
  return results;
521
491
  } catch (error) {
522
- this.logger('error','Pipeline执行失败:', error);
492
+ this.logger('error', 'Pipeline执行失败:', error);
523
493
  return [];
524
494
  }
525
495
  }
@@ -530,27 +500,27 @@ Redis.prototype.pipeline = function() {
530
500
  * @description 检查连接状态
531
501
  * @return {Boolean} 连接状态
532
502
  */
533
- Redis.prototype.isConnected = function() {
503
+ Redis.prototype.isConnected = function () {
534
504
  return this.connected && this.conn && this.conn.isReady;
535
505
  };
536
506
 
537
- /**
538
- * @description 查询或是设置缓存过期时间
539
- * @param {String} key 键
540
- * @param {Number} seconds 秒, 为空则查询有效时长
541
- * @return {Promise<Number|Boolean>} 查询时长或设置结果, 时长为-1表示永不过期
542
- */
543
507
  /**
544
508
  * 获取或设置键的过期时间
545
509
  * @param {String} key 键名
546
510
  * @param {Number} [seconds] 过期时间(秒),不传此参数则获取剩余过期时间
547
511
  * @return {Promise<Number|Boolean>} 不传seconds时返回剩余过期时间(-1表示无过期设置,-2表示已过期),传seconds时返回是否设置成功
548
512
  */
549
- Redis.prototype.ttl = async function(key, seconds) {
513
+ Redis.prototype.ttl = async function (key, seconds) {
514
+ // 参数验证
515
+ if (!key || typeof key !== 'string') {
516
+ throw new TypeError('key必须是非空字符串');
517
+ }
518
+
550
519
  try {
520
+
551
521
  const connection = await this._getConnection();
552
522
  try {
553
- key = this.applyPrefix(key);
523
+ key = this._addPrefix(key);
554
524
 
555
525
  // 如果传入了seconds参数,则设置过期时间
556
526
  if (seconds !== undefined) {
@@ -566,7 +536,7 @@ Redis.prototype.ttl = async function(key, seconds) {
566
536
  await connection.execute('EXPIRE', [key, seconds]);
567
537
  return true;
568
538
  } else {
569
- this.logger('error','连接对象不支持expire操作');
539
+ this.logger('error', '连接对象不支持expire操作');
570
540
  return false;
571
541
  }
572
542
  }
@@ -577,14 +547,14 @@ Redis.prototype.ttl = async function(key, seconds) {
577
547
  } else if (typeof connection.execute === 'function') {
578
548
  return await connection.execute('TTL', [key]);
579
549
  } else {
580
- this.logger('error','连接对象不支持ttl操作');
550
+ this.logger('error', '连接对象不支持ttl操作');
581
551
  return -2; // 默认返回键不存在
582
552
  }
583
553
  } finally {
584
554
  this._recycleConnection(connection);
585
555
  }
586
556
  } catch (err) {
587
- this.logger('error',`${seconds !== undefined ? '设置' : '获取'}过期时间失败 [${key}]:`, err);
557
+ this.logger('error', `${seconds !== undefined ? '设置' : '获取'}过期时间失败 [${key}]:`, err);
588
558
  return seconds !== undefined ? false : -2;
589
559
  }
590
560
  };
@@ -596,17 +566,16 @@ Redis.prototype.ttl = async function(key, seconds) {
596
566
  * @param {Number} [seconds=0] 过期时间(秒)
597
567
  * @return {Promise<Number>} 计算后的结果
598
568
  */
599
- Redis.prototype.addInt = async function(key, num, seconds = 0) {
569
+ Redis.prototype.addInt = async function (key, num, seconds = 0) {
570
+ // 参数验证
571
+ if (!key || typeof key !== 'string') {
572
+ throw new TypeError('key必须是非空字符串');
573
+ }
574
+ if (typeof num !== 'number' || isNaN(num) || !Number.isInteger(num)) {
575
+ throw new TypeError('num必须是有效整数');
576
+ }
577
+
600
578
  try {
601
- // 参数验证
602
- if (!key || typeof key !== 'string') {
603
- this.logger('error','参数错误: 键必须是非空字符串');
604
- return false;
605
- }
606
- if (typeof num !== 'number' || isNaN(num) || !Number.isInteger(num)) {
607
- this.logger('error','参数错误: 数值必须是有效整数');
608
- return false;
609
- }
610
579
 
611
580
  // 触发前置事件,允许修改或阻止操作
612
581
  const ctx = {
@@ -619,7 +588,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
619
588
 
620
589
  // 如果事件处理器标记为取消,则返回false
621
590
  if (ctx.cancel) {
622
- this.logger('debug',`addInt操作被事件处理器取消 [${key}]`);
591
+ this.logger('debug', `addInt操作被事件处理器取消 [${key}]`);
623
592
  return false;
624
593
  }
625
594
 
@@ -632,7 +601,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
632
601
 
633
602
  const connection = await this._getConnection();
634
603
  try {
635
- const prefixedKey = this.applyPrefix(finalKey);
604
+ const prefixedKey = this._addPrefix(finalKey);
636
605
  let result;
637
606
  if (finalNum > 0) {
638
607
  // 增加整数值
@@ -642,7 +611,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
642
611
  result = await connection.execute('INCRBY', [prefixedKey, finalNum]);
643
612
  } else {
644
613
  // 如果出错,回退到先获取再设置的方式
645
- throw new Error(`使用原子操作增加整数值失败,使用回退方案`);
614
+ this.logger('warn', `使用原子操作增加整数值失败,使用回退方案 [${key}]`);
646
615
  let value = await this.get(prefixedKey);
647
616
  let n = 0;
648
617
  if (value) {
@@ -660,7 +629,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
660
629
  result = await connection.execute('DECRBY', [prefixedKey, -finalNum]);
661
630
  } else {
662
631
  // 如果出错,回退到先获取再设置的方式
663
- throw new Error(`使用原子操作减少整数值失败,使用回退方案`);
632
+ this.logger('warn', `使用原子操作减少整数值失败,使用回退方案`);
664
633
  let value = await this.get(prefixedKey);
665
634
  let n = 0;
666
635
  if (value) {
@@ -701,7 +670,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
701
670
  seconds,
702
671
  error: err
703
672
  });
704
- this.logger('error',`增加整数值失败 [${key}]:`, err);
673
+ this.logger('error', `增加整数值失败 [${key}]:`, err);
705
674
  // 与CacheBase保持一致,错误时返回默认值而不是抛出异常
706
675
  return 0;
707
676
  }
@@ -714,20 +683,19 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
714
683
  * @param {Number} seconds 过期时间,0表示永不过期
715
684
  * @return {Promise<Number>} 计算后的结果
716
685
  */
717
- Redis.prototype.addFloat = async function(key, num, seconds = 0) {
718
- try {
719
- // 参数验证
720
- if (!key || typeof key !== 'string') {
721
- throw new Error(`addFloat: key参数必须是非空字符串`);
722
- return 0;
723
- }
686
+ Redis.prototype.addFloat = async function (key, num, seconds = 0) {
687
+ // 参数验证
688
+ if (!key || typeof key !== 'string') {
689
+ throw new TypeError('key必须是非空字符串');
690
+ }
724
691
 
725
- // 验证num参数
726
- num = parseFloat(num);
727
- if (isNaN(num)) {
728
- throw new Error(`addFloat: num参数必须是有效的数值`);
729
- return 0;
730
- }
692
+ // 验证num参数
693
+ num = parseFloat(num);
694
+ if (isNaN(num)) {
695
+ throw new TypeError('num必须是有效的数值');
696
+ }
697
+
698
+ try {
731
699
 
732
700
  // 触发前置事件,允许修改或阻止操作
733
701
  const ctx = {
@@ -740,7 +708,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
740
708
 
741
709
  // 如果事件处理器标记为取消,则返回默认值
742
710
  if (ctx.cancel) {
743
- this.logger('debug',`addFloat操作被事件处理器取消 [${key}]`);
711
+ this.logger('debug', `addFloat操作被事件处理器取消 [${key}]`);
744
712
  return 0;
745
713
  }
746
714
 
@@ -754,7 +722,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
754
722
 
755
723
  const connection = await this._getConnection();
756
724
  try {
757
- const prefixedKey = this.applyPrefix(finalKey);
725
+ const prefixedKey = this._addPrefix(finalKey);
758
726
 
759
727
  // 优先使用原生方法
760
728
  if (typeof connection.incrByFloat === 'function') {
@@ -766,7 +734,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
766
734
  }
767
735
  // 回退方案
768
736
  else {
769
- throw new Error(`连接对象不支持incrByFloat操作,使用回退方案`);
737
+ this.logger('warn', `连接对象不支持incrByFloat操作,使用回退方案`);
770
738
  let value = await this.get(prefixedKey);
771
739
  let n = 0;
772
740
  if (value) {
@@ -797,7 +765,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
797
765
  return result;
798
766
  } catch (err) {
799
767
  // 如果出错,回退到先获取再设置的方式
800
- throw new Error(`使用原子操作增加浮点数值失败,使用回退方案:`, err);
768
+ this.logger('warn', `使用原子操作增加浮点数值失败,使用回退方案:`, err);
801
769
  let value = await this.get(prefixedKey);
802
770
  let n = 0;
803
771
  if (value) {
@@ -826,7 +794,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
826
794
  seconds,
827
795
  error: err
828
796
  });
829
- this.logger('error',`增加浮点数值失败 [${key}]:`, err);
797
+ this.logger('error', `增加浮点数值失败 [${key}]:`, err);
830
798
  // 与CacheBase保持一致,错误时返回默认值而不是抛出异常
831
799
  return 0;
832
800
  }
@@ -839,18 +807,16 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
839
807
  * @param {Number} seconds 过期时间,0表示永不过期
840
808
  * @return {Promise<Number>} 添加后的字符串长度
841
809
  */
842
- Redis.prototype.addStr = async function(key, str, seconds = 0) {
810
+ Redis.prototype.addStr = async function (key, str, seconds = 0) {
843
811
  try {
844
812
  // 参数验证
845
813
  if (!key || typeof key !== 'string') {
846
- throw new Error(`addStr: key参数必须是非空字符串`);
847
- return 0;
814
+ throw new TypeError('key必须是非空字符串');
848
815
  }
849
816
 
850
817
  // 验证str参数
851
818
  if (str === undefined || str === null) {
852
- throw new Error(`addStr: str参数不能为undefined或null`);
853
- return 0;
819
+ throw new TypeError('str不能为undefined或null');
854
820
  }
855
821
  str = String(str);
856
822
 
@@ -865,7 +831,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
865
831
 
866
832
  // 如果事件处理器标记为取消,则返回0
867
833
  if (ctx.cancel) {
868
- this.logger('debug',`addStr操作被事件处理器取消 [${key}]`);
834
+ this.logger('debug', `addStr操作被事件处理器取消 [${key}]`);
869
835
  return 0;
870
836
  }
871
837
 
@@ -879,7 +845,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
879
845
 
880
846
  const connection = await this._getConnection();
881
847
  try {
882
- const prefixedKey = this.applyPrefix(finalKey);
848
+ const prefixedKey = this._addPrefix(finalKey);
883
849
 
884
850
  // 优先使用原生方法
885
851
  if (typeof connection.append === 'function') {
@@ -891,7 +857,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
891
857
  }
892
858
  // 回退方案
893
859
  else {
894
- throw new Error(`连接对象不支持append操作,使用回退方案`);
860
+ this.logger('warn', `连接对象不支持append操作,使用回退方案`);
895
861
  let value = await this.get(prefixedKey) || '';
896
862
  value += finalStr;
897
863
  await this.set(prefixedKey, value, finalSeconds);
@@ -917,7 +883,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
917
883
  });
918
884
  return result;
919
885
  } catch (err) {
920
- this.logger('error','追加字符串失败:', err);
886
+ this.logger('warn', '追加字符串失败:', err);
921
887
  // 回退方案:先获取再设置
922
888
  let value = await this.get(prefixedKey) || '';
923
889
  value += finalStr;
@@ -943,7 +909,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
943
909
  seconds,
944
910
  error: err
945
911
  });
946
- this.logger('error',`追加字符串失败 [${key}]:`, err);
912
+ this.logger('error', `追加字符串失败 [${key}]:`, err);
947
913
  return 0;
948
914
  }
949
915
  };
@@ -953,32 +919,28 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
953
919
  * @param {String|Array<String>} key 键或键数组
954
920
  * @return {Promise<Boolean>} 成功返回true,失败返回false
955
921
  */
956
- Redis.prototype.del = async function(key) {
922
+ Redis.prototype.del = async function (key) {
957
923
  try {
958
924
  // 键名必须是非空字符串或非空字符串数组
959
925
  if (!key) {
960
- throw new Error('del: key必须是非空字符串或非空字符串数组');
961
- return false;
926
+ throw new TypeError('key必须是非空字符串或非空字符串数组');
962
927
  }
963
928
 
964
929
  // 如果是单个键,验证是否为非空字符串
965
930
  if (typeof key === 'string') {
966
931
  if (!key) {
967
- throw new Error('del: key必须是非空字符串');
968
- return false;
932
+ throw new TypeError('key必须是非空字符串');
969
933
  }
970
934
  }
971
935
  // 如果是数组,验证每个元素是否为非空字符串
972
936
  else if (Array.isArray(key)) {
973
937
  for (const k of key) {
974
938
  if (!k || typeof k !== 'string') {
975
- throw new Error('del: key必须是非空字符串或非空字符串数组');
976
- return false;
939
+ throw new TypeError('key必须是非空字符串或非空字符串数组');
977
940
  }
978
941
  }
979
942
  } else {
980
- throw new Error('del: key必须是非空字符串或非空字符串数组');
981
- return false;
943
+ throw new TypeError('key必须是非空字符串或非空字符串数组');
982
944
  }
983
945
 
984
946
  // 触发前置事件,允许修改或阻止操作
@@ -990,7 +952,7 @@ Redis.prototype.del = async function(key) {
990
952
 
991
953
  // 如果事件处理器标记为取消,则返回false
992
954
  if (ctx.cancel) {
993
- this.logger('debug',`del操作被事件处理器取消 [${key}]`);
955
+ this.logger('debug', `del操作被事件处理器取消 [${key}]`);
994
956
  return false;
995
957
  }
996
958
 
@@ -1007,7 +969,7 @@ Redis.prototype.del = async function(key) {
1007
969
  success = true;
1008
970
  const deleted = [];
1009
971
  for (const k of finalKey) {
1010
- const prefixedKey = this.applyPrefix(k);
972
+ const prefixedKey = this._addPrefix(k);
1011
973
  const result = await connection.del(prefixedKey);
1012
974
  if (result !== 1) {
1013
975
  success = false;
@@ -1023,7 +985,7 @@ Redis.prototype.del = async function(key) {
1023
985
  });
1024
986
  } else {
1025
987
  // 单个删除
1026
- const prefixedKey = this.applyPrefix(finalKey);
988
+ const prefixedKey = this._addPrefix(finalKey);
1027
989
  const n = await connection.del(prefixedKey);
1028
990
  success = n === 1;
1029
991
  // 触发后置事件
@@ -1043,7 +1005,7 @@ Redis.prototype.del = async function(key) {
1043
1005
  key,
1044
1006
  error: err
1045
1007
  });
1046
- this.logger('error',`删除缓存失败 [${key}]:`, err);
1008
+ this.logger('error', `删除缓存失败 [${key}]:`, err);
1047
1009
  return false;
1048
1010
  }
1049
1011
  };
@@ -1062,12 +1024,11 @@ Redis.prototype.del = async function(key) {
1062
1024
  * @param {Number} seconds 过期时间,0表示永不过期
1063
1025
  * @return {Promise<Boolean>} 成功返回true,失败返回false
1064
1026
  */
1065
- Redis.prototype.set = async function(key, value, seconds = 0) {
1027
+ Redis.prototype.set = async function (key, value, seconds = 0) {
1066
1028
  try {
1067
1029
  // 键名必须是非空字符串
1068
1030
  if (!key || typeof key !== 'string') {
1069
- this.logger('error','参数错误: key必须是非空字符串');
1070
- return false;
1031
+ throw new TypeError('key必须是非空字符串');
1071
1032
  }
1072
1033
 
1073
1034
  // 触发前置事件,允许修改或阻止操作
@@ -1081,7 +1042,7 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
1081
1042
 
1082
1043
  // 如果事件处理器标记为取消,则返回false
1083
1044
  if (ctx.cancel) {
1084
- this.logger('debug',`set操作被事件处理器取消 [${key}]`);
1045
+ this.logger('debug', `set操作被事件处理器取消 [${key}]`);
1085
1046
  return false;
1086
1047
  }
1087
1048
 
@@ -1094,8 +1055,8 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
1094
1055
 
1095
1056
  const connection = await this._getConnection();
1096
1057
  try {
1097
- const prefixedKey = this.applyPrefix(finalKey);
1098
- if (typeof(finalValue) === "object") {
1058
+ const prefixedKey = this._addPrefix(finalKey);
1059
+ if (typeof (finalValue) === "object") {
1099
1060
  finalValue = JSON.stringify(finalValue);
1100
1061
  }
1101
1062
 
@@ -1119,7 +1080,7 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
1119
1080
  this._recycleConnection(connection);
1120
1081
  }
1121
1082
  } catch (err) {
1122
- this.logger('error',`设置缓存失败 [${key}]:`, err);
1083
+ this.logger('error', `设置缓存失败 [${key}]:`, err);
1123
1084
  // 触发错误事件
1124
1085
  this.emit('set:error', {
1125
1086
  key,
@@ -1138,8 +1099,16 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
1138
1099
  * @param {Number} seconds 过期时间(秒)
1139
1100
  * @return {Promise<Boolean>} 操作结果,键不存在时设置成功返回true,键已存在返回false
1140
1101
  */
1141
- Redis.prototype.add = async function(key, value, seconds) {
1102
+ Redis.prototype.add = async function (key, value, seconds) {
1142
1103
  try {
1104
+ // 参数验证
1105
+ if (!key || typeof key !== 'string') {
1106
+ throw new TypeError('key必须是非空字符串');
1107
+ }
1108
+ if (value === undefined || value === null) {
1109
+ throw new TypeError('value不能为undefined或null');
1110
+ }
1111
+
1143
1112
  // 触发前置事件,允许修改或阻止操作
1144
1113
  const ctx = {
1145
1114
  key,
@@ -1151,7 +1120,7 @@ Redis.prototype.add = async function(key, value, seconds) {
1151
1120
 
1152
1121
  // 如果事件处理器标记为取消,则返回false
1153
1122
  if (ctx.cancel) {
1154
- this.logger('debug',`add操作被事件处理器取消 [${key}]`);
1123
+ this.logger('debug', `add操作被事件处理器取消 [${key}]`);
1155
1124
  return false;
1156
1125
  }
1157
1126
 
@@ -1164,8 +1133,8 @@ Redis.prototype.add = async function(key, value, seconds) {
1164
1133
 
1165
1134
  const connection = await this._getConnection();
1166
1135
  try {
1167
- finalKey = this.applyPrefix(finalKey);
1168
- if (typeof(finalValue) === "object") {
1136
+ finalKey = this._addPrefix(finalKey);
1137
+ if (typeof (finalValue) === "object") {
1169
1138
  finalValue = JSON.stringify(finalValue);
1170
1139
  }
1171
1140
 
@@ -1191,7 +1160,7 @@ Redis.prototype.add = async function(key, value, seconds) {
1191
1160
  this._recycleConnection(connection);
1192
1161
  }
1193
1162
  } catch (err) {
1194
- this.logger('error',`添加缓存失败 [${key}]:`, err);
1163
+ this.logger('error', `添加缓存失败 [${key}]:`, err);
1195
1164
  // 触发错误事件
1196
1165
  this.emit('add:error', {
1197
1166
  key,
@@ -1213,12 +1182,11 @@ Redis.prototype.add = async function(key, value, seconds) {
1213
1182
  * @param {String} key 键
1214
1183
  * @return {Promise<*>} 获取到的值,不存在时返回null
1215
1184
  */
1216
- Redis.prototype.get = async function(key) {
1185
+ Redis.prototype.get = async function (key) {
1217
1186
  try {
1218
1187
  // 参数验证
1219
1188
  if (!key || typeof key !== 'string') {
1220
- throw new Error(`get: key参数必须是非空字符串`);
1221
- return null;
1189
+ throw new TypeError('key必须是非空字符串');
1222
1190
  }
1223
1191
 
1224
1192
  // 触发前置事件,允许修改或阻止操作
@@ -1230,7 +1198,7 @@ Redis.prototype.get = async function(key) {
1230
1198
 
1231
1199
  // 如果事件处理器标记为取消,则返回null
1232
1200
  if (ctx.cancel) {
1233
- this.logger('debug',`get操作被事件处理器取消 [${key}]`);
1201
+ this.logger('debug', `get操作被事件处理器取消 [${key}]`);
1234
1202
  return null;
1235
1203
  }
1236
1204
 
@@ -1239,10 +1207,10 @@ Redis.prototype.get = async function(key) {
1239
1207
 
1240
1208
  const connection = await this._getConnection();
1241
1209
  try {
1242
- const prefixedKey = this.applyPrefix(finalKey);
1210
+ const prefixedKey = this._addPrefix(finalKey);
1243
1211
  const value = await connection.get(prefixedKey);
1244
1212
  let result = value;
1245
- if (typeof(value) === "string") {
1213
+ if (typeof (value) === "string") {
1246
1214
  // 尝试解析JSON
1247
1215
  try {
1248
1216
  if (value.startsWith("{") && value.endsWith("}") ||
@@ -1271,7 +1239,7 @@ Redis.prototype.get = async function(key) {
1271
1239
  key,
1272
1240
  error: err
1273
1241
  });
1274
- this.logger('error',`获取缓存失败 [${key}]:`, err);
1242
+ this.logger('error', `获取缓存失败 [${key}]:`, err);
1275
1243
  return null;
1276
1244
  }
1277
1245
  };
@@ -1281,12 +1249,11 @@ Redis.prototype.get = async function(key) {
1281
1249
  * @param {String} key 键
1282
1250
  * @return {Promise<Boolean>} 有返回true, 没有返回false
1283
1251
  */
1284
- Redis.prototype.has = async function(key) {
1252
+ Redis.prototype.has = async function (key) {
1285
1253
  try {
1286
1254
  // 参数验证
1287
1255
  if (!key || typeof key !== 'string') {
1288
- throw new Error(`has: key参数必须是非空字符串`);
1289
- return false;
1256
+ throw new TypeError('key必须是非空字符串');
1290
1257
  }
1291
1258
 
1292
1259
  // 触发前置事件,允许修改或阻止操作
@@ -1298,7 +1265,7 @@ Redis.prototype.has = async function(key) {
1298
1265
 
1299
1266
  // 如果事件处理器标记为取消,则返回false
1300
1267
  if (ctx.cancel) {
1301
- this.logger('debug',`has操作被事件处理器取消 [${key}]`);
1268
+ this.logger('debug', `has操作被事件处理器取消 [${key}]`);
1302
1269
  return false;
1303
1270
  }
1304
1271
 
@@ -1307,7 +1274,7 @@ Redis.prototype.has = async function(key) {
1307
1274
 
1308
1275
  const connection = await this._getConnection();
1309
1276
  try {
1310
- const prefixedKey = this.applyPrefix(finalKey);
1277
+ const prefixedKey = this._addPrefix(finalKey);
1311
1278
  const result = await connection.exists(prefixedKey);
1312
1279
  const exists = result > 0;
1313
1280
 
@@ -1328,7 +1295,7 @@ Redis.prototype.has = async function(key) {
1328
1295
  key,
1329
1296
  error: err
1330
1297
  });
1331
- this.logger('error',`检查键是否存在失败 [${key}]:`, err);
1298
+ this.logger('error', `检查键是否存在失败 [${key}]:`, err);
1332
1299
  return false;
1333
1300
  }
1334
1301
  };
@@ -1338,32 +1305,28 @@ Redis.prototype.has = async function(key) {
1338
1305
  * @param {String|Array<String>} key 键或键数组
1339
1306
  * @return {Promise<Number>} 存在的键数量
1340
1307
  */
1341
- Redis.prototype.exists = async function(key) {
1308
+ Redis.prototype.exists = async function (key) {
1342
1309
  try {
1343
1310
  // 参数验证
1344
1311
  if (!key) {
1345
- throw new Error('exists: key必须是非空字符串或非空字符串数组');
1346
- return 0;
1312
+ throw new TypeError('key必须是非空字符串或非空字符串数组');
1347
1313
  }
1348
1314
 
1349
1315
  // 如果是单个键,验证是否为非空字符串
1350
1316
  if (typeof key === 'string') {
1351
1317
  if (!key) {
1352
- throw new Error('exists: key必须是非空字符串');
1353
- return 0;
1318
+ throw new TypeError('key必须是非空字符串');
1354
1319
  }
1355
1320
  }
1356
1321
  // 如果是数组,验证每个元素是否为非空字符串
1357
1322
  else if (Array.isArray(key)) {
1358
1323
  for (const k of key) {
1359
1324
  if (!k || typeof k !== 'string') {
1360
- throw new Error('exists: key必须是非空字符串或非空字符串数组');
1361
- return 0;
1325
+ throw new TypeError('key必须是非空字符串或非空字符串数组');
1362
1326
  }
1363
1327
  }
1364
1328
  } else {
1365
- throw new Error('exists: key必须是非空字符串或非空字符串数组');
1366
- return 0;
1329
+ throw new TypeError('key必须是非空字符串或非空字符串数组');
1367
1330
  }
1368
1331
 
1369
1332
  // 触发前置事件,允许修改或阻止操作
@@ -1375,7 +1338,7 @@ Redis.prototype.exists = async function(key) {
1375
1338
 
1376
1339
  // 如果事件处理器标记为取消,则返回0
1377
1340
  if (ctx.cancel) {
1378
- this.logger('debug',`exists操作被事件处理器取消 [${key}]`);
1341
+ this.logger('debug', `exists操作被事件处理器取消 [${key}]`);
1379
1342
  return 0;
1380
1343
  }
1381
1344
 
@@ -1386,10 +1349,10 @@ Redis.prototype.exists = async function(key) {
1386
1349
  try {
1387
1350
  let result;
1388
1351
  if (typeof finalKey === 'string') {
1389
- const prefixedKey = this.applyPrefix(finalKey);
1352
+ const prefixedKey = this._addPrefix(finalKey);
1390
1353
  result = await connection.exists(prefixedKey);
1391
1354
  } else if (Array.isArray(finalKey)) {
1392
- const prefixedKeys = finalKey.map(k => this.applyPrefix(k));
1355
+ const prefixedKeys = finalKey.map(k => this._addPrefix(k));
1393
1356
  result = await connection.exists(...prefixedKeys);
1394
1357
  }
1395
1358
 
@@ -1410,7 +1373,7 @@ Redis.prototype.exists = async function(key) {
1410
1373
  key,
1411
1374
  error: err
1412
1375
  });
1413
- this.logger('error',`检查键存在性失败 [${key}]:`, err);
1376
+ this.logger('error', `检查键存在性失败 [${key}]:`, err);
1414
1377
  return 0;
1415
1378
  }
1416
1379
  };
@@ -1422,14 +1385,14 @@ Redis.prototype.exists = async function(key) {
1422
1385
  * @param {Number} end 结束位置
1423
1386
  * @return {Promise<String>} 查询值
1424
1387
  */
1425
- Redis.prototype.getrange = async function(key, start, end) {
1388
+ Redis.prototype.getrange = async function (key, start, end) {
1426
1389
  try {
1427
1390
  const connection = await this._getConnection();
1428
1391
  try {
1429
- key = this.applyPrefix(key);
1392
+ key = this._addPrefix(key);
1430
1393
  return await connection.getRange(key, start, end);
1431
1394
  } catch (err) {
1432
- this.logger('error','获取字符串范围失败:', err);
1395
+ this.logger('error', '获取字符串范围失败:', err);
1433
1396
  // 回退方案
1434
1397
  const value = await this.get(key);
1435
1398
  if (value && typeof value === 'string') {
@@ -1440,7 +1403,7 @@ Redis.prototype.getrange = async function(key, start, end) {
1440
1403
  this._recycleConnection(connection);
1441
1404
  }
1442
1405
  } catch (err) {
1443
- this.logger('error',`获取字符串范围失败 [${key}]:`, err);
1406
+ this.logger('error', `获取字符串范围失败 [${key}]:`, err);
1444
1407
  return '';
1445
1408
  }
1446
1409
  };
@@ -1452,7 +1415,7 @@ Redis.prototype.getrange = async function(key, start, end) {
1452
1415
  * @param {String} str_sub 插入的字符串
1453
1416
  * @return {String} 插入后的字符串
1454
1417
  */
1455
- Redis.prototype.insertString = function(str, index, str_sub) {
1418
+ Redis.prototype.insertString = function (str, index, str_sub) {
1456
1419
  return str.substring(0, index) + str_sub + str.substring(index);
1457
1420
  };
1458
1421
 
@@ -1463,7 +1426,7 @@ Redis.prototype.insertString = function(str, index, str_sub) {
1463
1426
  * @param {String} value 变更的值
1464
1427
  * @return {Promise<Number>} 字符串长度
1465
1428
  */
1466
- Redis.prototype.setrange = async function(key, index, value) {
1429
+ Redis.prototype.setrange = async function (key, index, value) {
1467
1430
  try {
1468
1431
  // 触发前置事件,允许修改或阻止操作
1469
1432
  const ctx = {
@@ -1476,7 +1439,7 @@ Redis.prototype.setrange = async function(key, index, value) {
1476
1439
 
1477
1440
  // 如果事件处理器标记为取消,则返回0
1478
1441
  if (ctx.cancel) {
1479
- this.logger('debug',`setrange操作被事件处理器取消 [${key}]`);
1442
+ this.logger('debug', `setrange操作被事件处理器取消 [${key}]`);
1480
1443
  return 0;
1481
1444
  }
1482
1445
 
@@ -1490,7 +1453,7 @@ Redis.prototype.setrange = async function(key, index, value) {
1490
1453
 
1491
1454
  const connection = await this._getConnection();
1492
1455
  try {
1493
- finalKey = this.applyPrefix(finalKey);
1456
+ finalKey = this._addPrefix(finalKey);
1494
1457
 
1495
1458
  // 优先使用原生方法
1496
1459
  if (typeof connection.setRange === 'function') {
@@ -1502,7 +1465,7 @@ Redis.prototype.setrange = async function(key, index, value) {
1502
1465
  }
1503
1466
  // 回退方案
1504
1467
  else {
1505
- throw new Error(`连接对象不支持setRange操作,使用回退方案`);
1468
+ this.logger('warn', `连接对象不支持setRange操作,使用回退方案`);
1506
1469
  let str = await this.get(finalKey) || '';
1507
1470
  if (str.length < finalIndex) {
1508
1471
  // 填充空格到指定索引
@@ -1523,7 +1486,7 @@ Redis.prototype.setrange = async function(key, index, value) {
1523
1486
  });
1524
1487
  return result;
1525
1488
  } catch (err) {
1526
- this.logger('error','设置字符串范围失败:', err);
1489
+ this.logger('error', '设置字符串范围失败:', err);
1527
1490
  // 回退方案
1528
1491
  let str = await this.get(finalKey) || '';
1529
1492
  if (str.length < finalIndex) {
@@ -1553,7 +1516,7 @@ Redis.prototype.setrange = async function(key, index, value) {
1553
1516
  value,
1554
1517
  error: err
1555
1518
  });
1556
- this.logger('error',`设置字符串范围失败 [${key}]:`, err);
1519
+ this.logger('error', `设置字符串范围失败 [${key}]:`, err);
1557
1520
  return 0;
1558
1521
  }
1559
1522
  };
@@ -1563,7 +1526,7 @@ Redis.prototype.setrange = async function(key, index, value) {
1563
1526
  * @param {String} key 键, 为空则清空所有
1564
1527
  * @return {Promise<Boolean>} 执行结果
1565
1528
  */
1566
- Redis.prototype.clear = async function(key) {
1529
+ Redis.prototype.clear = async function (key) {
1567
1530
  try {
1568
1531
  // 触发前置事件,允许修改或阻止操作
1569
1532
  const ctx = {
@@ -1574,7 +1537,7 @@ Redis.prototype.clear = async function(key) {
1574
1537
 
1575
1538
  // 如果事件处理器标记为取消,则返回false
1576
1539
  if (ctx.cancel) {
1577
- this.logger('debug',`clear操作被事件处理器取消`);
1540
+ this.logger('debug', `clear操作被事件处理器取消`);
1578
1541
  return false;
1579
1542
  }
1580
1543
 
@@ -1598,7 +1561,7 @@ Redis.prototype.clear = async function(key) {
1598
1561
  await connection.execute('FLUSHDB');
1599
1562
  success = true;
1600
1563
  } else {
1601
- this.logger('error','连接对象不支持flushDb操作');
1564
+ this.logger('error', '连接对象不支持flushDb操作');
1602
1565
  success = false;
1603
1566
  }
1604
1567
  }
@@ -1618,7 +1581,7 @@ Redis.prototype.clear = async function(key) {
1618
1581
  key,
1619
1582
  error: err
1620
1583
  });
1621
- this.logger('error','清空缓存失败:', err);
1584
+ this.logger('error', '清空缓存失败:', err);
1622
1585
  return false;
1623
1586
  }
1624
1587
  };
@@ -1629,11 +1592,11 @@ Redis.prototype.clear = async function(key) {
1629
1592
  * @param {Object} options 排序选项
1630
1593
  * @return {Promise<Array>} 排序后的数组
1631
1594
  */
1632
- Redis.prototype.sort = async function(key, options = {}) {
1595
+ Redis.prototype.sort = async function (key, options = {}) {
1633
1596
  try {
1634
1597
  const connection = await this._getConnection();
1635
1598
  try {
1636
- key = this.applyPrefix(key);
1599
+ key = this._addPrefix(key);
1637
1600
  // 默认升序
1638
1601
  let sortOptions = [];
1639
1602
  if (options.by) {
@@ -1662,7 +1625,7 @@ Redis.prototype.sort = async function(key, options = {}) {
1662
1625
 
1663
1626
  return await connection.sort(key, sortOptions);
1664
1627
  } catch (err) {
1665
- this.logger('error','排序失败:', err);
1628
+ this.logger('error', '排序失败:', err);
1666
1629
  // 回退方案:获取数据后在内存中排序
1667
1630
  const data = await this.getForList(key);
1668
1631
  return data.sort((a, b) => {
@@ -1676,7 +1639,7 @@ Redis.prototype.sort = async function(key, options = {}) {
1676
1639
  this._recycleConnection(connection);
1677
1640
  }
1678
1641
  } catch (err) {
1679
- this.logger('error',`排序失败 [${key}]:`, err);
1642
+ this.logger('error', `排序失败 [${key}]:`, err);
1680
1643
  return [];
1681
1644
  }
1682
1645
  };
@@ -1686,7 +1649,7 @@ Redis.prototype.sort = async function(key, options = {}) {
1686
1649
  * @param {String} pattern 键模式 支持*号
1687
1650
  * @return {Promise<Array>} 键数组
1688
1651
  */
1689
- Redis.prototype.keys = async function(pattern = '*') {
1652
+ Redis.prototype.keys = async function (pattern = '*') {
1690
1653
  try {
1691
1654
  const connection = await this._getConnection();
1692
1655
  try {
@@ -1694,12 +1657,12 @@ Redis.prototype.keys = async function(pattern = '*') {
1694
1657
  const fullPattern = this.config.prefix ? this.config.prefix + pattern : pattern;
1695
1658
  const keys = await connection.keys(fullPattern);
1696
1659
  // 返回移除前缀的键
1697
- return keys.map(key => this.removePrefix(key));
1660
+ return keys.map(key => this._delPrefix(key));
1698
1661
  } finally {
1699
1662
  this._recycleConnection(connection);
1700
1663
  }
1701
1664
  } catch (err) {
1702
- this.logger('error','获取键列表失败:', err);
1665
+ this.logger('error', '获取键列表失败:', err);
1703
1666
  return [];
1704
1667
  }
1705
1668
  };
@@ -1711,7 +1674,7 @@ Redis.prototype.keys = async function(pattern = '*') {
1711
1674
  * @param {Number} seconds 秒
1712
1675
  * @return {Promise<Number>} 数组长度
1713
1676
  */
1714
- Redis.prototype.setForList = async function(key, value, seconds) {
1677
+ Redis.prototype.setForList = async function (key, value, seconds) {
1715
1678
  try {
1716
1679
  // 触发前置事件,允许修改或阻止操作
1717
1680
  const ctx = {
@@ -1724,7 +1687,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1724
1687
 
1725
1688
  // 如果事件处理器标记为取消,则返回0
1726
1689
  if (ctx.cancel) {
1727
- this.logger('debug',`setForList操作被事件处理器取消 [${key}]`);
1690
+ this.logger('debug', `setForList操作被事件处理器取消 [${key}]`);
1728
1691
  return 0;
1729
1692
  }
1730
1693
 
@@ -1738,7 +1701,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1738
1701
 
1739
1702
  const connection = await this._getConnection();
1740
1703
  try {
1741
- finalKey = this.applyPrefix(finalKey);
1704
+ finalKey = this._addPrefix(finalKey);
1742
1705
  // 先清空再添加
1743
1706
  if (typeof connection.del === 'function') {
1744
1707
  await connection.del(finalKey);
@@ -1754,7 +1717,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1754
1717
  } else if (typeof connection.execute === 'function') {
1755
1718
  len = await connection.execute('RPUSH', [finalKey, ...finalValue]);
1756
1719
  } else {
1757
- this.logger('error','连接对象不支持rPush操作');
1720
+ this.logger('error', '连接对象不支持rPush操作');
1758
1721
  len = 0;
1759
1722
  }
1760
1723
  }
@@ -1765,7 +1728,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1765
1728
  } else if (typeof connection.execute === 'function') {
1766
1729
  len = await connection.execute('RPUSH', [finalKey, finalValue]);
1767
1730
  } else {
1768
- this.logger('error','连接对象不支持rPush操作');
1731
+ this.logger('error', '连接对象不支持rPush操作');
1769
1732
  len = 0;
1770
1733
  }
1771
1734
  }
@@ -1776,7 +1739,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1776
1739
  } else if (typeof connection.execute === 'function') {
1777
1740
  await connection.execute('EXPIRE', [finalKey, finalSeconds]);
1778
1741
  } else {
1779
- this.logger('error','连接对象不支持expire操作');
1742
+ this.logger('error', '连接对象不支持expire操作');
1780
1743
  }
1781
1744
  }
1782
1745
 
@@ -1799,7 +1762,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1799
1762
  value,
1800
1763
  error: err
1801
1764
  });
1802
- this.logger('error',`设置列表失败 [${key}]:`, err);
1765
+ this.logger('error', `设置列表失败 [${key}]:`, err);
1803
1766
  return 0;
1804
1767
  }
1805
1768
  };
@@ -1811,7 +1774,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1811
1774
  * @param {Number} seconds 秒
1812
1775
  * @return {Promise<Number>} 追加后的数组长度
1813
1776
  */
1814
- Redis.prototype.addForList = async function(key, value, seconds = 0) {
1777
+ Redis.prototype.addForList = async function (key, value, seconds = 0) {
1815
1778
  try {
1816
1779
  // 触发前置事件,允许修改或阻止操作
1817
1780
  const ctx = {
@@ -1824,7 +1787,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
1824
1787
 
1825
1788
  // 如果事件处理器标记为取消,则返回0
1826
1789
  if (ctx.cancel) {
1827
- this.logger('debug',`addForList操作被事件处理器取消 [${key}]`);
1790
+ this.logger('debug', `addForList操作被事件处理器取消 [${key}]`);
1828
1791
  return 0;
1829
1792
  }
1830
1793
 
@@ -1838,7 +1801,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
1838
1801
 
1839
1802
  const connection = await this._getConnection();
1840
1803
  try {
1841
- finalKey = this.applyPrefix(finalKey);
1804
+ finalKey = this._addPrefix(finalKey);
1842
1805
 
1843
1806
  if (Array.isArray(finalValue)) {
1844
1807
  if (finalValue.length > 0) {
@@ -1848,7 +1811,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
1848
1811
  } else if (typeof connection.execute === 'function') {
1849
1812
  len = await connection.execute('RPUSH', [finalKey, ...finalValue]);
1850
1813
  } else {
1851
- this.logger('error','连接对象不支持rPush操作');
1814
+ this.logger('error', '连接对象不支持rPush操作');
1852
1815
  len = 0;
1853
1816
  }
1854
1817
  }
@@ -1859,7 +1822,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
1859
1822
  } else if (typeof connection.execute === 'function') {
1860
1823
  len = await connection.execute('RPUSH', [finalKey, finalValue]);
1861
1824
  } else {
1862
- this.logger('error','连接对象不支持rPush操作');
1825
+ this.logger('error', '连接对象不支持rPush操作');
1863
1826
  len = 0;
1864
1827
  }
1865
1828
  }
@@ -1870,7 +1833,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
1870
1833
  } else if (typeof connection.execute === 'function') {
1871
1834
  await connection.execute('EXPIRE', [finalKey, finalSeconds]);
1872
1835
  } else {
1873
- this.logger('error','连接对象不支持expire操作');
1836
+ this.logger('error', '连接对象不支持expire操作');
1874
1837
  }
1875
1838
  }
1876
1839
 
@@ -1893,7 +1856,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
1893
1856
  value,
1894
1857
  error: err
1895
1858
  });
1896
- this.logger('error',`添加列表项失败 [${key}]:`, err);
1859
+ this.logger('error', `添加列表项失败 [${key}]:`, err);
1897
1860
  return 0;
1898
1861
  }
1899
1862
  };
@@ -1904,15 +1867,14 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
1904
1867
  * @param {Object} value 值
1905
1868
  * @return {Promise<Boolean>} 存在返回true, 否则返回false
1906
1869
  */
1907
- Redis.prototype.hasForList = async function(key, value) {
1870
+ Redis.prototype.hasForList = async function (key, value) {
1908
1871
  if (!value) {
1909
- this.logger('error','参数错误: 值不能为空');
1910
- return false;
1872
+ throw new TypeError('值不能为空');
1911
1873
  }
1912
1874
 
1913
1875
  try {
1914
1876
  // 尝试使用Redis的LREM命令来检查是否存在
1915
- const keyWithPrefix = this.applyPrefix(key);
1877
+ const keyWithPrefix = this._addPrefix(key);
1916
1878
  // 转换value为字符串以便比较
1917
1879
  const valueStr = JSON.stringify(value);
1918
1880
  // 使用scan来查找,避免在大列表上的性能问题
@@ -1922,7 +1884,7 @@ Redis.prototype.hasForList = async function(key, value) {
1922
1884
  return itemStr === valueStr || item === value;
1923
1885
  });
1924
1886
  } catch (err) {
1925
- this.logger('error',`检查列表成员是否存在失败 [${key}]:`, err);
1887
+ this.logger('error', `检查列表成员是否存在失败 [${key}]:`, err);
1926
1888
  return false;
1927
1889
  }
1928
1890
  };
@@ -1934,7 +1896,7 @@ Redis.prototype.hasForList = async function(key, value) {
1934
1896
  * @param {Number} end 结束位置
1935
1897
  * @return {Promise<Array>} 查询到的数组
1936
1898
  */
1937
- Redis.prototype.getForList = async function(key, start = 0, end = -1) {
1899
+ Redis.prototype.getForList = async function (key, start = 0, end = -1) {
1938
1900
  try {
1939
1901
  // 触发前置事件,允许修改或阻止操作
1940
1902
  const ctx = {
@@ -1947,7 +1909,7 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
1947
1909
 
1948
1910
  // 如果事件处理器标记为取消,则返回空数组
1949
1911
  if (ctx.cancel) {
1950
- this.logger('debug',`getForList操作被事件处理器取消 [${key}]`);
1912
+ this.logger('debug', `getForList操作被事件处理器取消 [${key}]`);
1951
1913
  return [];
1952
1914
  }
1953
1915
 
@@ -1960,7 +1922,7 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
1960
1922
 
1961
1923
  const connection = await this._getConnection();
1962
1924
  try {
1963
- finalKey = this.applyPrefix(finalKey);
1925
+ finalKey = this._addPrefix(finalKey);
1964
1926
  let result;
1965
1927
  // 检查连接是否支持lRange方法,Redis v4 API
1966
1928
  if (typeof connection.lRange === 'function') {
@@ -1972,12 +1934,12 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
1972
1934
  // 回退到通用execute方法
1973
1935
  result = await connection.execute('LRANGE', [finalKey, finalStart, finalEnd]);
1974
1936
  } else {
1975
- this.logger('error','连接对象不支持lRange或lrange操作');
1937
+ this.logger('error', '连接对象不支持lRange或lrange操作');
1976
1938
  return [];
1977
1939
  }
1978
1940
  // 确保结果是数组
1979
1941
  if (!Array.isArray(result)) {
1980
- this.logger('error','列表查询结果不是数组格式');
1942
+ this.logger('error', '列表查询结果不是数组格式');
1981
1943
  return [];
1982
1944
  }
1983
1945
 
@@ -2017,7 +1979,7 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
2017
1979
  end,
2018
1980
  error: err
2019
1981
  });
2020
- this.logger('error',`获取列表失败 [${key}]:`, err);
1982
+ this.logger('error', `获取列表失败 [${key}]:`, err);
2021
1983
  return [];
2022
1984
  }
2023
1985
  };
@@ -2027,10 +1989,9 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
2027
1989
  * @param {String} key 键
2028
1990
  * @return {Promise<Boolean>} 成功返回true,失败返回false
2029
1991
  */
2030
- Redis.prototype.clearForList = async function(key) {
1992
+ Redis.prototype.clearForList = async function (key) {
2031
1993
  if (!key) {
2032
- this.logger('error','参数错误: 键不能为空');
2033
- return false;
1994
+ throw new TypeError('键不能为空');
2034
1995
  }
2035
1996
 
2036
1997
  try {
@@ -2043,7 +2004,7 @@ Redis.prototype.clearForList = async function(key) {
2043
2004
 
2044
2005
  // 如果事件处理器标记为取消,则返回false
2045
2006
  if (ctx.cancel) {
2046
- this.logger('debug',`clearForList操作被事件处理器取消 [${key}]`);
2007
+ this.logger('debug', `clearForList操作被事件处理器取消 [${key}]`);
2047
2008
  return false;
2048
2009
  }
2049
2010
 
@@ -2052,7 +2013,7 @@ Redis.prototype.clearForList = async function(key) {
2052
2013
 
2053
2014
  // 直接使用set方法设置为空数组,这样更可靠且与其他模块行为一致
2054
2015
  // 这样可以确保返回值是一个空数组,而不是null或undefined
2055
- finalKey = this.applyPrefix(finalKey);
2016
+ finalKey = this._addPrefix(finalKey);
2056
2017
  await this.set(finalKey, []);
2057
2018
 
2058
2019
  // 触发后置事件
@@ -2067,7 +2028,7 @@ Redis.prototype.clearForList = async function(key) {
2067
2028
  key,
2068
2029
  error: err
2069
2030
  });
2070
- this.logger('error',`清空列表失败 [${key}]:`, err);
2031
+ this.logger('error', `清空列表失败 [${key}]:`, err);
2071
2032
  return false;
2072
2033
  }
2073
2034
  };
@@ -2082,19 +2043,17 @@ Redis.prototype.clearForList = async function(key) {
2082
2043
  * @param {Array<String>} keys 键数组
2083
2044
  * @return {Promise<Array>} 返回值数组,顺序与键数组一致
2084
2045
  */
2085
- Redis.prototype.mget = async function(keys) {
2046
+ Redis.prototype.mget = async function (keys) {
2086
2047
  try {
2087
2048
  // 参数验证
2088
2049
  if (!keys || !Array.isArray(keys)) {
2089
- throw new Error('mget: keys必须是非空数组');
2090
- return [];
2050
+ throw new TypeError('keys必须是非空数组');
2091
2051
  }
2092
2052
 
2093
2053
  // 验证数组中的每个元素是否为非空字符串
2094
2054
  for (const key of keys) {
2095
2055
  if (!key || typeof key !== 'string') {
2096
- throw new Error('mget: keys数组中的每个元素必须是非空字符串');
2097
- return [];
2056
+ throw new TypeError('keys数组中的每个元素必须是非空字符串');
2098
2057
  }
2099
2058
  }
2100
2059
 
@@ -2107,7 +2066,7 @@ Redis.prototype.mget = async function(keys) {
2107
2066
 
2108
2067
  // 如果事件处理器标记为取消,则返回空数组
2109
2068
  if (ctx.cancel) {
2110
- this.logger('debug',`mget操作被事件处理器取消 [${keys}]`);
2069
+ this.logger('debug', `mget操作被事件处理器取消 [${keys}]`);
2111
2070
  return [];
2112
2071
  }
2113
2072
 
@@ -2117,13 +2076,13 @@ Redis.prototype.mget = async function(keys) {
2117
2076
  const connection = await this._getConnection();
2118
2077
  try {
2119
2078
  // 直接使用Redis v4的mGet方法,应用键前缀
2120
- const keysWithPrefix = finalKeys.map(k => this.applyPrefix(k));
2079
+ const keysWithPrefix = finalKeys.map(k => this._addPrefix(k));
2121
2080
  const values = await connection.mGet(keysWithPrefix);
2122
2081
 
2123
2082
  // 处理值的格式,保持数组返回格式
2124
2083
  const result = [];
2125
2084
  values.forEach((value, index) => {
2126
- if (typeof(value) === "string") {
2085
+ if (typeof (value) === "string") {
2127
2086
  try {
2128
2087
  if (value.startsWith("{") && value.endsWith("}") ||
2129
2088
  value.startsWith("[") && value.endsWith("]")) {
@@ -2153,7 +2112,7 @@ Redis.prototype.mget = async function(keys) {
2153
2112
  keys,
2154
2113
  error: err
2155
2114
  });
2156
- this.logger('error',`批量获取缓存失败 [${keys}]:`, err);
2115
+ this.logger('error', `批量获取缓存失败 [${keys}]:`, err);
2157
2116
  return [];
2158
2117
  }
2159
2118
  }
@@ -2164,7 +2123,7 @@ Redis.prototype.mget = async function(keys) {
2164
2123
  * @param {Number} seconds 过期时间(秒)
2165
2124
  * @return {Promise<boolean>} 返回true
2166
2125
  */
2167
- Redis.prototype.mset = async function(obj, seconds = 0) {
2126
+ Redis.prototype.mset = async function (obj, seconds = 0) {
2168
2127
  try {
2169
2128
  // 触发前置事件,允许修改或阻止操作
2170
2129
  const ctx = {
@@ -2176,7 +2135,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
2176
2135
 
2177
2136
  // 如果事件处理器标记为取消,则返回false
2178
2137
  if (ctx.cancel) {
2179
- this.logger('debug',`mset操作被事件处理器取消`);
2138
+ this.logger('debug', `mset操作被事件处理器取消`);
2180
2139
  return false;
2181
2140
  }
2182
2141
 
@@ -2188,8 +2147,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
2188
2147
 
2189
2148
  // 检查最终数据是否有效
2190
2149
  if (!finalData || typeof finalData !== 'object' || finalData === null || Array.isArray(finalData)) {
2191
- throw new Error('mset: 参数必须是非空对象');
2192
- return false;
2150
+ throw new TypeError('参数必须是非空对象');
2193
2151
  }
2194
2152
 
2195
2153
  // 使用Pipeline优化批量操作
@@ -2223,20 +2181,19 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
2223
2181
  });
2224
2182
 
2225
2183
  // 如果Pipeline失败,回退到原实现
2226
- throw new Error('使用Pipeline批量设置失败,使用回退方案:', err);
2184
+ this.logger('warn', '使用Pipeline批量设置失败,使用回退方案:', err);
2227
2185
  const connection = await this._getConnection();
2228
2186
  try {
2229
2187
  const pairs = [];
2230
2188
  for (const [key, value] of Object.entries(obj)) {
2231
- const fullKey = this.applyPrefix(key);
2232
- const val = typeof(value) === "object" ? JSON.stringify(value) : value;
2189
+ const fullKey = this._addPrefix(key);
2190
+ const val = typeof (value) === "object" ? JSON.stringify(value) : value;
2233
2191
  pairs.push(fullKey, val);
2234
2192
  }
2235
2193
 
2236
2194
  // 确保pairs数组格式正确,符合原生mSet方法要求
2237
2195
  if (pairs.length % 2 !== 0) {
2238
- throw new Error('mset: 键值对数量必须相等');
2239
- return false;
2196
+ throw new TypeError('键值对数量必须相等');
2240
2197
  }
2241
2198
 
2242
2199
  await connection.mSet(pairs);
@@ -2244,7 +2201,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
2244
2201
  // 如果设置了过期时间,为所有键设置过期
2245
2202
  if (seconds > 0) {
2246
2203
  for (const key of Object.keys(obj)) {
2247
- await connection.expire(this.applyPrefix(key), seconds);
2204
+ await connection.expire(this._addPrefix(key), seconds);
2248
2205
  }
2249
2206
  }
2250
2207
 
@@ -2262,7 +2219,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
2262
2219
  seconds,
2263
2220
  error: fallbackErr
2264
2221
  });
2265
- this.logger('error','回退方案批量设置也失败:', fallbackErr);
2222
+ this.logger('error', '回退方案批量设置也失败:', fallbackErr);
2266
2223
  return false;
2267
2224
  } finally {
2268
2225
  this._recycleConnection(connection);
@@ -2271,7 +2228,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
2271
2228
  };
2272
2229
 
2273
2230
  // 添加连接回收方法,作为_releaseConnection的别名
2274
- Redis.prototype._recycleConnection = function(connection) {
2231
+ Redis.prototype._recycleConnection = function (connection) {
2275
2232
  return this._releaseConnection(connection);
2276
2233
  };
2277
2234
 
@@ -2279,9 +2236,9 @@ Redis.prototype._recycleConnection = function(connection) {
2279
2236
  * @description 删除哈希字段
2280
2237
  * @param {String} key 键
2281
2238
  * @param {String|Array<String>} fields 字段名或字段名数组
2282
- * @return {Promise<Boolean>} 操作结果
2239
+ * @return {Promise<Number>} 删除的字段数量
2283
2240
  */
2284
- Redis.prototype.hdel = async function(key, fields) {
2241
+ Redis.prototype.hdel = async function (key, fields) {
2285
2242
  try {
2286
2243
  // 触发前置事件,允许修改或阻止操作
2287
2244
  const ctx = {
@@ -2293,7 +2250,7 @@ Redis.prototype.hdel = async function(key, fields) {
2293
2250
 
2294
2251
  // 如果事件处理器标记为取消,则返回false
2295
2252
  if (ctx.cancel) {
2296
- this.logger('debug',`hdel操作被事件处理器取消 [${key}]`);
2253
+ this.logger('debug', `hdel操作被事件处理器取消 [${key}]`);
2297
2254
  return false;
2298
2255
  }
2299
2256
 
@@ -2306,28 +2263,29 @@ Redis.prototype.hdel = async function(key, fields) {
2306
2263
 
2307
2264
  const connection = await this._getConnection();
2308
2265
  try {
2309
- finalKey = this.applyPrefix(finalKey);
2266
+ finalKey = this._addPrefix(finalKey);
2310
2267
  if (!Array.isArray(finalFields)) {
2311
2268
  finalFields = [finalFields];
2312
2269
  }
2313
2270
 
2271
+ let deletedCount = 0;
2314
2272
  // 执行删除操作
2315
2273
  if (typeof connection.hDel === 'function') {
2316
- await connection.hDel(finalKey, finalFields);
2274
+ deletedCount = await connection.hDel(finalKey, finalFields);
2317
2275
  } else if (typeof connection.execute === 'function') {
2318
- await connection.execute('HDEL', [finalKey, ...finalFields]);
2276
+ deletedCount = await connection.execute('HDEL', [finalKey, ...finalFields]);
2319
2277
  } else {
2320
- this.logger('error','连接对象不支持hDel操作');
2321
- success = false;
2278
+ this.logger('error', '连接对象不支持hDel操作');
2279
+ deletedCount = 0;
2322
2280
  }
2323
2281
 
2324
2282
  // 触发后置事件
2325
2283
  this.emit('hdel:after', {
2326
2284
  key: finalKey,
2327
2285
  value: finalFields,
2328
- success
2286
+ deletedCount
2329
2287
  });
2330
- return success;
2288
+ return deletedCount;
2331
2289
  } finally {
2332
2290
  this._recycleConnection(connection);
2333
2291
  }
@@ -2338,8 +2296,8 @@ Redis.prototype.hdel = async function(key, fields) {
2338
2296
  value: fields,
2339
2297
  error: err
2340
2298
  });
2341
- this.logger('error',`删除哈希字段失败 [${key}]:`, err);
2342
- return false;
2299
+ this.logger('error', `删除哈希字段失败 [${key}]:`, err);
2300
+ return 0; // 与Redis语义保持一致,返回删除的字段数量
2343
2301
  }
2344
2302
  };
2345
2303
 
@@ -2348,17 +2306,17 @@ Redis.prototype.hdel = async function(key, fields) {
2348
2306
  * @param {String} key 键
2349
2307
  * @return {Promise<Number>} 字段数量
2350
2308
  */
2351
- Redis.prototype.hlen = async function(key) {
2309
+ Redis.prototype.hlen = async function (key) {
2352
2310
  try {
2353
2311
  const connection = await this._getConnection();
2354
2312
  try {
2355
- key = this.applyPrefix(key);
2313
+ key = this._addPrefix(key);
2356
2314
  return await connection.hLen(key);
2357
2315
  } finally {
2358
2316
  this._recycleConnection(connection);
2359
2317
  }
2360
2318
  } catch (err) {
2361
- this.logger('error',`获取哈希字段数量失败 [${key}]:`, err);
2319
+ this.logger('error', `获取哈希字段数量失败 [${key}]:`, err);
2362
2320
  return 0;
2363
2321
  }
2364
2322
  };
@@ -2442,10 +2400,10 @@ class RedisPipeline {
2442
2400
  if (name !== 'multi' && name !== 'exec' && args.length > 0) {
2443
2401
  processedArgs = [...args];
2444
2402
  // 大多数Redis命令第一个参数是key
2445
- processedArgs[0] = this.redis.applyPrefix(args[0]);
2403
+ processedArgs[0] = this.redis._addPrefix(args[0]);
2446
2404
 
2447
2405
  // 对于set命令,处理value的JSON序列化
2448
- if (name === 'set' && args.length >= 2 && typeof(args[1]) === 'object') {
2406
+ if (name === 'set' && args.length >= 2 && typeof (args[1]) === 'object') {
2449
2407
  processedArgs[1] = JSON.stringify(args[1]);
2450
2408
  }
2451
2409
  }
@@ -2472,12 +2430,12 @@ class RedisPipeline {
2472
2430
  // 处理结果(解析JSON等)
2473
2431
  return results.map(([err, value], index) => {
2474
2432
  if (err) {
2475
- this.logger('error',`[Pipeline] Pipeline命令执行失败 [${this.commands[index].name}]:`, err);
2433
+ this.logger('error', `[Pipeline] Pipeline命令执行失败 [${this.commands[index].name}]:`, err);
2476
2434
  return null;
2477
2435
  }
2478
2436
 
2479
2437
  // 尝试解析JSON结果
2480
- if (typeof(value) === 'string') {
2438
+ if (typeof (value) === 'string') {
2481
2439
  try {
2482
2440
  if (value.startsWith('{') && value.endsWith('}') ||
2483
2441
  value.startsWith('[') && value.endsWith(']')) {
@@ -2498,7 +2456,7 @@ class RedisPipeline {
2498
2456
  * 清空所有过期的数据
2499
2457
  * @return {Promise<Boolean>} 是否成功
2500
2458
  */
2501
- Redis.prototype.clearExpire = async function() {
2459
+ Redis.prototype.clearExpire = async function () {
2502
2460
  try {
2503
2461
  this.emit('clearExpire:before', {});
2504
2462
  // Redis没有直接的方法来清空所有过期数据
@@ -2512,14 +2470,14 @@ Redis.prototype.clearExpire = async function() {
2512
2470
  this.emit('clearExpire:error', {
2513
2471
  error: err
2514
2472
  });
2515
- this.logger('error','清空过期数据失败:', err);
2473
+ this.logger('error', '清空过期数据失败:', err);
2516
2474
  return false;
2517
2475
  }
2518
2476
  };
2519
2477
 
2520
2478
 
2521
2479
  // 获取连接的方法
2522
- Redis.prototype._getConnection = function() {
2480
+ Redis.prototype._getConnection = function () {
2523
2481
  return new Promise((resolve, reject) => {
2524
2482
  try {
2525
2483
  // 如果有可用连接,直接返回
@@ -2533,7 +2491,7 @@ Redis.prototype._getConnection = function() {
2533
2491
  // 如果连接数未达到上限,创建新连接
2534
2492
  const totalConnections = this.connection_pool.available.length + this.connection_pool.in_use
2535
2493
  .size;
2536
- if (totalConnections < this.connection_pool.config.max_clients) {
2494
+ if (totalConnections < this.config.max_clients) {
2537
2495
  this._createConnection().then(connection => {
2538
2496
  this.connection_pool.in_use.add(connection);
2539
2497
  resolve(connection);
@@ -2541,27 +2499,27 @@ Redis.prototype._getConnection = function() {
2541
2499
  } else {
2542
2500
  // 否则等待连接释放
2543
2501
  // 这里简化处理,直接创建连接(生产环境应实现等待队列)
2544
- throw new Error('连接池已满,创建额外连接');
2502
+ this.logger('warn', '连接池已满,创建额外连接');
2545
2503
  this._createConnection().then(connection => {
2546
2504
  this.connection_pool.in_use.add(connection);
2547
2505
  resolve(connection);
2548
2506
  }).catch(reject);
2549
2507
  }
2550
2508
  } catch (error) {
2551
- this.logger('error','获取连接失败:', error);
2509
+ this.logger('error', '获取连接失败:', error);
2552
2510
  reject(error);
2553
2511
  }
2554
2512
  });
2555
2513
  };
2556
2514
 
2557
2515
  // 释放连接回池
2558
- Redis.prototype._releaseConnection = function(connection) {
2516
+ Redis.prototype._releaseConnection = function (connection) {
2559
2517
  try {
2560
2518
  if (this.connection_pool.in_use.has(connection)) {
2561
2519
  this.connection_pool.in_use.delete(connection);
2562
2520
 
2563
2521
  // 检查是否超过最大空闲连接数
2564
- if (this.connection_pool.available.length < this.connection_pool.config.max_idle) {
2522
+ if (this.connection_pool.available.length < this.config.max_idle) {
2565
2523
  this.connection_pool.available.push(connection);
2566
2524
  } else {
2567
2525
  // 否则关闭多余的连接
@@ -2571,16 +2529,15 @@ Redis.prototype._releaseConnection = function(connection) {
2571
2529
  } else if (typeof connection.end === 'function') {
2572
2530
  connection.end();
2573
2531
  } else {
2574
- this.logger('debug','连接对象没有可用的关闭方法');
2532
+ this.logger('debug', '连接对象没有可用的关闭方法');
2575
2533
  }
2576
- // this.logger('debug','关闭多余连接:', connection.id);
2577
2534
  } catch (e) {
2578
- this.logger('error','关闭多余连接失败:', e);
2535
+ this.logger('error', '关闭多余连接失败:', e);
2579
2536
  }
2580
2537
  }
2581
2538
  }
2582
2539
  } catch (error) {
2583
- this.logger('error','释放连接失败:', error);
2540
+ this.logger('error', '释放连接失败:', error);
2584
2541
  }
2585
2542
  };
2586
2543
 
@@ -2596,7 +2553,7 @@ Redis.prototype._recycleConnection = Redis.prototype._releaseConnection;
2596
2553
  * @param {Object} value 值
2597
2554
  * @return {Promise<Boolean>} 操作结果
2598
2555
  */
2599
- Redis.prototype.hset = async function(key, field, value) {
2556
+ Redis.prototype.hset = async function (key, field, value) {
2600
2557
  try {
2601
2558
  // 触发前置事件,允许修改或阻止操作
2602
2559
  const ctx = {
@@ -2609,7 +2566,7 @@ Redis.prototype.hset = async function(key, field, value) {
2609
2566
 
2610
2567
  // 如果事件处理器标记为取消,则返回false
2611
2568
  if (ctx.cancel) {
2612
- this.logger('debug',`hset操作被事件处理器取消 [${key}:${field}]`);
2569
+ this.logger('debug', `hset操作被事件处理器取消 [${key}:${field}]`);
2613
2570
  return false;
2614
2571
  }
2615
2572
 
@@ -2622,9 +2579,9 @@ Redis.prototype.hset = async function(key, field, value) {
2622
2579
 
2623
2580
  const connection = await this._getConnection();
2624
2581
  try {
2625
- finalKey = this.applyPrefix(finalKey);
2582
+ finalKey = this._addPrefix(finalKey);
2626
2583
  // 确保所有非字符串值都转换为字符串
2627
- if (typeof(finalValue) !== "string") {
2584
+ if (typeof (finalValue) !== "string") {
2628
2585
  finalValue = JSON.stringify(finalValue);
2629
2586
  }
2630
2587
  // 使用正确的Redis v4 API - hSet方法
@@ -2637,12 +2594,12 @@ Redis.prototype.hset = async function(key, field, value) {
2637
2594
  value: finalValue,
2638
2595
  success: true
2639
2596
  });
2640
- return result > 0; // 如果field是新的,hSet返回1,否则返回0
2597
+ return true; // hSet操作成功返回true
2641
2598
  } finally {
2642
2599
  this._recycleConnection(connection);
2643
2600
  }
2644
2601
  } catch (err) {
2645
- this.logger('error',`设置哈希字段失败 [${key}:${field}]:`, err);
2602
+ this.logger('error', `设置哈希字段失败 [${key}:${field}]:`, err);
2646
2603
  // 触发错误事件
2647
2604
  this.emit('hset:error', {
2648
2605
  key,
@@ -2660,15 +2617,15 @@ Redis.prototype.hset = async function(key, field, value) {
2660
2617
  * @param {String} field 字段
2661
2618
  * @return {Promise<Object>} 字段值
2662
2619
  */
2663
- Redis.prototype.hget = async function(key, field) {
2620
+ Redis.prototype.hget = async function (key, field) {
2664
2621
  try {
2665
2622
  const connection = await this._getConnection();
2666
2623
  try {
2667
- key = this.applyPrefix(key);
2624
+ key = this._addPrefix(key);
2668
2625
  // 使用正确的Redis v4 API - hGet方法
2669
2626
  const value = await connection.hGet(key, field);
2670
2627
  // 尝试解析JSON
2671
- if (value && typeof(value) === 'string') {
2628
+ if (value && typeof (value) === 'string') {
2672
2629
  try {
2673
2630
  if (value.startsWith('{') && value.endsWith('}') ||
2674
2631
  value.startsWith('[') && value.endsWith(']')) {
@@ -2684,7 +2641,7 @@ Redis.prototype.hget = async function(key, field) {
2684
2641
  this._recycleConnection(connection);
2685
2642
  }
2686
2643
  } catch (err) {
2687
- this.logger('error',`获取哈希字段失败 [${key}:${field}]:`, err);
2644
+ this.logger('error', `获取哈希字段失败 [${key}:${field}]:`, err);
2688
2645
  return null;
2689
2646
  }
2690
2647
  };
@@ -2695,7 +2652,7 @@ Redis.prototype.hget = async function(key, field) {
2695
2652
  * @param {Object} obj 字段值对象
2696
2653
  * @return {Promise<Boolean>} 操作结果
2697
2654
  */
2698
- Redis.prototype.hmset = async function(key, obj) {
2655
+ Redis.prototype.hmset = async function (key, obj) {
2699
2656
  try {
2700
2657
  // 触发前置事件,允许修改或阻止操作
2701
2658
  const ctx = {
@@ -2707,7 +2664,7 @@ Redis.prototype.hmset = async function(key, obj) {
2707
2664
 
2708
2665
  // 如果事件处理器标记为取消,则返回false
2709
2666
  if (ctx.cancel) {
2710
- this.logger('debug',`hmset操作被事件处理器取消 [${key}]`);
2667
+ this.logger('debug', `hmset操作被事件处理器取消 [${key}]`);
2711
2668
  return false;
2712
2669
  }
2713
2670
 
@@ -2719,7 +2676,7 @@ Redis.prototype.hmset = async function(key, obj) {
2719
2676
 
2720
2677
  const connection = await this._getConnection();
2721
2678
  try {
2722
- finalKey = this.applyPrefix(finalKey);
2679
+ finalKey = this._addPrefix(finalKey);
2723
2680
 
2724
2681
  // 检查Redis版本,支持不同的方法
2725
2682
  this.logger('debug',
@@ -2728,14 +2685,14 @@ Redis.prototype.hmset = async function(key, obj) {
2728
2685
  let success = true;
2729
2686
 
2730
2687
  // 优化策略:对所有Redis版本都使用逐个字段设置的方式,确保兼容性和正确性
2731
- this.logger('debug',`对键 ${finalKey} 强制使用逐个字段设置策略`);
2688
+ this.logger('debug', `对键 ${finalKey} 强制使用逐个字段设置策略`);
2732
2689
 
2733
2690
  // 获取适合的设置字段方法
2734
2691
  const setFieldMethod = typeof connection.hSet === 'function' ? 'hSet' :
2735
2692
  typeof connection.hset === 'function' ? 'hset' : null;
2736
2693
 
2737
2694
  if (!setFieldMethod) {
2738
- this.logger('error',`连接对象不支持hSet或hset操作`);
2695
+ this.logger('error', `连接对象不支持hSet或hset操作`);
2739
2696
  return false;
2740
2697
  }
2741
2698
 
@@ -2747,10 +2704,10 @@ Redis.prototype.hmset = async function(key, obj) {
2747
2704
  if (typeof value !== 'string') {
2748
2705
  value = JSON.stringify(value);
2749
2706
  }
2750
- this.logger('debug',`单独设置字段: ${setFieldMethod}(${finalKey}, ${field}, ${value})`);
2707
+ this.logger('debug', `单独设置字段: ${setFieldMethod}(${finalKey}, ${field}, ${value})`);
2751
2708
  await connection[setFieldMethod](finalKey, field, value);
2752
2709
  } catch (singleError) {
2753
- this.logger('error',`设置单个字段失败 ${field}: ${singleError.message}`);
2710
+ this.logger('error', `设置单个字段失败 ${field}: ${singleError.message}`);
2754
2711
  success = false;
2755
2712
  break;
2756
2713
  }
@@ -2759,19 +2716,19 @@ Redis.prototype.hmset = async function(key, obj) {
2759
2716
 
2760
2717
  // 验证所有字段是否都设置成功
2761
2718
  if (success) {
2762
- this.logger('debug',`验证所有字段设置情况`);
2719
+ this.logger('debug', `验证所有字段设置情况`);
2763
2720
  for (const field in finalObj) {
2764
2721
  if (finalObj.hasOwnProperty(field)) {
2765
2722
  try {
2766
2723
  const hgetMethod = typeof connection.hGet === 'function' ? 'hGet' : 'hget';
2767
2724
  const value = await connection[hgetMethod](finalKey, field);
2768
- this.logger('debug',`验证字段 ${field}: ${value !== null ? '存在' : '不存在'}`);
2725
+ this.logger('debug', `验证字段 ${field}: ${value !== null ? '存在' : '不存在'}`);
2769
2726
  // 如果字段不存在,更新success状态
2770
2727
  if (value === null) {
2771
2728
  success = false;
2772
2729
  }
2773
2730
  } catch (getError) {
2774
- this.logger('error',`验证字段失败 ${field}: ${getError.message}`);
2731
+ this.logger('error', `验证字段失败 ${field}: ${getError.message}`);
2775
2732
  success = false;
2776
2733
  }
2777
2734
  }
@@ -2789,7 +2746,7 @@ Redis.prototype.hmset = async function(key, obj) {
2789
2746
  this._recycleConnection(connection);
2790
2747
  }
2791
2748
  } catch (err) {
2792
- this.logger('error',`批量设置哈希字段失败 [${key}]:`, err);
2749
+ this.logger('error', `批量设置哈希字段失败 [${key}]:`, err);
2793
2750
  // 触发错误事件
2794
2751
  this.emit('hmset:error', {
2795
2752
  key,
@@ -2805,23 +2762,23 @@ Redis.prototype.hmset = async function(key, obj) {
2805
2762
  * @param {String} key 键
2806
2763
  * @return {Promise<Object>} 所有字段和值
2807
2764
  */
2808
- Redis.prototype.hgetall = async function(key) {
2765
+ Redis.prototype.hgetall = async function (key) {
2809
2766
  try {
2810
2767
  const connection = await this._getConnection();
2811
2768
  try {
2812
- key = this.applyPrefix(key);
2769
+ key = this._addPrefix(key);
2813
2770
  if (typeof connection.hGetAll === 'function') {
2814
2771
  const result = await connection.hGetAll(key);
2815
2772
  return result;
2816
2773
  } else {
2817
- this.logger('error','连接对象不支持hGetAll操作');
2774
+ this.logger('error', '连接对象不支持hGetAll操作');
2818
2775
  return {};
2819
2776
  }
2820
2777
  } finally {
2821
2778
  this._recycleConnection(connection);
2822
2779
  }
2823
2780
  } catch (err) {
2824
- this.logger('error',`获取哈希所有字段和值失败 [${key}]:`, err);
2781
+ this.logger('error', `获取哈希所有字段和值失败 [${key}]:`, err);
2825
2782
  return {};
2826
2783
  }
2827
2784
  };
@@ -2832,12 +2789,10 @@ Redis.prototype.hgetall = async function(key) {
2832
2789
  * @param {Array} fields 字段名数组
2833
2790
  * @return {Promise<Array>} 与字段名顺序对应的数组值,不存在返回undefined
2834
2791
  */
2835
- Redis.prototype.hmget = async function(key, fields) {
2792
+ Redis.prototype.hmget = async function (key, fields) {
2836
2793
  try {
2837
2794
  if (!Array.isArray(fields)) {
2838
- this.logger('error','参数错误: fields必须是数组');
2839
- // 返回空数组而不是false,保持与注释类型一致
2840
- return [];
2795
+ throw new TypeError('fields必须是数组');
2841
2796
  }
2842
2797
 
2843
2798
  this.emit('hmget:before', {
@@ -2847,7 +2802,7 @@ Redis.prototype.hmget = async function(key, fields) {
2847
2802
 
2848
2803
  const connection = await this._getConnection();
2849
2804
  try {
2850
- key = this.applyPrefix(key);
2805
+ key = this._addPrefix(key);
2851
2806
 
2852
2807
  if (typeof connection.hmGet === 'function') {
2853
2808
  const result = await connection.hmGet(key, fields);
@@ -2873,8 +2828,8 @@ Redis.prototype.hmget = async function(key, fields) {
2873
2828
  });
2874
2829
  return finalResult;
2875
2830
  } else {
2876
- this.logger('error','连接对象不支持hmGet操作');
2877
- this.logger('error','错误: Redis客户端不支持hmget操作');
2831
+ this.logger('error', '连接对象不支持hmGet操作');
2832
+ this.logger('error', '错误: Redis客户端不支持hmget操作');
2878
2833
  // 返回与fields长度相同的undefined数组,保持与注释一致
2879
2834
  return fields.map(() => undefined);
2880
2835
  }
@@ -2887,7 +2842,7 @@ Redis.prototype.hmget = async function(key, fields) {
2887
2842
  fields,
2888
2843
  error: err
2889
2844
  });
2890
- this.logger('error',`哈希表批量获取失败 [${key}]:`, err);
2845
+ this.logger('error', `哈希表批量获取失败 [${key}]:`, err);
2891
2846
  // 返回与fields长度相同的undefined数组,保持与注释类型一致
2892
2847
  return fields.map(() => undefined);
2893
2848
  }
@@ -2898,7 +2853,7 @@ Redis.prototype.hmget = async function(key, fields) {
2898
2853
  * @param {Number} seconds 过期时间(秒)
2899
2854
  * @return {Promise<Boolean>} 操作结果
2900
2855
  */
2901
- Redis.prototype.expire = async function(key, seconds) {
2856
+ Redis.prototype.expire = async function (key, seconds) {
2902
2857
  try {
2903
2858
  // 触发前置事件
2904
2859
  const ctx = {
@@ -2909,7 +2864,7 @@ Redis.prototype.expire = async function(key, seconds) {
2909
2864
  this.emit('expire:before', ctx);
2910
2865
 
2911
2866
  if (ctx.cancel) {
2912
- this.logger('debug',`expire操作被事件处理器取消 [${key}]`);
2867
+ this.logger('debug', `expire操作被事件处理器取消 [${key}]`);
2913
2868
  return false;
2914
2869
  }
2915
2870
 
@@ -2921,7 +2876,7 @@ Redis.prototype.expire = async function(key, seconds) {
2921
2876
  const connection = await this._getConnection();
2922
2877
  try {
2923
2878
  // 使用正确的Redis客户端Promise API
2924
- const result = await connection.expire(this.applyPrefix(finalKey), finalSeconds);
2879
+ const result = await connection.expire(this._addPrefix(finalKey), finalSeconds);
2925
2880
 
2926
2881
  // 触发后置事件
2927
2882
  this.emit('expire:after', {
@@ -2931,7 +2886,7 @@ Redis.prototype.expire = async function(key, seconds) {
2931
2886
  });
2932
2887
  return result === 1;
2933
2888
  } catch (err) {
2934
- this.logger('error',`设置过期时间失败 [${finalKey}]:`, err);
2889
+ this.logger('error', `设置过期时间失败 [${finalKey}]:`, err);
2935
2890
  this.emit('expire:error', {
2936
2891
  key: finalKey,
2937
2892
  ttl: finalSeconds,
@@ -2942,7 +2897,7 @@ Redis.prototype.expire = async function(key, seconds) {
2942
2897
  this._recycleConnection(connection);
2943
2898
  }
2944
2899
  } catch (err) {
2945
- this.logger('error',`设置过期时间失败 [${key}]:`, err);
2900
+ this.logger('error', `设置过期时间失败 [${key}]:`, err);
2946
2901
  this.emit('expire:error', {
2947
2902
  key,
2948
2903
  ttl: seconds,
@@ -2958,16 +2913,14 @@ Redis.prototype.expire = async function(key, seconds) {
2958
2913
  * @param {Number} milliseconds 过期时间(毫秒)
2959
2914
  * @return {Promise<Boolean>} 操作结果
2960
2915
  */
2961
- Redis.prototype.pexpire = async function(key, milliseconds) {
2916
+ Redis.prototype.pexpire = async function (key, milliseconds) {
2962
2917
  try {
2963
2918
  // 参数验证
2964
2919
  if (!key || typeof key !== 'string') {
2965
- this.logger('error','pexpire参数错误: key must be a non-empty string');
2966
- return false;
2920
+ throw new TypeError('key必须是非空字符串');
2967
2921
  }
2968
2922
  if (!Number.isInteger(milliseconds) || milliseconds <= 0) {
2969
- this.logger('error','pexpire参数错误: milliseconds must be a positive integer');
2970
- return false;
2923
+ throw new TypeError('milliseconds必须是正整数');
2971
2924
  }
2972
2925
 
2973
2926
  // 触发前置事件
@@ -2979,7 +2932,7 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
2979
2932
  this.emit('pexpire:before', ctx);
2980
2933
 
2981
2934
  if (ctx.cancel) {
2982
- this.logger('debug',`pexpire操作被事件处理器取消 [${key}]`);
2935
+ this.logger('debug', `pexpire操作被事件处理器取消 [${key}]`);
2983
2936
  return false;
2984
2937
  }
2985
2938
 
@@ -2993,11 +2946,11 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
2993
2946
  // 使用正确的Redis客户端Promise API
2994
2947
  let result;
2995
2948
  if (typeof connection.pexpire === 'function') {
2996
- result = await connection.pexpire(this.applyPrefix(finalKey), finalMilliseconds);
2949
+ result = await connection.pexpire(this._addPrefix(finalKey), finalMilliseconds);
2997
2950
  } else if (typeof connection.execute === 'function') {
2998
- result = await connection.execute('PEXPIRE', [this.applyPrefix(finalKey), finalMilliseconds]);
2951
+ result = await connection.execute('PEXPIRE', [this._addPrefix(finalKey), finalMilliseconds]);
2999
2952
  } else {
3000
- this.logger('error','连接对象不支持pexpire操作');
2953
+ this.logger('error', '连接对象不支持pexpire操作');
3001
2954
  return false;
3002
2955
  }
3003
2956
 
@@ -3009,7 +2962,7 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
3009
2962
  });
3010
2963
  return result === 1;
3011
2964
  } catch (err) {
3012
- this.logger('error',`设置过期时间(毫秒)失败 [${finalKey}]:`, err);
2965
+ this.logger('error', `设置过期时间(毫秒)失败 [${finalKey}]:`, err);
3013
2966
  this.emit('pexpire:error', {
3014
2967
  key: finalKey,
3015
2968
  ttl: finalMilliseconds,
@@ -3020,7 +2973,7 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
3020
2973
  this._recycleConnection(connection);
3021
2974
  }
3022
2975
  } catch (err) {
3023
- this.logger('error',`设置过期时间(毫秒)失败 [${key}]:`, err);
2976
+ this.logger('error', `设置过期时间(毫秒)失败 [${key}]:`, err);
3024
2977
  this.emit('pexpire:error', {
3025
2978
  key,
3026
2979
  ttl: milliseconds,
@@ -3031,7 +2984,7 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
3031
2984
  };
3032
2985
 
3033
2986
  // 新增persist方法实现
3034
- Redis.prototype.persist = async function(key) {
2987
+ Redis.prototype.persist = async function (key) {
3035
2988
  try {
3036
2989
  // 触发前置事件
3037
2990
  const ctx = {
@@ -3041,7 +2994,7 @@ Redis.prototype.persist = async function(key) {
3041
2994
  this.emit('persist:before', ctx);
3042
2995
 
3043
2996
  if (ctx.cancel) {
3044
- this.logger('debug',`persist操作被事件处理器取消 [${key}]`);
2997
+ this.logger('debug', `persist操作被事件处理器取消 [${key}]`);
3045
2998
  return false;
3046
2999
  }
3047
3000
 
@@ -3052,7 +3005,7 @@ Redis.prototype.persist = async function(key) {
3052
3005
  const connection = await this._getConnection();
3053
3006
  try {
3054
3007
  // 使用正确的Redis客户端Promise API
3055
- const result = await connection.persist(this.applyPrefix(finalKey));
3008
+ const result = await connection.persist(this._addPrefix(finalKey));
3056
3009
 
3057
3010
  // 触发后置事件
3058
3011
  this.emit('persist:after', {
@@ -3061,7 +3014,7 @@ Redis.prototype.persist = async function(key) {
3061
3014
  });
3062
3015
  return result === 1;
3063
3016
  } catch (err) {
3064
- this.logger('error',`移除过期时间失败 [${finalKey}]:`, err);
3017
+ this.logger('error', `移除过期时间失败 [${finalKey}]:`, err);
3065
3018
  this.emit('persist:error', {
3066
3019
  key: finalKey,
3067
3020
  error: err
@@ -3071,7 +3024,7 @@ Redis.prototype.persist = async function(key) {
3071
3024
  this._recycleConnection(connection);
3072
3025
  }
3073
3026
  } catch (err) {
3074
- this.logger('error',`移除过期时间失败 [${key}]:`, err);
3027
+ this.logger('error', `移除过期时间失败 [${key}]:`, err);
3075
3028
  this.emit('persist:error', {
3076
3029
  key,
3077
3030
  error: err
@@ -3086,12 +3039,11 @@ Redis.prototype.persist = async function(key) {
3086
3039
  * @param {Number} [seconds=0] 过期时间(秒)
3087
3040
  * @return {Promise<Number>} 增加后的值
3088
3041
  */
3089
- Redis.prototype.incr = async function(key, seconds = 0) {
3042
+ Redis.prototype.incr = async function (key, seconds = 0) {
3090
3043
  try {
3091
3044
  // 参数验证
3092
3045
  if (!key || typeof key !== 'string') {
3093
- this.logger('error','参数错误: 键必须是非空字符串');
3094
- return null;
3046
+ throw new TypeError('键必须是非空字符串');
3095
3047
  }
3096
3048
 
3097
3049
  // 触发前置事件
@@ -3103,7 +3055,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3103
3055
  this.emit('incr:before', ctx);
3104
3056
 
3105
3057
  if (ctx.cancel) {
3106
- this.logger('debug',`incr操作被事件处理器取消 [${key}]`);
3058
+ this.logger('debug', `incr操作被事件处理器取消 [${key}]`);
3107
3059
  return null;
3108
3060
  }
3109
3061
 
@@ -3114,7 +3066,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3114
3066
 
3115
3067
  const connection = await this._getConnection();
3116
3068
  try {
3117
- const prefixedKey = this.applyPrefix(finalKey);
3069
+ const prefixedKey = this._addPrefix(finalKey);
3118
3070
  // 使用正确的Redis客户端Promise API
3119
3071
  const result = await connection.incr(prefixedKey);
3120
3072
 
@@ -3134,7 +3086,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3134
3086
  });
3135
3087
  return result;
3136
3088
  } catch (err) {
3137
- this.logger('error',`增加整数值失败 [${finalKey}]:`, err);
3089
+ this.logger('error', `增加整数值失败 [${finalKey}]:`, err);
3138
3090
  this.emit('incr:error', {
3139
3091
  key: finalKey,
3140
3092
  seconds: finalSeconds,
@@ -3145,7 +3097,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3145
3097
  this._recycleConnection(connection);
3146
3098
  }
3147
3099
  } catch (err) {
3148
- this.logger('error',`增加整数值失败 [${key}]:`, err);
3100
+ this.logger('error', `增加整数值失败 [${key}]:`, err);
3149
3101
  this.emit('incr:error', {
3150
3102
  key,
3151
3103
  seconds,
@@ -3161,12 +3113,11 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3161
3113
  * @param {Number} [seconds=0] 过期时间(秒)
3162
3114
  * @return {Promise<Number>} 减少后的值
3163
3115
  */
3164
- Redis.prototype.decr = async function(key, seconds = 0) {
3116
+ Redis.prototype.decr = async function (key, seconds = 0) {
3165
3117
  try {
3166
3118
  // 参数验证
3167
3119
  if (!key || typeof key !== 'string') {
3168
- this.logger('error','参数错误: 键必须是非空字符串');
3169
- return null;
3120
+ throw new TypeError('键必须是非空字符串');
3170
3121
  }
3171
3122
 
3172
3123
  // 触发前置事件
@@ -3178,7 +3129,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3178
3129
  this.emit('decr:before', ctx);
3179
3130
 
3180
3131
  if (ctx.cancel) {
3181
- this.logger('debug',`decr操作被事件处理器取消 [${key}]`);
3132
+ this.logger('debug', `decr操作被事件处理器取消 [${key}]`);
3182
3133
  return null;
3183
3134
  }
3184
3135
 
@@ -3189,7 +3140,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3189
3140
 
3190
3141
  const connection = await this._getConnection();
3191
3142
  try {
3192
- const prefixedKey = this.applyPrefix(finalKey);
3143
+ const prefixedKey = this._addPrefix(finalKey);
3193
3144
  // 使用正确的Redis客户端Promise API
3194
3145
  const result = await connection.decr(prefixedKey);
3195
3146
 
@@ -3209,7 +3160,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3209
3160
  });
3210
3161
  return result;
3211
3162
  } catch (err) {
3212
- this.logger('error',`减少整数值失败 [${finalKey}]:`, err);
3163
+ this.logger('error', `减少整数值失败 [${finalKey}]:`, err);
3213
3164
  this.emit('decr:error', {
3214
3165
  key: finalKey,
3215
3166
  seconds: finalSeconds,
@@ -3220,7 +3171,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3220
3171
  this._recycleConnection(connection);
3221
3172
  }
3222
3173
  } catch (err) {
3223
- this.logger('error',`减少整数值失败 [${key}]:`, err);
3174
+ this.logger('error', `减少整数值失败 [${key}]:`, err);
3224
3175
  this.emit('decr:error', {
3225
3176
  key,
3226
3177
  seconds,
@@ -3237,16 +3188,14 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3237
3188
  * @param {Number} [seconds=0] 过期时间(秒)
3238
3189
  * @return {Promise<Number>} 增加后的值
3239
3190
  */
3240
- Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3191
+ Redis.prototype.incrbyfloat = async function (key, increment, seconds = 0) {
3241
3192
  try {
3242
3193
  // 参数验证
3243
3194
  if (!key || typeof key !== 'string') {
3244
- this.logger('error','参数错误: 键必须是非空字符串');
3245
- return null;
3195
+ throw new TypeError('键必须是非空字符串');
3246
3196
  }
3247
3197
  if (typeof increment !== 'number' || isNaN(increment)) {
3248
- this.logger('error','参数错误: 增量必须是有效数字');
3249
- return null;
3198
+ throw new TypeError('增量必须是有效数字');
3250
3199
  }
3251
3200
 
3252
3201
  // 触发前置事件
@@ -3259,7 +3208,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3259
3208
  this.emit('incrbyfloat:before', ctx);
3260
3209
 
3261
3210
  if (ctx.cancel) {
3262
- this.logger('debug',`incrbyfloat操作被事件处理器取消 [${key}]`);
3211
+ this.logger('debug', `incrbyfloat操作被事件处理器取消 [${key}]`);
3263
3212
  return null;
3264
3213
  }
3265
3214
 
@@ -3271,7 +3220,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3271
3220
 
3272
3221
  const connection = await this._getConnection();
3273
3222
  try {
3274
- const prefixedKey = this.applyPrefix(finalKey);
3223
+ const prefixedKey = this._addPrefix(finalKey);
3275
3224
  // 使用正确的Redis客户端Promise API
3276
3225
  const result = await connection.incrByFloat(prefixedKey, finalIncrement);
3277
3226
  const parsedResult = parseFloat(result);
@@ -3293,7 +3242,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3293
3242
  });
3294
3243
  return parsedResult;
3295
3244
  } catch (err) {
3296
- this.logger('error',`增加浮点数值失败 [${finalKey}]:`, err);
3245
+ this.logger('error', `增加浮点数值失败 [${finalKey}]:`, err);
3297
3246
  this.emit('incrbyfloat:error', {
3298
3247
  key: finalKey,
3299
3248
  increment: finalIncrement,
@@ -3305,7 +3254,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3305
3254
  this._recycleConnection(connection);
3306
3255
  }
3307
3256
  } catch (err) {
3308
- this.logger('error',`增加浮点数值失败 [${key}]:`, err);
3257
+ this.logger('error', `增加浮点数值失败 [${key}]:`, err);
3309
3258
  this.emit('incrbyfloat:error', {
3310
3259
  key,
3311
3260
  increment,
@@ -3323,16 +3272,14 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3323
3272
  * @param {Number} [seconds=0] 过期时间(秒)
3324
3273
  * @return {Promise<Number>} 增加后的值
3325
3274
  */
3326
- Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3275
+ Redis.prototype.incrby = async function (key, increment, seconds = 0) {
3327
3276
  try {
3328
3277
  // 参数验证
3329
3278
  if (!key || typeof key !== 'string') {
3330
- this.logger('error','参数错误: 键必须是非空字符串');
3331
- return null;
3279
+ throw new TypeError('键必须是非空字符串');
3332
3280
  }
3333
3281
  if (typeof increment !== 'number' || isNaN(increment) || !Number.isInteger(increment)) {
3334
- this.logger('error','参数错误: 增量必须是有效整数');
3335
- return null;
3282
+ throw new TypeError('增量必须是有效整数');
3336
3283
  }
3337
3284
 
3338
3285
  // 触发前置事件
@@ -3345,7 +3292,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3345
3292
  this.emit('incrby:before', ctx);
3346
3293
 
3347
3294
  if (ctx.cancel) {
3348
- this.logger('debug',`incrby操作被事件处理器取消 [${key}]`);
3295
+ this.logger('debug', `incrby操作被事件处理器取消 [${key}]`);
3349
3296
  return null;
3350
3297
  }
3351
3298
 
@@ -3357,7 +3304,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3357
3304
 
3358
3305
  const connection = await this._getConnection();
3359
3306
  try {
3360
- const prefixedKey = this.applyPrefix(finalKey);
3307
+ const prefixedKey = this._addPrefix(finalKey);
3361
3308
  // 使用正确的Redis客户端Promise API
3362
3309
  const result = await connection.incrBy(prefixedKey, finalIncrement);
3363
3310
 
@@ -3378,7 +3325,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3378
3325
  });
3379
3326
  return result;
3380
3327
  } catch (err) {
3381
- this.logger('error',`增加指定值失败 [${finalKey}]:`, err);
3328
+ this.logger('error', `增加指定值失败 [${finalKey}]:`, err);
3382
3329
  this.emit('incrby:error', {
3383
3330
  key: finalKey,
3384
3331
  increment: finalIncrement,
@@ -3390,7 +3337,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3390
3337
  this._recycleConnection(connection);
3391
3338
  }
3392
3339
  } catch (err) {
3393
- this.logger('error',`增加指定值失败 [${key}]:`, err);
3340
+ this.logger('error', `增加指定值失败 [${key}]:`, err);
3394
3341
  this.emit('incrby:error', {
3395
3342
  key,
3396
3343
  increment,
@@ -3408,16 +3355,14 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3408
3355
  * @param {Number} [seconds=0] 过期时间(秒)
3409
3356
  * @return {Promise<Number>} 减少后的值
3410
3357
  */
3411
- Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3358
+ Redis.prototype.decrby = async function (key, decrement, seconds = 0) {
3412
3359
  try {
3413
3360
  // 参数验证
3414
3361
  if (!key || typeof key !== 'string') {
3415
- this.logger('error','参数错误: 键必须是非空字符串');
3416
- return null;
3362
+ throw new TypeError('键必须是非空字符串');
3417
3363
  }
3418
3364
  if (typeof decrement !== 'number' || isNaN(decrement) || !Number.isInteger(decrement)) {
3419
- this.logger('error','参数错误: 减量必须是有效整数');
3420
- return null;
3365
+ throw new TypeError('减量必须是有效整数');
3421
3366
  }
3422
3367
 
3423
3368
  // 触发前置事件
@@ -3430,7 +3375,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3430
3375
  this.emit('decrby:before', ctx);
3431
3376
 
3432
3377
  if (ctx.cancel) {
3433
- this.logger('debug',`decrby操作被事件处理器取消 [${key}]`);
3378
+ this.logger('debug', `decrby操作被事件处理器取消 [${key}]`);
3434
3379
  return null;
3435
3380
  }
3436
3381
 
@@ -3442,7 +3387,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3442
3387
 
3443
3388
  const connection = await this._getConnection();
3444
3389
  try {
3445
- const prefixedKey = this.applyPrefix(finalKey);
3390
+ const prefixedKey = this._addPrefix(finalKey);
3446
3391
  // 使用正确的Redis客户端Promise API
3447
3392
  const result = await connection.decrBy(prefixedKey, finalDecrement);
3448
3393
 
@@ -3463,7 +3408,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3463
3408
  });
3464
3409
  return result;
3465
3410
  } catch (err) {
3466
- this.logger('error',`减少指定值失败 [${finalKey}]:`, err);
3411
+ this.logger('error', `减少指定值失败 [${finalKey}]:`, err);
3467
3412
  this.emit('decrby:error', {
3468
3413
  key: finalKey,
3469
3414
  decrement: finalDecrement,
@@ -3475,7 +3420,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3475
3420
  this._recycleConnection(connection);
3476
3421
  }
3477
3422
  } catch (err) {
3478
- this.logger('error',`减少指定值失败 [${key}]:`, err);
3423
+ this.logger('error', `减少指定值失败 [${key}]:`, err);
3479
3424
  this.emit('decrby:error', {
3480
3425
  key,
3481
3426
  decrement,
@@ -3494,7 +3439,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3494
3439
  * @param {String|Array} members 成员或成员数组
3495
3440
  * @return {Promise<Number>} 添加的成员数量
3496
3441
  */
3497
- Redis.prototype.sadd = async function(key, members) {
3442
+ Redis.prototype.sadd = async function (key, members) {
3498
3443
  try {
3499
3444
  // 触发前置事件
3500
3445
  const ctx = {
@@ -3505,7 +3450,7 @@ Redis.prototype.sadd = async function(key, members) {
3505
3450
  this.emit('sadd:before', ctx);
3506
3451
 
3507
3452
  if (ctx.cancel) {
3508
- this.logger('debug',`sadd操作被事件处理器取消 [${key}]`);
3453
+ this.logger('debug', `sadd操作被事件处理器取消 [${key}]`);
3509
3454
  return 0;
3510
3455
  }
3511
3456
 
@@ -3516,7 +3461,7 @@ Redis.prototype.sadd = async function(key, members) {
3516
3461
 
3517
3462
  const connection = await this._getConnection();
3518
3463
  try {
3519
- const prefixedKey = this.applyPrefix(finalKey);
3464
+ const prefixedKey = this._addPrefix(finalKey);
3520
3465
  const result = await connection.sAdd(prefixedKey, finalMembers);
3521
3466
 
3522
3467
  // 触发后置事件
@@ -3530,7 +3475,7 @@ Redis.prototype.sadd = async function(key, members) {
3530
3475
  this._recycleConnection(connection);
3531
3476
  }
3532
3477
  } catch (err) {
3533
- this.logger('error',`添加集合成员失败 [${key}]:`, err);
3478
+ this.logger('error', `添加集合成员失败 [${key}]:`, err);
3534
3479
  this.emit('sadd:error', {
3535
3480
  key,
3536
3481
  members,
@@ -3546,7 +3491,7 @@ Redis.prototype.sadd = async function(key, members) {
3546
3491
  * @param {String|Array} members 成员或成员数组
3547
3492
  * @return {Promise<Number>} 移除的成员数量
3548
3493
  */
3549
- Redis.prototype.srem = async function(key, members) {
3494
+ Redis.prototype.srem = async function (key, members) {
3550
3495
  try {
3551
3496
  // 触发前置事件
3552
3497
  const ctx = {
@@ -3557,7 +3502,7 @@ Redis.prototype.srem = async function(key, members) {
3557
3502
  this.emit('srem:before', ctx);
3558
3503
 
3559
3504
  if (ctx.cancel) {
3560
- this.logger('debug',`srem操作被事件处理器取消 [${key}]`);
3505
+ this.logger('debug', `srem操作被事件处理器取消 [${key}]`);
3561
3506
  return 0;
3562
3507
  }
3563
3508
 
@@ -3568,7 +3513,7 @@ Redis.prototype.srem = async function(key, members) {
3568
3513
 
3569
3514
  const connection = await this._getConnection();
3570
3515
  try {
3571
- const prefixedKey = this.applyPrefix(finalKey);
3516
+ const prefixedKey = this._addPrefix(finalKey);
3572
3517
  const result = await connection.sRem(prefixedKey, finalMembers);
3573
3518
 
3574
3519
  // 触发后置事件
@@ -3582,7 +3527,7 @@ Redis.prototype.srem = async function(key, members) {
3582
3527
  this._recycleConnection(connection);
3583
3528
  }
3584
3529
  } catch (err) {
3585
- this.logger('error',`移除集合成员失败 [${key}]:`, err);
3530
+ this.logger('error', `移除集合成员失败 [${key}]:`, err);
3586
3531
  this.emit('srem:error', {
3587
3532
  key,
3588
3533
  members,
@@ -3598,16 +3543,14 @@ Redis.prototype.srem = async function(key, members) {
3598
3543
  * @param {String} member 成员
3599
3544
  * @return {Promise<Boolean>} 是否存在
3600
3545
  */
3601
- Redis.prototype.sismember = async function(key, member) {
3546
+ Redis.prototype.sismember = async function (key, member) {
3602
3547
  try {
3603
3548
  // 参数验证
3604
3549
  if (!key || typeof key !== 'string') {
3605
- this.logger('error','sismember参数错误: key must be a non-empty string');
3606
- return false;
3550
+ throw new TypeError('key必须是非空字符串');
3607
3551
  }
3608
3552
  if (member === undefined || member === null) {
3609
- this.logger('error','sismember参数错误: member cannot be undefined or null');
3610
- return false;
3553
+ throw new TypeError('member不能为undefined或null');
3611
3554
  }
3612
3555
 
3613
3556
  this.emit('sismember:before', {
@@ -3616,7 +3559,7 @@ Redis.prototype.sismember = async function(key, member) {
3616
3559
  });
3617
3560
  const connection = await this._getConnection();
3618
3561
  try {
3619
- const prefixedKey = this.applyPrefix(key);
3562
+ const prefixedKey = this._addPrefix(key);
3620
3563
  // 将成员转换为字符串,与其他模块行为一致
3621
3564
  const memberStr = String(member);
3622
3565
  const result = await connection.sIsMember(prefixedKey, memberStr);
@@ -3635,7 +3578,7 @@ Redis.prototype.sismember = async function(key, member) {
3635
3578
  member,
3636
3579
  error: err
3637
3580
  });
3638
- this.logger('error',`检查集合成员失败 [${key}:${member}]:`, err);
3581
+ this.logger('error', `检查集合成员失败 [${key}:${member}]:`, err);
3639
3582
  return false;
3640
3583
  }
3641
3584
  };
@@ -3645,11 +3588,11 @@ Redis.prototype.sismember = async function(key, member) {
3645
3588
  * @param {String} key 键
3646
3589
  * @return {Promise<Array>} 成员数组
3647
3590
  */
3648
- Redis.prototype.smembers = async function(key) {
3591
+ Redis.prototype.smembers = async function (key) {
3649
3592
  try {
3650
3593
  const connection = await this._getConnection();
3651
3594
  try {
3652
- const prefixedKey = this.applyPrefix(key);
3595
+ const prefixedKey = this._addPrefix(key);
3653
3596
  const result = await connection.sMembers(prefixedKey);
3654
3597
  this.emit('smembers:after', {
3655
3598
  key,
@@ -3660,7 +3603,7 @@ Redis.prototype.smembers = async function(key) {
3660
3603
  this._recycleConnection(connection);
3661
3604
  }
3662
3605
  } catch (err) {
3663
- this.logger('error',`获取集合所有成员失败 [${key}]:`, err);
3606
+ this.logger('error', `获取集合所有成员失败 [${key}]:`, err);
3664
3607
  this.emit('smembers:error', {
3665
3608
  key,
3666
3609
  error: err
@@ -3674,18 +3617,18 @@ Redis.prototype.smembers = async function(key) {
3674
3617
  * @param {String} key 键
3675
3618
  * @return {Promise<Number>} 成员数量
3676
3619
  */
3677
- Redis.prototype.scard = async function(key) {
3620
+ Redis.prototype.scard = async function (key) {
3678
3621
  try {
3679
3622
  const connection = await this._getConnection();
3680
3623
  try {
3681
- const prefixedKey = this.applyPrefix(key);
3624
+ const prefixedKey = this._addPrefix(key);
3682
3625
  const result = await connection.sCard(prefixedKey);
3683
3626
  return result;
3684
3627
  } finally {
3685
3628
  this._recycleConnection(connection);
3686
3629
  }
3687
3630
  } catch (err) {
3688
- this.logger('error',`获取集合大小失败 [${key}]:`, err);
3631
+ this.logger('error', `获取集合大小失败 [${key}]:`, err);
3689
3632
  return 0;
3690
3633
  }
3691
3634
  };
@@ -3695,11 +3638,11 @@ Redis.prototype.scard = async function(key) {
3695
3638
  * @param {Array} keys 键数组
3696
3639
  * @return {Promise<Array>} 交集成员数组
3697
3640
  */
3698
- Redis.prototype.sinter = async function(keys) {
3641
+ Redis.prototype.sinter = async function (keys) {
3699
3642
  try {
3700
3643
  const connection = await this._getConnection();
3701
3644
  try {
3702
- const prefixedKeys = keys.map(key => this.applyPrefix(key));
3645
+ const prefixedKeys = keys.map(key => this._addPrefix(key));
3703
3646
  // 使用Redis v4正确的方法
3704
3647
  const result = await connection.sInter(...prefixedKeys);
3705
3648
  return result || [];
@@ -3707,7 +3650,7 @@ Redis.prototype.sinter = async function(keys) {
3707
3650
  this._recycleConnection(connection);
3708
3651
  }
3709
3652
  } catch (err) {
3710
- this.logger('error',`获取集合交集失败:`, err);
3653
+ this.logger('error', `获取集合交集失败:`, err);
3711
3654
  return [];
3712
3655
  }
3713
3656
  };
@@ -3717,11 +3660,11 @@ Redis.prototype.sinter = async function(keys) {
3717
3660
  * @param {Array} keys 键数组
3718
3661
  * @return {Promise<Array>} 并集成员数组
3719
3662
  */
3720
- Redis.prototype.sunion = async function(keys) {
3663
+ Redis.prototype.sunion = async function (keys) {
3721
3664
  try {
3722
3665
  const connection = await this._getConnection();
3723
3666
  try {
3724
- const prefixedKeys = keys.map(key => this.applyPrefix(key));
3667
+ const prefixedKeys = keys.map(key => this._addPrefix(key));
3725
3668
  // 使用Redis v4正确的方法
3726
3669
  const result = await connection.sUnion(...prefixedKeys);
3727
3670
  return result || [];
@@ -3729,7 +3672,7 @@ Redis.prototype.sunion = async function(keys) {
3729
3672
  this._recycleConnection(connection);
3730
3673
  }
3731
3674
  } catch (err) {
3732
- this.logger('error',`获取集合并集失败:`, err);
3675
+ this.logger('error', `获取集合并集失败:`, err);
3733
3676
  return [];
3734
3677
  }
3735
3678
  };
@@ -3739,11 +3682,11 @@ Redis.prototype.sunion = async function(keys) {
3739
3682
  * @param {Array} keys 键数组
3740
3683
  * @return {Promise<Array>} 差集成员数组
3741
3684
  */
3742
- Redis.prototype.sdiff = async function(keys) {
3685
+ Redis.prototype.sdiff = async function (keys) {
3743
3686
  try {
3744
3687
  const connection = await this._getConnection();
3745
3688
  try {
3746
- const prefixedKeys = keys.map(key => this.applyPrefix(key));
3689
+ const prefixedKeys = keys.map(key => this._addPrefix(key));
3747
3690
  // 使用Redis v4正确的方法
3748
3691
  const result = await connection.sDiff(...prefixedKeys);
3749
3692
  return result || [];
@@ -3751,7 +3694,7 @@ Redis.prototype.sdiff = async function(keys) {
3751
3694
  this._recycleConnection(connection);
3752
3695
  }
3753
3696
  } catch (err) {
3754
- this.logger('error',`获取集合差集失败:`, err);
3697
+ this.logger('error', `获取集合差集失败:`, err);
3755
3698
  return [];
3756
3699
  }
3757
3700
  };
@@ -3765,7 +3708,7 @@ Redis.prototype.sdiff = async function(keys) {
3765
3708
  * @param {String} member 成员
3766
3709
  * @return {Promise<Boolean>} 是否成功
3767
3710
  */
3768
- Redis.prototype.zadd = async function(key, score, member) {
3711
+ Redis.prototype.zadd = async function (key, score, member) {
3769
3712
  try {
3770
3713
  // 触发前置事件
3771
3714
  const ctx = {
@@ -3777,7 +3720,7 @@ Redis.prototype.zadd = async function(key, score, member) {
3777
3720
  this.emit('zadd:before', ctx);
3778
3721
 
3779
3722
  if (ctx.cancel) {
3780
- this.logger('debug',`zadd操作被事件处理器取消 [${key}]`);
3723
+ this.logger('debug', `zadd操作被事件处理器取消 [${key}]`);
3781
3724
  return false;
3782
3725
  }
3783
3726
 
@@ -3789,7 +3732,7 @@ Redis.prototype.zadd = async function(key, score, member) {
3789
3732
 
3790
3733
  const connection = await this._getConnection();
3791
3734
  try {
3792
- const prefixedKey = this.applyPrefix(finalKey);
3735
+ const prefixedKey = this._addPrefix(finalKey);
3793
3736
  const result = await connection.zAdd(prefixedKey, {
3794
3737
  score: finalScore,
3795
3738
  value: finalMember
@@ -3807,7 +3750,7 @@ Redis.prototype.zadd = async function(key, score, member) {
3807
3750
  this._recycleConnection(connection);
3808
3751
  }
3809
3752
  } catch (err) {
3810
- this.logger('error',`添加有序集合成员失败 [${key}]:`, err);
3753
+ this.logger('error', `添加有序集合成员失败 [${key}]:`, err);
3811
3754
  this.emit('zadd:error', {
3812
3755
  key,
3813
3756
  score,
@@ -3824,7 +3767,7 @@ Redis.prototype.zadd = async function(key, score, member) {
3824
3767
  * @param {String|Array} members 成员或成员数组
3825
3768
  * @return {Promise<Number>} 移除的成员数量
3826
3769
  */
3827
- Redis.prototype.zrem = async function(key, members) {
3770
+ Redis.prototype.zrem = async function (key, members) {
3828
3771
  try {
3829
3772
  // 触发前置事件
3830
3773
  const ctx = {
@@ -3835,7 +3778,7 @@ Redis.prototype.zrem = async function(key, members) {
3835
3778
  this.emit('zrem:before', ctx);
3836
3779
 
3837
3780
  if (ctx.cancel) {
3838
- this.logger('debug',`zrem操作被事件处理器取消 [${key}]`);
3781
+ this.logger('debug', `zrem操作被事件处理器取消 [${key}]`);
3839
3782
  return 0;
3840
3783
  }
3841
3784
 
@@ -3847,7 +3790,7 @@ Redis.prototype.zrem = async function(key, members) {
3847
3790
  const connection = await this._getConnection();
3848
3791
  try {
3849
3792
  // 使用正确的Redis客户端Promise API
3850
- const result = await connection.zRem(this.applyPrefix(finalKey), ...finalMembers);
3793
+ const result = await connection.zRem(this._addPrefix(finalKey), ...finalMembers);
3851
3794
 
3852
3795
  // 触发后置事件
3853
3796
  this.emit('zrem:after', {
@@ -3857,7 +3800,7 @@ Redis.prototype.zrem = async function(key, members) {
3857
3800
  });
3858
3801
  return result;
3859
3802
  } catch (err) {
3860
- this.logger('error',`移除有序集合成员失败 [${finalKey}]:`, err);
3803
+ this.logger('error', `移除有序集合成员失败 [${finalKey}]:`, err);
3861
3804
  this.emit('zrem:error', {
3862
3805
  key: finalKey,
3863
3806
  members: finalMembers,
@@ -3868,7 +3811,7 @@ Redis.prototype.zrem = async function(key, members) {
3868
3811
  this._recycleConnection(connection);
3869
3812
  }
3870
3813
  } catch (err) {
3871
- this.logger('error',`移除有序集合成员失败 [${key}]:`, err);
3814
+ this.logger('error', `移除有序集合成员失败 [${key}]:`, err);
3872
3815
  this.emit('zrem:error', {
3873
3816
  key,
3874
3817
  members,
@@ -3884,18 +3827,18 @@ Redis.prototype.zrem = async function(key, members) {
3884
3827
  * @param {String} member 成员
3885
3828
  * @return {Promise<Number|null>} 分数
3886
3829
  */
3887
- Redis.prototype.zscore = async function(key, member) {
3830
+ Redis.prototype.zscore = async function (key, member) {
3888
3831
  try {
3889
3832
  const connection = await this._getConnection();
3890
3833
  try {
3891
- const prefixedKey = this.applyPrefix(key);
3834
+ const prefixedKey = this._addPrefix(key);
3892
3835
  const result = await connection.zScore(prefixedKey, member);
3893
3836
  return result ? parseFloat(result) : null;
3894
3837
  } finally {
3895
3838
  this._recycleConnection(connection);
3896
3839
  }
3897
3840
  } catch (err) {
3898
- this.logger('error',`获取有序集合成员分数失败 [${key}:${member}]:`, err);
3841
+ this.logger('error', `获取有序集合成员分数失败 [${key}:${member}]:`, err);
3899
3842
  return null;
3900
3843
  }
3901
3844
  };
@@ -3906,18 +3849,18 @@ Redis.prototype.zscore = async function(key, member) {
3906
3849
  * @param {String} member 成员
3907
3850
  * @return {Promise<Number|null>} 排名
3908
3851
  */
3909
- Redis.prototype.zrank = async function(key, member) {
3852
+ Redis.prototype.zrank = async function (key, member) {
3910
3853
  try {
3911
3854
  const connection = await this._getConnection();
3912
3855
  try {
3913
- const prefixedKey = this.applyPrefix(key);
3856
+ const prefixedKey = this._addPrefix(key);
3914
3857
  const result = await connection.zRank(prefixedKey, member);
3915
3858
  return result;
3916
3859
  } finally {
3917
3860
  this._recycleConnection(connection);
3918
3861
  }
3919
3862
  } catch (err) {
3920
- this.logger('error',`获取有序集合成员排名失败 [${key}:${member}]:`, err);
3863
+ this.logger('error', `获取有序集合成员排名失败 [${key}:${member}]:`, err);
3921
3864
  return null;
3922
3865
  }
3923
3866
  };
@@ -3930,11 +3873,11 @@ Redis.prototype.zrank = async function(key, member) {
3930
3873
  * @param {Boolean} withScores 是否返回分数
3931
3874
  * @return {Promise<Array>} 成员数组
3932
3875
  */
3933
- Redis.prototype.zrange = async function(key, start, stop, withScores = false) {
3876
+ Redis.prototype.zrange = async function (key, start, stop, withScores = false) {
3934
3877
  try {
3935
3878
  const connection = await this._getConnection();
3936
3879
  try {
3937
- const prefixedKey = this.applyPrefix(key);
3880
+ const prefixedKey = this._addPrefix(key);
3938
3881
 
3939
3882
  if (withScores) {
3940
3883
  // 对于需要分数的情况,使用更可靠的方法
@@ -3958,13 +3901,13 @@ Redis.prototype.zrange = async function(key, start, stop, withScores = false) {
3958
3901
  member,
3959
3902
  score: 0
3960
3903
  });
3961
- throw new Error(`获取成员 ${member} 分数失败:`, scoreErr);
3904
+ this.logger('error', `获取成员 ${member} 分数失败:`, scoreErr);
3962
3905
  }
3963
3906
  }
3964
3907
 
3965
3908
  return results;
3966
3909
  } catch (err) {
3967
- this.logger('error',`使用备用方法获取有序集合范围失败 [${prefixedKey}]:`, err);
3910
+ this.logger('error', `使用备用方法获取有序集合范围失败 [${prefixedKey}]:`, err);
3968
3911
  // 回退到简单列表
3969
3912
  return [];
3970
3913
  }
@@ -3977,7 +3920,7 @@ Redis.prototype.zrange = async function(key, start, stop, withScores = false) {
3977
3920
  this._recycleConnection(connection);
3978
3921
  }
3979
3922
  } catch (err) {
3980
- this.logger('error',`获取有序集合范围成员失败 [${key}]:`, err);
3923
+ this.logger('error', `获取有序集合范围成员失败 [${key}]:`, err);
3981
3924
  return [];
3982
3925
  }
3983
3926
  };
@@ -3987,21 +3930,21 @@ Redis.prototype.zrange = async function(key, start, stop, withScores = false) {
3987
3930
  * @param {String} key 键
3988
3931
  * @return {Promise<Number>} 成员数量
3989
3932
  */
3990
- Redis.prototype.zcard = async function(key) {
3933
+ Redis.prototype.zcard = async function (key) {
3991
3934
  try {
3992
3935
  const connection = await this._getConnection();
3993
3936
  try {
3994
3937
  // 使用正确的Redis客户端Promise API
3995
- const result = await connection.zCard(this.applyPrefix(key));
3938
+ const result = await connection.zCard(this._addPrefix(key));
3996
3939
  return result || 0;
3997
3940
  } catch (err) {
3998
- this.logger('error',`获取有序集合成员数量失败 [${key}]:`, err);
3941
+ this.logger('error', `获取有序集合成员数量失败 [${key}]:`, err);
3999
3942
  return 0;
4000
3943
  } finally {
4001
3944
  this._recycleConnection(connection);
4002
3945
  }
4003
3946
  } catch (err) {
4004
- this.logger('error',`获取有序集合成员数量失败 [${key}]:`, err);
3947
+ this.logger('error', `获取有序集合成员数量失败 [${key}]:`, err);
4005
3948
  return 0;
4006
3949
  }
4007
3950
  };
@@ -4014,20 +3957,17 @@ Redis.prototype.zcard = async function(key) {
4014
3957
  * @param {Number} [seconds=0] 过期时间(秒)
4015
3958
  * @return {Promise<Number>} 增加后的分数
4016
3959
  */
4017
- Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
3960
+ Redis.prototype.zincrby = async function (key, increment, member, seconds = 0) {
4018
3961
  try {
4019
3962
  // 参数验证
4020
3963
  if (!key || typeof key !== 'string') {
4021
- this.logger('error','参数错误: 键必须是非空字符串');
4022
- return null;
3964
+ throw new TypeError('键必须是非空字符串');
4023
3965
  }
4024
3966
  if (typeof increment !== 'number' || isNaN(increment)) {
4025
- this.logger('error','参数错误: 增量必须是有效数字');
4026
- return null;
3967
+ throw new TypeError('增量必须是有效数字');
4027
3968
  }
4028
3969
  if (!member || typeof member !== 'string') {
4029
- this.logger('error','参数错误: 成员必须是非空字符串');
4030
- return null;
3970
+ throw new TypeError('成员必须是非空字符串');
4031
3971
  }
4032
3972
 
4033
3973
  // 触发前置事件
@@ -4041,7 +3981,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4041
3981
  this.emit('zincrby:before', ctx);
4042
3982
 
4043
3983
  if (ctx.cancel) {
4044
- this.logger('debug',`zincrby操作被事件处理器取消 [${key}]`);
3984
+ this.logger('debug', `zincrby操作被事件处理器取消 [${key}]`);
4045
3985
  return null;
4046
3986
  }
4047
3987
 
@@ -4054,7 +3994,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4054
3994
 
4055
3995
  const connection = await this._getConnection();
4056
3996
  try {
4057
- const prefixedKey = this.applyPrefix(finalKey);
3997
+ const prefixedKey = this._addPrefix(finalKey);
4058
3998
  // 使用正确的Redis客户端Promise API
4059
3999
  const result = await connection.zIncrBy(prefixedKey, finalIncrement, finalMember);
4060
4000
 
@@ -4077,7 +4017,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4077
4017
  });
4078
4018
  return parseFloat(result);
4079
4019
  } catch (err) {
4080
- this.logger('error',`增加有序集合成员分数失败 [${finalKey}]:`, err);
4020
+ this.logger('error', `增加有序集合成员分数失败 [${finalKey}]:`, err);
4081
4021
  this.emit('zincrby:error', {
4082
4022
  key: finalKey,
4083
4023
  increment: finalIncrement,
@@ -4090,7 +4030,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4090
4030
  this._recycleConnection(connection);
4091
4031
  }
4092
4032
  } catch (err) {
4093
- this.logger('error',`增加有序集合成员分数失败 [${key}]:`, err);
4033
+ this.logger('error', `增加有序集合成员分数失败 [${key}]:`, err);
4094
4034
  this.emit('zincrby:error', {
4095
4035
  key,
4096
4036
  increment,
@@ -4103,7 +4043,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4103
4043
  };
4104
4044
 
4105
4045
  // 创建真实Redis连接的方法
4106
- Redis.prototype._createConnection = function() {
4046
+ Redis.prototype._createConnection = function () {
4107
4047
  return new Promise((resolve, reject) => {
4108
4048
  try {
4109
4049
  const cg = this.config;
@@ -4124,7 +4064,7 @@ Redis.prototype._createConnection = function() {
4124
4064
 
4125
4065
  // 监听错误事件
4126
4066
  connection.on('error', (err) => {
4127
- this.logger('error',`连接 ${connection.id} 错误:`, err);
4067
+ this.logger('error', `连接 ${connection.id} 错误:`, err);
4128
4068
  });
4129
4069
 
4130
4070
  // 连接Redis
@@ -4132,11 +4072,11 @@ Redis.prototype._createConnection = function() {
4132
4072
  // this.logger('debug','新连接已创建并连接:', connection.id);
4133
4073
  resolve(connection);
4134
4074
  }).catch(err => {
4135
- this.logger('error','创建连接失败:', err);
4075
+ this.logger('error', '创建连接失败:', err);
4136
4076
  reject(err);
4137
4077
  });
4138
4078
  } catch (error) {
4139
- this.logger('error','创建连接时发生异常:', error);
4079
+ this.logger('error', '创建连接时发生异常:', error);
4140
4080
  reject(error);
4141
4081
  }
4142
4082
  });