mm_redis 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,9 +7,9 @@ mm_redis是一个高效的Redis客户端封装库,提供了全面的Redis功
7
7
 
8
8
  ## 版本信息
9
9
 
10
- - **当前版本**: 2.0.1
10
+ - **当前版本**: 2.0.3
11
11
  - **更新日期**: 2025-10-14
12
- - **作者**: 邱文武
12
+ - **作者**: qww
13
13
  - **NPM**: [https://www.npmjs.com/package/mm_redis](https://www.npmjs.com/package/mm_redis)
14
14
  - **GitHub**: [https://gitee.com/qiuwenwu91/mm_redis](https://gitee.com/qiuwenwu91/mm_redis)
15
15
 
package/index.js CHANGED
@@ -43,8 +43,8 @@ class Redis extends BaseService {
43
43
  // 连接池配置
44
44
  "pool": {
45
45
  "max_clients": 100, // 最大连接数
46
- "min_idle": 5, // 最小空闲连接数
47
- "max_idle": 20, // 最大空闲连接数
46
+ "min_idle": 5, // 最小空闲连接数
47
+ "max_idle": 20, // 最大空闲连接数
48
48
  "idle_timeout": 30000 // 空闲超时时间(ms)
49
49
  }
50
50
  };
@@ -181,7 +181,9 @@ Redis.prototype.setConfig = function(config) {
181
181
  }
182
182
  Object.assign(this.config.pool, config.pool);
183
183
  // 删除config中的pool属性,避免后续Object.assign重复处理
184
- const configCopy = { ...config };
184
+ const configCopy = {
185
+ ...config
186
+ };
185
187
  delete configCopy.pool;
186
188
  Object.assign(this.config, configCopy);
187
189
  } else {
@@ -190,12 +192,12 @@ Redis.prototype.setConfig = function(config) {
190
192
  } else {
191
193
  return;
192
194
  }
193
-
195
+
194
196
  // 确保连接池配置存在且包含所有必要字段
195
197
  if (!this.config.pool) {
196
198
  this.config.pool = {};
197
199
  }
198
-
200
+
199
201
  // 设置默认连接池配置,只覆盖未定义的字段
200
202
  const defaultPoolConfig = {
201
203
  max_clients: 100,
@@ -203,21 +205,21 @@ Redis.prototype.setConfig = function(config) {
203
205
  max_idle: 20,
204
206
  idle_timeout: 30000
205
207
  };
206
-
208
+
207
209
  for (const key in defaultPoolConfig) {
208
210
  if (this.config.pool[key] === undefined) {
209
211
  this.config.pool[key] = defaultPoolConfig[key];
210
212
  }
211
213
  }
212
-
214
+
213
215
  // 如果连接池已经初始化,更新其配置
214
216
  if (this.connection_pool) {
215
217
  this.connection_pool.config = this.config.pool;
216
218
  }
217
-
219
+
218
220
  // 初始化连接池
219
221
  this._initPool();
220
- $.log.debug('[Redis] 配置已更新');
222
+ // $.log.debug('[Redis] 配置已更新');
221
223
  this.identifier = this.config.host + "/" + this.config.database;
222
224
  return this;
223
225
  };
@@ -233,7 +235,7 @@ Redis.prototype._initPool = function() {
233
235
  idle_timeout: 30000
234
236
  };
235
237
  }
236
-
238
+
237
239
  // 初始化连接池相关属性
238
240
  this.connection_pool = {
239
241
  available: [],
@@ -242,7 +244,7 @@ Redis.prototype._initPool = function() {
242
244
  };
243
245
 
244
246
  // 记录连接池初始化
245
- $.log.debug('[Redis] 连接池已初始化,配置:', this.config.pool);
247
+ // $.log.debug('[Redis] 连接池已初始化,配置:', this.config.pool);
246
248
  };
247
249
 
248
250
  /**
@@ -279,14 +281,14 @@ Redis.prototype.open = async function() {
279
281
 
280
282
  // 监听连接成功事件
281
283
  this.conn.on('connect', () => {
282
- $.log.debug('[Redis] Redis已连接');
284
+ // $.log.debug('[Redis] Redis已连接');
283
285
  this.connected = true;
284
286
  this.retry_count = 0;
285
287
  });
286
288
 
287
289
  // 监听断开连接事件
288
290
  this.conn.on('end', () => {
289
- $.log.debug('[Redis] Redis连接已断开');
291
+ // $.log.debug('[Redis] Redis连接已断开');
290
292
  this.connected = false;
291
293
  });
292
294
 
@@ -346,7 +348,7 @@ Redis.prototype._reconnect = function() {
346
348
  // 修复close方法,正确处理连接池
347
349
  Redis.prototype.close = function() {
348
350
  try {
349
- $.log.debug('[Redis] 关闭所有连接...');
351
+ // $.log.debug('[Redis] 关闭所有连接...');
350
352
 
351
353
  // 关闭所有使用中的连接
352
354
  if (this.connection_pool && this.connection_pool.in_use) {
@@ -608,7 +610,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
608
610
  $.log.error('[Redis] 参数错误: 数值必须是有效整数');
609
611
  return false;
610
612
  }
611
-
613
+
612
614
  // 触发前置事件,允许修改或阻止操作
613
615
  const beforeEventData = {
614
616
  key,
@@ -672,7 +674,7 @@ Redis.prototype.addInt = async function(key, num, seconds = 0) {
672
674
  result = n;
673
675
  }
674
676
  }
675
-
677
+
676
678
  // 如果设置了过期时间
677
679
  if (finalSeconds > 0) {
678
680
  if (typeof connection.expire === 'function') {
@@ -2731,7 +2733,7 @@ Redis.prototype.hmset = async function(key, obj) {
2731
2733
  // 检查Redis版本,支持不同的方法
2732
2734
  $.log.debug(
2733
2735
  `[Redis] hmset方法检测到连接对象方法: hSet=${typeof connection.hSet === 'function'}, hmset=${typeof connection.hmset === 'function'}`
2734
- );
2736
+ );
2735
2737
  let success = true;
2736
2738
 
2737
2739
  // 优化策略:对所有Redis版本都使用逐个字段设置的方式,确保兼容性和正确性
@@ -3100,7 +3102,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3100
3102
  $.log.error('[Redis] 参数错误: 键必须是非空字符串');
3101
3103
  return null;
3102
3104
  }
3103
-
3105
+
3104
3106
  // 触发前置事件
3105
3107
  const beforeEventData = {
3106
3108
  key,
@@ -3124,7 +3126,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3124
3126
  const prefixedKey = this.applyPrefix(finalKey);
3125
3127
  // 使用正确的Redis客户端Promise API
3126
3128
  const result = await connection.incr(prefixedKey);
3127
-
3129
+
3128
3130
  // 如果设置了过期时间
3129
3131
  if (finalSeconds > 0) {
3130
3132
  if (typeof connection.expire === 'function') {
@@ -3133,7 +3135,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3133
3135
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3134
3136
  }
3135
3137
  }
3136
-
3138
+
3137
3139
  this.emit('incr_after', {
3138
3140
  key: finalKey,
3139
3141
  seconds: finalSeconds,
@@ -3175,7 +3177,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3175
3177
  $.log.error('[Redis] 参数错误: 键必须是非空字符串');
3176
3178
  return null;
3177
3179
  }
3178
-
3180
+
3179
3181
  // 触发前置事件
3180
3182
  const beforeEventData = {
3181
3183
  key,
@@ -3199,7 +3201,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3199
3201
  const prefixedKey = this.applyPrefix(finalKey);
3200
3202
  // 使用正确的Redis客户端Promise API
3201
3203
  const result = await connection.decr(prefixedKey);
3202
-
3204
+
3203
3205
  // 如果设置了过期时间
3204
3206
  if (finalSeconds > 0) {
3205
3207
  if (typeof connection.expire === 'function') {
@@ -3208,7 +3210,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3208
3210
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3209
3211
  }
3210
3212
  }
3211
-
3213
+
3212
3214
  this.emit('decr_after', {
3213
3215
  key: finalKey,
3214
3216
  seconds: finalSeconds,
@@ -3255,7 +3257,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3255
3257
  $.log.error('[Redis] 参数错误: 增量必须是有效数字');
3256
3258
  return null;
3257
3259
  }
3258
-
3260
+
3259
3261
  // 触发前置事件
3260
3262
  const beforeEventData = {
3261
3263
  key,
@@ -3282,7 +3284,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3282
3284
  // 使用正确的Redis客户端Promise API
3283
3285
  const result = await connection.incrByFloat(prefixedKey, finalIncrement);
3284
3286
  const parsedResult = parseFloat(result);
3285
-
3287
+
3286
3288
  // 如果设置了过期时间
3287
3289
  if (finalSeconds > 0) {
3288
3290
  if (typeof connection.expire === 'function') {
@@ -3291,7 +3293,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3291
3293
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3292
3294
  }
3293
3295
  }
3294
-
3296
+
3295
3297
  this.emit('incrbyfloat_after', {
3296
3298
  key: finalKey,
3297
3299
  increment: finalIncrement,
@@ -3341,7 +3343,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3341
3343
  $.log.error('[Redis] 参数错误: 增量必须是有效整数');
3342
3344
  return null;
3343
3345
  }
3344
-
3346
+
3345
3347
  // 触发前置事件
3346
3348
  const beforeEventData = {
3347
3349
  key,
@@ -3367,7 +3369,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3367
3369
  const prefixedKey = this.applyPrefix(finalKey);
3368
3370
  // 使用正确的Redis客户端Promise API
3369
3371
  const result = await connection.incrBy(prefixedKey, finalIncrement);
3370
-
3372
+
3371
3373
  // 如果设置了过期时间
3372
3374
  if (finalSeconds > 0) {
3373
3375
  if (typeof connection.expire === 'function') {
@@ -3376,7 +3378,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3376
3378
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3377
3379
  }
3378
3380
  }
3379
-
3381
+
3380
3382
  this.emit('incrby_after', {
3381
3383
  key: finalKey,
3382
3384
  increment: finalIncrement,
@@ -3426,7 +3428,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3426
3428
  $.log.error('[Redis] 参数错误: 减量必须是有效整数');
3427
3429
  return null;
3428
3430
  }
3429
-
3431
+
3430
3432
  // 触发前置事件
3431
3433
  const beforeEventData = {
3432
3434
  key,
@@ -3452,7 +3454,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3452
3454
  const prefixedKey = this.applyPrefix(finalKey);
3453
3455
  // 使用正确的Redis客户端Promise API
3454
3456
  const result = await connection.decrBy(prefixedKey, finalDecrement);
3455
-
3457
+
3456
3458
  // 如果设置了过期时间
3457
3459
  if (finalSeconds > 0) {
3458
3460
  if (typeof connection.expire === 'function') {
@@ -3461,7 +3463,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3461
3463
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3462
3464
  }
3463
3465
  }
3464
-
3466
+
3465
3467
  this.emit('decrby_after', {
3466
3468
  key: finalKey,
3467
3469
  decrement: finalDecrement,
@@ -4036,7 +4038,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4036
4038
  $.log.error('[Redis] 参数错误: 成员必须是非空字符串');
4037
4039
  return null;
4038
4040
  }
4039
-
4041
+
4040
4042
  // 触发前置事件
4041
4043
  const beforeEventData = {
4042
4044
  key,
@@ -4064,7 +4066,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4064
4066
  const prefixedKey = this.applyPrefix(finalKey);
4065
4067
  // 使用正确的Redis客户端Promise API
4066
4068
  const result = await connection.zIncrBy(prefixedKey, finalIncrement, finalMember);
4067
-
4069
+
4068
4070
  // 如果设置了过期时间
4069
4071
  if (finalSeconds > 0) {
4070
4072
  if (typeof connection.expire === 'function') {
@@ -4073,7 +4075,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4073
4075
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
4074
4076
  }
4075
4077
  }
4076
-
4078
+
4077
4079
  // 触发后置事件
4078
4080
  this.emit('zincrby_after', {
4079
4081
  key: finalKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_redis",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "高效的Redis客户端封装库,提供连接池管理、数据类型操作、错误处理和批量操作优化",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,7 +27,7 @@
27
27
  "mm_redis",
28
28
  "nodejs"
29
29
  ],
30
- "author": "邱文武",
30
+ "author": "qww",
31
31
  "license": "ISC",
32
32
  "bugs": {
33
33
  "url": "https://gitee.com/qiuwenwu91/mm_redis/issues"
package/test_hmset.js CHANGED
@@ -1,91 +1,91 @@
1
- // 直接测试Redis模块的hmset功能
2
- // 修改导入方式,查看实际导出的内容
3
- const redisModule = require('./index.js');
4
-
5
- // 简单的测试日志函数
6
- const log = (...args) => {
7
- console.log(`[${new Date().toISOString()}]`, ...args);
8
- };
9
-
10
- // 模拟$对象
11
- global.$ = {
12
- log: {
13
- debug: (...args) => log('[DEBUG]', ...args),
14
- info: (...args) => log('[INFO]', ...args),
15
- warn: (...args) => log('[WARN]', ...args),
16
- error: (...args) => log('[ERROR]', ...args)
17
- }
18
- };
19
-
20
- // 查看模块导出内容
21
- log('模块导出内容:', Object.keys(redisModule));
22
- const Redis = redisModule.Redis || redisModule.default || redisModule;
23
- log('使用的Redis构造函数:', Redis);
24
-
25
- // 创建测试函数
26
- async function testRedisHmset() {
27
- log('开始测试Redis模块的hmset方法');
28
-
29
- try {
30
- // 创建Redis实例(使用默认配置)
31
- const redis = new Redis({
32
- host: 'localhost',
33
- port: 6379,
34
- // 其他必要配置
35
- });
36
-
37
- // 初始化
38
- await redis.init();
39
- log('Redis模块初始化完成');
40
-
41
- // 测试数据
42
- const testKey = 'test_hmset_key';
43
- const testObj = {
44
- field1: 'v1',
45
- field2: 'v2',
46
- field3: { nested: 'value' }
47
- };
48
-
49
- // 先删除测试键(如果存在)
50
- await redis.del(testKey);
51
- log(`已清理测试键: ${testKey}`);
52
-
53
- // 调用hmset方法
54
- log(`调用hmset方法,键: ${testKey}, 对象:`, JSON.stringify(testObj));
55
- const result = await redis.hmset(testKey, testObj);
56
- log(`hmset方法返回结果: ${result}`);
57
-
58
- // 验证结果
59
- log(`验证设置结果...`);
60
- const field1Value = await redis.hget(testKey, 'field1');
61
- const field2Value = await redis.hget(testKey, 'field2');
62
- const field3Value = await redis.hget(testKey, 'field3');
63
- const allFields = await redis.hgetall(testKey);
64
-
65
- log(`field1值: ${field1Value}`);
66
- log(`field2值: ${field2Value}`);
67
- log(`field3值: ${field3Value}`);
68
- log(`所有字段:`, JSON.stringify(allFields));
69
-
70
- // 检查结果是否符合预期
71
- const success = field1Value === 'v1' && field2Value === 'v2';
72
- log(`测试${success ? '通过' : '失败'}`);
73
-
74
- // 清理
75
- await redis.del(testKey);
76
- await redis.destroy();
77
- log('测试完成,资源已清理');
78
-
79
- return success;
80
-
81
- } catch (error) {
82
- log('测试过程中发生错误:', error);
83
- return false;
84
- }
85
- }
86
-
87
- // 运行测试
88
- testRedisHmset().then(result => {
89
- log(`测试结束,总体结果: ${result ? '成功' : '失败'}`);
90
- process.exit(result ? 0 : 1);
1
+ // 直接测试Redis模块的hmset功能
2
+ // 修改导入方式,查看实际导出的内容
3
+ const redisModule = require('./index.js');
4
+
5
+ // 简单的测试日志函数
6
+ const log = (...args) => {
7
+ $.log.debug(`[${new Date().toISOString()}]`, ...args);
8
+ };
9
+
10
+ // 模拟$对象
11
+ global.$ = {
12
+ log: {
13
+ debug: (...args) => log('[DEBUG]', ...args),
14
+ info: (...args) => log('[INFO]', ...args),
15
+ warn: (...args) => log('[WARN]', ...args),
16
+ error: (...args) => log('[ERROR]', ...args)
17
+ }
18
+ };
19
+
20
+ // 查看模块导出内容
21
+ log('模块导出内容:', Object.keys(redisModule));
22
+ const Redis = redisModule.Redis || redisModule.default || redisModule;
23
+ log('使用的Redis构造函数:', Redis);
24
+
25
+ // 创建测试函数
26
+ async function testRedisHmset() {
27
+ log('开始测试Redis模块的hmset方法');
28
+
29
+ try {
30
+ // 创建Redis实例(使用默认配置)
31
+ const redis = new Redis({
32
+ host: 'localhost',
33
+ port: 6379,
34
+ // 其他必要配置
35
+ });
36
+
37
+ // 初始化
38
+ await redis.init();
39
+ log('Redis模块初始化完成');
40
+
41
+ // 测试数据
42
+ const testKey = 'test_hmset_key';
43
+ const testObj = {
44
+ field1: 'v1',
45
+ field2: 'v2',
46
+ field3: { nested: 'value' }
47
+ };
48
+
49
+ // 先删除测试键(如果存在)
50
+ await redis.del(testKey);
51
+ log(`已清理测试键: ${testKey}`);
52
+
53
+ // 调用hmset方法
54
+ log(`调用hmset方法,键: ${testKey}, 对象:`, JSON.stringify(testObj));
55
+ const result = await redis.hmset(testKey, testObj);
56
+ log(`hmset方法返回结果: ${result}`);
57
+
58
+ // 验证结果
59
+ log(`验证设置结果...`);
60
+ const field1Value = await redis.hget(testKey, 'field1');
61
+ const field2Value = await redis.hget(testKey, 'field2');
62
+ const field3Value = await redis.hget(testKey, 'field3');
63
+ const allFields = await redis.hgetall(testKey);
64
+
65
+ log(`field1值: ${field1Value}`);
66
+ log(`field2值: ${field2Value}`);
67
+ log(`field3值: ${field3Value}`);
68
+ log(`所有字段:`, JSON.stringify(allFields));
69
+
70
+ // 检查结果是否符合预期
71
+ const success = field1Value === 'v1' && field2Value === 'v2';
72
+ log(`测试${success ? '通过' : '失败'}`);
73
+
74
+ // 清理
75
+ await redis.del(testKey);
76
+ await redis.destroy();
77
+ log('测试完成,资源已清理');
78
+
79
+ return success;
80
+
81
+ } catch (error) {
82
+ log('测试过程中发生错误:', error);
83
+ return false;
84
+ }
85
+ }
86
+
87
+ // 运行测试
88
+ testRedisHmset().then(result => {
89
+ log(`测试结束,总体结果: ${result ? '成功' : '失败'}`);
90
+ process.exit(result ? 0 : 1);
91
91
  });