mm_redis 2.0.8 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -34,12 +34,10 @@ class Redis extends Base {
34
34
  // 重试间隔(ms)
35
35
  "retry_delay": 1000,
36
36
  // 连接池配置
37
- "pool": {
38
- "max_clients": 100, // 最大连接数
39
- "min_idle": 5, // 最小空闲连接数
40
- "max_idle": 20, // 最大空闲连接数
41
- "idle_timeout": 30000 // 空闲超时时间(ms)
42
- }
37
+ "max_clients": 100, // 最大连接数
38
+ "min_idle": 5, // 最小空闲连接数
39
+ "max_idle": 20, // 最大空闲连接数
40
+ "idle_timeout": 30000 // 空闲超时时间(ms)
43
41
  };
44
42
  this.setConfig(config);
45
43
  // 初始化连接池
@@ -87,20 +85,20 @@ Redis.prototype._delPrefix = function (key) {
87
85
  * @return {Promise<Boolean>} 订阅结果
88
86
  */
89
87
  Redis.prototype.subscribe = async function (channel, func) {
90
- try {
91
- // 参数验证
92
- if (!channel || typeof channel !== 'string') {
93
- throw new TypeError('channel必须是非空字符串');
94
- }
95
- if (typeof func !== 'function') {
96
- throw new TypeError('func必须是函数');
97
- }
88
+ // 参数验证
89
+ if (!channel || typeof channel !== 'string') {
90
+ throw new TypeError('channel必须是非空字符串');
91
+ }
92
+ if (typeof func !== 'function') {
93
+ throw new TypeError('func必须是函数');
94
+ }
98
95
 
96
+ try {
99
97
  await this.conn.subscribe(this._addPrefix(channel));
100
98
  this.func_list[channel] = func;
101
99
  return true;
102
100
  } catch (err) {
103
- this.logger('error', `订阅频道 ${channel} 失败:`, err);
101
+ this.log('error', `订阅频道 ${channel} 失败:`, err);
104
102
  return false;
105
103
  }
106
104
  };
@@ -111,17 +109,17 @@ Redis.prototype.subscribe = async function (channel, func) {
111
109
  * @return {Promise<Boolean>} 取消订阅结果
112
110
  */
113
111
  Redis.prototype.unsubscribe = async function (channel) {
114
- try {
115
- // 参数验证
116
- if (!channel || typeof channel !== 'string') {
117
- throw new TypeError('channel必须是非空字符串');
118
- }
112
+ // 参数验证
113
+ if (!channel || typeof channel !== 'string') {
114
+ throw new TypeError('channel必须是非空字符串');
115
+ }
119
116
 
117
+ try {
120
118
  await this.conn.unsubscribe(this._addPrefix(channel));
121
119
  delete this.func_list[channel];
122
120
  return true;
123
121
  } catch (err) {
124
- this.logger('error', `取消订阅频道 ${channel} 失败:`, err);
122
+ this.log('error', `取消订阅频道 ${channel} 失败:`, err);
125
123
  return false;
126
124
  }
127
125
  };
@@ -133,18 +131,18 @@ Redis.prototype.unsubscribe = async function (channel) {
133
131
  * @return {Promise<Number>} 接收消息的客户端数量
134
132
  */
135
133
  Redis.prototype.publish = async function (channel, message) {
136
- try {
137
- // 参数验证
138
- if (!channel || typeof channel !== 'string') {
139
- throw new TypeError('channel必须是非空字符串');
140
- }
141
- if (message === undefined || message === null) {
142
- throw new TypeError('message不能为undefined或null');
143
- }
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
+ }
144
141
 
142
+ try {
145
143
  return await this.conn.publish(this._addPrefix(channel), message);
146
144
  } catch (err) {
147
- this.logger('error', `发布消息到频道 ${channel} 失败:`, err);
145
+ this.log('error', `发布消息到频道 ${channel} 失败:`, err);
148
146
  return false;
149
147
  }
150
148
  };
@@ -156,7 +154,7 @@ Redis.prototype.publish = async function (channel, message) {
156
154
  */
157
155
  Redis.prototype.main = async function (channel, message) {
158
156
  // 默认实现,可由子类重写
159
- this.logger('debug', `收到未处理的消息 [${channel}]:`, message);
157
+ this.log('debug', `收到未处理的消息 [${channel}]:`, message);
160
158
  };
161
159
 
162
160
  /**
@@ -172,7 +170,7 @@ Redis.prototype.message = function (channel, message) {
172
170
  try {
173
171
  func(message);
174
172
  } catch (err) {
175
- this.logger('error', `处理频道 ${channel} 的消息时出错:`, err);
173
+ this.log('error', `处理频道 ${channel} 的消息时出错:`, err);
176
174
  }
177
175
  } else {
178
176
  this.main(channel, message);
@@ -189,52 +187,14 @@ Redis.prototype.setConfig = function (config) {
189
187
  obj = config.loadJson(__dirname);
190
188
  } else if (config) {
191
189
  // 合并配置参数
192
- if (config.pool) {
193
- // 确保连接池配置的合并不会覆盖所有默认值
194
- if (!this.config.pool) {
195
- this.config.pool = {};
196
- }
197
- Object.assign(this.config.pool, config.pool);
198
- // 删除config中的pool属性,避免后续Object.assign重复处理
199
- const configCopy = {
200
- ...config
201
- };
202
- delete configCopy.pool;
203
- Object.assign(this.config, configCopy);
204
- } else {
205
- Object.assign(this.config, config);
206
- }
190
+ Object.assign(this.config, config);
207
191
  } else {
208
192
  return;
209
193
  }
210
194
 
211
- // 确保连接池配置存在且包含所有必要字段
212
- if (!this.config.pool) {
213
- this.config.pool = {};
214
- }
215
-
216
- // 设置默认连接池配置,只覆盖未定义的字段
217
- const defaultPoolConfig = {
218
- max_clients: 100,
219
- min_idle: 5,
220
- max_idle: 20,
221
- idle_timeout: 30000
222
- };
223
-
224
- for (const key in defaultPoolConfig) {
225
- if (this.config.pool[key] === undefined) {
226
- this.config.pool[key] = defaultPoolConfig[key];
227
- }
228
- }
229
-
230
- // 如果连接池已经初始化,更新其配置
231
- if (this.connection_pool) {
232
- this.connection_pool.config = this.config.pool;
233
- }
234
-
235
195
  // 初始化连接池
236
196
  this._initPool();
237
- // this.logger('debug','配置已更新');
197
+ // this.log('debug','配置已更新');
238
198
  this.identifier = this.config.host + "/" + this.config.database;
239
199
  return this;
240
200
  };
@@ -242,24 +202,16 @@ Redis.prototype.setConfig = function (config) {
242
202
  // 添加连接池初始化方法
243
203
  Redis.prototype._initPool = function () {
244
204
  // 确保连接池配置存在
245
- if (!this.config.pool) {
246
- this.config.pool = {
247
- max_clients: 100,
248
- min_idle: 5,
249
- max_idle: 20,
250
- idle_timeout: 30000
251
- };
252
- }
253
205
 
254
206
  // 初始化连接池相关属性
255
207
  this.connection_pool = {
256
208
  available: [],
257
209
  in_use: new Set(),
258
- config: this.config.pool // 直接引用配置对象,确保同步更新
210
+ config: this.config // 直接引用配置对象,确保同步更新
259
211
  };
260
212
 
261
213
  // 记录连接池初始化
262
- // this.logger('debug','连接池已初始化,配置:', this.config.pool);
214
+ // this.log('debug','连接池已初始化,配置:', this.config);
263
215
  };
264
216
 
265
217
  /**
@@ -288,7 +240,7 @@ Redis.prototype.open = async function () {
288
240
 
289
241
  // 监听错误事件
290
242
  this.conn.on('error', (err) => {
291
- this.logger('error', 'Redis连接错误:', err);
243
+ this.log('error', 'Redis连接错误:', err);
292
244
  this.connected = false;
293
245
  // 尝试重连
294
246
  this._reconnect();
@@ -296,25 +248,25 @@ Redis.prototype.open = async function () {
296
248
 
297
249
  // 监听连接成功事件
298
250
  this.conn.on('connect', () => {
299
- // this.logger('debug','Redis已连接');
251
+ // this.log('debug','Redis已连接');
300
252
  this.connected = true;
301
253
  this.retry_count = 0;
302
254
  });
303
255
 
304
256
  // 监听断开连接事件
305
257
  this.conn.on('end', () => {
306
- // this.logger('debug','Redis连接已断开');
258
+ // this.log('debug','Redis连接已断开');
307
259
  this.connected = false;
308
260
  });
309
261
 
310
262
  // 监听订阅成功事件
311
263
  this.conn.on('subscribe', (channel, count) => {
312
- this.logger('debug', `频道${this._delPrefix(channel)}订阅成功, 当前总计订阅数:${count}`);
264
+ this.log('debug', `频道${this._delPrefix(channel)}订阅成功, 当前总计订阅数:${count}`);
313
265
  });
314
266
 
315
267
  // 监听取消订阅事件
316
268
  this.conn.on('unsubscribe', (channel, count) => {
317
- this.logger('debug', `频道${this._delPrefix(channel)}取消订阅, 当前总计订阅数:${count}`);
269
+ this.log('debug', `频道${this._delPrefix(channel)}取消订阅, 当前总计订阅数:${count}`);
318
270
  });
319
271
 
320
272
  // 收到消息后执行回调
@@ -327,7 +279,7 @@ Redis.prototype.open = async function () {
327
279
  this.connected = true;
328
280
  return true;
329
281
  } catch (err) {
330
- this.logger('error', 'Redis连接失败:', err);
282
+ this.log('error', 'Redis连接失败:', err);
331
283
  // 尝试重连
332
284
  this._reconnect();
333
285
  return false;
@@ -340,18 +292,18 @@ Redis.prototype.open = async function () {
340
292
  */
341
293
  Redis.prototype._reconnect = function () {
342
294
  if (this.retry_count >= this.config.retry_count) {
343
- this.logger('error', `达到最大重连次数 ${this.config.retry_count},停止重连`);
295
+ this.log('error', `达到最大重连次数 ${this.config.retry_count},停止重连`);
344
296
  return;
345
297
  }
346
298
 
347
299
  this.retry_count++;
348
- this.logger('debug', `尝试第 ${this.retry_count} 次重连...`);
300
+ this.log('debug', `尝试第 ${this.retry_count} 次重连...`);
349
301
 
350
302
  setTimeout(async () => {
351
303
  try {
352
304
  await this.open();
353
305
  } catch (err) {
354
- this.logger('error', `重连失败:`, err);
306
+ this.log('error', `重连失败:`, err);
355
307
  }
356
308
  }, this.config.retry_delay);
357
309
  };
@@ -363,7 +315,7 @@ Redis.prototype._reconnect = function () {
363
315
  // 修复close方法,正确处理连接池
364
316
  Redis.prototype.close = function () {
365
317
  try {
366
- // this.logger('debug','关闭所有连接...');
318
+ // this.log('debug','关闭所有连接...');
367
319
 
368
320
  // 关闭所有使用中的连接
369
321
  if (this.connection_pool && this.connection_pool.in_use) {
@@ -371,7 +323,7 @@ Redis.prototype.close = function () {
371
323
  try {
372
324
  connection.close();
373
325
  } catch (e) {
374
- this.logger('error', '关闭连接失败:', e);
326
+ this.log('error', '关闭连接失败:', e);
375
327
  }
376
328
  }
377
329
  this.connection_pool.in_use.clear();
@@ -388,20 +340,20 @@ Redis.prototype.close = function () {
388
340
  // 某些Redis客户端可能使用end方法
389
341
  connection.end();
390
342
  } else {
391
- this.logger('debug', '连接对象没有可用的关闭方法');
343
+ this.log('debug', '连接对象没有可用的关闭方法');
392
344
  }
393
345
  } catch (e) {
394
- this.logger('error', '关闭连接失败:', e);
346
+ this.log('error', '关闭连接失败:', e);
395
347
  }
396
348
  }
397
349
  this.connection_pool.available = [];
398
350
  }
399
351
 
400
352
  this.connected = false;
401
- this.logger('debug', 'Redis连接已断开');
353
+ this.log('debug', 'Redis连接已断开');
402
354
  return Promise.resolve(true);
403
355
  } catch (error) {
404
- this.logger('error', '关闭连接时发生错误:', error);
356
+ this.log('error', '关闭连接时发生错误:', error);
405
357
  return Promise.resolve(false);
406
358
  }
407
359
  };
@@ -528,7 +480,7 @@ Redis.prototype.pipeline = function () {
528
480
  // 直接返回结果值,符合测试期望的格式
529
481
  results.push(result);
530
482
  } catch (err) {
531
- this.logger('error', `Pipeline命令执行失败 [${cmd[0]}]:`, err);
483
+ this.log('error', `Pipeline命令执行失败 [${cmd[0]}]:`, err);
532
484
  results.push(null);
533
485
  }
534
486
  }
@@ -537,7 +489,7 @@ Redis.prototype.pipeline = function () {
537
489
  redisInstance._recycleConnection(connection);
538
490
  return results;
539
491
  } catch (error) {
540
- this.logger('error', 'Pipeline执行失败:', error);
492
+ this.log('error', 'Pipeline执行失败:', error);
541
493
  return [];
542
494
  }
543
495
  }
@@ -552,12 +504,6 @@ Redis.prototype.isConnected = function () {
552
504
  return this.connected && this.conn && this.conn.isReady;
553
505
  };
554
506
 
555
- /**
556
- * @description 查询或是设置缓存过期时间
557
- * @param {String} key 键
558
- * @param {Number} seconds 秒, 为空则查询有效时长
559
- * @return {Promise<Number|Boolean>} 查询时长或设置结果, 时长为-1表示永不过期
560
- */
561
507
  /**
562
508
  * 获取或设置键的过期时间
563
509
  * @param {String} key 键名
@@ -565,11 +511,12 @@ Redis.prototype.isConnected = function () {
565
511
  * @return {Promise<Number|Boolean>} 不传seconds时返回剩余过期时间(-1表示无过期设置,-2表示已过期),传seconds时返回是否设置成功
566
512
  */
567
513
  Redis.prototype.ttl = async function (key, seconds) {
514
+ // 参数验证
515
+ if (!key || typeof key !== 'string') {
516
+ throw new TypeError('key必须是非空字符串');
517
+ }
518
+
568
519
  try {
569
- // 参数验证
570
- if (!key || typeof key !== 'string') {
571
- throw new TypeError('key必须是非空字符串');
572
- }
573
520
 
574
521
  const connection = await this._getConnection();
575
522
  try {
@@ -589,7 +536,7 @@ Redis.prototype.ttl = async function (key, seconds) {
589
536
  await connection.execute('EXPIRE', [key, seconds]);
590
537
  return true;
591
538
  } else {
592
- this.logger('error', '连接对象不支持expire操作');
539
+ this.log('error', '连接对象不支持expire操作');
593
540
  return false;
594
541
  }
595
542
  }
@@ -600,14 +547,14 @@ Redis.prototype.ttl = async function (key, seconds) {
600
547
  } else if (typeof connection.execute === 'function') {
601
548
  return await connection.execute('TTL', [key]);
602
549
  } else {
603
- this.logger('error', '连接对象不支持ttl操作');
550
+ this.log('error', '连接对象不支持ttl操作');
604
551
  return -2; // 默认返回键不存在
605
552
  }
606
553
  } finally {
607
554
  this._recycleConnection(connection);
608
555
  }
609
556
  } catch (err) {
610
- this.logger('error', `${seconds !== undefined ? '设置' : '获取'}过期时间失败 [${key}]:`, err);
557
+ this.log('error', `${seconds !== undefined ? '设置' : '获取'}过期时间失败 [${key}]:`, err);
611
558
  return seconds !== undefined ? false : -2;
612
559
  }
613
560
  };
@@ -620,16 +567,15 @@ Redis.prototype.ttl = async function (key, seconds) {
620
567
  * @return {Promise<Number>} 计算后的结果
621
568
  */
622
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
+
623
578
  try {
624
- // 参数验证
625
- if (!key || typeof key !== 'string') {
626
- this.logger('error', '参数错误: 键必须是非空字符串');
627
- return false;
628
- }
629
- if (typeof num !== 'number' || isNaN(num) || !Number.isInteger(num)) {
630
- this.logger('error', '参数错误: 数值必须是有效整数');
631
- return false;
632
- }
633
579
 
634
580
  // 触发前置事件,允许修改或阻止操作
635
581
  const ctx = {
@@ -642,7 +588,7 @@ Redis.prototype.addInt = async function (key, num, seconds = 0) {
642
588
 
643
589
  // 如果事件处理器标记为取消,则返回false
644
590
  if (ctx.cancel) {
645
- this.logger('debug', `addInt操作被事件处理器取消 [${key}]`);
591
+ this.log('debug', `addInt操作被事件处理器取消 [${key}]`);
646
592
  return false;
647
593
  }
648
594
 
@@ -665,7 +611,7 @@ Redis.prototype.addInt = async function (key, num, seconds = 0) {
665
611
  result = await connection.execute('INCRBY', [prefixedKey, finalNum]);
666
612
  } else {
667
613
  // 如果出错,回退到先获取再设置的方式
668
- this.logger('warn', `使用原子操作增加整数值失败,使用回退方案 [${key}]`);
614
+ this.log('warn', `使用原子操作增加整数值失败,使用回退方案 [${key}]`);
669
615
  let value = await this.get(prefixedKey);
670
616
  let n = 0;
671
617
  if (value) {
@@ -683,7 +629,7 @@ Redis.prototype.addInt = async function (key, num, seconds = 0) {
683
629
  result = await connection.execute('DECRBY', [prefixedKey, -finalNum]);
684
630
  } else {
685
631
  // 如果出错,回退到先获取再设置的方式
686
- this.logger('warn', `使用原子操作减少整数值失败,使用回退方案`);
632
+ this.log('warn', `使用原子操作减少整数值失败,使用回退方案`);
687
633
  let value = await this.get(prefixedKey);
688
634
  let n = 0;
689
635
  if (value) {
@@ -724,7 +670,7 @@ Redis.prototype.addInt = async function (key, num, seconds = 0) {
724
670
  seconds,
725
671
  error: err
726
672
  });
727
- this.logger('error', `增加整数值失败 [${key}]:`, err);
673
+ this.log('error', `增加整数值失败 [${key}]:`, err);
728
674
  // 与CacheBase保持一致,错误时返回默认值而不是抛出异常
729
675
  return 0;
730
676
  }
@@ -738,17 +684,18 @@ Redis.prototype.addInt = async function (key, num, seconds = 0) {
738
684
  * @return {Promise<Number>} 计算后的结果
739
685
  */
740
686
  Redis.prototype.addFloat = async function (key, num, seconds = 0) {
741
- try {
742
- // 参数验证
743
- if (!key || typeof key !== 'string') {
744
- throw new TypeError('key必须是非空字符串');
745
- }
687
+ // 参数验证
688
+ if (!key || typeof key !== 'string') {
689
+ throw new TypeError('key必须是非空字符串');
690
+ }
746
691
 
747
- // 验证num参数
748
- num = parseFloat(num);
749
- if (isNaN(num)) {
750
- throw new TypeError('num必须是有效的数值');
751
- }
692
+ // 验证num参数
693
+ num = parseFloat(num);
694
+ if (isNaN(num)) {
695
+ throw new TypeError('num必须是有效的数值');
696
+ }
697
+
698
+ try {
752
699
 
753
700
  // 触发前置事件,允许修改或阻止操作
754
701
  const ctx = {
@@ -761,7 +708,7 @@ Redis.prototype.addFloat = async function (key, num, seconds = 0) {
761
708
 
762
709
  // 如果事件处理器标记为取消,则返回默认值
763
710
  if (ctx.cancel) {
764
- this.logger('debug', `addFloat操作被事件处理器取消 [${key}]`);
711
+ this.log('debug', `addFloat操作被事件处理器取消 [${key}]`);
765
712
  return 0;
766
713
  }
767
714
 
@@ -787,7 +734,7 @@ Redis.prototype.addFloat = async function (key, num, seconds = 0) {
787
734
  }
788
735
  // 回退方案
789
736
  else {
790
- this.logger('warn', `连接对象不支持incrByFloat操作,使用回退方案`);
737
+ this.log('warn', `连接对象不支持incrByFloat操作,使用回退方案`);
791
738
  let value = await this.get(prefixedKey);
792
739
  let n = 0;
793
740
  if (value) {
@@ -818,7 +765,7 @@ Redis.prototype.addFloat = async function (key, num, seconds = 0) {
818
765
  return result;
819
766
  } catch (err) {
820
767
  // 如果出错,回退到先获取再设置的方式
821
- this.logger('warn', `使用原子操作增加浮点数值失败,使用回退方案:`, err);
768
+ this.log('warn', `使用原子操作增加浮点数值失败,使用回退方案:`, err);
822
769
  let value = await this.get(prefixedKey);
823
770
  let n = 0;
824
771
  if (value) {
@@ -847,7 +794,7 @@ Redis.prototype.addFloat = async function (key, num, seconds = 0) {
847
794
  seconds,
848
795
  error: err
849
796
  });
850
- this.logger('error', `增加浮点数值失败 [${key}]:`, err);
797
+ this.log('error', `增加浮点数值失败 [${key}]:`, err);
851
798
  // 与CacheBase保持一致,错误时返回默认值而不是抛出异常
852
799
  return 0;
853
800
  }
@@ -884,7 +831,7 @@ Redis.prototype.addStr = async function (key, str, seconds = 0) {
884
831
 
885
832
  // 如果事件处理器标记为取消,则返回0
886
833
  if (ctx.cancel) {
887
- this.logger('debug', `addStr操作被事件处理器取消 [${key}]`);
834
+ this.log('debug', `addStr操作被事件处理器取消 [${key}]`);
888
835
  return 0;
889
836
  }
890
837
 
@@ -910,7 +857,7 @@ Redis.prototype.addStr = async function (key, str, seconds = 0) {
910
857
  }
911
858
  // 回退方案
912
859
  else {
913
- this.logger('warn', `连接对象不支持append操作,使用回退方案`);
860
+ this.log('warn', `连接对象不支持append操作,使用回退方案`);
914
861
  let value = await this.get(prefixedKey) || '';
915
862
  value += finalStr;
916
863
  await this.set(prefixedKey, value, finalSeconds);
@@ -936,7 +883,7 @@ Redis.prototype.addStr = async function (key, str, seconds = 0) {
936
883
  });
937
884
  return result;
938
885
  } catch (err) {
939
- this.logger('warn', '追加字符串失败:', err);
886
+ this.log('warn', '追加字符串失败:', err);
940
887
  // 回退方案:先获取再设置
941
888
  let value = await this.get(prefixedKey) || '';
942
889
  value += finalStr;
@@ -962,7 +909,7 @@ Redis.prototype.addStr = async function (key, str, seconds = 0) {
962
909
  seconds,
963
910
  error: err
964
911
  });
965
- this.logger('error', `追加字符串失败 [${key}]:`, err);
912
+ this.log('error', `追加字符串失败 [${key}]:`, err);
966
913
  return 0;
967
914
  }
968
915
  };
@@ -1005,7 +952,7 @@ Redis.prototype.del = async function (key) {
1005
952
 
1006
953
  // 如果事件处理器标记为取消,则返回false
1007
954
  if (ctx.cancel) {
1008
- this.logger('debug', `del操作被事件处理器取消 [${key}]`);
955
+ this.log('debug', `del操作被事件处理器取消 [${key}]`);
1009
956
  return false;
1010
957
  }
1011
958
 
@@ -1058,7 +1005,7 @@ Redis.prototype.del = async function (key) {
1058
1005
  key,
1059
1006
  error: err
1060
1007
  });
1061
- this.logger('error', `删除缓存失败 [${key}]:`, err);
1008
+ this.log('error', `删除缓存失败 [${key}]:`, err);
1062
1009
  return false;
1063
1010
  }
1064
1011
  };
@@ -1095,7 +1042,7 @@ Redis.prototype.set = async function (key, value, seconds = 0) {
1095
1042
 
1096
1043
  // 如果事件处理器标记为取消,则返回false
1097
1044
  if (ctx.cancel) {
1098
- this.logger('debug', `set操作被事件处理器取消 [${key}]`);
1045
+ this.log('debug', `set操作被事件处理器取消 [${key}]`);
1099
1046
  return false;
1100
1047
  }
1101
1048
 
@@ -1133,7 +1080,7 @@ Redis.prototype.set = async function (key, value, seconds = 0) {
1133
1080
  this._recycleConnection(connection);
1134
1081
  }
1135
1082
  } catch (err) {
1136
- this.logger('error', `设置缓存失败 [${key}]:`, err);
1083
+ this.log('error', `设置缓存失败 [${key}]:`, err);
1137
1084
  // 触发错误事件
1138
1085
  this.emit('set:error', {
1139
1086
  key,
@@ -1173,7 +1120,7 @@ Redis.prototype.add = async function (key, value, seconds) {
1173
1120
 
1174
1121
  // 如果事件处理器标记为取消,则返回false
1175
1122
  if (ctx.cancel) {
1176
- this.logger('debug', `add操作被事件处理器取消 [${key}]`);
1123
+ this.log('debug', `add操作被事件处理器取消 [${key}]`);
1177
1124
  return false;
1178
1125
  }
1179
1126
 
@@ -1213,7 +1160,7 @@ Redis.prototype.add = async function (key, value, seconds) {
1213
1160
  this._recycleConnection(connection);
1214
1161
  }
1215
1162
  } catch (err) {
1216
- this.logger('error', `添加缓存失败 [${key}]:`, err);
1163
+ this.log('error', `添加缓存失败 [${key}]:`, err);
1217
1164
  // 触发错误事件
1218
1165
  this.emit('add:error', {
1219
1166
  key,
@@ -1251,7 +1198,7 @@ Redis.prototype.get = async function (key) {
1251
1198
 
1252
1199
  // 如果事件处理器标记为取消,则返回null
1253
1200
  if (ctx.cancel) {
1254
- this.logger('debug', `get操作被事件处理器取消 [${key}]`);
1201
+ this.log('debug', `get操作被事件处理器取消 [${key}]`);
1255
1202
  return null;
1256
1203
  }
1257
1204
 
@@ -1292,7 +1239,7 @@ Redis.prototype.get = async function (key) {
1292
1239
  key,
1293
1240
  error: err
1294
1241
  });
1295
- this.logger('error', `获取缓存失败 [${key}]:`, err);
1242
+ this.log('error', `获取缓存失败 [${key}]:`, err);
1296
1243
  return null;
1297
1244
  }
1298
1245
  };
@@ -1318,7 +1265,7 @@ Redis.prototype.has = async function (key) {
1318
1265
 
1319
1266
  // 如果事件处理器标记为取消,则返回false
1320
1267
  if (ctx.cancel) {
1321
- this.logger('debug', `has操作被事件处理器取消 [${key}]`);
1268
+ this.log('debug', `has操作被事件处理器取消 [${key}]`);
1322
1269
  return false;
1323
1270
  }
1324
1271
 
@@ -1348,7 +1295,7 @@ Redis.prototype.has = async function (key) {
1348
1295
  key,
1349
1296
  error: err
1350
1297
  });
1351
- this.logger('error', `检查键是否存在失败 [${key}]:`, err);
1298
+ this.log('error', `检查键是否存在失败 [${key}]:`, err);
1352
1299
  return false;
1353
1300
  }
1354
1301
  };
@@ -1391,7 +1338,7 @@ Redis.prototype.exists = async function (key) {
1391
1338
 
1392
1339
  // 如果事件处理器标记为取消,则返回0
1393
1340
  if (ctx.cancel) {
1394
- this.logger('debug', `exists操作被事件处理器取消 [${key}]`);
1341
+ this.log('debug', `exists操作被事件处理器取消 [${key}]`);
1395
1342
  return 0;
1396
1343
  }
1397
1344
 
@@ -1426,7 +1373,7 @@ Redis.prototype.exists = async function (key) {
1426
1373
  key,
1427
1374
  error: err
1428
1375
  });
1429
- this.logger('error', `检查键存在性失败 [${key}]:`, err);
1376
+ this.log('error', `检查键存在性失败 [${key}]:`, err);
1430
1377
  return 0;
1431
1378
  }
1432
1379
  };
@@ -1445,7 +1392,7 @@ Redis.prototype.getrange = async function (key, start, end) {
1445
1392
  key = this._addPrefix(key);
1446
1393
  return await connection.getRange(key, start, end);
1447
1394
  } catch (err) {
1448
- this.logger('error', '获取字符串范围失败:', err);
1395
+ this.log('error', '获取字符串范围失败:', err);
1449
1396
  // 回退方案
1450
1397
  const value = await this.get(key);
1451
1398
  if (value && typeof value === 'string') {
@@ -1456,7 +1403,7 @@ Redis.prototype.getrange = async function (key, start, end) {
1456
1403
  this._recycleConnection(connection);
1457
1404
  }
1458
1405
  } catch (err) {
1459
- this.logger('error', `获取字符串范围失败 [${key}]:`, err);
1406
+ this.log('error', `获取字符串范围失败 [${key}]:`, err);
1460
1407
  return '';
1461
1408
  }
1462
1409
  };
@@ -1492,7 +1439,7 @@ Redis.prototype.setrange = async function (key, index, value) {
1492
1439
 
1493
1440
  // 如果事件处理器标记为取消,则返回0
1494
1441
  if (ctx.cancel) {
1495
- this.logger('debug', `setrange操作被事件处理器取消 [${key}]`);
1442
+ this.log('debug', `setrange操作被事件处理器取消 [${key}]`);
1496
1443
  return 0;
1497
1444
  }
1498
1445
 
@@ -1518,7 +1465,7 @@ Redis.prototype.setrange = async function (key, index, value) {
1518
1465
  }
1519
1466
  // 回退方案
1520
1467
  else {
1521
- this.logger('warn', `连接对象不支持setRange操作,使用回退方案`);
1468
+ this.log('warn', `连接对象不支持setRange操作,使用回退方案`);
1522
1469
  let str = await this.get(finalKey) || '';
1523
1470
  if (str.length < finalIndex) {
1524
1471
  // 填充空格到指定索引
@@ -1539,7 +1486,7 @@ Redis.prototype.setrange = async function (key, index, value) {
1539
1486
  });
1540
1487
  return result;
1541
1488
  } catch (err) {
1542
- this.logger('error', '设置字符串范围失败:', err);
1489
+ this.log('error', '设置字符串范围失败:', err);
1543
1490
  // 回退方案
1544
1491
  let str = await this.get(finalKey) || '';
1545
1492
  if (str.length < finalIndex) {
@@ -1569,7 +1516,7 @@ Redis.prototype.setrange = async function (key, index, value) {
1569
1516
  value,
1570
1517
  error: err
1571
1518
  });
1572
- this.logger('error', `设置字符串范围失败 [${key}]:`, err);
1519
+ this.log('error', `设置字符串范围失败 [${key}]:`, err);
1573
1520
  return 0;
1574
1521
  }
1575
1522
  };
@@ -1590,7 +1537,7 @@ Redis.prototype.clear = async function (key) {
1590
1537
 
1591
1538
  // 如果事件处理器标记为取消,则返回false
1592
1539
  if (ctx.cancel) {
1593
- this.logger('debug', `clear操作被事件处理器取消`);
1540
+ this.log('debug', `clear操作被事件处理器取消`);
1594
1541
  return false;
1595
1542
  }
1596
1543
 
@@ -1614,7 +1561,7 @@ Redis.prototype.clear = async function (key) {
1614
1561
  await connection.execute('FLUSHDB');
1615
1562
  success = true;
1616
1563
  } else {
1617
- this.logger('error', '连接对象不支持flushDb操作');
1564
+ this.log('error', '连接对象不支持flushDb操作');
1618
1565
  success = false;
1619
1566
  }
1620
1567
  }
@@ -1634,7 +1581,7 @@ Redis.prototype.clear = async function (key) {
1634
1581
  key,
1635
1582
  error: err
1636
1583
  });
1637
- this.logger('error', '清空缓存失败:', err);
1584
+ this.log('error', '清空缓存失败:', err);
1638
1585
  return false;
1639
1586
  }
1640
1587
  };
@@ -1678,7 +1625,7 @@ Redis.prototype.sort = async function (key, options = {}) {
1678
1625
 
1679
1626
  return await connection.sort(key, sortOptions);
1680
1627
  } catch (err) {
1681
- this.logger('error', '排序失败:', err);
1628
+ this.log('error', '排序失败:', err);
1682
1629
  // 回退方案:获取数据后在内存中排序
1683
1630
  const data = await this.getForList(key);
1684
1631
  return data.sort((a, b) => {
@@ -1692,7 +1639,7 @@ Redis.prototype.sort = async function (key, options = {}) {
1692
1639
  this._recycleConnection(connection);
1693
1640
  }
1694
1641
  } catch (err) {
1695
- this.logger('error', `排序失败 [${key}]:`, err);
1642
+ this.log('error', `排序失败 [${key}]:`, err);
1696
1643
  return [];
1697
1644
  }
1698
1645
  };
@@ -1715,7 +1662,7 @@ Redis.prototype.keys = async function (pattern = '*') {
1715
1662
  this._recycleConnection(connection);
1716
1663
  }
1717
1664
  } catch (err) {
1718
- this.logger('error', '获取键列表失败:', err);
1665
+ this.log('error', '获取键列表失败:', err);
1719
1666
  return [];
1720
1667
  }
1721
1668
  };
@@ -1740,7 +1687,7 @@ Redis.prototype.setForList = async function (key, value, seconds) {
1740
1687
 
1741
1688
  // 如果事件处理器标记为取消,则返回0
1742
1689
  if (ctx.cancel) {
1743
- this.logger('debug', `setForList操作被事件处理器取消 [${key}]`);
1690
+ this.log('debug', `setForList操作被事件处理器取消 [${key}]`);
1744
1691
  return 0;
1745
1692
  }
1746
1693
 
@@ -1770,7 +1717,7 @@ Redis.prototype.setForList = async function (key, value, seconds) {
1770
1717
  } else if (typeof connection.execute === 'function') {
1771
1718
  len = await connection.execute('RPUSH', [finalKey, ...finalValue]);
1772
1719
  } else {
1773
- this.logger('error', '连接对象不支持rPush操作');
1720
+ this.log('error', '连接对象不支持rPush操作');
1774
1721
  len = 0;
1775
1722
  }
1776
1723
  }
@@ -1781,7 +1728,7 @@ Redis.prototype.setForList = async function (key, value, seconds) {
1781
1728
  } else if (typeof connection.execute === 'function') {
1782
1729
  len = await connection.execute('RPUSH', [finalKey, finalValue]);
1783
1730
  } else {
1784
- this.logger('error', '连接对象不支持rPush操作');
1731
+ this.log('error', '连接对象不支持rPush操作');
1785
1732
  len = 0;
1786
1733
  }
1787
1734
  }
@@ -1792,7 +1739,7 @@ Redis.prototype.setForList = async function (key, value, seconds) {
1792
1739
  } else if (typeof connection.execute === 'function') {
1793
1740
  await connection.execute('EXPIRE', [finalKey, finalSeconds]);
1794
1741
  } else {
1795
- this.logger('error', '连接对象不支持expire操作');
1742
+ this.log('error', '连接对象不支持expire操作');
1796
1743
  }
1797
1744
  }
1798
1745
 
@@ -1815,7 +1762,7 @@ Redis.prototype.setForList = async function (key, value, seconds) {
1815
1762
  value,
1816
1763
  error: err
1817
1764
  });
1818
- this.logger('error', `设置列表失败 [${key}]:`, err);
1765
+ this.log('error', `设置列表失败 [${key}]:`, err);
1819
1766
  return 0;
1820
1767
  }
1821
1768
  };
@@ -1840,7 +1787,7 @@ Redis.prototype.addForList = async function (key, value, seconds = 0) {
1840
1787
 
1841
1788
  // 如果事件处理器标记为取消,则返回0
1842
1789
  if (ctx.cancel) {
1843
- this.logger('debug', `addForList操作被事件处理器取消 [${key}]`);
1790
+ this.log('debug', `addForList操作被事件处理器取消 [${key}]`);
1844
1791
  return 0;
1845
1792
  }
1846
1793
 
@@ -1864,7 +1811,7 @@ Redis.prototype.addForList = async function (key, value, seconds = 0) {
1864
1811
  } else if (typeof connection.execute === 'function') {
1865
1812
  len = await connection.execute('RPUSH', [finalKey, ...finalValue]);
1866
1813
  } else {
1867
- this.logger('error', '连接对象不支持rPush操作');
1814
+ this.log('error', '连接对象不支持rPush操作');
1868
1815
  len = 0;
1869
1816
  }
1870
1817
  }
@@ -1875,7 +1822,7 @@ Redis.prototype.addForList = async function (key, value, seconds = 0) {
1875
1822
  } else if (typeof connection.execute === 'function') {
1876
1823
  len = await connection.execute('RPUSH', [finalKey, finalValue]);
1877
1824
  } else {
1878
- this.logger('error', '连接对象不支持rPush操作');
1825
+ this.log('error', '连接对象不支持rPush操作');
1879
1826
  len = 0;
1880
1827
  }
1881
1828
  }
@@ -1886,7 +1833,7 @@ Redis.prototype.addForList = async function (key, value, seconds = 0) {
1886
1833
  } else if (typeof connection.execute === 'function') {
1887
1834
  await connection.execute('EXPIRE', [finalKey, finalSeconds]);
1888
1835
  } else {
1889
- this.logger('error', '连接对象不支持expire操作');
1836
+ this.log('error', '连接对象不支持expire操作');
1890
1837
  }
1891
1838
  }
1892
1839
 
@@ -1909,7 +1856,7 @@ Redis.prototype.addForList = async function (key, value, seconds = 0) {
1909
1856
  value,
1910
1857
  error: err
1911
1858
  });
