mm_redis 2.0.6 → 2.0.7
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 +438 -441
- package/package.json +4 -6
- package/test_hmset.js +0 -91
package/index.js
CHANGED
|
@@ -3,18 +3,16 @@
|
|
|
3
3
|
* @author <a href="http://qww.elins.cn">邱文武</a>
|
|
4
4
|
* @version 1.5.0
|
|
5
5
|
*/
|
|
6
|
-
const {
|
|
7
|
-
BaseService
|
|
8
|
-
} = require('mm_base_service');
|
|
9
6
|
const {
|
|
10
7
|
createClient
|
|
11
8
|
} = require('redis');
|
|
9
|
+
const {Event} = require('mm_expand');
|
|
12
10
|
|
|
13
11
|
// Redis缓存帮助类函数
|
|
14
12
|
|
|
15
13
|
/* 创建Redis帮助类函数 */
|
|
16
14
|
/// 构造函数
|
|
17
|
-
class Redis extends
|
|
15
|
+
class Redis extends Event {
|
|
18
16
|
/**
|
|
19
17
|
* @description 创建Redis帮助类函数 (构造函数)
|
|
20
18
|
* @param {Object} config 配置参数
|
|
@@ -97,7 +95,7 @@ Redis.prototype.subscribe = async function(channel, func) {
|
|
|
97
95
|
this.func_list[channel] = func;
|
|
98
96
|
return true;
|
|
99
97
|
} catch (err) {
|
|
100
|
-
|
|
98
|
+
this.logger('error',`订阅频道 ${channel} 失败:`, err);
|
|
101
99
|
return false;
|
|
102
100
|
}
|
|
103
101
|
};
|
|
@@ -113,7 +111,7 @@ Redis.prototype.unsubscribe = async function(channel) {
|
|
|
113
111
|
delete this.func_list[channel];
|
|
114
112
|
return true;
|
|
115
113
|
} catch (err) {
|
|
116
|
-
|
|
114
|
+
this.logger('error',`取消订阅频道 ${channel} 失败:`, err);
|
|
117
115
|
return false;
|
|
118
116
|
}
|
|
119
117
|
};
|
|
@@ -128,8 +126,7 @@ Redis.prototype.publish = async function(channel, message) {
|
|
|
128
126
|
try {
|
|
129
127
|
return await this.conn.publish(this.applyPrefix(channel), message);
|
|
130
128
|
} catch (err) {
|
|
131
|
-
|
|
132
|
-
$.log.error('[Redis] 操作失败:', err);
|
|
129
|
+
this.logger('error',`发布消息到频道 ${channel} 失败:`, err);
|
|
133
130
|
return false;
|
|
134
131
|
}
|
|
135
132
|
};
|
|
@@ -141,7 +138,7 @@ Redis.prototype.publish = async function(channel, message) {
|
|
|
141
138
|
*/
|
|
142
139
|
Redis.prototype.main = async function(channel, message) {
|
|
143
140
|
// 默认实现,可由子类重写
|
|
144
|
-
|
|
141
|
+
this.logger('debug',`收到未处理的消息 [${channel}]:`, message);
|
|
145
142
|
};
|
|
146
143
|
|
|
147
144
|
/**
|
|
@@ -157,7 +154,7 @@ Redis.prototype.message = function(channel, message) {
|
|
|
157
154
|
try {
|
|
158
155
|
func(message);
|
|
159
156
|
} catch (err) {
|
|
160
|
-
|
|
157
|
+
this.logger('error',`处理频道 ${channel} 的消息时出错:`, err);
|
|
161
158
|
}
|
|
162
159
|
} else {
|
|
163
160
|
this.main(channel, message);
|
|
@@ -219,7 +216,7 @@ Redis.prototype.setConfig = function(config) {
|
|
|
219
216
|
|
|
220
217
|
// 初始化连接池
|
|
221
218
|
this._initPool();
|
|
222
|
-
//
|
|
219
|
+
// this.logger('debug','配置已更新');
|
|
223
220
|
this.identifier = this.config.host + "/" + this.config.database;
|
|
224
221
|
return this;
|
|
225
222
|
};
|
|
@@ -244,7 +241,7 @@ Redis.prototype._initPool = function() {
|
|
|
244
241
|
};
|
|
245
242
|
|
|
246
243
|
// 记录连接池初始化
|
|
247
|
-
//
|
|
244
|
+
// this.logger('debug','连接池已初始化,配置:', this.config.pool);
|
|
248
245
|
};
|
|
249
246
|
|
|
250
247
|
/**
|
|
@@ -273,7 +270,7 @@ Redis.prototype.open = async function() {
|
|
|
273
270
|
|
|
274
271
|
// 监听错误事件
|
|
275
272
|
this.conn.on('error', (err) => {
|
|
276
|
-
|
|
273
|
+
this.logger('error','Redis连接错误:', err);
|
|
277
274
|
this.connected = false;
|
|
278
275
|
// 尝试重连
|
|
279
276
|
this._reconnect();
|
|
@@ -281,25 +278,25 @@ Redis.prototype.open = async function() {
|
|
|
281
278
|
|
|
282
279
|
// 监听连接成功事件
|
|
283
280
|
this.conn.on('connect', () => {
|
|
284
|
-
//
|
|
281
|
+
// this.logger('debug','Redis已连接');
|
|
285
282
|
this.connected = true;
|
|
286
283
|
this.retry_count = 0;
|
|
287
284
|
});
|
|
288
285
|
|
|
289
286
|
// 监听断开连接事件
|
|
290
287
|
this.conn.on('end', () => {
|
|
291
|
-
//
|
|
288
|
+
// this.logger('debug','Redis连接已断开');
|
|
292
289
|
this.connected = false;
|
|
293
290
|
});
|
|
294
291
|
|
|
295
292
|
// 监听订阅成功事件
|
|
296
293
|
this.conn.on('subscribe', (channel, count) => {
|
|
297
|
-
|
|
294
|
+
this.logger('debug',`频道${this.removePrefix(channel)}订阅成功, 当前总计订阅数:${count}`);
|
|
298
295
|
});
|
|
299
296
|
|
|
300
297
|
// 监听取消订阅事件
|
|
301
298
|
this.conn.on('unsubscribe', (channel, count) => {
|
|
302
|
-
|
|
299
|
+
this.logger('debug',`频道${this.removePrefix(channel)}取消订阅, 当前总计订阅数:${count}`);
|
|
303
300
|
});
|
|
304
301
|
|
|
305
302
|
// 收到消息后执行回调
|
|
@@ -312,7 +309,7 @@ Redis.prototype.open = async function() {
|
|
|
312
309
|
this.connected = true;
|
|
313
310
|
return true;
|
|
314
311
|
} catch (err) {
|
|
315
|
-
|
|
312
|
+
this.logger('error','Redis连接失败:', err);
|
|
316
313
|
// 尝试重连
|
|
317
314
|
this._reconnect();
|
|
318
315
|
return false;
|
|
@@ -325,18 +322,18 @@ Redis.prototype.open = async function() {
|
|
|
325
322
|
*/
|
|
326
323
|
Redis.prototype._reconnect = function() {
|
|
327
324
|
if (this.retry_count >= this.config.retry_count) {
|
|
328
|
-
|
|
325
|
+
this.logger('error',`达到最大重连次数 ${this.config.retry_count},停止重连`);
|
|
329
326
|
return;
|
|
330
327
|
}
|
|
331
328
|
|
|
332
329
|
this.retry_count++;
|
|
333
|
-
|
|
330
|
+
this.logger('debug',`尝试第 ${this.retry_count} 次重连...`);
|
|
334
331
|
|
|
335
332
|
setTimeout(async () => {
|
|
336
333
|
try {
|
|
337
334
|
await this.open();
|
|
338
335
|
} catch (err) {
|
|
339
|
-
|
|
336
|
+
this.logger('error',`重连失败:`, err);
|
|
340
337
|
}
|
|
341
338
|
}, this.config.retry_delay);
|
|
342
339
|
};
|
|
@@ -348,7 +345,7 @@ Redis.prototype._reconnect = function() {
|
|
|
348
345
|
// 修复close方法,正确处理连接池
|
|
349
346
|
Redis.prototype.close = function() {
|
|
350
347
|
try {
|
|
351
|
-
//
|
|
348
|
+
// this.logger('debug','关闭所有连接...');
|
|
352
349
|
|
|
353
350
|
// 关闭所有使用中的连接
|
|
354
351
|
if (this.connection_pool && this.connection_pool.in_use) {
|
|
@@ -356,7 +353,7 @@ Redis.prototype.close = function() {
|
|
|
356
353
|
try {
|
|
357
354
|
connection.close();
|
|
358
355
|
} catch (e) {
|
|
359
|
-
|
|
356
|
+
this.logger('error','关闭连接失败:', e);
|
|
360
357
|
}
|
|
361
358
|
}
|
|
362
359
|
this.connection_pool.in_use.clear();
|
|
@@ -373,20 +370,20 @@ Redis.prototype.close = function() {
|
|
|
373
370
|
// 某些Redis客户端可能使用end方法
|
|
374
371
|
connection.end();
|
|
375
372
|
} else {
|
|
376
|
-
|
|
373
|
+
this.logger('debug','连接对象没有可用的关闭方法');
|
|
377
374
|
}
|
|
378
375
|
} catch (e) {
|
|
379
|
-
|
|
376
|
+
this.logger('error','关闭连接失败:', e);
|
|
380
377
|
}
|
|
381
378
|
}
|
|
382
379
|
this.connection_pool.available = [];
|
|
383
380
|
}
|
|
384
381
|
|
|
385
382
|
this.connected = false;
|
|
386
|
-
|
|
383
|
+
this.logger('debug','Redis连接已断开');
|
|
387
384
|
return Promise.resolve(true);
|
|
388
385
|
} catch (error) {
|
|
389
|
-
|
|
386
|
+
this.logger('error','关闭连接时发生错误:', error);
|
|
390
387
|
return Promise.resolve(false);
|
|
391
388
|
}
|
|
392
389
|
};
|
|
@@ -513,7 +510,7 @@ Redis.prototype.pipeline = function() {
|
|
|
513
510
|
// 直接返回结果值,符合测试期望的格式
|
|
514
511
|
results.push(result);
|
|
515
512
|
} catch (err) {
|
|
516
|
-
|
|
513
|
+
this.logger('error',`Pipeline命令执行失败 [${cmd[0]}]:`, err);
|
|
517
514
|
results.push(null);
|
|
518
515
|
}
|
|
519
516
|
}
|
|
@@ -522,7 +519,7 @@ Redis.prototype.pipeline = function() {
|
|
|
522
519
|
redisInstance._recycleConnection(connection);
|
|
523
520
|
return results;
|
|
524
521
|
} catch (error) {
|
|
525
|
-
|
|
522
|
+
this.logger('error','Pipeline执行失败:', error);
|
|
526
523
|
return [];
|
|
527
524
|
}
|
|
528
525
|
}
|
|
@@ -569,7 +566,7 @@ Redis.prototype.ttl = async function(key, seconds) {
|
|
|
569
566
|
await connection.execute('EXPIRE', [key, seconds]);
|
|
570
567
|
return true;
|
|
571
568
|
} else {
|
|
572
|
-
|
|
569
|
+
this.logger('error','连接对象不支持expire操作');
|
|
573
570
|
return false;
|
|
574
571
|
}
|
|
575
572
|
}
|
|
@@ -580,14 +577,14 @@ Redis.prototype.ttl = async function(key, seconds) {
|
|
|
580
577
|
} else if (typeof connection.execute === 'function') {
|
|
581
578
|
return await connection.execute('TTL', [key]);
|
|
582
579
|
} else {
|
|
583
|
-
|
|
580
|
+
this.logger('error','连接对象不支持ttl操作');
|
|
584
581
|
return -2; // 默认返回键不存在
|
|
585
582
|
}
|
|
586
583
|
} finally {
|
|
587
584
|
this._recycleConnection(connection);
|
|
588
585
|
}
|
|
589
586
|
} catch (err) {
|
|
590
|
-
|
|
587
|
+
this.logger('error',`${seconds !== undefined ? '设置' : '获取'}过期时间失败 [${key}]:`, err);
|
|
591
588
|
return seconds !== undefined ? false : -2;
|
|
592
589
|
}
|
|
593
590
|
};
|
|
@@ -603,26 +600,26 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
|
|
|
603
600
|
try {
|
|
604
601
|
// 参数验证
|
|
605
602
|
if (!key || typeof key !== 'string') {
|
|
606
|
-
|
|
603
|
+
this.logger('error','参数错误: 键必须是非空字符串');
|
|
607
604
|
return false;
|
|
608
605
|
}
|
|
609
606
|
if (typeof num !== 'number' || isNaN(num) || !Number.isInteger(num)) {
|
|
610
|
-
|
|
607
|
+
this.logger('error','参数错误: 数值必须是有效整数');
|
|
611
608
|
return false;
|
|
612
609
|
}
|
|
613
610
|
|
|
614
611
|
// 触发前置事件,允许修改或阻止操作
|
|
615
|
-
const
|
|
612
|
+
const ctx = {
|
|
616
613
|
key,
|
|
617
614
|
value: num,
|
|
618
615
|
seconds,
|
|
619
616
|
cancel: false
|
|
620
617
|
};
|
|
621
|
-
this.emit('
|
|
618
|
+
this.emit('addInt:before', ctx);
|
|
622
619
|
|
|
623
620
|
// 如果事件处理器标记为取消,则返回false
|
|
624
|
-
if (
|
|
625
|
-
|
|
621
|
+
if (ctx.cancel) {
|
|
622
|
+
this.logger('debug',`addInt操作被事件处理器取消 [${key}]`);
|
|
626
623
|
return false;
|
|
627
624
|
}
|
|
628
625
|
|
|
@@ -631,7 +628,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
|
|
|
631
628
|
key: finalKey,
|
|
632
629
|
value: finalNum,
|
|
633
630
|
seconds: finalSeconds
|
|
634
|
-
} =
|
|
631
|
+
} = ctx;
|
|
635
632
|
|
|
636
633
|
const connection = await this._getConnection();
|
|
637
634
|
try {
|
|
@@ -645,7 +642,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
|
|
|
645
642
|
result = await connection.execute('INCRBY', [prefixedKey, finalNum]);
|
|
646
643
|
} else {
|
|
647
644
|
// 如果出错,回退到先获取再设置的方式
|
|
648
|
-
|
|
645
|
+
throw new Error(`使用原子操作增加整数值失败,使用回退方案`);
|
|
649
646
|
let value = await this.get(prefixedKey);
|
|
650
647
|
let n = 0;
|
|
651
648
|
if (value) {
|
|
@@ -663,7 +660,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
|
|
|
663
660
|
result = await connection.execute('DECRBY', [prefixedKey, -finalNum]);
|
|
664
661
|
} else {
|
|
665
662
|
// 如果出错,回退到先获取再设置的方式
|
|
666
|
-
|
|
663
|
+
throw new Error(`使用原子操作减少整数值失败,使用回退方案`);
|
|
667
664
|
let value = await this.get(prefixedKey);
|
|
668
665
|
let n = 0;
|
|
669
666
|
if (value) {
|
|
@@ -685,7 +682,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
|
|
|
685
682
|
}
|
|
686
683
|
|
|
687
684
|
// 触发后置事件
|
|
688
|
-
this.emit('
|
|
685
|
+
this.emit('addInt:after', {
|
|
689
686
|
key: finalKey,
|
|
690
687
|
value: finalNum,
|
|
691
688
|
seconds: finalSeconds,
|
|
@@ -698,13 +695,13 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
|
|
|
698
695
|
}
|
|
699
696
|
} catch (err) {
|
|
700
697
|
// 触发错误事件
|
|
701
|
-
this.emit('
|
|
698
|
+
this.emit('addInt:error', {
|
|
702
699
|
key,
|
|
703
700
|
value: num,
|
|
704
701
|
seconds,
|
|
705
702
|
error: err
|
|
706
703
|
});
|
|
707
|
-
|
|
704
|
+
this.logger('error',`增加整数值失败 [${key}]:`, err);
|
|
708
705
|
// 与CacheBase保持一致,错误时返回默认值而不是抛出异常
|
|
709
706
|
return 0;
|
|
710
707
|
}
|
|
@@ -721,29 +718,29 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
|
|
|
721
718
|
try {
|
|
722
719
|
// 参数验证
|
|
723
720
|
if (!key || typeof key !== 'string') {
|
|
724
|
-
|
|
721
|
+
throw new Error(`addFloat: key参数必须是非空字符串`);
|
|
725
722
|
return 0;
|
|
726
723
|
}
|
|
727
724
|
|
|
728
725
|
// 验证num参数
|
|
729
726
|
num = parseFloat(num);
|
|
730
727
|
if (isNaN(num)) {
|
|
731
|
-
|
|
728
|
+
throw new Error(`addFloat: num参数必须是有效的数值`);
|
|
732
729
|
return 0;
|
|
733
730
|
}
|
|
734
731
|
|
|
735
732
|
// 触发前置事件,允许修改或阻止操作
|
|
736
|
-
const
|
|
733
|
+
const ctx = {
|
|
737
734
|
key,
|
|
738
735
|
value: num,
|
|
739
736
|
seconds,
|
|
740
737
|
cancel: false
|
|
741
738
|
};
|
|
742
|
-
this.emit('
|
|
739
|
+
this.emit('addFloat:before', ctx);
|
|
743
740
|
|
|
744
741
|
// 如果事件处理器标记为取消,则返回默认值
|
|
745
|
-
if (
|
|
746
|
-
|
|
742
|
+
if (ctx.cancel) {
|
|
743
|
+
this.logger('debug',`addFloat操作被事件处理器取消 [${key}]`);
|
|
747
744
|
return 0;
|
|
748
745
|
}
|
|
749
746
|
|
|
@@ -752,7 +749,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
|
|
|
752
749
|
key: finalKey,
|
|
753
750
|
value: finalNum,
|
|
754
751
|
seconds: finalSeconds
|
|
755
|
-
} =
|
|
752
|
+
} = ctx;
|
|
756
753
|
let result;
|
|
757
754
|
|
|
758
755
|
const connection = await this._getConnection();
|
|
@@ -769,7 +766,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
|
|
|
769
766
|
}
|
|
770
767
|
// 回退方案
|
|
771
768
|
else {
|
|
772
|
-
|
|
769
|
+
throw new Error(`连接对象不支持incrByFloat操作,使用回退方案`);
|
|
773
770
|
let value = await this.get(prefixedKey);
|
|
774
771
|
let n = 0;
|
|
775
772
|
if (value) {
|
|
@@ -790,7 +787,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
|
|
|
790
787
|
}
|
|
791
788
|
|
|
792
789
|
// 触发后置事件
|
|
793
|
-
this.emit('
|
|
790
|
+
this.emit('addFloat:after', {
|
|
794
791
|
key: finalKey,
|
|
795
792
|
value: finalNum,
|
|
796
793
|
seconds: finalSeconds,
|
|
@@ -800,7 +797,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
|
|
|
800
797
|
return result;
|
|
801
798
|
} catch (err) {
|
|
802
799
|
// 如果出错,回退到先获取再设置的方式
|
|
803
|
-
|
|
800
|
+
throw new Error(`使用原子操作增加浮点数值失败,使用回退方案:`, err);
|
|
804
801
|
let value = await this.get(prefixedKey);
|
|
805
802
|
let n = 0;
|
|
806
803
|
if (value) {
|
|
@@ -810,7 +807,7 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
|
|
|
810
807
|
await this.set(prefixedKey, n, finalSeconds);
|
|
811
808
|
|
|
812
809
|
// 触发后置事件
|
|
813
|
-
this.emit('
|
|
810
|
+
this.emit('addFloat:after', {
|
|
814
811
|
key: finalKey,
|
|
815
812
|
value: finalNum,
|
|
816
813
|
seconds: finalSeconds,
|
|
@@ -823,13 +820,13 @@ Redis.prototype.addFloat = async function(key, num, seconds = 0) {
|
|
|
823
820
|
}
|
|
824
821
|
} catch (err) {
|
|
825
822
|
// 触发错误事件
|
|
826
|
-
this.emit('
|
|
823
|
+
this.emit('addFloat:error', {
|
|
827
824
|
key,
|
|
828
825
|
value: num,
|
|
829
826
|
seconds,
|
|
830
827
|
error: err
|
|
831
828
|
});
|
|
832
|
-
|
|
829
|
+
this.logger('error',`增加浮点数值失败 [${key}]:`, err);
|
|
833
830
|
// 与CacheBase保持一致,错误时返回默认值而不是抛出异常
|
|
834
831
|
return 0;
|
|
835
832
|
}
|
|
@@ -846,29 +843,29 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
|
|
|
846
843
|
try {
|
|
847
844
|
// 参数验证
|
|
848
845
|
if (!key || typeof key !== 'string') {
|
|
849
|
-
|
|
846
|
+
throw new Error(`addStr: key参数必须是非空字符串`);
|
|
850
847
|
return 0;
|
|
851
848
|
}
|
|
852
849
|
|
|
853
850
|
// 验证str参数
|
|
854
851
|
if (str === undefined || str === null) {
|
|
855
|
-
|
|
852
|
+
throw new Error(`addStr: str参数不能为undefined或null`);
|
|
856
853
|
return 0;
|
|
857
854
|
}
|
|
858
855
|
str = String(str);
|
|
859
856
|
|
|
860
857
|
// 触发前置事件,允许修改或阻止操作
|
|
861
|
-
const
|
|
858
|
+
const ctx = {
|
|
862
859
|
key,
|
|
863
860
|
value: str,
|
|
864
861
|
seconds,
|
|
865
862
|
cancel: false
|
|
866
863
|
};
|
|
867
|
-
this.emit('
|
|
864
|
+
this.emit('addStr:before', ctx);
|
|
868
865
|
|
|
869
866
|
// 如果事件处理器标记为取消,则返回0
|
|
870
|
-
if (
|
|
871
|
-
|
|
867
|
+
if (ctx.cancel) {
|
|
868
|
+
this.logger('debug',`addStr操作被事件处理器取消 [${key}]`);
|
|
872
869
|
return 0;
|
|
873
870
|
}
|
|
874
871
|
|
|
@@ -877,7 +874,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
|
|
|
877
874
|
key: finalKey,
|
|
878
875
|
value: finalStr,
|
|
879
876
|
seconds: finalSeconds
|
|
880
|
-
} =
|
|
877
|
+
} = ctx;
|
|
881
878
|
let result;
|
|
882
879
|
|
|
883
880
|
const connection = await this._getConnection();
|
|
@@ -894,7 +891,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
|
|
|
894
891
|
}
|
|
895
892
|
// 回退方案
|
|
896
893
|
else {
|
|
897
|
-
|
|
894
|
+
throw new Error(`连接对象不支持append操作,使用回退方案`);
|
|
898
895
|
let value = await this.get(prefixedKey) || '';
|
|
899
896
|
value += finalStr;
|
|
900
897
|
await this.set(prefixedKey, value, finalSeconds);
|
|
@@ -911,7 +908,7 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
|
|
|
911
908
|
}
|
|
912
909
|
|
|
913
910
|
// 触发后置事件
|
|
914
|
-
this.emit('
|
|
911
|
+
this.emit('addStr:after', {
|
|
915
912
|
key: finalKey,
|
|
916
913
|
value: finalStr,
|
|
917
914
|
seconds: finalSeconds,
|
|
@@ -920,14 +917,14 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
|
|
|
920
917
|
});
|
|
921
918
|
return result;
|
|
922
919
|
} catch (err) {
|
|
923
|
-
|
|
920
|
+
this.logger('error','追加字符串失败:', err);
|
|
924
921
|
// 回退方案:先获取再设置
|
|
925
922
|
let value = await this.get(prefixedKey) || '';
|
|
926
923
|
value += finalStr;
|
|
927
924
|
await this.set(prefixedKey, value, finalSeconds);
|
|
928
925
|
|
|
929
926
|
// 触发后置事件
|
|
930
|
-
this.emit('
|
|
927
|
+
this.emit('addStr:after', {
|
|
931
928
|
key: finalKey,
|
|
932
929
|
value: finalStr,
|
|
933
930
|
seconds: finalSeconds,
|
|
@@ -940,13 +937,13 @@ Redis.prototype.addStr = async function(key, str, seconds = 0) {
|
|
|
940
937
|
}
|
|
941
938
|
} catch (err) {
|
|
942
939
|
// 触发错误事件
|
|
943
|
-
this.emit('
|
|
940
|
+
this.emit('addStr:error', {
|
|
944
941
|
key,
|
|
945
942
|
value: str,
|
|
946
943
|
seconds,
|
|
947
944
|
error: err
|
|
948
945
|
});
|
|
949
|
-
|
|
946
|
+
this.logger('error',`追加字符串失败 [${key}]:`, err);
|
|
950
947
|
return 0;
|
|
951
948
|
}
|
|
952
949
|
};
|
|
@@ -960,14 +957,14 @@ Redis.prototype.del = async function(key) {
|
|
|
960
957
|
try {
|
|
961
958
|
// 键名必须是非空字符串或非空字符串数组
|
|
962
959
|
if (!key) {
|
|
963
|
-
|
|
960
|
+
throw new Error('del: key必须是非空字符串或非空字符串数组');
|
|
964
961
|
return false;
|
|
965
962
|
}
|
|
966
963
|
|
|
967
964
|
// 如果是单个键,验证是否为非空字符串
|
|
968
965
|
if (typeof key === 'string') {
|
|
969
966
|
if (!key) {
|
|
970
|
-
|
|
967
|
+
throw new Error('del: key必须是非空字符串');
|
|
971
968
|
return false;
|
|
972
969
|
}
|
|
973
970
|
}
|
|
@@ -975,32 +972,32 @@ Redis.prototype.del = async function(key) {
|
|
|
975
972
|
else if (Array.isArray(key)) {
|
|
976
973
|
for (const k of key) {
|
|
977
974
|
if (!k || typeof k !== 'string') {
|
|
978
|
-
|
|
975
|
+
throw new Error('del: key必须是非空字符串或非空字符串数组');
|
|
979
976
|
return false;
|
|
980
977
|
}
|
|
981
978
|
}
|
|
982
979
|
} else {
|
|
983
|
-
|
|
980
|
+
throw new Error('del: key必须是非空字符串或非空字符串数组');
|
|
984
981
|
return false;
|
|
985
982
|
}
|
|
986
983
|
|
|
987
984
|
// 触发前置事件,允许修改或阻止操作
|
|
988
|
-
const
|
|
985
|
+
const ctx = {
|
|
989
986
|
key,
|
|
990
987
|
cancel: false
|
|
991
988
|
};
|
|
992
|
-
this.emit('
|
|
989
|
+
this.emit('del:before', ctx);
|
|
993
990
|
|
|
994
991
|
// 如果事件处理器标记为取消,则返回false
|
|
995
|
-
if (
|
|
996
|
-
|
|
992
|
+
if (ctx.cancel) {
|
|
993
|
+
this.logger('debug',`del操作被事件处理器取消 [${key}]`);
|
|
997
994
|
return false;
|
|
998
995
|
}
|
|
999
996
|
|
|
1000
997
|
// 使用可能被修改的值
|
|
1001
998
|
let {
|
|
1002
999
|
key: finalKey
|
|
1003
|
-
} =
|
|
1000
|
+
} = ctx;
|
|
1004
1001
|
|
|
1005
1002
|
const connection = await this._getConnection();
|
|
1006
1003
|
try {
|
|
@@ -1019,7 +1016,7 @@ Redis.prototype.del = async function(key) {
|
|
|
1019
1016
|
}
|
|
1020
1017
|
}
|
|
1021
1018
|
// 触发后置事件
|
|
1022
|
-
this.emit('
|
|
1019
|
+
this.emit('del:after', {
|
|
1023
1020
|
key: finalKey,
|
|
1024
1021
|
success,
|
|
1025
1022
|
deleted
|
|
@@ -1030,7 +1027,7 @@ Redis.prototype.del = async function(key) {
|
|
|
1030
1027
|
const n = await connection.del(prefixedKey);
|
|
1031
1028
|
success = n === 1;
|
|
1032
1029
|
// 触发后置事件
|
|
1033
|
-
this.emit('
|
|
1030
|
+
this.emit('del:after', {
|
|
1034
1031
|
key: finalKey,
|
|
1035
1032
|
success,
|
|
1036
1033
|
deleted: success ? [finalKey] : []
|
|
@@ -1042,11 +1039,11 @@ Redis.prototype.del = async function(key) {
|
|
|
1042
1039
|
}
|
|
1043
1040
|
} catch (err) {
|
|
1044
1041
|
// 触发错误事件
|
|
1045
|
-
this.emit('
|
|
1042
|
+
this.emit('del:error', {
|
|
1046
1043
|
key,
|
|
1047
1044
|
error: err
|
|
1048
1045
|
});
|
|
1049
|
-
|
|
1046
|
+
this.logger('error',`删除缓存失败 [${key}]:`, err);
|
|
1050
1047
|
return false;
|
|
1051
1048
|
}
|
|
1052
1049
|
};
|
|
@@ -1069,22 +1066,22 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
|
|
|
1069
1066
|
try {
|
|
1070
1067
|
// 键名必须是非空字符串
|
|
1071
1068
|
if (!key || typeof key !== 'string') {
|
|
1072
|
-
|
|
1069
|
+
this.logger('error','参数错误: key必须是非空字符串');
|
|
1073
1070
|
return false;
|
|
1074
1071
|
}
|
|
1075
1072
|
|
|
1076
1073
|
// 触发前置事件,允许修改或阻止操作
|
|
1077
|
-
const
|
|
1074
|
+
const ctx = {
|
|
1078
1075
|
key,
|
|
1079
1076
|
value,
|
|
1080
1077
|
seconds,
|
|
1081
1078
|
cancel: false
|
|
1082
1079
|
};
|
|
1083
|
-
this.emit('
|
|
1080
|
+
this.emit('set:before', ctx);
|
|
1084
1081
|
|
|
1085
1082
|
// 如果事件处理器标记为取消,则返回false
|
|
1086
|
-
if (
|
|
1087
|
-
|
|
1083
|
+
if (ctx.cancel) {
|
|
1084
|
+
this.logger('debug',`set操作被事件处理器取消 [${key}]`);
|
|
1088
1085
|
return false;
|
|
1089
1086
|
}
|
|
1090
1087
|
|
|
@@ -1093,7 +1090,7 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
|
|
|
1093
1090
|
key: finalKey,
|
|
1094
1091
|
value: finalValue,
|
|
1095
1092
|
seconds: finalSeconds
|
|
1096
|
-
} =
|
|
1093
|
+
} = ctx;
|
|
1097
1094
|
|
|
1098
1095
|
const connection = await this._getConnection();
|
|
1099
1096
|
try {
|
|
@@ -1111,7 +1108,7 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
|
|
|
1111
1108
|
}
|
|
1112
1109
|
|
|
1113
1110
|
// 触发后置事件
|
|
1114
|
-
this.emit('
|
|
1111
|
+
this.emit('set:after', {
|
|
1115
1112
|
key,
|
|
1116
1113
|
value,
|
|
1117
1114
|
seconds,
|
|
@@ -1122,9 +1119,9 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
|
|
|
1122
1119
|
this._recycleConnection(connection);
|
|
1123
1120
|
}
|
|
1124
1121
|
} catch (err) {
|
|
1125
|
-
|
|
1122
|
+
this.logger('error',`设置缓存失败 [${key}]:`, err);
|
|
1126
1123
|
// 触发错误事件
|
|
1127
|
-
this.emit('
|
|
1124
|
+
this.emit('set:error', {
|
|
1128
1125
|
key,
|
|
1129
1126
|
value,
|
|
1130
1127
|
seconds,
|
|
@@ -1144,17 +1141,17 @@ Redis.prototype.set = async function(key, value, seconds = 0) {
|
|
|
1144
1141
|
Redis.prototype.add = async function(key, value, seconds) {
|
|
1145
1142
|
try {
|
|
1146
1143
|
// 触发前置事件,允许修改或阻止操作
|
|
1147
|
-
const
|
|
1144
|
+
const ctx = {
|
|
1148
1145
|
key,
|
|
1149
1146
|
value,
|
|
1150
1147
|
ttl: seconds,
|
|
1151
1148
|
cancel: false
|
|
1152
1149
|
};
|
|
1153
|
-
this.emit('
|
|
1150
|
+
this.emit('add:before', ctx);
|
|
1154
1151
|
|
|
1155
1152
|
// 如果事件处理器标记为取消,则返回false
|
|
1156
|
-
if (
|
|
1157
|
-
|
|
1153
|
+
if (ctx.cancel) {
|
|
1154
|
+
this.logger('debug',`add操作被事件处理器取消 [${key}]`);
|
|
1158
1155
|
return false;
|
|
1159
1156
|
}
|
|
1160
1157
|
|
|
@@ -1163,7 +1160,7 @@ Redis.prototype.add = async function(key, value, seconds) {
|
|
|
1163
1160
|
key: finalKey,
|
|
1164
1161
|
value: finalValue,
|
|
1165
1162
|
ttl: finalSeconds
|
|
1166
|
-
} =
|
|
1163
|
+
} = ctx;
|
|
1167
1164
|
|
|
1168
1165
|
const connection = await this._getConnection();
|
|
1169
1166
|
try {
|
|
@@ -1183,7 +1180,7 @@ Redis.prototype.add = async function(key, value, seconds) {
|
|
|
1183
1180
|
const success = result === "OK";
|
|
1184
1181
|
|
|
1185
1182
|
// 触发后置事件
|
|
1186
|
-
this.emit('
|
|
1183
|
+
this.emit('add:after', {
|
|
1187
1184
|
key: finalKey,
|
|
1188
1185
|
value: finalValue,
|
|
1189
1186
|
ttl: finalSeconds,
|
|
@@ -1194,9 +1191,9 @@ Redis.prototype.add = async function(key, value, seconds) {
|
|
|
1194
1191
|
this._recycleConnection(connection);
|
|
1195
1192
|
}
|
|
1196
1193
|
} catch (err) {
|
|
1197
|
-
|
|
1194
|
+
this.logger('error',`添加缓存失败 [${key}]:`, err);
|
|
1198
1195
|
// 触发错误事件
|
|
1199
|
-
this.emit('
|
|
1196
|
+
this.emit('add:error', {
|
|
1200
1197
|
key,
|
|
1201
1198
|
value,
|
|
1202
1199
|
ttl: seconds,
|
|
@@ -1220,25 +1217,25 @@ Redis.prototype.get = async function(key) {
|
|
|
1220
1217
|
try {
|
|
1221
1218
|
// 参数验证
|
|
1222
1219
|
if (!key || typeof key !== 'string') {
|
|
1223
|
-
|
|
1220
|
+
throw new Error(`get: key参数必须是非空字符串`);
|
|
1224
1221
|
return null;
|
|
1225
1222
|
}
|
|
1226
1223
|
|
|
1227
1224
|
// 触发前置事件,允许修改或阻止操作
|
|
1228
|
-
const
|
|
1225
|
+
const ctx = {
|
|
1229
1226
|
key,
|
|
1230
1227
|
cancel: false
|
|
1231
1228
|
};
|
|
1232
|
-
this.emit('
|
|
1229
|
+
this.emit('get:before', ctx);
|
|
1233
1230
|
|
|
1234
1231
|
// 如果事件处理器标记为取消,则返回null
|
|
1235
|
-
if (
|
|
1236
|
-
|
|
1232
|
+
if (ctx.cancel) {
|
|
1233
|
+
this.logger('debug',`get操作被事件处理器取消 [${key}]`);
|
|
1237
1234
|
return null;
|
|
1238
1235
|
}
|
|
1239
1236
|
|
|
1240
1237
|
// 使用可能被修改的值
|
|
1241
|
-
const finalKey =
|
|
1238
|
+
const finalKey = ctx.key;
|
|
1242
1239
|
|
|
1243
1240
|
const connection = await this._getConnection();
|
|
1244
1241
|
try {
|
|
@@ -1258,7 +1255,7 @@ Redis.prototype.get = async function(key) {
|
|
|
1258
1255
|
}
|
|
1259
1256
|
|
|
1260
1257
|
// 触发后置事件
|
|
1261
|
-
this.emit('
|
|
1258
|
+
this.emit('get:after', {
|
|
1262
1259
|
key: finalKey,
|
|
1263
1260
|
value: result,
|
|
1264
1261
|
success: true
|
|
@@ -1270,11 +1267,11 @@ Redis.prototype.get = async function(key) {
|
|
|
1270
1267
|
}
|
|
1271
1268
|
} catch (err) {
|
|
1272
1269
|
// 触发错误事件
|
|
1273
|
-
this.emit('
|
|
1270
|
+
this.emit('get:error', {
|
|
1274
1271
|
key,
|
|
1275
1272
|
error: err
|
|
1276
1273
|
});
|
|
1277
|
-
|
|
1274
|
+
this.logger('error',`获取缓存失败 [${key}]:`, err);
|
|
1278
1275
|
return null;
|
|
1279
1276
|
}
|
|
1280
1277
|
};
|
|
@@ -1288,25 +1285,25 @@ Redis.prototype.has = async function(key) {
|
|
|
1288
1285
|
try {
|
|
1289
1286
|
// 参数验证
|
|
1290
1287
|
if (!key || typeof key !== 'string') {
|
|
1291
|
-
|
|
1288
|
+
throw new Error(`has: key参数必须是非空字符串`);
|
|
1292
1289
|
return false;
|
|
1293
1290
|
}
|
|
1294
1291
|
|
|
1295
1292
|
// 触发前置事件,允许修改或阻止操作
|
|
1296
|
-
const
|
|
1293
|
+
const ctx = {
|
|
1297
1294
|
key,
|
|
1298
1295
|
cancel: false
|
|
1299
1296
|
};
|
|
1300
|
-
this.emit('
|
|
1297
|
+
this.emit('has:before', ctx);
|
|
1301
1298
|
|
|
1302
1299
|
// 如果事件处理器标记为取消,则返回false
|
|
1303
|
-
if (
|
|
1304
|
-
|
|
1300
|
+
if (ctx.cancel) {
|
|
1301
|
+
this.logger('debug',`has操作被事件处理器取消 [${key}]`);
|
|
1305
1302
|
return false;
|
|
1306
1303
|
}
|
|
1307
1304
|
|
|
1308
1305
|
// 使用可能被修改的值
|
|
1309
|
-
const finalKey =
|
|
1306
|
+
const finalKey = ctx.key;
|
|
1310
1307
|
|
|
1311
1308
|
const connection = await this._getConnection();
|
|
1312
1309
|
try {
|
|
@@ -1315,7 +1312,7 @@ Redis.prototype.has = async function(key) {
|
|
|
1315
1312
|
const exists = result > 0;
|
|
1316
1313
|
|
|
1317
1314
|
// 触发后置事件
|
|
1318
|
-
this.emit('
|
|
1315
|
+
this.emit('has:after', {
|
|
1319
1316
|
key: finalKey,
|
|
1320
1317
|
result: exists,
|
|
1321
1318
|
success: true
|
|
@@ -1327,11 +1324,11 @@ Redis.prototype.has = async function(key) {
|
|
|
1327
1324
|
}
|
|
1328
1325
|
} catch (err) {
|
|
1329
1326
|
// 触发错误事件
|
|
1330
|
-
this.emit('
|
|
1327
|
+
this.emit('has:error', {
|
|
1331
1328
|
key,
|
|
1332
1329
|
error: err
|
|
1333
1330
|
});
|
|
1334
|
-
|
|
1331
|
+
this.logger('error',`检查键是否存在失败 [${key}]:`, err);
|
|
1335
1332
|
return false;
|
|
1336
1333
|
}
|
|
1337
1334
|
};
|
|
@@ -1345,14 +1342,14 @@ Redis.prototype.exists = async function(key) {
|
|
|
1345
1342
|
try {
|
|
1346
1343
|
// 参数验证
|
|
1347
1344
|
if (!key) {
|
|
1348
|
-
|
|
1345
|
+
throw new Error('exists: key必须是非空字符串或非空字符串数组');
|
|
1349
1346
|
return 0;
|
|
1350
1347
|
}
|
|
1351
1348
|
|
|
1352
1349
|
// 如果是单个键,验证是否为非空字符串
|
|
1353
1350
|
if (typeof key === 'string') {
|
|
1354
1351
|
if (!key) {
|
|
1355
|
-
|
|
1352
|
+
throw new Error('exists: key必须是非空字符串');
|
|
1356
1353
|
return 0;
|
|
1357
1354
|
}
|
|
1358
1355
|
}
|
|
@@ -1360,30 +1357,30 @@ Redis.prototype.exists = async function(key) {
|
|
|
1360
1357
|
else if (Array.isArray(key)) {
|
|
1361
1358
|
for (const k of key) {
|
|
1362
1359
|
if (!k || typeof k !== 'string') {
|
|
1363
|
-
|
|
1360
|
+
throw new Error('exists: key必须是非空字符串或非空字符串数组');
|
|
1364
1361
|
return 0;
|
|
1365
1362
|
}
|
|
1366
1363
|
}
|
|
1367
1364
|
} else {
|
|
1368
|
-
|
|
1365
|
+
throw new Error('exists: key必须是非空字符串或非空字符串数组');
|
|
1369
1366
|
return 0;
|
|
1370
1367
|
}
|
|
1371
1368
|
|
|
1372
1369
|
// 触发前置事件,允许修改或阻止操作
|
|
1373
|
-
const
|
|
1370
|
+
const ctx = {
|
|
1374
1371
|
key,
|
|
1375
1372
|
cancel: false
|
|
1376
1373
|
};
|
|
1377
|
-
this.emit('
|
|
1374
|
+
this.emit('exists:before', ctx);
|
|
1378
1375
|
|
|
1379
1376
|
// 如果事件处理器标记为取消,则返回0
|
|
1380
|
-
if (
|
|
1381
|
-
|
|
1377
|
+
if (ctx.cancel) {
|
|
1378
|
+
this.logger('debug',`exists操作被事件处理器取消 [${key}]`);
|
|
1382
1379
|
return 0;
|
|
1383
1380
|
}
|
|
1384
1381
|
|
|
1385
1382
|
// 使用可能被修改的值
|
|
1386
|
-
const finalKey =
|
|
1383
|
+
const finalKey = ctx.key;
|
|
1387
1384
|
|
|
1388
1385
|
const connection = await this._getConnection();
|
|
1389
1386
|
try {
|
|
@@ -1397,7 +1394,7 @@ Redis.prototype.exists = async function(key) {
|
|
|
1397
1394
|
}
|
|
1398
1395
|
|
|
1399
1396
|
// 触发后置事件
|
|
1400
|
-
this.emit('
|
|
1397
|
+
this.emit('exists:after', {
|
|
1401
1398
|
key: finalKey,
|
|
1402
1399
|
result,
|
|
1403
1400
|
success: true
|
|
@@ -1409,11 +1406,11 @@ Redis.prototype.exists = async function(key) {
|
|
|
1409
1406
|
}
|
|
1410
1407
|
} catch (err) {
|
|
1411
1408
|
// 触发错误事件
|
|
1412
|
-
this.emit('
|
|
1409
|
+
this.emit('exists:error', {
|
|
1413
1410
|
key,
|
|
1414
1411
|
error: err
|
|
1415
1412
|
});
|
|
1416
|
-
|
|
1413
|
+
this.logger('error',`检查键存在性失败 [${key}]:`, err);
|
|
1417
1414
|
return 0;
|
|
1418
1415
|
}
|
|
1419
1416
|
};
|
|
@@ -1432,7 +1429,7 @@ Redis.prototype.getrange = async function(key, start, end) {
|
|
|
1432
1429
|
key = this.applyPrefix(key);
|
|
1433
1430
|
return await connection.getRange(key, start, end);
|
|
1434
1431
|
} catch (err) {
|
|
1435
|
-
|
|
1432
|
+
this.logger('error','获取字符串范围失败:', err);
|
|
1436
1433
|
// 回退方案
|
|
1437
1434
|
const value = await this.get(key);
|
|
1438
1435
|
if (value && typeof value === 'string') {
|
|
@@ -1443,7 +1440,7 @@ Redis.prototype.getrange = async function(key, start, end) {
|
|
|
1443
1440
|
this._recycleConnection(connection);
|
|
1444
1441
|
}
|
|
1445
1442
|
} catch (err) {
|
|
1446
|
-
|
|
1443
|
+
this.logger('error',`获取字符串范围失败 [${key}]:`, err);
|
|
1447
1444
|
return '';
|
|
1448
1445
|
}
|
|
1449
1446
|
};
|
|
@@ -1469,17 +1466,17 @@ Redis.prototype.insertString = function(str, index, str_sub) {
|
|
|
1469
1466
|
Redis.prototype.setrange = async function(key, index, value) {
|
|
1470
1467
|
try {
|
|
1471
1468
|
// 触发前置事件,允许修改或阻止操作
|
|
1472
|
-
const
|
|
1469
|
+
const ctx = {
|
|
1473
1470
|
key,
|
|
1474
1471
|
index,
|
|
1475
1472
|
value,
|
|
1476
1473
|
cancel: false
|
|
1477
1474
|
};
|
|
1478
|
-
this.emit('
|
|
1475
|
+
this.emit('setrange:before', ctx);
|
|
1479
1476
|
|
|
1480
1477
|
// 如果事件处理器标记为取消,则返回0
|
|
1481
|
-
if (
|
|
1482
|
-
|
|
1478
|
+
if (ctx.cancel) {
|
|
1479
|
+
this.logger('debug',`setrange操作被事件处理器取消 [${key}]`);
|
|
1483
1480
|
return 0;
|
|
1484
1481
|
}
|
|
1485
1482
|
|
|
@@ -1488,7 +1485,7 @@ Redis.prototype.setrange = async function(key, index, value) {
|
|
|
1488
1485
|
key: finalKey,
|
|
1489
1486
|
index: finalIndex,
|
|
1490
1487
|
value: finalValue
|
|
1491
|
-
} =
|
|
1488
|
+
} = ctx;
|
|
1492
1489
|
let result;
|
|
1493
1490
|
|
|
1494
1491
|
const connection = await this._getConnection();
|
|
@@ -1505,7 +1502,7 @@ Redis.prototype.setrange = async function(key, index, value) {
|
|
|
1505
1502
|
}
|
|
1506
1503
|
// 回退方案
|
|
1507
1504
|
else {
|
|
1508
|
-
|
|
1505
|
+
throw new Error(`连接对象不支持setRange操作,使用回退方案`);
|
|
1509
1506
|
let str = await this.get(finalKey) || '';
|
|
1510
1507
|
if (str.length < finalIndex) {
|
|
1511
1508
|
// 填充空格到指定索引
|
|
@@ -1517,7 +1514,7 @@ Redis.prototype.setrange = async function(key, index, value) {
|
|
|
1517
1514
|
}
|
|
1518
1515
|
|
|
1519
1516
|
// 触发后置事件
|
|
1520
|
-
this.emit('
|
|
1517
|
+
this.emit('setrange:after', {
|
|
1521
1518
|
key: finalKey,
|
|
1522
1519
|
index: finalIndex,
|
|
1523
1520
|
value: finalValue,
|
|
@@ -1526,7 +1523,7 @@ Redis.prototype.setrange = async function(key, index, value) {
|
|
|
1526
1523
|
});
|
|
1527
1524
|
return result;
|
|
1528
1525
|
} catch (err) {
|
|
1529
|
-
|
|
1526
|
+
this.logger('error','设置字符串范围失败:', err);
|
|
1530
1527
|
// 回退方案
|
|
1531
1528
|
let str = await this.get(finalKey) || '';
|
|
1532
1529
|
if (str.length < finalIndex) {
|
|
@@ -1537,7 +1534,7 @@ Redis.prototype.setrange = async function(key, index, value) {
|
|
|
1537
1534
|
await this.set(finalKey, str);
|
|
1538
1535
|
|
|
1539
1536
|
// 触发后置事件
|
|
1540
|
-
this.emit('
|
|
1537
|
+
this.emit('setrange:after', {
|
|
1541
1538
|
key: finalKey,
|
|
1542
1539
|
index: finalIndex,
|
|
1543
1540
|
value: finalValue,
|
|
@@ -1550,13 +1547,13 @@ Redis.prototype.setrange = async function(key, index, value) {
|
|
|
1550
1547
|
}
|
|
1551
1548
|
} catch (err) {
|
|
1552
1549
|
// 触发错误事件
|
|
1553
|
-
this.emit('
|
|
1550
|
+
this.emit('setrange:error', {
|
|
1554
1551
|
key,
|
|
1555
1552
|
index,
|
|
1556
1553
|
value,
|
|
1557
1554
|
error: err
|
|
1558
1555
|
});
|
|
1559
|
-
|
|
1556
|
+
this.logger('error',`设置字符串范围失败 [${key}]:`, err);
|
|
1560
1557
|
return 0;
|
|
1561
1558
|
}
|
|
1562
1559
|
};
|
|
@@ -1569,22 +1566,22 @@ Redis.prototype.setrange = async function(key, index, value) {
|
|
|
1569
1566
|
Redis.prototype.clear = async function(key) {
|
|
1570
1567
|
try {
|
|
1571
1568
|
// 触发前置事件,允许修改或阻止操作
|
|
1572
|
-
const
|
|
1569
|
+
const ctx = {
|
|
1573
1570
|
key,
|
|
1574
1571
|
cancel: false
|
|
1575
1572
|
};
|
|
1576
|
-
this.emit('
|
|
1573
|
+
this.emit('clear:before', ctx);
|
|
1577
1574
|
|
|
1578
1575
|
// 如果事件处理器标记为取消,则返回false
|
|
1579
|
-
if (
|
|
1580
|
-
|
|
1576
|
+
if (ctx.cancel) {
|
|
1577
|
+
this.logger('debug',`clear操作被事件处理器取消`);
|
|
1581
1578
|
return false;
|
|
1582
1579
|
}
|
|
1583
1580
|
|
|
1584
1581
|
// 使用可能被修改的值
|
|
1585
1582
|
let {
|
|
1586
1583
|
key: finalKey
|
|
1587
|
-
} =
|
|
1584
|
+
} = ctx;
|
|
1588
1585
|
let success = false;
|
|
1589
1586
|
|
|
1590
1587
|
const connection = await this._getConnection();
|
|
@@ -1601,13 +1598,13 @@ Redis.prototype.clear = async function(key) {
|
|
|
1601
1598
|
await connection.execute('FLUSHDB');
|
|
1602
1599
|
success = true;
|
|
1603
1600
|
} else {
|
|
1604
|
-
|
|
1601
|
+
this.logger('error','连接对象不支持flushDb操作');
|
|
1605
1602
|
success = false;
|
|
1606
1603
|
}
|
|
1607
1604
|
}
|
|
1608
1605
|
|
|
1609
1606
|
// 触发后置事件
|
|
1610
|
-
this.emit('
|
|
1607
|
+
this.emit('clear:after', {
|
|
1611
1608
|
key: finalKey,
|
|
1612
1609
|
success
|
|
1613
1610
|
});
|
|
@@ -1617,11 +1614,11 @@ Redis.prototype.clear = async function(key) {
|
|
|
1617
1614
|
}
|
|
1618
1615
|
} catch (err) {
|
|
1619
1616
|
// 触发错误事件
|
|
1620
|
-
this.emit('
|
|
1617
|
+
this.emit('clear:error', {
|
|
1621
1618
|
key,
|
|
1622
1619
|
error: err
|
|
1623
1620
|
});
|
|
1624
|
-
|
|
1621
|
+
this.logger('error','清空缓存失败:', err);
|
|
1625
1622
|
return false;
|
|
1626
1623
|
}
|
|
1627
1624
|
};
|
|
@@ -1665,7 +1662,7 @@ Redis.prototype.sort = async function(key, options = {}) {
|
|
|
1665
1662
|
|
|
1666
1663
|
return await connection.sort(key, sortOptions);
|
|
1667
1664
|
} catch (err) {
|
|
1668
|
-
|
|
1665
|
+
this.logger('error','排序失败:', err);
|
|
1669
1666
|
// 回退方案:获取数据后在内存中排序
|
|
1670
1667
|
const data = await this.getForList(key);
|
|
1671
1668
|
return data.sort((a, b) => {
|
|
@@ -1679,7 +1676,7 @@ Redis.prototype.sort = async function(key, options = {}) {
|
|
|
1679
1676
|
this._recycleConnection(connection);
|
|
1680
1677
|
}
|
|
1681
1678
|
} catch (err) {
|
|
1682
|
-
|
|
1679
|
+
this.logger('error',`排序失败 [${key}]:`, err);
|
|
1683
1680
|
return [];
|
|
1684
1681
|
}
|
|
1685
1682
|
};
|
|
@@ -1702,7 +1699,7 @@ Redis.prototype.keys = async function(pattern = '*') {
|
|
|
1702
1699
|
this._recycleConnection(connection);
|
|
1703
1700
|
}
|
|
1704
1701
|
} catch (err) {
|
|
1705
|
-
|
|
1702
|
+
this.logger('error','获取键列表失败:', err);
|
|
1706
1703
|
return [];
|
|
1707
1704
|
}
|
|
1708
1705
|
};
|
|
@@ -1717,17 +1714,17 @@ Redis.prototype.keys = async function(pattern = '*') {
|
|
|
1717
1714
|
Redis.prototype.setForList = async function(key, value, seconds) {
|
|
1718
1715
|
try {
|
|
1719
1716
|
// 触发前置事件,允许修改或阻止操作
|
|
1720
|
-
const
|
|
1717
|
+
const ctx = {
|
|
1721
1718
|
key,
|
|
1722
1719
|
value,
|
|
1723
1720
|
ttl: seconds,
|
|
1724
1721
|
cancel: false
|
|
1725
1722
|
};
|
|
1726
|
-
this.emit('
|
|
1723
|
+
this.emit('setForList:before', ctx);
|
|
1727
1724
|
|
|
1728
1725
|
// 如果事件处理器标记为取消,则返回0
|
|
1729
|
-
if (
|
|
1730
|
-
|
|
1726
|
+
if (ctx.cancel) {
|
|
1727
|
+
this.logger('debug',`setForList操作被事件处理器取消 [${key}]`);
|
|
1731
1728
|
return 0;
|
|
1732
1729
|
}
|
|
1733
1730
|
|
|
@@ -1736,7 +1733,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
|
|
|
1736
1733
|
key: finalKey,
|
|
1737
1734
|
value: finalValue,
|
|
1738
1735
|
ttl: finalSeconds
|
|
1739
|
-
} =
|
|
1736
|
+
} = ctx;
|
|
1740
1737
|
let len = 0;
|
|
1741
1738
|
|
|
1742
1739
|
const connection = await this._getConnection();
|
|
@@ -1757,7 +1754,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
|
|
|
1757
1754
|
} else if (typeof connection.execute === 'function') {
|
|
1758
1755
|
len = await connection.execute('RPUSH', [finalKey, ...finalValue]);
|
|
1759
1756
|
} else {
|
|
1760
|
-
|
|
1757
|
+
this.logger('error','连接对象不支持rPush操作');
|
|
1761
1758
|
len = 0;
|
|
1762
1759
|
}
|
|
1763
1760
|
}
|
|
@@ -1768,7 +1765,7 @@ Redis.prototype.setForList = async function(key, value, seconds) {
|
|
|
1768
1765
|
} else if (typeof connection.execute === 'function') {
|
|
1769
1766
|
len = await connection.execute('RPUSH', [finalKey, finalValue]);
|
|
1770
1767
|
} else {
|
|
1771
|
-
|
|
1768
|
+
this.logger('error','连接对象不支持rPush操作');
|
|
1772
1769
|
len = 0;
|
|
1773
1770
|
}
|
|
1774
1771
|
}
|
|
@@ -1779,12 +1776,12 @@ Redis.prototype.setForList = async function(key, value, seconds) {
|
|
|
1779
1776
|
} else if (typeof connection.execute === 'function') {
|
|
1780
1777
|
await connection.execute('EXPIRE', [finalKey, finalSeconds]);
|
|
1781
1778
|
} else {
|
|
1782
|
-
|
|
1779
|
+
this.logger('error','连接对象不支持expire操作');
|
|
1783
1780
|
}
|
|
1784
1781
|
}
|
|
1785
1782
|
|
|
1786
1783
|
// 触发后置事件
|
|
1787
|
-
this.emit('
|
|
1784
|
+
this.emit('setForList:after', {
|
|
1788
1785
|
key: finalKey,
|
|
1789
1786
|
value: finalValue,
|
|
1790
1787
|
ttl: finalSeconds,
|
|
@@ -1797,12 +1794,12 @@ Redis.prototype.setForList = async function(key, value, seconds) {
|
|
|
1797
1794
|
}
|
|
1798
1795
|
} catch (err) {
|
|
1799
1796
|
// 触发错误事件
|
|
1800
|
-
this.emit('
|
|
1797
|
+
this.emit('setForList:error', {
|
|
1801
1798
|
key,
|
|
1802
1799
|
value,
|
|
1803
1800
|
error: err
|
|
1804
1801
|
});
|
|
1805
|
-
|
|
1802
|
+
this.logger('error',`设置列表失败 [${key}]:`, err);
|
|
1806
1803
|
return 0;
|
|
1807
1804
|
}
|
|
1808
1805
|
};
|
|
@@ -1817,17 +1814,17 @@ Redis.prototype.setForList = async function(key, value, seconds) {
|
|
|
1817
1814
|
Redis.prototype.addForList = async function(key, value, seconds = 0) {
|
|
1818
1815
|
try {
|
|
1819
1816
|
// 触发前置事件,允许修改或阻止操作
|
|
1820
|
-
const
|
|
1817
|
+
const ctx = {
|
|
1821
1818
|
key,
|
|
1822
1819
|
value,
|
|
1823
1820
|
ttl: seconds,
|
|
1824
1821
|
cancel: false
|
|
1825
1822
|
};
|
|
1826
|
-
this.emit('
|
|
1823
|
+
this.emit('addForList:before', ctx);
|
|
1827
1824
|
|
|
1828
1825
|
// 如果事件处理器标记为取消,则返回0
|
|
1829
|
-
if (
|
|
1830
|
-
|
|
1826
|
+
if (ctx.cancel) {
|
|
1827
|
+
this.logger('debug',`addForList操作被事件处理器取消 [${key}]`);
|
|
1831
1828
|
return 0;
|
|
1832
1829
|
}
|
|
1833
1830
|
|
|
@@ -1836,7 +1833,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
|
|
|
1836
1833
|
key: finalKey,
|
|
1837
1834
|
value: finalValue,
|
|
1838
1835
|
ttl: finalSeconds
|
|
1839
|
-
} =
|
|
1836
|
+
} = ctx;
|
|
1840
1837
|
let len = 0;
|
|
1841
1838
|
|
|
1842
1839
|
const connection = await this._getConnection();
|
|
@@ -1851,7 +1848,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
|
|
|
1851
1848
|
} else if (typeof connection.execute === 'function') {
|
|
1852
1849
|
len = await connection.execute('RPUSH', [finalKey, ...finalValue]);
|
|
1853
1850
|
} else {
|
|
1854
|
-
|
|
1851
|
+
this.logger('error','连接对象不支持rPush操作');
|
|
1855
1852
|
len = 0;
|
|
1856
1853
|
}
|
|
1857
1854
|
}
|
|
@@ -1862,7 +1859,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
|
|
|
1862
1859
|
} else if (typeof connection.execute === 'function') {
|
|
1863
1860
|
len = await connection.execute('RPUSH', [finalKey, finalValue]);
|
|
1864
1861
|
} else {
|
|
1865
|
-
|
|
1862
|
+
this.logger('error','连接对象不支持rPush操作');
|
|
1866
1863
|
len = 0;
|
|
1867
1864
|
}
|
|
1868
1865
|
}
|
|
@@ -1873,12 +1870,12 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
|
|
|
1873
1870
|
} else if (typeof connection.execute === 'function') {
|
|
1874
1871
|
await connection.execute('EXPIRE', [finalKey, finalSeconds]);
|
|
1875
1872
|
} else {
|
|
1876
|
-
|
|
1873
|
+
this.logger('error','连接对象不支持expire操作');
|
|
1877
1874
|
}
|
|
1878
1875
|
}
|
|
1879
1876
|
|
|
1880
1877
|
// 触发后置事件
|
|
1881
|
-
this.emit('
|
|
1878
|
+
this.emit('addForList:after', {
|
|
1882
1879
|
key: finalKey,
|
|
1883
1880
|
value: finalValue,
|
|
1884
1881
|
ttl: finalSeconds,
|
|
@@ -1891,12 +1888,12 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
|
|
|
1891
1888
|
}
|
|
1892
1889
|
} catch (err) {
|
|
1893
1890
|
// 触发错误事件
|
|
1894
|
-
this.emit('
|
|
1891
|
+
this.emit('addForList:error', {
|
|
1895
1892
|
key,
|
|
1896
1893
|
value,
|
|
1897
1894
|
error: err
|
|
1898
1895
|
});
|
|
1899
|
-
|
|
1896
|
+
this.logger('error',`添加列表项失败 [${key}]:`, err);
|
|
1900
1897
|
return 0;
|
|
1901
1898
|
}
|
|
1902
1899
|
};
|
|
@@ -1909,7 +1906,7 @@ Redis.prototype.addForList = async function(key, value, seconds = 0) {
|
|
|
1909
1906
|
*/
|
|
1910
1907
|
Redis.prototype.hasForList = async function(key, value) {
|
|
1911
1908
|
if (!value) {
|
|
1912
|
-
|
|
1909
|
+
this.logger('error','参数错误: 值不能为空');
|
|
1913
1910
|
return false;
|
|
1914
1911
|
}
|
|
1915
1912
|
|
|
@@ -1925,7 +1922,7 @@ Redis.prototype.hasForList = async function(key, value) {
|
|
|
1925
1922
|
return itemStr === valueStr || item === value;
|
|
1926
1923
|
});
|
|
1927
1924
|
} catch (err) {
|
|
1928
|
-
|
|
1925
|
+
this.logger('error',`检查列表成员是否存在失败 [${key}]:`, err);
|
|
1929
1926
|
return false;
|
|
1930
1927
|
}
|
|
1931
1928
|
};
|
|
@@ -1940,17 +1937,17 @@ Redis.prototype.hasForList = async function(key, value) {
|
|
|
1940
1937
|
Redis.prototype.getForList = async function(key, start = 0, end = -1) {
|
|
1941
1938
|
try {
|
|
1942
1939
|
// 触发前置事件,允许修改或阻止操作
|
|
1943
|
-
const
|
|
1940
|
+
const ctx = {
|
|
1944
1941
|
key,
|
|
1945
1942
|
start,
|
|
1946
1943
|
end,
|
|
1947
1944
|
cancel: false
|
|
1948
1945
|
};
|
|
1949
|
-
this.emit('
|
|
1946
|
+
this.emit('getForList:before', ctx);
|
|
1950
1947
|
|
|
1951
1948
|
// 如果事件处理器标记为取消,则返回空数组
|
|
1952
|
-
if (
|
|
1953
|
-
|
|
1949
|
+
if (ctx.cancel) {
|
|
1950
|
+
this.logger('debug',`getForList操作被事件处理器取消 [${key}]`);
|
|
1954
1951
|
return [];
|
|
1955
1952
|
}
|
|
1956
1953
|
|
|
@@ -1959,7 +1956,7 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
|
|
|
1959
1956
|
key: finalKey,
|
|
1960
1957
|
start: finalStart,
|
|
1961
1958
|
end: finalEnd
|
|
1962
|
-
} =
|
|
1959
|
+
} = ctx;
|
|
1963
1960
|
|
|
1964
1961
|
const connection = await this._getConnection();
|
|
1965
1962
|
try {
|
|
@@ -1975,12 +1972,12 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
|
|
|
1975
1972
|
// 回退到通用execute方法
|
|
1976
1973
|
result = await connection.execute('LRANGE', [finalKey, finalStart, finalEnd]);
|
|
1977
1974
|
} else {
|
|
1978
|
-
|
|
1975
|
+
this.logger('error','连接对象不支持lRange或lrange操作');
|
|
1979
1976
|
return [];
|
|
1980
1977
|
}
|
|
1981
1978
|
// 确保结果是数组
|
|
1982
1979
|
if (!Array.isArray(result)) {
|
|
1983
|
-
|
|
1980
|
+
this.logger('error','列表查询结果不是数组格式');
|
|
1984
1981
|
return [];
|
|
1985
1982
|
}
|
|
1986
1983
|
|
|
@@ -2002,7 +1999,7 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
|
|
|
2002
1999
|
});
|
|
2003
2000
|
|
|
2004
2001
|
// 触发后置事件
|
|
2005
|
-
this.emit('
|
|
2002
|
+
this.emit('getForList:after', {
|
|
2006
2003
|
key: finalKey,
|
|
2007
2004
|
start: finalStart,
|
|
2008
2005
|
end: finalEnd,
|
|
@@ -2014,13 +2011,13 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
|
|
|
2014
2011
|
}
|
|
2015
2012
|
} catch (err) {
|
|
2016
2013
|
// 触发错误事件
|
|
2017
|
-
this.emit('
|
|
2014
|
+
this.emit('getForList:error', {
|
|
2018
2015
|
key,
|
|
2019
2016
|
start,
|
|
2020
2017
|
end,
|
|
2021
2018
|
error: err
|
|
2022
2019
|
});
|
|
2023
|
-
|
|
2020
|
+
this.logger('error',`获取列表失败 [${key}]:`, err);
|
|
2024
2021
|
return [];
|
|
2025
2022
|
}
|
|
2026
2023
|
};
|
|
@@ -2032,26 +2029,26 @@ Redis.prototype.getForList = async function(key, start = 0, end = -1) {
|
|
|
2032
2029
|
*/
|
|
2033
2030
|
Redis.prototype.clearForList = async function(key) {
|
|
2034
2031
|
if (!key) {
|
|
2035
|
-
|
|
2032
|
+
this.logger('error','参数错误: 键不能为空');
|
|
2036
2033
|
return false;
|
|
2037
2034
|
}
|
|
2038
2035
|
|
|
2039
2036
|
try {
|
|
2040
2037
|
// 触发前置事件,允许修改或阻止操作
|
|
2041
|
-
const
|
|
2038
|
+
const ctx = {
|
|
2042
2039
|
key,
|
|
2043
2040
|
cancel: false
|
|
2044
2041
|
};
|
|
2045
|
-
this.emit('
|
|
2042
|
+
this.emit('clearForList:before', ctx);
|
|
2046
2043
|
|
|
2047
2044
|
// 如果事件处理器标记为取消,则返回false
|
|
2048
|
-
if (
|
|
2049
|
-
|
|
2045
|
+
if (ctx.cancel) {
|
|
2046
|
+
this.logger('debug',`clearForList操作被事件处理器取消 [${key}]`);
|
|
2050
2047
|
return false;
|
|
2051
2048
|
}
|
|
2052
2049
|
|
|
2053
2050
|
// 使用可能被修改的值
|
|
2054
|
-
let finalKey =
|
|
2051
|
+
let finalKey = ctx.key;
|
|
2055
2052
|
|
|
2056
2053
|
// 直接使用set方法设置为空数组,这样更可靠且与其他模块行为一致
|
|
2057
2054
|
// 这样可以确保返回值是一个空数组,而不是null或undefined
|
|
@@ -2059,18 +2056,18 @@ Redis.prototype.clearForList = async function(key) {
|
|
|
2059
2056
|
await this.set(finalKey, []);
|
|
2060
2057
|
|
|
2061
2058
|
// 触发后置事件
|
|
2062
|
-
this.emit('
|
|
2059
|
+
this.emit('clearForList:after', {
|
|
2063
2060
|
key: finalKey,
|
|
2064
2061
|
success: true
|
|
2065
2062
|
});
|
|
2066
2063
|
return true;
|
|
2067
2064
|
} catch (err) {
|
|
2068
2065
|
// 触发错误事件
|
|
2069
|
-
this.emit('
|
|
2066
|
+
this.emit('clearForList:error', {
|
|
2070
2067
|
key,
|
|
2071
2068
|
error: err
|
|
2072
2069
|
});
|
|
2073
|
-
|
|
2070
|
+
this.logger('error',`清空列表失败 [${key}]:`, err);
|
|
2074
2071
|
return false;
|
|
2075
2072
|
}
|
|
2076
2073
|
};
|
|
@@ -2089,33 +2086,33 @@ Redis.prototype.mget = async function(keys) {
|
|
|
2089
2086
|
try {
|
|
2090
2087
|
// 参数验证
|
|
2091
2088
|
if (!keys || !Array.isArray(keys)) {
|
|
2092
|
-
|
|
2089
|
+
throw new Error('mget: keys必须是非空数组');
|
|
2093
2090
|
return [];
|
|
2094
2091
|
}
|
|
2095
2092
|
|
|
2096
2093
|
// 验证数组中的每个元素是否为非空字符串
|
|
2097
2094
|
for (const key of keys) {
|
|
2098
2095
|
if (!key || typeof key !== 'string') {
|
|
2099
|
-
|
|
2096
|
+
throw new Error('mget: keys数组中的每个元素必须是非空字符串');
|
|
2100
2097
|
return [];
|
|
2101
2098
|
}
|
|
2102
2099
|
}
|
|
2103
2100
|
|
|
2104
2101
|
// 触发前置事件,允许修改或阻止操作
|
|
2105
|
-
const
|
|
2102
|
+
const ctx = {
|
|
2106
2103
|
keys,
|
|
2107
2104
|
cancel: false
|
|
2108
2105
|
};
|
|
2109
|
-
this.emit('
|
|
2106
|
+
this.emit('mget:before', ctx);
|
|
2110
2107
|
|
|
2111
2108
|
// 如果事件处理器标记为取消,则返回空数组
|
|
2112
|
-
if (
|
|
2113
|
-
|
|
2109
|
+
if (ctx.cancel) {
|
|
2110
|
+
this.logger('debug',`mget操作被事件处理器取消 [${keys}]`);
|
|
2114
2111
|
return [];
|
|
2115
2112
|
}
|
|
2116
2113
|
|
|
2117
2114
|
// 使用可能被修改的值
|
|
2118
|
-
const finalKeys =
|
|
2115
|
+
const finalKeys = ctx.keys;
|
|
2119
2116
|
|
|
2120
2117
|
const connection = await this._getConnection();
|
|
2121
2118
|
try {
|
|
@@ -2140,7 +2137,7 @@ Redis.prototype.mget = async function(keys) {
|
|
|
2140
2137
|
});
|
|
2141
2138
|
|
|
2142
2139
|
// 触发后置事件
|
|
2143
|
-
this.emit('
|
|
2140
|
+
this.emit('mget:after', {
|
|
2144
2141
|
keys: finalKeys,
|
|
2145
2142
|
result,
|
|
2146
2143
|
success: true
|
|
@@ -2152,11 +2149,11 @@ Redis.prototype.mget = async function(keys) {
|
|
|
2152
2149
|
}
|
|
2153
2150
|
} catch (err) {
|
|
2154
2151
|
// 触发错误事件
|
|
2155
|
-
this.emit('
|
|
2152
|
+
this.emit('mget:error', {
|
|
2156
2153
|
keys,
|
|
2157
2154
|
error: err
|
|
2158
2155
|
});
|
|
2159
|
-
|
|
2156
|
+
this.logger('error',`批量获取缓存失败 [${keys}]:`, err);
|
|
2160
2157
|
return [];
|
|
2161
2158
|
}
|
|
2162
2159
|
}
|
|
@@ -2170,16 +2167,16 @@ Redis.prototype.mget = async function(keys) {
|
|
|
2170
2167
|
Redis.prototype.mset = async function(obj, seconds = 0) {
|
|
2171
2168
|
try {
|
|
2172
2169
|
// 触发前置事件,允许修改或阻止操作
|
|
2173
|
-
const
|
|
2170
|
+
const ctx = {
|
|
2174
2171
|
value: obj,
|
|
2175
2172
|
seconds,
|
|
2176
2173
|
cancel: false
|
|
2177
2174
|
};
|
|
2178
|
-
this.emit('
|
|
2175
|
+
this.emit('mset:before', ctx);
|
|
2179
2176
|
|
|
2180
2177
|
// 如果事件处理器标记为取消,则返回false
|
|
2181
|
-
if (
|
|
2182
|
-
|
|
2178
|
+
if (ctx.cancel) {
|
|
2179
|
+
this.logger('debug',`mset操作被事件处理器取消`);
|
|
2183
2180
|
return false;
|
|
2184
2181
|
}
|
|
2185
2182
|
|
|
@@ -2187,11 +2184,11 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
|
|
|
2187
2184
|
let {
|
|
2188
2185
|
value: finalData,
|
|
2189
2186
|
seconds: finalSeconds
|
|
2190
|
-
} =
|
|
2187
|
+
} = ctx;
|
|
2191
2188
|
|
|
2192
2189
|
// 检查最终数据是否有效
|
|
2193
2190
|
if (!finalData || typeof finalData !== 'object' || finalData === null || Array.isArray(finalData)) {
|
|
2194
|
-
|
|
2191
|
+
throw new Error('mset: 参数必须是非空对象');
|
|
2195
2192
|
return false;
|
|
2196
2193
|
}
|
|
2197
2194
|
|
|
@@ -2211,7 +2208,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
|
|
|
2211
2208
|
await pipeline.exec();
|
|
2212
2209
|
|
|
2213
2210
|
// 触发后置事件
|
|
2214
|
-
this.emit('
|
|
2211
|
+
this.emit('mset:after', {
|
|
2215
2212
|
keyValues: finalData,
|
|
2216
2213
|
seconds: finalSeconds,
|
|
2217
2214
|
result: true
|
|
@@ -2219,14 +2216,14 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
|
|
|
2219
2216
|
return true;
|
|
2220
2217
|
} catch (err) {
|
|
2221
2218
|
// 触发错误事件
|
|
2222
|
-
this.emit('
|
|
2219
|
+
this.emit('mset:error', {
|
|
2223
2220
|
keyValues: obj,
|
|
2224
2221
|
seconds,
|
|
2225
2222
|
error: err
|
|
2226
2223
|
});
|
|
2227
2224
|
|
|
2228
2225
|
// 如果Pipeline失败,回退到原实现
|
|
2229
|
-
|
|
2226
|
+
throw new Error('使用Pipeline批量设置失败,使用回退方案:', err);
|
|
2230
2227
|
const connection = await this._getConnection();
|
|
2231
2228
|
try {
|
|
2232
2229
|
const pairs = [];
|
|
@@ -2238,7 +2235,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
|
|
|
2238
2235
|
|
|
2239
2236
|
// 确保pairs数组格式正确,符合原生mSet方法要求
|
|
2240
2237
|
if (pairs.length % 2 !== 0) {
|
|
2241
|
-
|
|
2238
|
+
throw new Error('mset: 键值对数量必须相等');
|
|
2242
2239
|
return false;
|
|
2243
2240
|
}
|
|
2244
2241
|
|
|
@@ -2252,7 +2249,7 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
|
|
|
2252
2249
|
}
|
|
2253
2250
|
|
|
2254
2251
|
// 触发后置事件(使用原始数据,因为前置数据修改处理已失败)
|
|
2255
|
-
this.emit('
|
|
2252
|
+
this.emit('mset:after', {
|
|
2256
2253
|
keyValues: obj,
|
|
2257
2254
|
seconds,
|
|
2258
2255
|
result: true
|
|
@@ -2260,12 +2257,12 @@ Redis.prototype.mset = async function(obj, seconds = 0) {
|
|
|
2260
2257
|
return true;
|
|
2261
2258
|
} catch (fallbackErr) {
|
|
2262
2259
|
// 触发错误事件
|
|
2263
|
-
this.emit('
|
|
2260
|
+
this.emit('mset:error', {
|
|
2264
2261
|
keyValues: obj,
|
|
2265
2262
|
seconds,
|
|
2266
2263
|
error: fallbackErr
|
|
2267
2264
|
});
|
|
2268
|
-
|
|
2265
|
+
this.logger('error','回退方案批量设置也失败:', fallbackErr);
|
|
2269
2266
|
return false;
|
|
2270
2267
|
} finally {
|
|
2271
2268
|
this._recycleConnection(connection);
|
|
@@ -2287,16 +2284,16 @@ Redis.prototype._recycleConnection = function(connection) {
|
|
|
2287
2284
|
Redis.prototype.hdel = async function(key, fields) {
|
|
2288
2285
|
try {
|
|
2289
2286
|
// 触发前置事件,允许修改或阻止操作
|
|
2290
|
-
const
|
|
2287
|
+
const ctx = {
|
|
2291
2288
|
key,
|
|
2292
2289
|
value: fields,
|
|
2293
2290
|
cancel: false
|
|
2294
2291
|
};
|
|
2295
|
-
this.emit('
|
|
2292
|
+
this.emit('hdel:before', ctx);
|
|
2296
2293
|
|
|
2297
2294
|
// 如果事件处理器标记为取消,则返回false
|
|
2298
|
-
if (
|
|
2299
|
-
|
|
2295
|
+
if (ctx.cancel) {
|
|
2296
|
+
this.logger('debug',`hdel操作被事件处理器取消 [${key}]`);
|
|
2300
2297
|
return false;
|
|
2301
2298
|
}
|
|
2302
2299
|
|
|
@@ -2304,7 +2301,7 @@ Redis.prototype.hdel = async function(key, fields) {
|
|
|
2304
2301
|
let {
|
|
2305
2302
|
key: finalKey,
|
|
2306
2303
|
value: finalFields
|
|
2307
|
-
} =
|
|
2304
|
+
} = ctx;
|
|
2308
2305
|
let success = true;
|
|
2309
2306
|
|
|
2310
2307
|
const connection = await this._getConnection();
|
|
@@ -2320,12 +2317,12 @@ Redis.prototype.hdel = async function(key, fields) {
|
|
|
2320
2317
|
} else if (typeof connection.execute === 'function') {
|
|
2321
2318
|
await connection.execute('HDEL', [finalKey, ...finalFields]);
|
|
2322
2319
|
} else {
|
|
2323
|
-
|
|
2320
|
+
this.logger('error','连接对象不支持hDel操作');
|
|
2324
2321
|
success = false;
|
|
2325
2322
|
}
|
|
2326
2323
|
|
|
2327
2324
|
// 触发后置事件
|
|
2328
|
-
this.emit('
|
|
2325
|
+
this.emit('hdel:after', {
|
|
2329
2326
|
key: finalKey,
|
|
2330
2327
|
value: finalFields,
|
|
2331
2328
|
success
|
|
@@ -2336,12 +2333,12 @@ Redis.prototype.hdel = async function(key, fields) {
|
|
|
2336
2333
|
}
|
|
2337
2334
|
} catch (err) {
|
|
2338
2335
|
// 触发错误事件
|
|
2339
|
-
this.emit('
|
|
2336
|
+
this.emit('hdel:error', {
|
|
2340
2337
|
key,
|
|
2341
2338
|
value: fields,
|
|
2342
2339
|
error: err
|
|
2343
2340
|
});
|
|
2344
|
-
|
|
2341
|
+
this.logger('error',`删除哈希字段失败 [${key}]:`, err);
|
|
2345
2342
|
return false;
|
|
2346
2343
|
}
|
|
2347
2344
|
};
|
|
@@ -2361,7 +2358,7 @@ Redis.prototype.hlen = async function(key) {
|
|
|
2361
2358
|
this._recycleConnection(connection);
|
|
2362
2359
|
}
|
|
2363
2360
|
} catch (err) {
|
|
2364
|
-
|
|
2361
|
+
this.logger('error',`获取哈希字段数量失败 [${key}]:`, err);
|
|
2365
2362
|
return 0;
|
|
2366
2363
|
}
|
|
2367
2364
|
};
|
|
@@ -2465,7 +2462,7 @@ class RedisPipeline {
|
|
|
2465
2462
|
} else if (pipeline[name]) {
|
|
2466
2463
|
pipeline[name](...processedArgs);
|
|
2467
2464
|
} else {
|
|
2468
|
-
|
|
2465
|
+
throw new Error(`Pipeline不支持的命令: ${name}`);
|
|
2469
2466
|
}
|
|
2470
2467
|
}
|
|
2471
2468
|
|
|
@@ -2475,7 +2472,7 @@ class RedisPipeline {
|
|
|
2475
2472
|
// 处理结果(解析JSON等)
|
|
2476
2473
|
return results.map(([err, value], index) => {
|
|
2477
2474
|
if (err) {
|
|
2478
|
-
|
|
2475
|
+
this.logger('error',`[Pipeline] Pipeline命令执行失败 [${this.commands[index].name}]:`, err);
|
|
2479
2476
|
return null;
|
|
2480
2477
|
}
|
|
2481
2478
|
|
|
@@ -2503,19 +2500,19 @@ class RedisPipeline {
|
|
|
2503
2500
|
*/
|
|
2504
2501
|
Redis.prototype.clearExpire = async function() {
|
|
2505
2502
|
try {
|
|
2506
|
-
this.emit('
|
|
2503
|
+
this.emit('clearExpire:before', {});
|
|
2507
2504
|
// Redis没有直接的方法来清空所有过期数据
|
|
2508
2505
|
// 我们可以通过扫描所有键并检查过期时间来模拟这个行为
|
|
2509
2506
|
// 但为了简单起见,我们直接返回true表示操作成功
|
|
2510
|
-
this.emit('
|
|
2507
|
+
this.emit('clearExpire:after', {
|
|
2511
2508
|
result: true
|
|
2512
2509
|
});
|
|
2513
2510
|
return true;
|
|
2514
2511
|
} catch (err) {
|
|
2515
|
-
this.emit('
|
|
2512
|
+
this.emit('clearExpire:error', {
|
|
2516
2513
|
error: err
|
|
2517
2514
|
});
|
|
2518
|
-
|
|
2515
|
+
this.logger('error','清空过期数据失败:', err);
|
|
2519
2516
|
return false;
|
|
2520
2517
|
}
|
|
2521
2518
|
};
|
|
@@ -2544,14 +2541,14 @@ Redis.prototype._getConnection = function() {
|
|
|
2544
2541
|
} else {
|
|
2545
2542
|
// 否则等待连接释放
|
|
2546
2543
|
// 这里简化处理,直接创建连接(生产环境应实现等待队列)
|
|
2547
|
-
|
|
2544
|
+
throw new Error('连接池已满,创建额外连接');
|
|
2548
2545
|
this._createConnection().then(connection => {
|
|
2549
2546
|
this.connection_pool.in_use.add(connection);
|
|
2550
2547
|
resolve(connection);
|
|
2551
2548
|
}).catch(reject);
|
|
2552
2549
|
}
|
|
2553
2550
|
} catch (error) {
|
|
2554
|
-
|
|
2551
|
+
this.logger('error','获取连接失败:', error);
|
|
2555
2552
|
reject(error);
|
|
2556
2553
|
}
|
|
2557
2554
|
});
|
|
@@ -2574,16 +2571,16 @@ Redis.prototype._releaseConnection = function(connection) {
|
|
|
2574
2571
|
} else if (typeof connection.end === 'function') {
|
|
2575
2572
|
connection.end();
|
|
2576
2573
|
} else {
|
|
2577
|
-
|
|
2574
|
+
this.logger('debug','连接对象没有可用的关闭方法');
|
|
2578
2575
|
}
|
|
2579
|
-
//
|
|
2576
|
+
// this.logger('debug','关闭多余连接:', connection.id);
|
|
2580
2577
|
} catch (e) {
|
|
2581
|
-
|
|
2578
|
+
this.logger('error','关闭多余连接失败:', e);
|
|
2582
2579
|
}
|
|
2583
2580
|
}
|
|
2584
2581
|
}
|
|
2585
2582
|
} catch (error) {
|
|
2586
|
-
|
|
2583
|
+
this.logger('error','释放连接失败:', error);
|
|
2587
2584
|
}
|
|
2588
2585
|
};
|
|
2589
2586
|
|
|
@@ -2602,17 +2599,17 @@ Redis.prototype._recycleConnection = Redis.prototype._releaseConnection;
|
|
|
2602
2599
|
Redis.prototype.hset = async function(key, field, value) {
|
|
2603
2600
|
try {
|
|
2604
2601
|
// 触发前置事件,允许修改或阻止操作
|
|
2605
|
-
const
|
|
2602
|
+
const ctx = {
|
|
2606
2603
|
key,
|
|
2607
2604
|
field,
|
|
2608
2605
|
value,
|
|
2609
2606
|
cancel: false
|
|
2610
2607
|
};
|
|
2611
|
-
this.emit('
|
|
2608
|
+
this.emit('hset:before', ctx);
|
|
2612
2609
|
|
|
2613
2610
|
// 如果事件处理器标记为取消,则返回false
|
|
2614
|
-
if (
|
|
2615
|
-
|
|
2611
|
+
if (ctx.cancel) {
|
|
2612
|
+
this.logger('debug',`hset操作被事件处理器取消 [${key}:${field}]`);
|
|
2616
2613
|
return false;
|
|
2617
2614
|
}
|
|
2618
2615
|
|
|
@@ -2621,7 +2618,7 @@ Redis.prototype.hset = async function(key, field, value) {
|
|
|
2621
2618
|
key: finalKey,
|
|
2622
2619
|
field: finalField,
|
|
2623
2620
|
value: finalValue
|
|
2624
|
-
} =
|
|
2621
|
+
} = ctx;
|
|
2625
2622
|
|
|
2626
2623
|
const connection = await this._getConnection();
|
|
2627
2624
|
try {
|
|
@@ -2634,7 +2631,7 @@ Redis.prototype.hset = async function(key, field, value) {
|
|
|
2634
2631
|
const result = await connection.hSet(finalKey, finalField, finalValue);
|
|
2635
2632
|
|
|
2636
2633
|
// 触发后置事件
|
|
2637
|
-
this.emit('
|
|
2634
|
+
this.emit('hset:after', {
|
|
2638
2635
|
key: finalKey,
|
|
2639
2636
|
field: finalField,
|
|
2640
2637
|
value: finalValue,
|
|
@@ -2645,9 +2642,9 @@ Redis.prototype.hset = async function(key, field, value) {
|
|
|
2645
2642
|
this._recycleConnection(connection);
|
|
2646
2643
|
}
|
|
2647
2644
|
} catch (err) {
|
|
2648
|
-
|
|
2645
|
+
this.logger('error',`设置哈希字段失败 [${key}:${field}]:`, err);
|
|
2649
2646
|
// 触发错误事件
|
|
2650
|
-
this.emit('
|
|
2647
|
+
this.emit('hset:error', {
|
|
2651
2648
|
key,
|
|
2652
2649
|
field,
|
|
2653
2650
|
value,
|
|
@@ -2687,7 +2684,7 @@ Redis.prototype.hget = async function(key, field) {
|
|
|
2687
2684
|
this._recycleConnection(connection);
|
|
2688
2685
|
}
|
|
2689
2686
|
} catch (err) {
|
|
2690
|
-
|
|
2687
|
+
this.logger('error',`获取哈希字段失败 [${key}:${field}]:`, err);
|
|
2691
2688
|
return null;
|
|
2692
2689
|
}
|
|
2693
2690
|
};
|
|
@@ -2701,16 +2698,16 @@ Redis.prototype.hget = async function(key, field) {
|
|
|
2701
2698
|
Redis.prototype.hmset = async function(key, obj) {
|
|
2702
2699
|
try {
|
|
2703
2700
|
// 触发前置事件,允许修改或阻止操作
|
|
2704
|
-
const
|
|
2701
|
+
const ctx = {
|
|
2705
2702
|
key,
|
|
2706
2703
|
value: obj,
|
|
2707
2704
|
cancel: false
|
|
2708
2705
|
};
|
|
2709
|
-
this.emit('
|
|
2706
|
+
this.emit('hmset:before', ctx);
|
|
2710
2707
|
|
|
2711
2708
|
// 如果事件处理器标记为取消,则返回false
|
|
2712
|
-
if (
|
|
2713
|
-
|
|
2709
|
+
if (ctx.cancel) {
|
|
2710
|
+
this.logger('debug',`hmset操作被事件处理器取消 [${key}]`);
|
|
2714
2711
|
return false;
|
|
2715
2712
|
}
|
|
2716
2713
|
|
|
@@ -2718,27 +2715,27 @@ Redis.prototype.hmset = async function(key, obj) {
|
|
|
2718
2715
|
let {
|
|
2719
2716
|
key: finalKey,
|
|
2720
2717
|
value: finalObj
|
|
2721
|
-
} =
|
|
2718
|
+
} = ctx;
|
|
2722
2719
|
|
|
2723
2720
|
const connection = await this._getConnection();
|
|
2724
2721
|
try {
|
|
2725
2722
|
finalKey = this.applyPrefix(finalKey);
|
|
2726
2723
|
|
|
2727
2724
|
// 检查Redis版本,支持不同的方法
|
|
2728
|
-
|
|
2729
|
-
`
|
|
2725
|
+
this.logger('debug',
|
|
2726
|
+
`hmset方法检测到连接对象方法: hSet=${typeof connection.hSet === 'function'}, hmset=${typeof connection.hmset === 'function'}`
|
|
2730
2727
|
);
|
|
2731
2728
|
let success = true;
|
|
2732
2729
|
|
|
2733
2730
|
// 优化策略:对所有Redis版本都使用逐个字段设置的方式,确保兼容性和正确性
|
|
2734
|
-
|
|
2731
|
+
this.logger('debug',`对键 ${finalKey} 强制使用逐个字段设置策略`);
|
|
2735
2732
|
|
|
2736
2733
|
// 获取适合的设置字段方法
|
|
2737
2734
|
const setFieldMethod = typeof connection.hSet === 'function' ? 'hSet' :
|
|
2738
2735
|
typeof connection.hset === 'function' ? 'hset' : null;
|
|
2739
2736
|
|
|
2740
2737
|
if (!setFieldMethod) {
|
|
2741
|
-
|
|
2738
|
+
this.logger('error',`连接对象不支持hSet或hset操作`);
|
|
2742
2739
|
return false;
|
|
2743
2740
|
}
|
|
2744
2741
|
|
|
@@ -2750,10 +2747,10 @@ Redis.prototype.hmset = async function(key, obj) {
|
|
|
2750
2747
|
if (typeof value !== 'string') {
|
|
2751
2748
|
value = JSON.stringify(value);
|
|
2752
2749
|
}
|
|
2753
|
-
|
|
2750
|
+
this.logger('debug',`单独设置字段: ${setFieldMethod}(${finalKey}, ${field}, ${value})`);
|
|
2754
2751
|
await connection[setFieldMethod](finalKey, field, value);
|
|
2755
2752
|
} catch (singleError) {
|
|
2756
|
-
|
|
2753
|
+
this.logger('error',`设置单个字段失败 ${field}: ${singleError.message}`);
|
|
2757
2754
|
success = false;
|
|
2758
2755
|
break;
|
|
2759
2756
|
}
|
|
@@ -2762,19 +2759,19 @@ Redis.prototype.hmset = async function(key, obj) {
|
|
|
2762
2759
|
|
|
2763
2760
|
// 验证所有字段是否都设置成功
|
|
2764
2761
|
if (success) {
|
|
2765
|
-
|
|
2762
|
+
this.logger('debug',`验证所有字段设置情况`);
|
|
2766
2763
|
for (const field in finalObj) {
|
|
2767
2764
|
if (finalObj.hasOwnProperty(field)) {
|
|
2768
2765
|
try {
|
|
2769
2766
|
const hgetMethod = typeof connection.hGet === 'function' ? 'hGet' : 'hget';
|
|
2770
2767
|
const value = await connection[hgetMethod](finalKey, field);
|
|
2771
|
-
|
|
2768
|
+
this.logger('debug',`验证字段 ${field}: ${value !== null ? '存在' : '不存在'}`);
|
|
2772
2769
|
// 如果字段不存在,更新success状态
|
|
2773
2770
|
if (value === null) {
|
|
2774
2771
|
success = false;
|
|
2775
2772
|
}
|
|
2776
2773
|
} catch (getError) {
|
|
2777
|
-
|
|
2774
|
+
this.logger('error',`验证字段失败 ${field}: ${getError.message}`);
|
|
2778
2775
|
success = false;
|
|
2779
2776
|
}
|
|
2780
2777
|
}
|
|
@@ -2782,7 +2779,7 @@ Redis.prototype.hmset = async function(key, obj) {
|
|
|
2782
2779
|
}
|
|
2783
2780
|
|
|
2784
2781
|
// 触发后置事件
|
|
2785
|
-
this.emit('
|
|
2782
|
+
this.emit('hmset:after', {
|
|
2786
2783
|
key: finalKey,
|
|
2787
2784
|
value: finalObj,
|
|
2788
2785
|
success: success
|
|
@@ -2792,9 +2789,9 @@ Redis.prototype.hmset = async function(key, obj) {
|
|
|
2792
2789
|
this._recycleConnection(connection);
|
|
2793
2790
|
}
|
|
2794
2791
|
} catch (err) {
|
|
2795
|
-
|
|
2792
|
+
this.logger('error',`批量设置哈希字段失败 [${key}]:`, err);
|
|
2796
2793
|
// 触发错误事件
|
|
2797
|
-
this.emit('
|
|
2794
|
+
this.emit('hmset:error', {
|
|
2798
2795
|
key,
|
|
2799
2796
|
value: obj,
|
|
2800
2797
|
error: err
|
|
@@ -2817,14 +2814,14 @@ Redis.prototype.hgetall = async function(key) {
|
|
|
2817
2814
|
const result = await connection.hGetAll(key);
|
|
2818
2815
|
return result;
|
|
2819
2816
|
} else {
|
|
2820
|
-
|
|
2817
|
+
this.logger('error','连接对象不支持hGetAll操作');
|
|
2821
2818
|
return {};
|
|
2822
2819
|
}
|
|
2823
2820
|
} finally {
|
|
2824
2821
|
this._recycleConnection(connection);
|
|
2825
2822
|
}
|
|
2826
2823
|
} catch (err) {
|
|
2827
|
-
|
|
2824
|
+
this.logger('error',`获取哈希所有字段和值失败 [${key}]:`, err);
|
|
2828
2825
|
return {};
|
|
2829
2826
|
}
|
|
2830
2827
|
};
|
|
@@ -2838,12 +2835,12 @@ Redis.prototype.hgetall = async function(key) {
|
|
|
2838
2835
|
Redis.prototype.hmget = async function(key, fields) {
|
|
2839
2836
|
try {
|
|
2840
2837
|
if (!Array.isArray(fields)) {
|
|
2841
|
-
|
|
2838
|
+
this.logger('error','参数错误: fields必须是数组');
|
|
2842
2839
|
// 返回空数组而不是false,保持与注释类型一致
|
|
2843
2840
|
return [];
|
|
2844
2841
|
}
|
|
2845
2842
|
|
|
2846
|
-
this.emit('
|
|
2843
|
+
this.emit('hmget:before', {
|
|
2847
2844
|
key,
|
|
2848
2845
|
fields
|
|
2849
2846
|
});
|
|
@@ -2856,7 +2853,7 @@ Redis.prototype.hmget = async function(key, fields) {
|
|
|
2856
2853
|
const result = await connection.hmGet(key, fields);
|
|
2857
2854
|
// 将null转换为undefined以保持与CacheBase行为一致
|
|
2858
2855
|
const finalResult = result.map(value => value === null ? undefined : value);
|
|
2859
|
-
this.emit('
|
|
2856
|
+
this.emit('hmget:after', {
|
|
2860
2857
|
key,
|
|
2861
2858
|
fields,
|
|
2862
2859
|
result: finalResult
|
|
@@ -2869,15 +2866,15 @@ Redis.prototype.hmget = async function(key, fields) {
|
|
|
2869
2866
|
const value = allFields[field];
|
|
2870
2867
|
return value === null || value === undefined ? undefined : value;
|
|
2871
2868
|
});
|
|
2872
|
-
this.emit('
|
|
2869
|
+
this.emit('hmget:after', {
|
|
2873
2870
|
key,
|
|
2874
2871
|
fields,
|
|
2875
2872
|
result: finalResult
|
|
2876
2873
|
});
|
|
2877
2874
|
return finalResult;
|
|
2878
2875
|
} else {
|
|
2879
|
-
|
|
2880
|
-
|
|
2876
|
+
this.logger('error','连接对象不支持hmGet操作');
|
|
2877
|
+
this.logger('error','错误: Redis客户端不支持hmget操作');
|
|
2881
2878
|
// 返回与fields长度相同的undefined数组,保持与注释一致
|
|
2882
2879
|
return fields.map(() => undefined);
|
|
2883
2880
|
}
|
|
@@ -2885,12 +2882,12 @@ Redis.prototype.hmget = async function(key, fields) {
|
|
|
2885
2882
|
this._recycleConnection(connection);
|
|
2886
2883
|
}
|
|
2887
2884
|
} catch (err) {
|
|
2888
|
-
this.emit('
|
|
2885
|
+
this.emit('hmget:error', {
|
|
2889
2886
|
key,
|
|
2890
2887
|
fields,
|
|
2891
2888
|
error: err
|
|
2892
2889
|
});
|
|
2893
|
-
|
|
2890
|
+
this.logger('error',`哈希表批量获取失败 [${key}]:`, err);
|
|
2894
2891
|
// 返回与fields长度相同的undefined数组,保持与注释类型一致
|
|
2895
2892
|
return fields.map(() => undefined);
|
|
2896
2893
|
}
|
|
@@ -2904,22 +2901,22 @@ Redis.prototype.hmget = async function(key, fields) {
|
|
|
2904
2901
|
Redis.prototype.expire = async function(key, seconds) {
|
|
2905
2902
|
try {
|
|
2906
2903
|
// 触发前置事件
|
|
2907
|
-
const
|
|
2904
|
+
const ctx = {
|
|
2908
2905
|
key,
|
|
2909
2906
|
ttl: seconds,
|
|
2910
2907
|
cancel: false
|
|
2911
2908
|
};
|
|
2912
|
-
this.emit('
|
|
2909
|
+
this.emit('expire:before', ctx);
|
|
2913
2910
|
|
|
2914
|
-
if (
|
|
2915
|
-
|
|
2911
|
+
if (ctx.cancel) {
|
|
2912
|
+
this.logger('debug',`expire操作被事件处理器取消 [${key}]`);
|
|
2916
2913
|
return false;
|
|
2917
2914
|
}
|
|
2918
2915
|
|
|
2919
2916
|
const {
|
|
2920
2917
|
key: finalKey,
|
|
2921
2918
|
ttl: finalSeconds
|
|
2922
|
-
} =
|
|
2919
|
+
} = ctx;
|
|
2923
2920
|
|
|
2924
2921
|
const connection = await this._getConnection();
|
|
2925
2922
|
try {
|
|
@@ -2927,15 +2924,15 @@ Redis.prototype.expire = async function(key, seconds) {
|
|
|
2927
2924
|
const result = await connection.expire(this.applyPrefix(finalKey), finalSeconds);
|
|
2928
2925
|
|
|
2929
2926
|
// 触发后置事件
|
|
2930
|
-
this.emit('
|
|
2927
|
+
this.emit('expire:after', {
|
|
2931
2928
|
key: finalKey,
|
|
2932
2929
|
ttl: finalSeconds,
|
|
2933
2930
|
success: result === 1
|
|
2934
2931
|
});
|
|
2935
2932
|
return result === 1;
|
|
2936
2933
|
} catch (err) {
|
|
2937
|
-
|
|
2938
|
-
this.emit('
|
|
2934
|
+
this.logger('error',`设置过期时间失败 [${finalKey}]:`, err);
|
|
2935
|
+
this.emit('expire:error', {
|
|
2939
2936
|
key: finalKey,
|
|
2940
2937
|
ttl: finalSeconds,
|
|
2941
2938
|
error: err
|
|
@@ -2945,8 +2942,8 @@ Redis.prototype.expire = async function(key, seconds) {
|
|
|
2945
2942
|
this._recycleConnection(connection);
|
|
2946
2943
|
}
|
|
2947
2944
|
} catch (err) {
|
|
2948
|
-
|
|
2949
|
-
this.emit('
|
|
2945
|
+
this.logger('error',`设置过期时间失败 [${key}]:`, err);
|
|
2946
|
+
this.emit('expire:error', {
|
|
2950
2947
|
key,
|
|
2951
2948
|
ttl: seconds,
|
|
2952
2949
|
error: err
|
|
@@ -2965,31 +2962,31 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
|
|
|
2965
2962
|
try {
|
|
2966
2963
|
// 参数验证
|
|
2967
2964
|
if (!key || typeof key !== 'string') {
|
|
2968
|
-
|
|
2965
|
+
this.logger('error','pexpire参数错误: key must be a non-empty string');
|
|
2969
2966
|
return false;
|
|
2970
2967
|
}
|
|
2971
2968
|
if (!Number.isInteger(milliseconds) || milliseconds <= 0) {
|
|
2972
|
-
|
|
2969
|
+
this.logger('error','pexpire参数错误: milliseconds must be a positive integer');
|
|
2973
2970
|
return false;
|
|
2974
2971
|
}
|
|
2975
2972
|
|
|
2976
2973
|
// 触发前置事件
|
|
2977
|
-
const
|
|
2974
|
+
const ctx = {
|
|
2978
2975
|
key,
|
|
2979
2976
|
ttl: milliseconds,
|
|
2980
2977
|
cancel: false
|
|
2981
2978
|
};
|
|
2982
|
-
this.emit('
|
|
2979
|
+
this.emit('pexpire:before', ctx);
|
|
2983
2980
|
|
|
2984
|
-
if (
|
|
2985
|
-
|
|
2981
|
+
if (ctx.cancel) {
|
|
2982
|
+
this.logger('debug',`pexpire操作被事件处理器取消 [${key}]`);
|
|
2986
2983
|
return false;
|
|
2987
2984
|
}
|
|
2988
2985
|
|
|
2989
2986
|
const {
|
|
2990
2987
|
key: finalKey,
|
|
2991
2988
|
ttl: finalMilliseconds
|
|
2992
|
-
} =
|
|
2989
|
+
} = ctx;
|
|
2993
2990
|
|
|
2994
2991
|
const connection = await this._getConnection();
|
|
2995
2992
|
try {
|
|
@@ -3000,20 +2997,20 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
|
|
|
3000
2997
|
} else if (typeof connection.execute === 'function') {
|
|
3001
2998
|
result = await connection.execute('PEXPIRE', [this.applyPrefix(finalKey), finalMilliseconds]);
|
|
3002
2999
|
} else {
|
|
3003
|
-
|
|
3000
|
+
this.logger('error','连接对象不支持pexpire操作');
|
|
3004
3001
|
return false;
|
|
3005
3002
|
}
|
|
3006
3003
|
|
|
3007
3004
|
// 触发后置事件
|
|
3008
|
-
this.emit('
|
|
3005
|
+
this.emit('pexpire:after', {
|
|
3009
3006
|
key: finalKey,
|
|
3010
3007
|
ttl: finalMilliseconds,
|
|
3011
3008
|
success: result === 1
|
|
3012
3009
|
});
|
|
3013
3010
|
return result === 1;
|
|
3014
3011
|
} catch (err) {
|
|
3015
|
-
|
|
3016
|
-
this.emit('
|
|
3012
|
+
this.logger('error',`设置过期时间(毫秒)失败 [${finalKey}]:`, err);
|
|
3013
|
+
this.emit('pexpire:error', {
|
|
3017
3014
|
key: finalKey,
|
|
3018
3015
|
ttl: finalMilliseconds,
|
|
3019
3016
|
error: err
|
|
@@ -3023,8 +3020,8 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
|
|
|
3023
3020
|
this._recycleConnection(connection);
|
|
3024
3021
|
}
|
|
3025
3022
|
} catch (err) {
|
|
3026
|
-
|
|
3027
|
-
this.emit('
|
|
3023
|
+
this.logger('error',`设置过期时间(毫秒)失败 [${key}]:`, err);
|
|
3024
|
+
this.emit('pexpire:error', {
|
|
3028
3025
|
key,
|
|
3029
3026
|
ttl: milliseconds,
|
|
3030
3027
|
error: err
|
|
@@ -3037,20 +3034,20 @@ Redis.prototype.pexpire = async function(key, milliseconds) {
|
|
|
3037
3034
|
Redis.prototype.persist = async function(key) {
|
|
3038
3035
|
try {
|
|
3039
3036
|
// 触发前置事件
|
|
3040
|
-
const
|
|
3037
|
+
const ctx = {
|
|
3041
3038
|
key,
|
|
3042
3039
|
cancel: false
|
|
3043
3040
|
};
|
|
3044
|
-
this.emit('
|
|
3041
|
+
this.emit('persist:before', ctx);
|
|
3045
3042
|
|
|
3046
|
-
if (
|
|
3047
|
-
|
|
3043
|
+
if (ctx.cancel) {
|
|
3044
|
+
this.logger('debug',`persist操作被事件处理器取消 [${key}]`);
|
|
3048
3045
|
return false;
|
|
3049
3046
|
}
|
|
3050
3047
|
|
|
3051
3048
|
const {
|
|
3052
3049
|
key: finalKey
|
|
3053
|
-
} =
|
|
3050
|
+
} = ctx;
|
|
3054
3051
|
|
|
3055
3052
|
const connection = await this._getConnection();
|
|
3056
3053
|
try {
|
|
@@ -3058,14 +3055,14 @@ Redis.prototype.persist = async function(key) {
|
|
|
3058
3055
|
const result = await connection.persist(this.applyPrefix(finalKey));
|
|
3059
3056
|
|
|
3060
3057
|
// 触发后置事件
|
|
3061
|
-
this.emit('
|
|
3058
|
+
this.emit('persist:after', {
|
|
3062
3059
|
key: finalKey,
|
|
3063
3060
|
success: result === 1
|
|
3064
3061
|
});
|
|
3065
3062
|
return result === 1;
|
|
3066
3063
|
} catch (err) {
|
|
3067
|
-
|
|
3068
|
-
this.emit('
|
|
3064
|
+
this.logger('error',`移除过期时间失败 [${finalKey}]:`, err);
|
|
3065
|
+
this.emit('persist:error', {
|
|
3069
3066
|
key: finalKey,
|
|
3070
3067
|
error: err
|
|
3071
3068
|
});
|
|
@@ -3074,8 +3071,8 @@ Redis.prototype.persist = async function(key) {
|
|
|
3074
3071
|
this._recycleConnection(connection);
|
|
3075
3072
|
}
|
|
3076
3073
|
} catch (err) {
|
|
3077
|
-
|
|
3078
|
-
this.emit('
|
|
3074
|
+
this.logger('error',`移除过期时间失败 [${key}]:`, err);
|
|
3075
|
+
this.emit('persist:error', {
|
|
3079
3076
|
key,
|
|
3080
3077
|
error: err
|
|
3081
3078
|
});
|
|
@@ -3093,27 +3090,27 @@ Redis.prototype.incr = async function(key, seconds = 0) {
|
|
|
3093
3090
|
try {
|
|
3094
3091
|
// 参数验证
|
|
3095
3092
|
if (!key || typeof key !== 'string') {
|
|
3096
|
-
|
|
3093
|
+
this.logger('error','参数错误: 键必须是非空字符串');
|
|
3097
3094
|
return null;
|
|
3098
3095
|
}
|
|
3099
3096
|
|
|
3100
3097
|
// 触发前置事件
|
|
3101
|
-
const
|
|
3098
|
+
const ctx = {
|
|
3102
3099
|
key,
|
|
3103
3100
|
seconds,
|
|
3104
3101
|
cancel: false
|
|
3105
3102
|
};
|
|
3106
|
-
this.emit('
|
|
3103
|
+
this.emit('incr:before', ctx);
|
|
3107
3104
|
|
|
3108
|
-
if (
|
|
3109
|
-
|
|
3105
|
+
if (ctx.cancel) {
|
|
3106
|
+
this.logger('debug',`incr操作被事件处理器取消 [${key}]`);
|
|
3110
3107
|
return null;
|
|
3111
3108
|
}
|
|
3112
3109
|
|
|
3113
3110
|
const {
|
|
3114
3111
|
key: finalKey,
|
|
3115
3112
|
seconds: finalSeconds
|
|
3116
|
-
} =
|
|
3113
|
+
} = ctx;
|
|
3117
3114
|
|
|
3118
3115
|
const connection = await this._getConnection();
|
|
3119
3116
|
try {
|
|
@@ -3130,15 +3127,15 @@ Redis.prototype.incr = async function(key, seconds = 0) {
|
|
|
3130
3127
|
}
|
|
3131
3128
|
}
|
|
3132
3129
|
|
|
3133
|
-
this.emit('
|
|
3130
|
+
this.emit('incr:after', {
|
|
3134
3131
|
key: finalKey,
|
|
3135
3132
|
seconds: finalSeconds,
|
|
3136
3133
|
result
|
|
3137
3134
|
});
|
|
3138
3135
|
return result;
|
|
3139
3136
|
} catch (err) {
|
|
3140
|
-
|
|
3141
|
-
this.emit('
|
|
3137
|
+
this.logger('error',`增加整数值失败 [${finalKey}]:`, err);
|
|
3138
|
+
this.emit('incr:error', {
|
|
3142
3139
|
key: finalKey,
|
|
3143
3140
|
seconds: finalSeconds,
|
|
3144
3141
|
error: err
|
|
@@ -3148,8 +3145,8 @@ Redis.prototype.incr = async function(key, seconds = 0) {
|
|
|
3148
3145
|
this._recycleConnection(connection);
|
|
3149
3146
|
}
|
|
3150
3147
|
} catch (err) {
|
|
3151
|
-
|
|
3152
|
-
this.emit('
|
|
3148
|
+
this.logger('error',`增加整数值失败 [${key}]:`, err);
|
|
3149
|
+
this.emit('incr:error', {
|
|
3153
3150
|
key,
|
|
3154
3151
|
seconds,
|
|
3155
3152
|
error: err
|
|
@@ -3168,27 +3165,27 @@ Redis.prototype.decr = async function(key, seconds = 0) {
|
|
|
3168
3165
|
try {
|
|
3169
3166
|
// 参数验证
|
|
3170
3167
|
if (!key || typeof key !== 'string') {
|
|
3171
|
-
|
|
3168
|
+
this.logger('error','参数错误: 键必须是非空字符串');
|
|
3172
3169
|
return null;
|
|
3173
3170
|
}
|
|
3174
3171
|
|
|
3175
3172
|
// 触发前置事件
|
|
3176
|
-
const
|
|
3173
|
+
const ctx = {
|
|
3177
3174
|
key,
|
|
3178
3175
|
seconds,
|
|
3179
3176
|
cancel: false
|
|
3180
3177
|
};
|
|
3181
|
-
this.emit('
|
|
3178
|
+
this.emit('decr:before', ctx);
|
|
3182
3179
|
|
|
3183
|
-
if (
|
|
3184
|
-
|
|
3180
|
+
if (ctx.cancel) {
|
|
3181
|
+
this.logger('debug',`decr操作被事件处理器取消 [${key}]`);
|
|
3185
3182
|
return null;
|
|
3186
3183
|
}
|
|
3187
3184
|
|
|
3188
3185
|
const {
|
|
3189
3186
|
key: finalKey,
|
|
3190
3187
|
seconds: finalSeconds
|
|
3191
|
-
} =
|
|
3188
|
+
} = ctx;
|
|
3192
3189
|
|
|
3193
3190
|
const connection = await this._getConnection();
|
|
3194
3191
|
try {
|
|
@@ -3205,15 +3202,15 @@ Redis.prototype.decr = async function(key, seconds = 0) {
|
|
|
3205
3202
|
}
|
|
3206
3203
|
}
|
|
3207
3204
|
|
|
3208
|
-
this.emit('
|
|
3205
|
+
this.emit('decr:after', {
|
|
3209
3206
|
key: finalKey,
|
|
3210
3207
|
seconds: finalSeconds,
|
|
3211
3208
|
result
|
|
3212
3209
|
});
|
|
3213
3210
|
return result;
|
|
3214
3211
|
} catch (err) {
|
|
3215
|
-
|
|
3216
|
-
this.emit('
|
|
3212
|
+
this.logger('error',`减少整数值失败 [${finalKey}]:`, err);
|
|
3213
|
+
this.emit('decr:error', {
|
|
3217
3214
|
key: finalKey,
|
|
3218
3215
|
seconds: finalSeconds,
|
|
3219
3216
|
error: err
|
|
@@ -3223,8 +3220,8 @@ Redis.prototype.decr = async function(key, seconds = 0) {
|
|
|
3223
3220
|
this._recycleConnection(connection);
|
|
3224
3221
|
}
|
|
3225
3222
|
} catch (err) {
|
|
3226
|
-
|
|
3227
|
-
this.emit('
|
|
3223
|
+
this.logger('error',`减少整数值失败 [${key}]:`, err);
|
|
3224
|
+
this.emit('decr:error', {
|
|
3228
3225
|
key,
|
|
3229
3226
|
seconds,
|
|
3230
3227
|
error: err
|
|
@@ -3244,25 +3241,25 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
|
|
|
3244
3241
|
try {
|
|
3245
3242
|
// 参数验证
|
|
3246
3243
|
if (!key || typeof key !== 'string') {
|
|
3247
|
-
|
|
3244
|
+
this.logger('error','参数错误: 键必须是非空字符串');
|
|
3248
3245
|
return null;
|
|
3249
3246
|
}
|
|
3250
3247
|
if (typeof increment !== 'number' || isNaN(increment)) {
|
|
3251
|
-
|
|
3248
|
+
this.logger('error','参数错误: 增量必须是有效数字');
|
|
3252
3249
|
return null;
|
|
3253
3250
|
}
|
|
3254
3251
|
|
|
3255
3252
|
// 触发前置事件
|
|
3256
|
-
const
|
|
3253
|
+
const ctx = {
|
|
3257
3254
|
key,
|
|
3258
3255
|
increment,
|
|
3259
3256
|
seconds,
|
|
3260
3257
|
cancel: false
|
|
3261
3258
|
};
|
|
3262
|
-
this.emit('
|
|
3259
|
+
this.emit('incrbyfloat:before', ctx);
|
|
3263
3260
|
|
|
3264
|
-
if (
|
|
3265
|
-
|
|
3261
|
+
if (ctx.cancel) {
|
|
3262
|
+
this.logger('debug',`incrbyfloat操作被事件处理器取消 [${key}]`);
|
|
3266
3263
|
return null;
|
|
3267
3264
|
}
|
|
3268
3265
|
|
|
@@ -3270,7 +3267,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
|
|
|
3270
3267
|
key: finalKey,
|
|
3271
3268
|
increment: finalIncrement,
|
|
3272
3269
|
seconds: finalSeconds
|
|
3273
|
-
} =
|
|
3270
|
+
} = ctx;
|
|
3274
3271
|
|
|
3275
3272
|
const connection = await this._getConnection();
|
|
3276
3273
|
try {
|
|
@@ -3288,7 +3285,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
|
|
|
3288
3285
|
}
|
|
3289
3286
|
}
|
|
3290
3287
|
|
|
3291
|
-
this.emit('
|
|
3288
|
+
this.emit('incrbyfloat:after', {
|
|
3292
3289
|
key: finalKey,
|
|
3293
3290
|
increment: finalIncrement,
|
|
3294
3291
|
seconds: finalSeconds,
|
|
@@ -3296,8 +3293,8 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
|
|
|
3296
3293
|
});
|
|
3297
3294
|
return parsedResult;
|
|
3298
3295
|
} catch (err) {
|
|
3299
|
-
|
|
3300
|
-
this.emit('
|
|
3296
|
+
this.logger('error',`增加浮点数值失败 [${finalKey}]:`, err);
|
|
3297
|
+
this.emit('incrbyfloat:error', {
|
|
3301
3298
|
key: finalKey,
|
|
3302
3299
|
increment: finalIncrement,
|
|
3303
3300
|
seconds: finalSeconds,
|
|
@@ -3308,8 +3305,8 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
|
|
|
3308
3305
|
this._recycleConnection(connection);
|
|
3309
3306
|
}
|
|
3310
3307
|
} catch (err) {
|
|
3311
|
-
|
|
3312
|
-
this.emit('
|
|
3308
|
+
this.logger('error',`增加浮点数值失败 [${key}]:`, err);
|
|
3309
|
+
this.emit('incrbyfloat:error', {
|
|
3313
3310
|
key,
|
|
3314
3311
|
increment,
|
|
3315
3312
|
seconds,
|
|
@@ -3330,25 +3327,25 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
|
|
|
3330
3327
|
try {
|
|
3331
3328
|
// 参数验证
|
|
3332
3329
|
if (!key || typeof key !== 'string') {
|
|
3333
|
-
|
|
3330
|
+
this.logger('error','参数错误: 键必须是非空字符串');
|
|
3334
3331
|
return null;
|
|
3335
3332
|
}
|
|
3336
3333
|
if (typeof increment !== 'number' || isNaN(increment) || !Number.isInteger(increment)) {
|
|
3337
|
-
|
|
3334
|
+
this.logger('error','参数错误: 增量必须是有效整数');
|
|
3338
3335
|
return null;
|
|
3339
3336
|
}
|
|
3340
3337
|
|
|
3341
3338
|
// 触发前置事件
|
|
3342
|
-
const
|
|
3339
|
+
const ctx = {
|
|
3343
3340
|
key,
|
|
3344
3341
|
increment,
|
|
3345
3342
|
seconds,
|
|
3346
3343
|
cancel: false
|
|
3347
3344
|
};
|
|
3348
|
-
this.emit('
|
|
3345
|
+
this.emit('incrby:before', ctx);
|
|
3349
3346
|
|
|
3350
|
-
if (
|
|
3351
|
-
|
|
3347
|
+
if (ctx.cancel) {
|
|
3348
|
+
this.logger('debug',`incrby操作被事件处理器取消 [${key}]`);
|
|
3352
3349
|
return null;
|
|
3353
3350
|
}
|
|
3354
3351
|
|
|
@@ -3356,7 +3353,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
|
|
|
3356
3353
|
key: finalKey,
|
|
3357
3354
|
increment: finalIncrement,
|
|
3358
3355
|
seconds: finalSeconds
|
|
3359
|
-
} =
|
|
3356
|
+
} = ctx;
|
|
3360
3357
|
|
|
3361
3358
|
const connection = await this._getConnection();
|
|
3362
3359
|
try {
|
|
@@ -3373,7 +3370,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
|
|
|
3373
3370
|
}
|
|
3374
3371
|
}
|
|
3375
3372
|
|
|
3376
|
-
this.emit('
|
|
3373
|
+
this.emit('incrby:after', {
|
|
3377
3374
|
key: finalKey,
|
|
3378
3375
|
increment: finalIncrement,
|
|
3379
3376
|
seconds: finalSeconds,
|
|
@@ -3381,8 +3378,8 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
|
|
|
3381
3378
|
});
|
|
3382
3379
|
return result;
|
|
3383
3380
|
} catch (err) {
|
|
3384
|
-
|
|
3385
|
-
this.emit('
|
|
3381
|
+
this.logger('error',`增加指定值失败 [${finalKey}]:`, err);
|
|
3382
|
+
this.emit('incrby:error', {
|
|
3386
3383
|
key: finalKey,
|
|
3387
3384
|
increment: finalIncrement,
|
|
3388
3385
|
seconds: finalSeconds,
|
|
@@ -3393,8 +3390,8 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
|
|
|
3393
3390
|
this._recycleConnection(connection);
|
|
3394
3391
|
}
|
|
3395
3392
|
} catch (err) {
|
|
3396
|
-
|
|
3397
|
-
this.emit('
|
|
3393
|
+
this.logger('error',`增加指定值失败 [${key}]:`, err);
|
|
3394
|
+
this.emit('incrby:error', {
|
|
3398
3395
|
key,
|
|
3399
3396
|
increment,
|
|
3400
3397
|
seconds,
|
|
@@ -3415,25 +3412,25 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
|
|
|
3415
3412
|
try {
|
|
3416
3413
|
// 参数验证
|
|
3417
3414
|
if (!key || typeof key !== 'string') {
|
|
3418
|
-
|
|
3415
|
+
this.logger('error','参数错误: 键必须是非空字符串');
|
|
3419
3416
|
return null;
|
|
3420
3417
|
}
|
|
3421
3418
|
if (typeof decrement !== 'number' || isNaN(decrement) || !Number.isInteger(decrement)) {
|
|
3422
|
-
|
|
3419
|
+
this.logger('error','参数错误: 减量必须是有效整数');
|
|
3423
3420
|
return null;
|
|
3424
3421
|
}
|
|
3425
3422
|
|
|
3426
3423
|
// 触发前置事件
|
|
3427
|
-
const
|
|
3424
|
+
const ctx = {
|
|
3428
3425
|
key,
|
|
3429
3426
|
decrement,
|
|
3430
3427
|
seconds,
|
|
3431
3428
|
cancel: false
|
|
3432
3429
|
};
|
|
3433
|
-
this.emit('
|
|
3430
|
+
this.emit('decrby:before', ctx);
|
|
3434
3431
|
|
|
3435
|
-
if (
|
|
3436
|
-
|
|
3432
|
+
if (ctx.cancel) {
|
|
3433
|
+
this.logger('debug',`decrby操作被事件处理器取消 [${key}]`);
|
|
3437
3434
|
return null;
|
|
3438
3435
|
}
|
|
3439
3436
|
|
|
@@ -3441,7 +3438,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
|
|
|
3441
3438
|
key: finalKey,
|
|
3442
3439
|
decrement: finalDecrement,
|
|
3443
3440
|
seconds: finalSeconds
|
|
3444
|
-
} =
|
|
3441
|
+
} = ctx;
|
|
3445
3442
|
|
|
3446
3443
|
const connection = await this._getConnection();
|
|
3447
3444
|
try {
|
|
@@ -3458,7 +3455,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
|
|
|
3458
3455
|
}
|
|
3459
3456
|
}
|
|
3460
3457
|
|
|
3461
|
-
this.emit('
|
|
3458
|
+
this.emit('decrby:after', {
|
|
3462
3459
|
key: finalKey,
|
|
3463
3460
|
decrement: finalDecrement,
|
|
3464
3461
|
seconds: finalSeconds,
|
|
@@ -3466,8 +3463,8 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
|
|
|
3466
3463
|
});
|
|
3467
3464
|
return result;
|
|
3468
3465
|
} catch (err) {
|
|
3469
|
-
|
|
3470
|
-
this.emit('
|
|
3466
|
+
this.logger('error',`减少指定值失败 [${finalKey}]:`, err);
|
|
3467
|
+
this.emit('decrby:error', {
|
|
3471
3468
|
key: finalKey,
|
|
3472
3469
|
decrement: finalDecrement,
|
|
3473
3470
|
seconds: finalSeconds,
|
|
@@ -3478,8 +3475,8 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
|
|
|
3478
3475
|
this._recycleConnection(connection);
|
|
3479
3476
|
}
|
|
3480
3477
|
} catch (err) {
|
|
3481
|
-
|
|
3482
|
-
this.emit('
|
|
3478
|
+
this.logger('error',`减少指定值失败 [${key}]:`, err);
|
|
3479
|
+
this.emit('decrby:error', {
|
|
3483
3480
|
key,
|
|
3484
3481
|
decrement,
|
|
3485
3482
|
seconds,
|
|
@@ -3500,22 +3497,22 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
|
|
|
3500
3497
|
Redis.prototype.sadd = async function(key, members) {
|
|
3501
3498
|
try {
|
|
3502
3499
|
// 触发前置事件
|
|
3503
|
-
const
|
|
3500
|
+
const ctx = {
|
|
3504
3501
|
key,
|
|
3505
3502
|
members: Array.isArray(members) ? members : [members],
|
|
3506
3503
|
cancel: false
|
|
3507
3504
|
};
|
|
3508
|
-
this.emit('
|
|
3505
|
+
this.emit('sadd:before', ctx);
|
|
3509
3506
|
|
|
3510
|
-
if (
|
|
3511
|
-
|
|
3507
|
+
if (ctx.cancel) {
|
|
3508
|
+
this.logger('debug',`sadd操作被事件处理器取消 [${key}]`);
|
|
3512
3509
|
return 0;
|
|
3513
3510
|
}
|
|
3514
3511
|
|
|
3515
3512
|
const {
|
|
3516
3513
|
key: finalKey,
|
|
3517
3514
|
members: finalMembers
|
|
3518
|
-
} =
|
|
3515
|
+
} = ctx;
|
|
3519
3516
|
|
|
3520
3517
|
const connection = await this._getConnection();
|
|
3521
3518
|
try {
|
|
@@ -3523,7 +3520,7 @@ Redis.prototype.sadd = async function(key, members) {
|
|
|
3523
3520
|
const result = await connection.sAdd(prefixedKey, finalMembers);
|
|
3524
3521
|
|
|
3525
3522
|
// 触发后置事件
|
|
3526
|
-
this.emit('
|
|
3523
|
+
this.emit('sadd:after', {
|
|
3527
3524
|
key: finalKey,
|
|
3528
3525
|
members: finalMembers,
|
|
3529
3526
|
result
|
|
@@ -3533,8 +3530,8 @@ Redis.prototype.sadd = async function(key, members) {
|
|
|
3533
3530
|
this._recycleConnection(connection);
|
|
3534
3531
|
}
|
|
3535
3532
|
} catch (err) {
|
|
3536
|
-
|
|
3537
|
-
this.emit('
|
|
3533
|
+
this.logger('error',`添加集合成员失败 [${key}]:`, err);
|
|
3534
|
+
this.emit('sadd:error', {
|
|
3538
3535
|
key,
|
|
3539
3536
|
members,
|
|
3540
3537
|
error: err
|
|
@@ -3552,22 +3549,22 @@ Redis.prototype.sadd = async function(key, members) {
|
|
|
3552
3549
|
Redis.prototype.srem = async function(key, members) {
|
|
3553
3550
|
try {
|
|
3554
3551
|
// 触发前置事件
|
|
3555
|
-
const
|
|
3552
|
+
const ctx = {
|
|
3556
3553
|
key,
|
|
3557
3554
|
members: Array.isArray(members) ? members : [members],
|
|
3558
3555
|
cancel: false
|
|
3559
3556
|
};
|
|
3560
|
-
this.emit('
|
|
3557
|
+
this.emit('srem:before', ctx);
|
|
3561
3558
|
|
|
3562
|
-
if (
|
|
3563
|
-
|
|
3559
|
+
if (ctx.cancel) {
|
|
3560
|
+
this.logger('debug',`srem操作被事件处理器取消 [${key}]`);
|
|
3564
3561
|
return 0;
|
|
3565
3562
|
}
|
|
3566
3563
|
|
|
3567
3564
|
const {
|
|
3568
3565
|
key: finalKey,
|
|
3569
3566
|
members: finalMembers
|
|
3570
|
-
} =
|
|
3567
|
+
} = ctx;
|
|
3571
3568
|
|
|
3572
3569
|
const connection = await this._getConnection();
|
|
3573
3570
|
try {
|
|
@@ -3575,7 +3572,7 @@ Redis.prototype.srem = async function(key, members) {
|
|
|
3575
3572
|
const result = await connection.sRem(prefixedKey, finalMembers);
|
|
3576
3573
|
|
|
3577
3574
|
// 触发后置事件
|
|
3578
|
-
this.emit('
|
|
3575
|
+
this.emit('srem:after', {
|
|
3579
3576
|
key: finalKey,
|
|
3580
3577
|
members: finalMembers,
|
|
3581
3578
|
result
|
|
@@ -3585,8 +3582,8 @@ Redis.prototype.srem = async function(key, members) {
|
|
|
3585
3582
|
this._recycleConnection(connection);
|
|
3586
3583
|
}
|
|
3587
3584
|
} catch (err) {
|
|
3588
|
-
|
|
3589
|
-
this.emit('
|
|
3585
|
+
this.logger('error',`移除集合成员失败 [${key}]:`, err);
|
|
3586
|
+
this.emit('srem:error', {
|
|
3590
3587
|
key,
|
|
3591
3588
|
members,
|
|
3592
3589
|
error: err
|
|
@@ -3605,15 +3602,15 @@ Redis.prototype.sismember = async function(key, member) {
|
|
|
3605
3602
|
try {
|
|
3606
3603
|
// 参数验证
|
|
3607
3604
|
if (!key || typeof key !== 'string') {
|
|
3608
|
-
|
|
3605
|
+
this.logger('error','sismember参数错误: key must be a non-empty string');
|
|
3609
3606
|
return false;
|
|
3610
3607
|
}
|
|
3611
3608
|
if (member === undefined || member === null) {
|
|
3612
|
-
|
|
3609
|
+
this.logger('error','sismember参数错误: member cannot be undefined or null');
|
|
3613
3610
|
return false;
|
|
3614
3611
|
}
|
|
3615
3612
|
|
|
3616
|
-
this.emit('
|
|
3613
|
+
this.emit('sismember:before', {
|
|
3617
3614
|
key,
|
|
3618
3615
|
member
|
|
3619
3616
|
});
|
|
@@ -3623,7 +3620,7 @@ Redis.prototype.sismember = async function(key, member) {
|
|
|
3623
3620
|
// 将成员转换为字符串,与其他模块行为一致
|
|
3624
3621
|
const memberStr = String(member);
|
|
3625
3622
|
const result = await connection.sIsMember(prefixedKey, memberStr);
|
|
3626
|
-
this.emit('
|
|
3623
|
+
this.emit('sismember:after', {
|
|
3627
3624
|
key,
|
|
3628
3625
|
member,
|
|
3629
3626
|
result
|
|
@@ -3633,12 +3630,12 @@ Redis.prototype.sismember = async function(key, member) {
|
|
|
3633
3630
|
this._recycleConnection(connection);
|
|
3634
3631
|
}
|
|
3635
3632
|
} catch (err) {
|
|
3636
|
-
this.emit('
|
|
3633
|
+
this.emit('sismember:error', {
|
|
3637
3634
|
key,
|
|
3638
3635
|
member,
|
|
3639
3636
|
error: err
|
|
3640
3637
|
});
|
|
3641
|
-
|
|
3638
|
+
this.logger('error',`检查集合成员失败 [${key}:${member}]:`, err);
|
|
3642
3639
|
return false;
|
|
3643
3640
|
}
|
|
3644
3641
|
};
|
|
@@ -3654,7 +3651,7 @@ Redis.prototype.smembers = async function(key) {
|
|
|
3654
3651
|
try {
|
|
3655
3652
|
const prefixedKey = this.applyPrefix(key);
|
|
3656
3653
|
const result = await connection.sMembers(prefixedKey);
|
|
3657
|
-
this.emit('
|
|
3654
|
+
this.emit('smembers:after', {
|
|
3658
3655
|
key,
|
|
3659
3656
|
result
|
|
3660
3657
|
});
|
|
@@ -3663,8 +3660,8 @@ Redis.prototype.smembers = async function(key) {
|
|
|
3663
3660
|
this._recycleConnection(connection);
|
|
3664
3661
|
}
|
|
3665
3662
|
} catch (err) {
|
|
3666
|
-
|
|
3667
|
-
this.emit('
|
|
3663
|
+
this.logger('error',`获取集合所有成员失败 [${key}]:`, err);
|
|
3664
|
+
this.emit('smembers:error', {
|
|
3668
3665
|
key,
|
|
3669
3666
|
error: err
|
|
3670
3667
|
});
|
|
@@ -3688,7 +3685,7 @@ Redis.prototype.scard = async function(key) {
|
|
|
3688
3685
|
this._recycleConnection(connection);
|
|
3689
3686
|
}
|
|
3690
3687
|
} catch (err) {
|
|
3691
|
-
|
|
3688
|
+
this.logger('error',`获取集合大小失败 [${key}]:`, err);
|
|
3692
3689
|
return 0;
|
|
3693
3690
|
}
|
|
3694
3691
|
};
|
|
@@ -3710,7 +3707,7 @@ Redis.prototype.sinter = async function(keys) {
|
|
|
3710
3707
|
this._recycleConnection(connection);
|
|
3711
3708
|
}
|
|
3712
3709
|
} catch (err) {
|
|
3713
|
-
|
|
3710
|
+
this.logger('error',`获取集合交集失败:`, err);
|
|
3714
3711
|
return [];
|
|
3715
3712
|
}
|
|
3716
3713
|
};
|
|
@@ -3732,7 +3729,7 @@ Redis.prototype.sunion = async function(keys) {
|
|
|
3732
3729
|
this._recycleConnection(connection);
|
|
3733
3730
|
}
|
|
3734
3731
|
} catch (err) {
|
|
3735
|
-
|
|
3732
|
+
this.logger('error',`获取集合并集失败:`, err);
|
|
3736
3733
|
return [];
|
|
3737
3734
|
}
|
|
3738
3735
|
};
|
|
@@ -3754,7 +3751,7 @@ Redis.prototype.sdiff = async function(keys) {
|
|
|
3754
3751
|
this._recycleConnection(connection);
|
|
3755
3752
|
}
|
|
3756
3753
|
} catch (err) {
|
|
3757
|
-
|
|
3754
|
+
this.logger('error',`获取集合差集失败:`, err);
|
|
3758
3755
|
return [];
|
|
3759
3756
|
}
|
|
3760
3757
|
};
|
|
@@ -3771,16 +3768,16 @@ Redis.prototype.sdiff = async function(keys) {
|
|
|
3771
3768
|
Redis.prototype.zadd = async function(key, score, member) {
|
|
3772
3769
|
try {
|
|
3773
3770
|
// 触发前置事件
|
|
3774
|
-
const
|
|
3771
|
+
const ctx = {
|
|
3775
3772
|
key,
|
|
3776
3773
|
score,
|
|
3777
3774
|
member,
|
|
3778
3775
|
cancel: false
|
|
3779
3776
|
};
|
|
3780
|
-
this.emit('
|
|
3777
|
+
this.emit('zadd:before', ctx);
|
|
3781
3778
|
|
|
3782
|
-
if (
|
|
3783
|
-
|
|
3779
|
+
if (ctx.cancel) {
|
|
3780
|
+
this.logger('debug',`zadd操作被事件处理器取消 [${key}]`);
|
|
3784
3781
|
return false;
|
|
3785
3782
|
}
|
|
3786
3783
|
|
|
@@ -3788,7 +3785,7 @@ Redis.prototype.zadd = async function(key, score, member) {
|
|
|
3788
3785
|
key: finalKey,
|
|
3789
3786
|
score: finalScore,
|
|
3790
3787
|
member: finalMember
|
|
3791
|
-
} =
|
|
3788
|
+
} = ctx;
|
|
3792
3789
|
|
|
3793
3790
|
const connection = await this._getConnection();
|
|
3794
3791
|
try {
|
|
@@ -3799,7 +3796,7 @@ Redis.prototype.zadd = async function(key, score, member) {
|
|
|
3799
3796
|
});
|
|
3800
3797
|
|
|
3801
3798
|
// 触发后置事件
|
|
3802
|
-
this.emit('
|
|
3799
|
+
this.emit('zadd:after', {
|
|
3803
3800
|
key: finalKey,
|
|
3804
3801
|
score: finalScore,
|
|
3805
3802
|
member: finalMember,
|
|
@@ -3810,8 +3807,8 @@ Redis.prototype.zadd = async function(key, score, member) {
|
|
|
3810
3807
|
this._recycleConnection(connection);
|
|
3811
3808
|
}
|
|
3812
3809
|
} catch (err) {
|
|
3813
|
-
|
|
3814
|
-
this.emit('
|
|
3810
|
+
this.logger('error',`添加有序集合成员失败 [${key}]:`, err);
|
|
3811
|
+
this.emit('zadd:error', {
|
|
3815
3812
|
key,
|
|
3816
3813
|
score,
|
|
3817
3814
|
member,
|
|
@@ -3830,22 +3827,22 @@ Redis.prototype.zadd = async function(key, score, member) {
|
|
|
3830
3827
|
Redis.prototype.zrem = async function(key, members) {
|
|
3831
3828
|
try {
|
|
3832
3829
|
// 触发前置事件
|
|
3833
|
-
const
|
|
3830
|
+
const ctx = {
|
|
3834
3831
|
key,
|
|
3835
3832
|
members: Array.isArray(members) ? members : [members],
|
|
3836
3833
|
cancel: false
|
|
3837
3834
|
};
|
|
3838
|
-
this.emit('
|
|
3835
|
+
this.emit('zrem:before', ctx);
|
|
3839
3836
|
|
|
3840
|
-
if (
|
|
3841
|
-
|
|
3837
|
+
if (ctx.cancel) {
|
|
3838
|
+
this.logger('debug',`zrem操作被事件处理器取消 [${key}]`);
|
|
3842
3839
|
return 0;
|
|
3843
3840
|
}
|
|
3844
3841
|
|
|
3845
3842
|
const {
|
|
3846
3843
|
key: finalKey,
|
|
3847
3844
|
members: finalMembers
|
|
3848
|
-
} =
|
|
3845
|
+
} = ctx;
|
|
3849
3846
|
|
|
3850
3847
|
const connection = await this._getConnection();
|
|
3851
3848
|
try {
|
|
@@ -3853,15 +3850,15 @@ Redis.prototype.zrem = async function(key, members) {
|
|
|
3853
3850
|
const result = await connection.zRem(this.applyPrefix(finalKey), ...finalMembers);
|
|
3854
3851
|
|
|
3855
3852
|
// 触发后置事件
|
|
3856
|
-
this.emit('
|
|
3853
|
+
this.emit('zrem:after', {
|
|
3857
3854
|
key: finalKey,
|
|
3858
3855
|
members: finalMembers,
|
|
3859
3856
|
result
|
|
3860
3857
|
});
|
|
3861
3858
|
return result;
|
|
3862
3859
|
} catch (err) {
|
|
3863
|
-
|
|
3864
|
-
this.emit('
|
|
3860
|
+
this.logger('error',`移除有序集合成员失败 [${finalKey}]:`, err);
|
|
3861
|
+
this.emit('zrem:error', {
|
|
3865
3862
|
key: finalKey,
|
|
3866
3863
|
members: finalMembers,
|
|
3867
3864
|
error: err
|
|
@@ -3871,8 +3868,8 @@ Redis.prototype.zrem = async function(key, members) {
|
|
|
3871
3868
|
this._recycleConnection(connection);
|
|
3872
3869
|
}
|
|
3873
3870
|
} catch (err) {
|
|
3874
|
-
|
|
3875
|
-
this.emit('
|
|
3871
|
+
this.logger('error',`移除有序集合成员失败 [${key}]:`, err);
|
|
3872
|
+
this.emit('zrem:error', {
|
|
3876
3873
|
key,
|
|
3877
3874
|
members,
|
|
3878
3875
|
error: err
|
|
@@ -3898,7 +3895,7 @@ Redis.prototype.zscore = async function(key, member) {
|
|
|
3898
3895
|
this._recycleConnection(connection);
|
|
3899
3896
|
}
|
|
3900
3897
|
} catch (err) {
|
|
3901
|
-
|
|
3898
|
+
this.logger('error',`获取有序集合成员分数失败 [${key}:${member}]:`, err);
|
|
3902
3899
|
return null;
|
|
3903
3900
|
}
|
|
3904
3901
|
};
|
|
@@ -3920,7 +3917,7 @@ Redis.prototype.zrank = async function(key, member) {
|
|
|
3920
3917
|
this._recycleConnection(connection);
|
|
3921
3918
|
}
|
|
3922
3919
|
} catch (err) {
|
|
3923
|
-
|
|
3920
|
+
this.logger('error',`获取有序集合成员排名失败 [${key}:${member}]:`, err);
|
|
3924
3921
|
return null;
|
|
3925
3922
|
}
|
|
3926
3923
|
};
|
|
@@ -3961,13 +3958,13 @@ Redis.prototype.zrange = async function(key, start, stop, withScores = false) {
|
|
|
3961
3958
|
member,
|
|
3962
3959
|
score: 0
|
|
3963
3960
|
});
|
|
3964
|
-
|
|
3961
|
+
throw new Error(`获取成员 ${member} 分数失败:`, scoreErr);
|
|
3965
3962
|
}
|
|
3966
3963
|
}
|
|
3967
3964
|
|
|
3968
3965
|
return results;
|
|
3969
3966
|
} catch (err) {
|
|
3970
|
-
|
|
3967
|
+
this.logger('error',`使用备用方法获取有序集合范围失败 [${prefixedKey}]:`, err);
|
|
3971
3968
|
// 回退到简单列表
|
|
3972
3969
|
return [];
|
|
3973
3970
|
}
|
|
@@ -3980,7 +3977,7 @@ Redis.prototype.zrange = async function(key, start, stop, withScores = false) {
|
|
|
3980
3977
|
this._recycleConnection(connection);
|
|
3981
3978
|
}
|
|
3982
3979
|
} catch (err) {
|
|
3983
|
-
|
|
3980
|
+
this.logger('error',`获取有序集合范围成员失败 [${key}]:`, err);
|
|
3984
3981
|
return [];
|
|
3985
3982
|
}
|
|
3986
3983
|
};
|
|
@@ -3998,13 +3995,13 @@ Redis.prototype.zcard = async function(key) {
|
|
|
3998
3995
|
const result = await connection.zCard(this.applyPrefix(key));
|
|
3999
3996
|
return result || 0;
|
|
4000
3997
|
} catch (err) {
|
|
4001
|
-
|
|
3998
|
+
this.logger('error',`获取有序集合成员数量失败 [${key}]:`, err);
|
|
4002
3999
|
return 0;
|
|
4003
4000
|
} finally {
|
|
4004
4001
|
this._recycleConnection(connection);
|
|
4005
4002
|
}
|
|
4006
4003
|
} catch (err) {
|
|
4007
|
-
|
|
4004
|
+
this.logger('error',`获取有序集合成员数量失败 [${key}]:`, err);
|
|
4008
4005
|
return 0;
|
|
4009
4006
|
}
|
|
4010
4007
|
};
|
|
@@ -4021,30 +4018,30 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
|
|
|
4021
4018
|
try {
|
|
4022
4019
|
// 参数验证
|
|
4023
4020
|
if (!key || typeof key !== 'string') {
|
|
4024
|
-
|
|
4021
|
+
this.logger('error','参数错误: 键必须是非空字符串');
|
|
4025
4022
|
return null;
|
|
4026
4023
|
}
|
|
4027
4024
|
if (typeof increment !== 'number' || isNaN(increment)) {
|
|
4028
|
-
|
|
4025
|
+
this.logger('error','参数错误: 增量必须是有效数字');
|
|
4029
4026
|
return null;
|
|
4030
4027
|
}
|
|
4031
4028
|
if (!member || typeof member !== 'string') {
|
|
4032
|
-
|
|
4029
|
+
this.logger('error','参数错误: 成员必须是非空字符串');
|
|
4033
4030
|
return null;
|
|
4034
4031
|
}
|
|
4035
4032
|
|
|
4036
4033
|
// 触发前置事件
|
|
4037
|
-
const
|
|
4034
|
+
const ctx = {
|
|
4038
4035
|
key,
|
|
4039
4036
|
increment,
|
|
4040
4037
|
member,
|
|
4041
4038
|
seconds,
|
|
4042
4039
|
cancel: false
|
|
4043
4040
|
};
|
|
4044
|
-
this.emit('
|
|
4041
|
+
this.emit('zincrby:before', ctx);
|
|
4045
4042
|
|
|
4046
|
-
if (
|
|
4047
|
-
|
|
4043
|
+
if (ctx.cancel) {
|
|
4044
|
+
this.logger('debug',`zincrby操作被事件处理器取消 [${key}]`);
|
|
4048
4045
|
return null;
|
|
4049
4046
|
}
|
|
4050
4047
|
|
|
@@ -4053,7 +4050,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
|
|
|
4053
4050
|
increment: finalIncrement,
|
|
4054
4051
|
member: finalMember,
|
|
4055
4052
|
seconds: finalSeconds
|
|
4056
|
-
} =
|
|
4053
|
+
} = ctx;
|
|
4057
4054
|
|
|
4058
4055
|
const connection = await this._getConnection();
|
|
4059
4056
|
try {
|
|
@@ -4071,7 +4068,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
|
|
|
4071
4068
|
}
|
|
4072
4069
|
|
|
4073
4070
|
// 触发后置事件
|
|
4074
|
-
this.emit('
|
|
4071
|
+
this.emit('zincrby:after', {
|
|
4075
4072
|
key: finalKey,
|
|
4076
4073
|
increment: finalIncrement,
|
|
4077
4074
|
member: finalMember,
|
|
@@ -4080,8 +4077,8 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
|
|
|
4080
4077
|
});
|
|
4081
4078
|
return parseFloat(result);
|
|
4082
4079
|
} catch (err) {
|
|
4083
|
-
|
|
4084
|
-
this.emit('
|
|
4080
|
+
this.logger('error',`增加有序集合成员分数失败 [${finalKey}]:`, err);
|
|
4081
|
+
this.emit('zincrby:error', {
|
|
4085
4082
|
key: finalKey,
|
|
4086
4083
|
increment: finalIncrement,
|
|
4087
4084
|
member: finalMember,
|
|
@@ -4093,8 +4090,8 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
|
|
|
4093
4090
|
this._recycleConnection(connection);
|
|
4094
4091
|
}
|
|
4095
4092
|
} catch (err) {
|
|
4096
|
-
|
|
4097
|
-
this.emit('
|
|
4093
|
+
this.logger('error',`增加有序集合成员分数失败 [${key}]:`, err);
|
|
4094
|
+
this.emit('zincrby:error', {
|
|
4098
4095
|
key,
|
|
4099
4096
|
increment,
|
|
4100
4097
|
member,
|
|
@@ -4127,19 +4124,19 @@ Redis.prototype._createConnection = function() {
|
|
|
4127
4124
|
|
|
4128
4125
|
// 监听错误事件
|
|
4129
4126
|
connection.on('error', (err) => {
|
|
4130
|
-
|
|
4127
|
+
this.logger('error',`连接 ${connection.id} 错误:`, err);
|
|
4131
4128
|
});
|
|
4132
4129
|
|
|
4133
4130
|
// 连接Redis
|
|
4134
4131
|
connection.connect().then(() => {
|
|
4135
|
-
//
|
|
4132
|
+
// this.logger('debug','新连接已创建并连接:', connection.id);
|
|
4136
4133
|
resolve(connection);
|
|
4137
4134
|
}).catch(err => {
|
|
4138
|
-
|
|
4135
|
+
this.logger('error','创建连接失败:', err);
|
|
4139
4136
|
reject(err);
|
|
4140
4137
|
});
|
|
4141
4138
|
} catch (error) {
|
|
4142
|
-
|
|
4139
|
+
this.logger('error','创建连接时发生异常:', error);
|
|
4143
4140
|
reject(error);
|
|
4144
4141
|
}
|
|
4145
4142
|
});
|