1912
- this.logger('error', `添加列表项失败 [${key}]:`, err);
1859
+ this.log('error', `添加列表项失败 [${key}]:`, err);
1913
1860
  return 0;
1914
1861
  }
1915
1862
  };
@@ -1937,7 +1884,7 @@ Redis.prototype.hasForList = async function (key, value) {
1937
1884
  return itemStr === valueStr || item === value;
1938
1885
  });
1939
1886
  } catch (err) {
1940
- this.logger('error', `检查列表成员是否存在失败 [${key}]:`, err);
1887
+ this.log('error', `检查列表成员是否存在失败 [${key}]:`, err);
1941
1888
  return false;
1942
1889
  }
1943
1890
  };
@@ -1962,7 +1909,7 @@ Redis.prototype.getForList = async function (key, start = 0, end = -1) {
1962
1909
 
1963
1910
  // 如果事件处理器标记为取消,则返回空数组
1964
1911
  if (ctx.cancel) {
1965
- this.logger('debug', `getForList操作被事件处理器取消 [${key}]`);
1912
+ this.log('debug', `getForList操作被事件处理器取消 [${key}]`);
1966
1913
  return [];
1967
1914
  }
1968
1915
 
@@ -1987,12 +1934,12 @@ Redis.prototype.getForList = async function (key, start = 0, end = -1) {
1987
1934
  // 回退到通用execute方法
1988
1935
  result = await connection.execute('LRANGE', [finalKey, finalStart, finalEnd]);
1989
1936
  } else {
1990
- this.logger('error', '连接对象不支持lRange或lrange操作');
1937
+ this.log('error', '连接对象不支持lRange或lrange操作');
1991
1938
  return [];
1992
1939
  }
1993
1940
  // 确保结果是数组
1994
1941
  if (!Array.isArray(result)) {
1995
- this.logger('error', '列表查询结果不是数组格式');
1942
+ this.log('error', '列表查询结果不是数组格式');
1996
1943
  return [];
1997
1944
  }
1998
1945
 
@@ -2032,7 +1979,7 @@ Redis.prototype.getForList = async function (key, start = 0, end = -1) {
2032
1979
  end,
2033
1980
  error: err
2034
1981
  });
2035
- this.logger('error', `获取列表失败 [${key}]:`, err);
1982
+ this.log('error', `获取列表失败 [${key}]:`, err);
2036
1983
  return [];
2037
1984
  }
2038
1985
  };
@@ -2057,7 +2004,7 @@ Redis.prototype.clearForList = async function (key) {
2057
2004
 
2058
2005
  // 如果事件处理器标记为取消,则返回false
2059
2006
  if (ctx.cancel) {
2060
- this.logger('debug', `clearForList操作被事件处理器取消 [${key}]`);
2007
+ this.log('debug', `clearForList操作被事件处理器取消 [${key}]`);
2061
2008
  return false;
2062
2009
  }
2063
2010
 
@@ -2081,7 +2028,7 @@ Redis.prototype.clearForList = async function (key) {
2081
2028
  key,
2082
2029
  error: err
2083
2030
  });
2084
- this.logger('error', `清空列表失败 [${key}]:`, err);
2031
+ this.log('error', `清空列表失败 [${key}]:`, err);
2085
2032
  return false;
2086
2033
  }
2087
2034
  };
@@ -2119,7 +2066,7 @@ Redis.prototype.mget = async function (keys) {
2119
2066
 
2120
2067
  // 如果事件处理器标记为取消,则返回空数组
2121
2068
  if (ctx.cancel) {
2122
- this.logger('debug', `mget操作被事件处理器取消 [${keys}]`);
2069
+ this.log('debug', `mget操作被事件处理器取消 [${keys}]`);
2123
2070
  return [];
2124
2071
  }
2125
2072
 
@@ -2165,7 +2112,7 @@ Redis.prototype.mget = async function (keys) {
2165
2112
  keys,
2166
2113
  error: err
2167
2114
  });
2168
- this.logger('error', `批量获取缓存失败 [${keys}]:`, err);
2115
+ this.log('error', `批量获取缓存失败 [${keys}]:`, err);
2169
2116
  return [];
2170
2117
  }
2171
2118
  }
@@ -2188,7 +2135,7 @@ Redis.prototype.mset = async function (obj, seconds = 0) {
2188
2135
 
2189
2136
  // 如果事件处理器标记为取消,则返回false
2190
2137
  if (ctx.cancel) {
2191
- this.logger('debug', `mset操作被事件处理器取消`);
2138
+ this.log('debug', `mset操作被事件处理器取消`);
2192
2139
  return false;
2193
2140
  }
2194
2141
 
@@ -2234,7 +2181,7 @@ Redis.prototype.mset = async function (obj, seconds = 0) {
2234
2181
  });
2235
2182
 
2236
2183
  // 如果Pipeline失败,回退到原实现
2237
- this.logger('warn', '使用Pipeline批量设置失败,使用回退方案:', err);
2184
+ this.log('warn', '使用Pipeline批量设置失败,使用回退方案:', err);
2238
2185
  const connection = await this._getConnection();
2239
2186
  try {
2240
2187
  const pairs = [];
@@ -2272,7 +2219,7 @@ Redis.prototype.mset = async function (obj, seconds = 0) {
2272
2219
  seconds,
2273
2220
  error: fallbackErr
2274
2221
  });
2275
- this.logger('error', '回退方案批量设置也失败:', fallbackErr);
2222
+ this.log('error', '回退方案批量设置也失败:', fallbackErr);
2276
2223
  return false;
2277
2224
  } finally {
2278
2225
  this._recycleConnection(connection);
@@ -2289,7 +2236,7 @@ Redis.prototype._recycleConnection = function (connection) {
2289
2236
  * @description 删除哈希字段
2290
2237
  * @param {String} key 键
2291
2238
  * @param {String|Array<String>} fields 字段名或字段名数组
2292
- * @return {Promise<Boolean>} 操作结果
2239
+ * @return {Promise<Number>} 删除的字段数量
2293
2240
  */
2294
2241
  Redis.prototype.hdel = async function (key, fields) {
2295
2242
  try {
@@ -2303,7 +2250,7 @@ Redis.prototype.hdel = async function (key, fields) {
2303
2250
 
2304
2251
  // 如果事件处理器标记为取消,则返回false
2305
2252
  if (ctx.cancel) {
2306
- this.logger('debug', `hdel操作被事件处理器取消 [${key}]`);
2253
+ this.log('debug', `hdel操作被事件处理器取消 [${key}]`);
2307
2254
  return false;
2308
2255
  }
2309
2256
 
@@ -2321,23 +2268,24 @@ Redis.prototype.hdel = async function (key, fields) {
2321
2268
  finalFields = [finalFields];
2322
2269
  }
2323
2270
 
2271
+ let deletedCount = 0;
2324
2272
  // 执行删除操作
2325
2273
  if (typeof connection.hDel === 'function') {
2326
- await connection.hDel(finalKey, finalFields);
2274
+ deletedCount = await connection.hDel(finalKey, finalFields);
2327
2275
  } else if (typeof connection.execute === 'function') {
2328
- await connection.execute('HDEL', [finalKey, ...finalFields]);
2276
+ deletedCount = await connection.execute('HDEL', [finalKey, ...finalFields]);
2329
2277
  } else {
2330
- this.logger('error', '连接对象不支持hDel操作');
2331
- success = false;
2278
+ this.log('error', '连接对象不支持hDel操作');
2279
+ deletedCount = 0;
2332
2280
  }
2333
2281
 
2334
2282
  // 触发后置事件
2335
2283
  this.emit('hdel:after', {
2336
2284
  key: finalKey,
2337
2285
  value: finalFields,
2338
- success
2286
+ deletedCount
2339
2287
  });
2340
- return success;
2288
+ return deletedCount;
2341
2289
  } finally {
2342
2290
  this._recycleConnection(connection);
2343
2291
  }
@@ -2348,8 +2296,8 @@ Redis.prototype.hdel = async function (key, fields) {
2348
2296
  value: fields,
2349
2297
  error: err
2350
2298
  });
2351
- this.logger('error', `删除哈希字段失败 [${key}]:`, err);
2352
- return false;
2299
+ this.log('error', `删除哈希字段失败 [${key}]:`, err);
2300
+ return 0; // 与Redis语义保持一致,返回删除的字段数量
2353
2301
  }
2354
2302
  };
2355
2303
 
@@ -2368,7 +2316,7 @@ Redis.prototype.hlen = async function (key) {
2368
2316
  this._recycleConnection(connection);
2369
2317
  }
2370
2318
  } catch (err) {
2371
- this.logger('error', `获取哈希字段数量失败 [${key}]:`, err);
2319
+ this.log('error', `获取哈希字段数量失败 [${key}]:`, err);
2372
2320
  return 0;
2373
2321
  }
2374
2322
  };
@@ -2482,7 +2430,7 @@ class RedisPipeline {
2482
2430
  // 处理结果(解析JSON等)
2483
2431
  return results.map(([err, value], index) => {
2484
2432
  if (err) {
2485
- this.logger('error', `[Pipeline] Pipeline命令执行失败 [${this.commands[index].name}]:`, err);
2433
+ this.log('error', `[Pipeline] Pipeline命令执行失败 [${this.commands[index].name}]:`, err);
2486
2434
  return null;
2487
2435
  }
2488
2436
 
@@ -2522,7 +2470,7 @@ Redis.prototype.clearExpire = async function () {
2522
2470
  this.emit('clearExpire:error', {
2523
2471
  error: err
2524
2472
  });
2525
- this.logger('error', '清空过期数据失败:', err);
2473
+ this.log('error', '清空过期数据失败:', err);
2526
2474
  return false;
2527
2475
  }
2528
2476
  };
@@ -2543,7 +2491,7 @@ Redis.prototype._getConnection = function () {
2543
2491
  // 如果连接数未达到上限,创建新连接
2544
2492
  const totalConnections = this.connection_pool.available.length + this.connection_pool.in_use
2545
2493
  .size;
2546
- if (totalConnections < this.connection_pool.config.max_clients) {
2494
+ if (totalConnections < this.config.max_clients) {
2547
2495
  this._createConnection().then(connection => {
2548
2496
  this.connection_pool.in_use.add(connection);
2549
2497
  resolve(connection);
@@ -2551,14 +2499,14 @@ Redis.prototype._getConnection = function () {
2551
2499
  } else {
2552
2500
  // 否则等待连接释放
2553
2501
  // 这里简化处理,直接创建连接(生产环境应实现等待队列)
2554
- this.logger('warn', '连接池已满,创建额外连接');
2502
+ this.log('warn', '连接池已满,创建额外连接');
2555
2503
  this._createConnection().then(connection => {
2556
2504
  this.connection_pool.in_use.add(connection);
2557
2505
  resolve(connection);
2558
2506
  }).catch(reject);
2559
2507
  }
2560
2508
  } catch (error) {
2561
- this.logger('error', '获取连接失败:', error);
2509
+ this.log('error', '获取连接失败:', error);
2562
2510
  reject(error);
2563
2511
  }
2564
2512
  });
@@ -2571,7 +2519,7 @@ Redis.prototype._releaseConnection = function (connection) {
2571
2519
  this.connection_pool.in_use.delete(connection);
2572
2520
 
2573
2521
  // 检查是否超过最大空闲连接数
2574
- if (this.connection_pool.available.length < this.connection_pool.config.max_idle) {
2522
+ if (this.connection_pool.available.length < this.config.max_idle) {
2575
2523
  this.connection_pool.available.push(connection);
2576
2524
  } else {
2577
2525
  // 否则关闭多余的连接
@@ -2581,15 +2529,15 @@ Redis.prototype._releaseConnection = function (connection) {
2581
2529
  } else if (typeof connection.end === 'function') {
2582
2530
  connection.end();
2583
2531
  } else {
2584
- this.logger('debug', '连接对象没有可用的关闭方法');
2532
+ this.log('debug', '连接对象没有可用的关闭方法');
2585
2533
  }
2586
2534
  } catch (e) {
2587
- this.logger('error', '关闭多余连接失败:', e);
2535
+ this.log('error', '关闭多余连接失败:', e);
2588
2536
  }
2589
2537
  }
2590
2538
  }
2591
2539
  } catch (error) {
2592
- this.logger('error', '释放连接失败:', error);
2540
+ this.log('error', '释放连接失败:', error);
2593
2541
  }
2594
2542
  };
2595
2543
 
@@ -2618,7 +2566,7 @@ Redis.prototype.hset = async function (key, field, value) {
2618
2566
 
2619
2567
  // 如果事件处理器标记为取消,则返回false
2620
2568
  if (ctx.cancel) {
2621
- this.logger('debug', `hset操作被事件处理器取消 [${key}:${field}]`);
2569
+ this.log('debug', `hset操作被事件处理器取消 [${key}:${field}]`);
2622
2570
  return false;
2623
2571
  }
2624
2572
 
@@ -2646,12 +2594,12 @@ Redis.prototype.hset = async function (key, field, value) {
2646
2594
  value: finalValue,
2647
2595
  success: true
2648
2596
  });
2649
- return result > 0; // 如果field是新的,hSet返回1,否则返回0
2597
+ return true; // hSet操作成功返回true
2650
2598
  } finally {
2651
2599
  this._recycleConnection(connection);
2652
2600
  }
2653
2601
  } catch (err) {
2654
- this.logger('error', `设置哈希字段失败 [${key}:${field}]:`, err);
2602
+ this.log('error', `设置哈希字段失败 [${key}:${field}]:`, err);
2655
2603
  // 触发错误事件
2656
2604
  this.emit('hset:error', {
2657
2605
  key,
@@ -2693,7 +2641,7 @@ Redis.prototype.hget = async function (key, field) {
2693
2641
  this._recycleConnection(connection);
2694
2642
  }
2695
2643
  } catch (err) {
2696
- this.logger('error', `获取哈希字段失败 [${key}:${field}]:`, err);
2644
+ this.log('error', `获取哈希字段失败 [${key}:${field}]:`, err);
2697
2645
  return null;
2698
2646
  }
2699
2647
  };
@@ -2716,7 +2664,7 @@ Redis.prototype.hmset = async function (key, obj) {
2716
2664
 
2717
2665
  // 如果事件处理器标记为取消,则返回false
2718
2666
  if (ctx.cancel) {
2719
- this.logger('debug', `hmset操作被事件处理器取消 [${key}]`);
2667
+ this.log('debug', `hmset操作被事件处理器取消 [${key}]`);
2720
2668
  return false;
2721
2669
  }
2722
2670
 
@@ -2731,20 +2679,20 @@ Redis.prototype.hmset = async function (key, obj) {
2731
2679
  finalKey = this._addPrefix(finalKey);
2732
2680
 
2733
2681
  // 检查Redis版本,支持不同的方法
2734
- this.logger('debug',
2682
+ this.log('debug',
2735
2683
  `hmset方法检测到连接对象方法: hSet=${typeof connection.hSet === 'function'}, hmset=${typeof connection.hmset === 'function'}`
2736
2684
  );
2737
2685
  let success = true;
2738
2686
 
2739
2687
  // 优化策略:对所有Redis版本都使用逐个字段设置的方式,确保兼容性和正确性
2740
- this.logger('debug', `对键 ${finalKey} 强制使用逐个字段设置策略`);
2688
+ this.log('debug', `对键 ${finalKey} 强制使用逐个字段设置策略`);
2741
2689
 
2742
2690
  // 获取适合的设置字段方法
2743
2691
  const setFieldMethod = typeof connection.hSet === 'function' ? 'hSet' :
2744
2692
  typeof connection.hset === 'function' ? 'hset' : null;
2745
2693
 
2746
2694
  if (!setFieldMethod) {
2747
- this.logger('error', `连接对象不支持hSet或hset操作`);
2695
+ this.log('error', `连接对象不支持hSet或hset操作`);
2748
2696
  return false;
2749
2697
  }
2750
2698
 
@@ -2756,10 +2704,10 @@ Redis.prototype.hmset = async function (key, obj) {
2756
2704
  if (typeof value !== 'string') {
2757
2705
  value = JSON.stringify(value);
2758
2706
  }
2759
- this.logger('debug', `单独设置字段: ${setFieldMethod}(${finalKey}, ${field}, ${value})`);
2707
+ this.log('debug', `单独设置字段: ${setFieldMethod}(${finalKey}, ${field}, ${value})`);
2760
2708
  await connection[setFieldMethod](finalKey, field, value);
2761
2709
  } catch (singleError) {
2762
- this.logger('error', `设置单个字段失败 ${field}: ${singleError.message}`);
2710
+ this.log('error', `设置单个字段失败 ${field}: ${singleError.message}`);
2763
2711
  success = false;
2764
2712
  break;
2765
2713
  }
@@ -2768,19 +2716,19 @@ Redis.prototype.hmset = async function (key, obj) {
2768
2716
 
2769
2717
  // 验证所有字段是否都设置成功
2770
2718
  if (success) {
2771
- this.logger('debug', `验证所有字段设置情况`);
2719
+ this.log('debug', `验证所有字段设置情况`);
2772
2720
  for (const field in finalObj) {
2773
2721
  if (finalObj.hasOwnProperty(field)) {
2774
2722
  try {
2775
2723
  const hgetMethod = typeof connection.hGet === 'function' ? 'hGet' : 'hget';
2776
2724
  const value = await connection[hgetMethod](finalKey, field);
2777
- this.logger('debug', `验证字段 ${field}: ${value !== null ? '存在' : '不存在'}`);
2725
+ this.log('debug', `验证字段 ${field}: ${value !== null ? '存在' : '不存在'}`);
2778
2726
  // 如果字段不存在,更新success状态
2779
2727
  if (value === null) {
2780
2728
  success = false;
2781
2729
  }
2782
2730
  } catch (getError) {
2783
- this.logger('error', `验证字段失败 ${field}: ${getError.message}`);
2731
+ this.log('error', `验证字段失败 ${field}: ${getError.message}`);
2784
2732
  success = false;
2785
2733
  }
2786
2734
  }
@@ -2798,7 +2746,7 @@ Redis.prototype.hmset = async function (key, obj) {
2798
2746
  this._recycleConnection(connection);
2799
2747
  }
2800
2748
  } catch (err) {
2801
- this.logger('error', `批量设置哈希字段失败 [${key}]:`, err);
2749
+ this.log('error', `批量设置哈希字段失败 [${key}]:`, err);
2802
2750
  // 触发错误事件
2803
2751
  this.emit('hmset:error', {
2804
2752
  key,
@@ -2823,14 +2771,14 @@ Redis.prototype.hgetall = async function (key) {
2823
2771
  const result = await connection.hGetAll(key);
2824
2772
  return result;
2825
2773
  } else {
2826
- this.logger('error', '连接对象不支持hGetAll操作');
2774
+ this.log('error', '连接对象不支持hGetAll操作');
2827
2775
  return {};
2828
2776
  }
2829
2777
  } finally {
2830
2778
  this._recycleConnection(connection);
2831
2779
  }
2832
2780
  } catch (err) {
2833
- this.logger('error', `获取哈希所有字段和值失败 [${key}]:`, err);
2781
+ this.log('error', `获取哈希所有字段和值失败 [${key}]:`, err);
2834
2782
  return {};
2835
2783
  }
2836
2784
  };
@@ -2880,8 +2828,8 @@ Redis.prototype.hmget = async function (key, fields) {
2880
2828
  });
2881
2829
  return finalResult;
2882
2830
  } else {
2883
- this.logger('error', '连接对象不支持hmGet操作');
2884
- this.logger('error', '错误: Redis客户端不支持hmget操作');
2831
+ this.log('error', '连接对象不支持hmGet操作');
2832
+ this.log('error', '错误: Redis客户端不支持hmget操作');
2885
2833
  // 返回与fields长度相同的undefined数组,保持与注释一致
2886
2834
  return fields.map(() => undefined);
2887
2835
  }
@@ -2894,7 +2842,7 @@ Redis.prototype.hmget = async function (key, fields) {
2894
2842
  fields,
2895
2843
  error: err
2896
2844
  });
2897
- this.logger('error', `哈希表批量获取失败 [${key}]:`, err);
2845
+ this.log('error', `哈希表批量获取失败 [${key}]:`, err);
2898
2846
  // 返回与fields长度相同的undefined数组,保持与注释类型一致
2899
2847
  return fields.map(() => undefined);
2900
2848
  }
@@ -2916,7 +2864,7 @@ Redis.prototype.expire = async function (key, seconds) {
2916
2864
  this.emit('expire:before', ctx);
2917
2865
 
2918
2866
  if (ctx.cancel) {
2919
- this.logger('debug', `expire操作被事件处理器取消 [${key}]`);
2867
+ this.log('debug', `expire操作被事件处理器取消 [${key}]`);
2920
2868
  return false;
2921
2869
  }
2922
2870
 
@@ -2938,7 +2886,7 @@ Redis.prototype.expire = async function (key, seconds) {
2938
2886
  });
2939
2887
  return result === 1;
2940
2888
  } catch (err) {
2941
- this.logger('error', `设置过期时间失败 [${finalKey}]:`, err);
2889
+ this.log('error', `设置过期时间失败 [${finalKey}]:`, err);
2942
2890
  this.emit('expire:error', {
2943
2891
  key: finalKey,
2944
2892
  ttl: finalSeconds,
@@ -2949,7 +2897,7 @@ Redis.prototype.expire = async function (key, seconds) {
2949
2897
  this._recycleConnection(connection);
2950
2898
  }
2951
2899
  } catch (err) {
2952
- this.logger('error', `设置过期时间失败 [${key}]:`, err);
2900
+ this.log('error', `设置过期时间失败 [${key}]:`, err);
2953
2901
  this.emit('expire:error', {
2954
2902
  key,
2955
2903
  ttl: seconds,
@@ -2984,7 +2932,7 @@ Redis.prototype.pexpire = async function (key, milliseconds) {
2984
2932
  this.emit('pexpire:before', ctx);
2985
2933
 
2986
2934
  if (ctx.cancel) {
2987
- this.logger('debug', `pexpire操作被事件处理器取消 [${key}]`);
2935
+ this.log('debug', `pexpire操作被事件处理器取消 [${key}]`);
2988
2936
  return false;
2989
2937
  }
2990
2938
 
@@ -3002,7 +2950,7 @@ Redis.prototype.pexpire = async function (key, milliseconds) {
3002
2950
  } else if (typeof connection.execute === 'function') {
3003
2951
  result = await connection.execute('PEXPIRE', [this._addPrefix(finalKey), finalMilliseconds]);
3004
2952
  } else {
3005
- this.logger('error', '连接对象不支持pexpire操作');
2953
+ this.log('error', '连接对象不支持pexpire操作');
3006
2954
  return false;
3007
2955
  }
3008
2956
 
@@ -3014,7 +2962,7 @@ Redis.prototype.pexpire = async function (key, milliseconds) {
3014
2962
  });
3015
2963
  return result === 1;
3016
2964
  } catch (err) {
3017
- this.logger('error', `设置过期时间(毫秒)失败 [${finalKey}]:`, err);
2965
+ this.log('error', `设置过期时间(毫秒)失败 [${finalKey}]:`, err);
3018
2966
  this.emit('pexpire:error', {
3019
2967
  key: finalKey,
3020
2968
  ttl: finalMilliseconds,
@@ -3025,7 +2973,7 @@ Redis.prototype.pexpire = async function (key, milliseconds) {
3025
2973
  this._recycleConnection(connection);
3026
2974
  }
3027
2975
  } catch (err) {
3028
- this.logger('error', `设置过期时间(毫秒)失败 [${key}]:`, err);
2976
+ this.log('error', `设置过期时间(毫秒)失败 [${key}]:`, err);
3029
2977
  this.emit('pexpire:error', {
3030
2978
  key,
3031
2979
  ttl: milliseconds,
@@ -3046,7 +2994,7 @@ Redis.prototype.persist = async function (key) {
3046
2994
  this.emit('persist:before', ctx);
3047
2995
 
3048
2996
  if (ctx.cancel) {
3049
- this.logger('debug', `persist操作被事件处理器取消 [${key}]`);
2997
+ this.log('debug', `persist操作被事件处理器取消 [${key}]`);
3050
2998
  return false;
3051
2999
  }
3052
3000
 
@@ -3066,7 +3014,7 @@ Redis.prototype.persist = async function (key) {
3066
3014
  });
3067
3015
  return result === 1;
3068
3016
  } catch (err) {
3069
- this.logger('error', `移除过期时间失败 [${finalKey}]:`, err);
3017
+ this.log('error', `移除过期时间失败 [${finalKey}]:`, err);
3070
3018
  this.emit('persist:error', {
3071
3019
  key: finalKey,
3072
3020
  error: err
@@ -3076,7 +3024,7 @@ Redis.prototype.persist = async function (key) {
3076
3024
  this._recycleConnection(connection);
3077
3025
  }
3078
3026
  } catch (err) {
3079
- this.logger('error', `移除过期时间失败 [${key}]:`, err);
3027
+ this.log('error', `移除过期时间失败 [${key}]:`, err);
3080
3028
  this.emit('persist:error', {
3081
3029
  key,
3082
3030
  error: err
@@ -3107,7 +3055,7 @@ Redis.prototype.incr = async function (key, seconds = 0) {
3107
3055
  this.emit('incr:before', ctx);
3108
3056
 
3109
3057
  if (ctx.cancel) {
3110
- this.logger('debug', `incr操作被事件处理器取消 [${key}]`);
3058
+ this.log('debug', `incr操作被事件处理器取消 [${key}]`);
3111
3059
  return null;
3112
3060
  }
3113
3061
 
@@ -3138,7 +3086,7 @@ Redis.prototype.incr = async function (key, seconds = 0) {
3138
3086
  });
3139
3087
  return result;
3140
3088
  } catch (err) {
3141
- this.logger('error', `增加整数值失败 [${finalKey}]:`, err);
3089
+ this.log('error', `增加整数值失败 [${finalKey}]:`, err);
3142
3090
  this.emit('incr:error', {
3143
3091
  key: finalKey,
3144
3092
  seconds: finalSeconds,
@@ -3149,7 +3097,7 @@ Redis.prototype.incr = async function (key, seconds = 0) {
3149
3097
  this._recycleConnection(connection);
3150
3098
  }
3151
3099
  } catch (err) {
3152
- this.logger('error', `增加整数值失败 [${key}]:`, err);
3100
+ this.log('error', `增加整数值失败 [${key}]:`, err);
3153
3101
  this.emit('incr:error', {
3154
3102
  key,
3155
3103
  seconds,
@@ -3181,7 +3129,7 @@ Redis.prototype.decr = async function (key, seconds = 0) {
3181
3129
  this.emit('decr:before', ctx);
3182
3130
 
3183
3131
  if (ctx.cancel) {
3184
- this.logger('debug', `decr操作被事件处理器取消 [${key}]`);
3132
+ this.log('debug', `decr操作被事件处理器取消 [${key}]`);
3185
3133
  return null;
3186
3134
  }
3187
3135
 
@@ -3212,7 +3160,7 @@ Redis.prototype.decr = async function (key, seconds = 0) {
3212
3160
  });
3213
3161
  return result;
3214
3162
  } catch (err) {
3215
- this.logger('error', `减少整数值失败 [${finalKey}]:`, err);
3163
+ this.log('error', `减少整数值失败 [${finalKey}]:`, err);
3216
3164
  this.emit('decr:error', {
3217
3165
  key: finalKey,
3218
3166
  seconds: finalSeconds,
@@ -3223,7 +3171,7 @@ Redis.prototype.decr = async function (key, seconds = 0) {
3223
3171
  this._recycleConnection(connection);
3224
3172
  }
3225
3173
  } catch (err) {
3226
- this.logger('error', `减少整数值失败 [${key}]:`, err);
3174
+ this.log('error', `减少整数值失败 [${key}]:`, err);
3227
3175
  this.emit('decr:error', {
3228
3176
  key,
3229
3177
  seconds,
@@ -3260,7 +3208,7 @@ Redis.prototype.incrbyfloat = async function (key, increment, seconds = 0) {
3260
3208
  this.emit('incrbyfloat:before', ctx);
3261
3209
 
3262
3210
  if (ctx.cancel) {
3263
- this.logger('debug', `incrbyfloat操作被事件处理器取消 [${key}]`);
3211
+ this.log('debug', `incrbyfloat操作被事件处理器取消 [${key}]`);
3264
3212
  return null;
3265
3213
  }
3266
3214
 
@@ -3294,7 +3242,7 @@ Redis.prototype.incrbyfloat = async function (key, increment, seconds = 0) {
3294
3242
  });
3295
3243
  return parsedResult;
3296
3244
  } catch (err) {
3297
- this.logger('error', `增加浮点数值失败 [${finalKey}]:`, err);
3245
+ this.log('error', `增加浮点数值失败 [${finalKey}]:`, err);
3298
3246
  this.emit('incrbyfloat:error', {
3299
3247
  key: finalKey,
3300
3248
  increment: finalIncrement,
@@ -3306,7 +3254,7 @@ Redis.prototype.incrbyfloat = async function (key, increment, seconds = 0) {
3306
3254
  this._recycleConnection(connection);
3307
3255
  }
3308
3256
  } catch (err) {
3309
- this.logger('error', `增加浮点数值失败 [${key}]:`, err);
3257
+ this.log('error', `增加浮点数值失败 [${key}]:`, err);
3310
3258
  this.emit('incrbyfloat:error', {
3311
3259
  key,
3312
3260
  increment,
@@ -3344,7 +3292,7 @@ Redis.prototype.incrby = async function (key, increment, seconds = 0) {
3344
3292
  this.emit('incrby:before', ctx);
3345
3293
 
3346
3294
  if (ctx.cancel) {
3347
- this.logger('debug', `incrby操作被事件处理器取消 [${key}]`);
3295
+ this.log('debug', `incrby操作被事件处理器取消 [${key}]`);
3348
3296
  return null;
3349
3297
  }
3350
3298
 
@@ -3377,7 +3325,7 @@ Redis.prototype.incrby = async function (key, increment, seconds = 0) {
3377
3325
  });
3378
3326
  return result;
3379
3327
  } catch (err) {
3380
- this.logger('error', `增加指定值失败 [${finalKey}]:`, err);
3328
+ this.log('error', `增加指定值失败 [${finalKey}]:`, err);
3381
3329
  this.emit('incrby:error', {
3382
3330
  key: finalKey,
3383
3331
  increment: finalIncrement,
@@ -3389,7 +3337,7 @@ Redis.prototype.incrby = async function (key, increment, seconds = 0) {
3389
3337
  this._recycleConnection(connection);
3390
3338
  }
3391
3339
  } catch (err) {
3392
- this.logger('error', `增加指定值失败 [${key}]:`, err);
3340
+ this.log('error', `增加指定值失败 [${key}]:`, err);
3393
3341
  this.emit('incrby:error', {
3394
3342
  key,
3395
3343
  increment,
@@ -3427,7 +3375,7 @@ Redis.prototype.decrby = async function (key, decrement, seconds = 0) {
3427
3375
  this.emit('decrby:before', ctx);
3428
3376
 
3429
3377
  if (ctx.cancel) {
3430
- this.logger('debug', `decrby操作被事件处理器取消 [${key}]`);
3378
+ this.log('debug', `decrby操作被事件处理器取消 [${key}]`);
3431
3379
  return null;
3432
3380
  }
3433
3381
 
@@ -3460,7 +3408,7 @@ Redis.prototype.decrby = async function (key, decrement, seconds = 0) {
3460
3408
  });
3461
3409
  return result;
3462
3410
  } catch (err) {
3463
- this.logger('error', `减少指定值失败 [${finalKey}]:`, err);
3411
+ this.log('error', `减少指定值失败 [${finalKey}]:`, err);
3464
3412
  this.emit('decrby:error', {
3465
3413
  key: finalKey,
3466
3414
  decrement: finalDecrement,
@@ -3472,7 +3420,7 @@ Redis.prototype.decrby = async function (key, decrement, seconds = 0) {
3472
3420
  this._recycleConnection(connection);
3473
3421
  }
3474
3422
  } catch (err) {
3475
- this.logger('error', `减少指定值失败 [${key}]:`, err);
3423
+ this.log('error', `减少指定值失败 [${key}]:`, err);
3476
3424
  this.emit('decrby:error', {
3477
3425
  key,
3478
3426
  decrement,
@@ -3502,7 +3450,7 @@ Redis.prototype.sadd = async function (key, members) {
3502
3450
  this.emit('sadd:before', ctx);
3503
3451
 
3504
3452
  if (ctx.cancel) {
3505
- this.logger('debug', `sadd操作被事件处理器取消 [${key}]`);
3453
+ this.log('debug', `sadd操作被事件处理器取消 [${key}]`);
3506
3454
  return 0;
3507
3455
  }
3508
3456
 
@@ -3527,7 +3475,7 @@ Redis.prototype.sadd = async function (key, members) {
3527
3475
  this._recycleConnection(connection);
3528
3476
  }
3529
3477
  } catch (err) {
3530
- this.logger('error', `添加集合成员失败 [${key}]:`, err);
3478
+ this.log('error', `添加集合成员失败 [${key}]:`, err);
3531
3479
  this.emit('sadd:error', {
3532
3480
  key,
3533
3481
  members,
@@ -3554,7 +3502,7 @@ Redis.prototype.srem = async function (key, members) {
3554
3502
  this.emit('srem:before', ctx);
3555
3503
 
3556
3504
  if (ctx.cancel) {
3557
- this.logger('debug', `srem操作被事件处理器取消 [${key}]`);
3505
+ this.log('debug', `srem操作被事件处理器取消 [${key}]`);
3558
3506
  return 0;
3559
3507
  }
3560
3508
 
@@ -3579,7 +3527,7 @@ Redis.prototype.srem = async function (key, members) {
3579
3527
  this._recycleConnection(connection);
3580
3528
  }
3581
3529
  } catch (err) {
3582
- this.logger('error', `移除集合成员失败 [${key}]:`, err);
3530
+ this.log('error', `移除集合成员失败 [${key}]:`, err);
3583
3531
  this.emit('srem:error', {
3584
3532
  key,
3585
3533
  members,
@@ -3630,7 +3578,7 @@ Redis.prototype.sismember = async function (key, member) {
3630
3578
  member,
3631
3579
  error: err
3632
3580
  });
3633
- this.logger('error', `检查集合成员失败 [${key}:${member}]:`, err);
3581
+ this.log('error', `检查集合成员失败 [${key}:${member}]:`, err);
3634
3582
  return false;
3635
3583
  }
3636
3584
  };
@@ -3655,7 +3603,7 @@ Redis.prototype.smembers = async function (key) {
3655
3603
  this._recycleConnection(connection);
3656
3604
  }
3657
3605
  } catch (err) {
3658
- this.logger('error', `获取集合所有成员失败 [${key}]:`, err);
3606
+ this.log('error', `获取集合所有成员失败 [${key}]:`, err);
3659
3607
  this.emit('smembers:error', {
3660
3608
  key,
3661
3609
  error: err
@@ -3680,7 +3628,7 @@ Redis.prototype.scard = async function (key) {
3680
3628
  this._recycleConnection(connection);
3681
3629
  }
3682
3630
  } catch (err) {
3683
- this.logger('error', `获取集合大小失败 [${key}]:`, err);
3631
+ this.log('error', `获取集合大小失败 [${key}]:`, err);
3684
3632
  return 0;
3685
3633
  }
3686
3634
  };
@@ -3702,7 +3650,7 @@ Redis.prototype.sinter = async function (keys) {
3702
3650
  this._recycleConnection(connection);
3703
3651
  }
3704
3652
  } catch (err) {
3705
- this.logger('error', `获取集合交集失败:`, err);
3653
+ this.log('error', `获取集合交集失败:`, err);
3706
3654
  return [];
3707
3655
  }
3708
3656
  };
@@ -3724,7 +3672,7 @@ Redis.prototype.sunion = async function (keys) {
3724
3672
  this._recycleConnection(connection);
3725
3673
  }
3726
3674
  } catch (err) {
3727
- this.logger('error', `获取集合并集失败:`, err);
3675
+ this.log('error', `获取集合并集失败:`, err);
3728
3676
  return [];
3729
3677
  }
3730
3678
  };
@@ -3746,7 +3694,7 @@ Redis.prototype.sdiff = async function (keys) {
3746
3694
  this._recycleConnection(connection);
3747
3695
  }
3748
3696
  } catch (err) {
3749
- this.logger('error', `获取集合差集失败:`, err);
3697
+ this.log('error', `获取集合差集失败:`, err);
3750
3698
  return [];
3751
3699
  }
3752
3700
  };
@@ -3772,7 +3720,7 @@ Redis.prototype.zadd = async function (key, score, member) {
3772
3720
  this.emit('zadd:before', ctx);
3773
3721
 
3774
3722
  if (ctx.cancel) {
3775
- this.logger('debug', `zadd操作被事件处理器取消 [${key}]`);
3723
+ this.log('debug', `zadd操作被事件处理器取消 [${key}]`);
3776
3724
  return false;
3777
3725
  }
3778
3726
 
@@ -3802,7 +3750,7 @@ Redis.prototype.zadd = async function (key, score, member) {
3802
3750
  this._recycleConnection(connection);
3803
3751
  }
3804
3752
  } catch (err) {
3805
- this.logger('error', `添加有序集合成员失败 [${key}]:`, err);
3753
+ this.log('error', `添加有序集合成员失败 [${key}]:`, err);
3806
3754
  this.emit('zadd:error', {
3807
3755
  key,
3808
3756
  score,
@@ -3830,7 +3778,7 @@ Redis.prototype.zrem = async function (key, members) {
3830
3778
  this.emit('zrem:before', ctx);
3831
3779
 
3832
3780
  if (ctx.cancel) {
3833
- this.logger('debug', `zrem操作被事件处理器取消 [${key}]`);
3781
+ this.log('debug', `zrem操作被事件处理器取消 [${key}]`);
3834
3782
  return 0;
3835
3783
  }
3836
3784
 
@@ -3852,7 +3800,7 @@ Redis.prototype.zrem = async function (key, members) {
3852
3800
  });
3853
3801
  return result;
3854
3802
  } catch (err) {
3855
- this.logger('error', `移除有序集合成员失败 [${finalKey}]:`, err);
3803
+ this.log('error', `移除有序集合成员失败 [${finalKey}]:`, err);
3856
3804
  this.emit('zrem:error', {
3857
3805
  key: finalKey,
3858
3806
  members: finalMembers,
@@ -3863,7 +3811,7 @@ Redis.prototype.zrem = async function (key, members) {
3863
3811
  this._recycleConnection(connection);
3864
3812
  }
3865
3813
  } catch (err) {
3866
- this.logger('error', `移除有序集合成员失败 [${key}]:`, err);
3814
+ this.log('error', `移除有序集合成员失败 [${key}]:`, err);
3867
3815
  this.emit('zrem:error', {
3868
3816
  key,
3869
3817
  members,
@@ -3890,7 +3838,7 @@ Redis.prototype.zscore = async function (key, member) {
3890
3838
  this._recycleConnection(connection);
3891
3839
  }
3892
3840
  } catch (err) {
3893
- this.logger('error', `获取有序集合成员分数失败 [${key}:${member}]:`, err);
3841
+ this.log('error', `获取有序集合成员分数失败 [${key}:${member}]:`, err);
3894
3842
  return null;
3895
3843
  }
3896
3844
  };
@@ -3912,7 +3860,7 @@ Redis.prototype.zrank = async function (key, member) {
3912
3860
  this._recycleConnection(connection);
3913
3861
  }
3914
3862
  } catch (err) {
3915
- this.logger('error', `获取有序集合成员排名失败 [${key}:${member}]:`, err);
3863
+ this.log('error', `获取有序集合成员排名失败 [${key}:${member}]:`, err);
3916
3864
  return null;
3917
3865
  }
3918
3866
  };
@@ -3953,13 +3901,13 @@ Redis.prototype.zrange = async function (key, start, stop, withScores = false) {
3953
3901
  member,
3954
3902
  score: 0
3955
3903
  });
3956
- this.logger('error', `获取成员 ${member} 分数失败:`, scoreErr);
3904
+ this.log('error', `获取成员 ${member} 分数失败:`, scoreErr);
3957
3905
  }
3958
3906
  }
3959
3907
 
3960
3908
  return results;
3961
3909
  } catch (err) {
3962
- this.logger('error', `使用备用方法获取有序集合范围失败 [${prefixedKey}]:`, err);
3910
+ this.log('error', `使用备用方法获取有序集合范围失败 [${prefixedKey}]:`, err);
3963
3911
  // 回退到简单列表
3964
3912
  return [];
3965
3913
  }
@@ -3972,7 +3920,7 @@ Redis.prototype.zrange = async function (key, start, stop, withScores = false) {
3972
3920
  this._recycleConnection(connection);
3973
3921
  }
3974
3922
  } catch (err) {
3975
- this.logger('error', `获取有序集合范围成员失败 [${key}]:`, err);
3923
+ this.log('error', `获取有序集合范围成员失败 [${key}]:`, err);
3976
3924
  return [];
3977
3925
  }
3978
3926
  };
@@ -3990,13 +3938,13 @@ Redis.prototype.zcard = async function (key) {
3990
3938
  const result = await connection.zCard(this._addPrefix(key));
3991
3939
  return result || 0;
3992
3940
  } catch (err) {
3993
- this.logger('error', `获取有序集合成员数量失败 [${key}]:`, err);
3941
+ this.log('error', `获取有序集合成员数量失败 [${key}]:`, err);
3994
3942
  return 0;
3995
3943
  } finally {
3996
3944
  this._recycleConnection(connection);
3997
3945
  }
3998
3946
  } catch (err) {
3999
- this.logger('error', `获取有序集合成员数量失败 [${key}]:`, err);
3947
+ this.log('error', `获取有序集合成员数量失败 [${key}]:`, err);
4000
3948
  return 0;
4001
3949
  }
4002
3950
  };
@@ -4033,7 +3981,7 @@ Redis.prototype.zincrby = async function (key, increment, member, seconds = 0) {
4033
3981
  this.emit('zincrby:before', ctx);
4034
3982
 
4035
3983
  if (ctx.cancel) {
4036
- this.logger('debug', `zincrby操作被事件处理器取消 [${key}]`);
3984
+ this.log('debug', `zincrby操作被事件处理器取消 [${key}]`);
4037
3985
  return null;
4038
3986
  }
4039
3987
 
@@ -4069,7 +4017,7 @@ Redis.prototype.zincrby = async function (key, increment, member, seconds = 0) {
4069
4017
  });
4070
4018
  return parseFloat(result);
4071
4019
  } catch (err) {
4072
- this.logger('error', `增加有序集合成员分数失败 [${finalKey}]:`, err);
4020
+ this.log('error', `增加有序集合成员分数失败 [${finalKey}]:`, err);
4073
4021
  this.emit('zincrby:error', {
4074
4022
  key: finalKey,
4075
4023
  increment: finalIncrement,
@@ -4082,7 +4030,7 @@ Redis.prototype.zincrby = async function (key, increment, member, seconds = 0) {
4082
4030
  this._recycleConnection(connection);
4083
4031
  }
4084
4032
  } catch (err) {
4085
- this.logger('error', `增加有序集合成员分数失败 [${key}]:`, err);
4033
+ this.log('error', `增加有序集合成员分数失败 [${key}]:`, err);
4086
4034
  this.emit('zincrby:error', {
4087
4035
  key,
4088
4036
  increment,
@@ -4116,19 +4064,19 @@ Redis.prototype._createConnection = function () {
4116
4064
 
4117
4065
  // 监听错误事件
4118
4066
  connection.on('error', (err) => {
4119
- this.logger('error', `连接 ${connection.id} 错误:`, err);
4067
+ this.log('error', `连接 ${connection.id} 错误:`, err);
4120
4068
  });
4121
4069
 
4122
4070
  // 连接Redis
4123
4071
  connection.connect().then(() => {
4124
- // this.logger('debug','新连接已创建并连接:', connection.id);
4072
+ // this.log('debug','新连接已创建并连接:', connection.id);
4125
4073
  resolve(connection);
4126
4074
  }).catch(err => {
4127
- this.logger('error', '创建连接失败:', err);
4075
+ this.log('error', '创建连接失败:', err);
4128
4076
  reject(err);
4129
4077
  });
4130
4078
  } catch (error) {
4131
- this.logger('error', '创建连接时发生异常:', error);
4079
+ this.log('error', '创建连接时发生异常:', error);
4132
4080
  reject(error);
4133
4081
  }
4134
4082
  });