mm_redis 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +16 -65
  2. package/index.js +36 -115
  3. package/package.json +1 -1
  4. package/test_hmset.js +90 -90
package/README.md CHANGED
@@ -7,8 +7,8 @@ mm_redis是一个高效的Redis客户端封装库,提供了全面的Redis功
7
7
 
8
8
  ## 版本信息
9
9
 
10
- - **当前版本**: 2.0.0
11
- - **更新日期**: 2024-12-22
10
+ - **当前版本**: 2.0.1
11
+ - **更新日期**: 2025-10-14
12
12
  - **作者**: 邱文武
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)
@@ -79,6 +79,11 @@ redis.setConfig('./config.json');
79
79
  ```
80
80
  ## 变更日志
81
81
 
82
+ ### v2.0.0 (2025-10-14)
83
+ - 移除list_push方法,统一使用addForList方法进行列表添加操作
84
+ - 更新文档,将所有list_push引用替换为addForList
85
+ - 更新文档中的API列表和使用示例
86
+
82
87
  ### v1.9.8 (2024-12-22)
83
88
  - 更新文档,完善npm发布所需内容
84
89
  - 添加GitHub仓库链接到文档
@@ -136,7 +141,7 @@ await redis.close();
136
141
  await redis.setForList('users', ['user1', 'user2', 'user3']);
137
142
 
138
143
  // 添加元素到列表
139
- await redis.list_push('users', 'user4');
144
+ await redis.addForList('users', 'user4');
140
145
 
141
146
  // 获取列表
142
147
  const users = await redis.getForList('users');
@@ -291,7 +296,7 @@ redis.on('set_error', (data) => {
291
296
  | setrange | setrange_before | setrange_after | setrange_error |
292
297
  | clear | clear_before | clear_after | clear_error |
293
298
  | setForList | setForList_before | setForList_after | setForList_error |
294
- | list_push | list_push_before | list_push_after | list_push_error |
299
+ | addForList | addForList_before | addForList_after | addForList_error |
295
300
  | clearForList | clearForList_before | clearForList_after | clearForList_error |
296
301
  | mset | mset_before | mset_after | mset_error |
297
302
  | mget | mget_before | mget_after | mget_error |
@@ -335,7 +340,7 @@ redis.on('set_error', (data) => {
335
340
  ### 列表操作
336
341
 
337
342
  - `setForList(key, value, seconds)`: 设置列表
338
- - `list_push(key, value)`: 添加元素到列表
343
+ - `addForList(key, value)`: 添加元素到列表
339
344
  - `getForList(key, start, end)`: 获取列表
340
345
  - `hasForList(key, value)`: 判断列表中是否存在元素
341
346
  - `clearForList(key, value)`: 清空列表或重置列表
@@ -423,66 +428,12 @@ redis.on('set_error', (data) => {
423
428
 
424
429
  ## 版本更新
425
430
 
426
- ### 1.9.5 (2024-12-21)
427
- - 修复hmset和hgetall方法的兼容性问题,确保与Redis客户端v5兼容
428
- - 移除已废弃的hexists、hkeys和hvals方法,优化API设计
429
- - 优化mget和mset方法的实现,确保与Redis v5 API完全兼容
430
- - 更新测试脚本,移除对已废弃方法的调用
431
- - 更新兼容性表格,确保文档与实际API一致
432
- - 修复原生Redis API调用中的错误,提升代码稳定性
433
- - 优化连接池管理,修复潜在的内存泄漏问题
434
-
435
- ### 1.9.4 (2024-12-20)
436
- - 优化了配置引入方式,支持对象配置和文件路径配置两种方式
437
- - 改进了setConfig方法的实现,使用Object.assign替代原来的合并方式
438
- - 修复了连接池相关的警告信息
439
- - 增强了错误处理机制
440
-
441
- ### 1.9.0 (2024-11-20)
442
- - 适配Redis v4 API,确保与最新版Redis客户端兼容
443
- - 重写pipeline方法,使用Redis v4推荐的实现方式
444
- - 优化mget方法,直接使用Redis v4的mGet API
445
- - 修复集合操作(srem)和有序集合操作(zadd/zrange/zscore/zrank)的兼容性问题
446
- - 完善测试用例,确保所有功能正常工作
447
- - 更新依赖版本,优化连接管理
448
- - 完善事件系统,为incr、decr、incrby、decrby方法添加前置事件(_before)支持
449
- - 确保所有非查询方法都有完整的_before、_after、_error事件触发机制
450
- - 更新文档,添加新支持的事件到事件列表中
451
- - 版本号更新和发布日期更新
452
-
453
- ### 1.8.1 (2024-10-09)
454
- - 修复mget方法返回格式问题,确保与原生Redis保持一致
455
- - 修复mset方法参数验证逻辑,优化批量设置功能
456
- - 优化Pipeline实现,确保命令正确执行
457
- - 修复连接池初始化和管理逻辑
458
- - 修复存储机制,确保数据正确持久化
459
- - 准备npm发布,优化文档格式和结构
460
- - 添加npm徽章和下载统计
461
-
462
- ### 1.8.0 (2024-10-08)
463
- - 新增集合(Set)操作支持:sadd、srem、sismember、smembers、scard、sinter、sunion、sdiff
464
- - 新增有序集合(Sorted Set)操作支持:zadd、zrem、zscore、zrank、zrange、zcard、zincrby
465
- - 新增数值操作支持:incr、decr、incrby、decrby、expire
466
- - 更新文档,添加新功能的API说明
467
- - 更新版本号和发布日期
468
-
469
- ### 1.7.0 (2024-10-07)
470
- - 统一事件系统的属性命名规范
471
- - 更新所有方法的事件emit属性,使用统一的key、value、ttl、result等命名
472
- - 增加事件系统的详细文档
473
- - 修复部分方法中的拼写错误
474
-
475
- ### 1.6.0 (2024-01-10)
476
- - 添加完整的连接池管理功能
477
- - 新增Pipeline批量操作优化
478
- - 增加Hash数据类型支持
479
- - 增强错误处理和日志记录
480
- - 优化重连机制,实现指数退避策略
481
-
482
- ### 1.5.0
483
- - 优化连接管理
484
- - 完善错误处理
485
- - 提升代码稳定性
431
+ ### 2.0.0 (2025-10-14)
432
+ - 移除list_push方法,统一使用addForList方法进行列表添加操作
433
+ - 更新文档,将所有list_push引用替换为addForList
434
+ - 更新文档中的API列表和使用示例
435
+ - 优化连接池配置管理,统一配置到this.config.pool中
436
+ - 新增连接池配置参数:min_idle、max_idle、idle_timeout
486
437
 
487
438
  ## 贡献指南
488
439
 
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,18 +205,18 @@ 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
222
  $.log.debug('[Redis] 配置已更新');
@@ -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') {
@@ -1805,87 +1807,6 @@ Redis.prototype.setForList = async function(key, value, seconds) {
1805
1807
  }
1806
1808
  };
1807
1809
 
1808
- /**
1809
- * @description 数组缓存追加对象
1810
- * @param {String} key 键
1811
- * @param {Object|Array} value 值
1812
- * @return {Promise<Number>} 追加后的数组长度
1813
- */
1814
- Redis.prototype.list_push = async function(key, value) {
1815
- try {
1816
- // 触发前置事件,允许修改或阻止操作
1817
- const beforeEventData = {
1818
- key,
1819
- value,
1820
- cancel: false
1821
- };
1822
- this.emit('list_push_before', beforeEventData);
1823
-
1824
- // 如果事件处理器标记为取消,则返回0
1825
- if (beforeEventData.cancel) {
1826
- $.log.debug(`[Redis] list_push操作被事件处理器取消 [${key}]`);
1827
- return 0;
1828
- }
1829
-
1830
- // 使用可能被修改的值
1831
- let {
1832
- key: finalKey,
1833
- value: finalValue
1834
- } = beforeEventData;
1835
- let len = 0;
1836
- let success = true;
1837
-
1838
- const connection = await this._getConnection();
1839
- try {
1840
- finalKey = this.applyPrefix(finalKey);
1841
-
1842
- if (Array.isArray(finalValue)) {
1843
- // 添加数组元素
1844
- if (typeof connection.rPush === 'function') {
1845
- len = await connection.rPush(finalKey, finalValue);
1846
- } else if (typeof connection.execute === 'function') {
1847
- len = await connection.execute('RPUSH', [finalKey, ...finalValue]);
1848
- } else {
1849
- $.log.error('[Redis] 连接对象不支持rPush操作');
1850
- success = false;
1851
- len = 0;
1852
- }
1853
- } else {
1854
- // 添加单个元素
1855
- if (typeof connection.rPush === 'function') {
1856
- len = await connection.rPush(finalKey, finalValue);
1857
- } else if (typeof connection.execute === 'function') {
1858
- len = await connection.execute('RPUSH', [finalKey, finalValue]);
1859
- } else {
1860
- $.log.error('[Redis] 连接对象不支持rPush操作');
1861
- success = false;
1862
- len = 0;
1863
- }
1864
- }
1865
-
1866
- // 触发后置事件
1867
- this.emit('list_push_after', {
1868
- key: finalKey,
1869
- value: finalValue,
1870
- result: len,
1871
- success
1872
- });
1873
- return len;
1874
- } finally {
1875
- this._recycleConnection(connection);
1876
- }
1877
- } catch (err) {
1878
- // 触发错误事件
1879
- this.emit('list_push_error', {
1880
- key,
1881
- value,
1882
- error: err
1883
- });
1884
- $.log.error(`[Redis] 向列表追加元素失败 [${key}]:`, err);
1885
- return 0;
1886
- }
1887
- };
1888
-
1889
1810
  /**
1890
1811
  * @description 数组缓存追加对象
1891
1812
  * @param {String} key 键
@@ -2812,7 +2733,7 @@ Redis.prototype.hmset = async function(key, obj) {
2812
2733
  // 检查Redis版本,支持不同的方法
2813
2734
  $.log.debug(
2814
2735
  `[Redis] hmset方法检测到连接对象方法: hSet=${typeof connection.hSet === 'function'}, hmset=${typeof connection.hmset === 'function'}`
2815
- );
2736
+ );
2816
2737
  let success = true;
2817
2738
 
2818
2739
  // 优化策略:对所有Redis版本都使用逐个字段设置的方式,确保兼容性和正确性
@@ -3181,7 +3102,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3181
3102
  $.log.error('[Redis] 参数错误: 键必须是非空字符串');
3182
3103
  return null;
3183
3104
  }
3184
-
3105
+
3185
3106
  // 触发前置事件
3186
3107
  const beforeEventData = {
3187
3108
  key,
@@ -3205,7 +3126,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3205
3126
  const prefixedKey = this.applyPrefix(finalKey);
3206
3127
  // 使用正确的Redis客户端Promise API
3207
3128
  const result = await connection.incr(prefixedKey);
3208
-
3129
+
3209
3130
  // 如果设置了过期时间
3210
3131
  if (finalSeconds > 0) {
3211
3132
  if (typeof connection.expire === 'function') {
@@ -3214,7 +3135,7 @@ Redis.prototype.incr = async function(key, seconds = 0) {
3214
3135
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3215
3136
  }
3216
3137
  }
3217
-
3138
+
3218
3139
  this.emit('incr_after', {
3219
3140
  key: finalKey,
3220
3141
  seconds: finalSeconds,
@@ -3256,7 +3177,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3256
3177
  $.log.error('[Redis] 参数错误: 键必须是非空字符串');
3257
3178
  return null;
3258
3179
  }
3259
-
3180
+
3260
3181
  // 触发前置事件
3261
3182
  const beforeEventData = {
3262
3183
  key,
@@ -3280,7 +3201,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3280
3201
  const prefixedKey = this.applyPrefix(finalKey);
3281
3202
  // 使用正确的Redis客户端Promise API
3282
3203
  const result = await connection.decr(prefixedKey);
3283
-
3204
+
3284
3205
  // 如果设置了过期时间
3285
3206
  if (finalSeconds > 0) {
3286
3207
  if (typeof connection.expire === 'function') {
@@ -3289,7 +3210,7 @@ Redis.prototype.decr = async function(key, seconds = 0) {
3289
3210
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3290
3211
  }
3291
3212
  }
3292
-
3213
+
3293
3214
  this.emit('decr_after', {
3294
3215
  key: finalKey,
3295
3216
  seconds: finalSeconds,
@@ -3336,7 +3257,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3336
3257
  $.log.error('[Redis] 参数错误: 增量必须是有效数字');
3337
3258
  return null;
3338
3259
  }
3339
-
3260
+
3340
3261
  // 触发前置事件
3341
3262
  const beforeEventData = {
3342
3263
  key,
@@ -3363,7 +3284,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3363
3284
  // 使用正确的Redis客户端Promise API
3364
3285
  const result = await connection.incrByFloat(prefixedKey, finalIncrement);
3365
3286
  const parsedResult = parseFloat(result);
3366
-
3287
+
3367
3288
  // 如果设置了过期时间
3368
3289
  if (finalSeconds > 0) {
3369
3290
  if (typeof connection.expire === 'function') {
@@ -3372,7 +3293,7 @@ Redis.prototype.incrbyfloat = async function(key, increment, seconds = 0) {
3372
3293
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3373
3294
  }
3374
3295
  }
3375
-
3296
+
3376
3297
  this.emit('incrbyfloat_after', {
3377
3298
  key: finalKey,
3378
3299
  increment: finalIncrement,
@@ -3422,7 +3343,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3422
3343
  $.log.error('[Redis] 参数错误: 增量必须是有效整数');
3423
3344
  return null;
3424
3345
  }
3425
-
3346
+
3426
3347
  // 触发前置事件
3427
3348
  const beforeEventData = {
3428
3349
  key,
@@ -3448,7 +3369,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3448
3369
  const prefixedKey = this.applyPrefix(finalKey);
3449
3370
  // 使用正确的Redis客户端Promise API
3450
3371
  const result = await connection.incrBy(prefixedKey, finalIncrement);
3451
-
3372
+
3452
3373
  // 如果设置了过期时间
3453
3374
  if (finalSeconds > 0) {
3454
3375
  if (typeof connection.expire === 'function') {
@@ -3457,7 +3378,7 @@ Redis.prototype.incrby = async function(key, increment, seconds = 0) {
3457
3378
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3458
3379
  }
3459
3380
  }
3460
-
3381
+
3461
3382
  this.emit('incrby_after', {
3462
3383
  key: finalKey,
3463
3384
  increment: finalIncrement,
@@ -3507,7 +3428,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3507
3428
  $.log.error('[Redis] 参数错误: 减量必须是有效整数');
3508
3429
  return null;
3509
3430
  }
3510
-
3431
+
3511
3432
  // 触发前置事件
3512
3433
  const beforeEventData = {
3513
3434
  key,
@@ -3533,7 +3454,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3533
3454
  const prefixedKey = this.applyPrefix(finalKey);
3534
3455
  // 使用正确的Redis客户端Promise API
3535
3456
  const result = await connection.decrBy(prefixedKey, finalDecrement);
3536
-
3457
+
3537
3458
  // 如果设置了过期时间
3538
3459
  if (finalSeconds > 0) {
3539
3460
  if (typeof connection.expire === 'function') {
@@ -3542,7 +3463,7 @@ Redis.prototype.decrby = async function(key, decrement, seconds = 0) {
3542
3463
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
3543
3464
  }
3544
3465
  }
3545
-
3466
+
3546
3467
  this.emit('decrby_after', {
3547
3468
  key: finalKey,
3548
3469
  decrement: finalDecrement,
@@ -4117,7 +4038,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4117
4038
  $.log.error('[Redis] 参数错误: 成员必须是非空字符串');
4118
4039
  return null;
4119
4040
  }
4120
-
4041
+
4121
4042
  // 触发前置事件
4122
4043
  const beforeEventData = {
4123
4044
  key,
@@ -4145,7 +4066,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4145
4066
  const prefixedKey = this.applyPrefix(finalKey);
4146
4067
  // 使用正确的Redis客户端Promise API
4147
4068
  const result = await connection.zIncrBy(prefixedKey, finalIncrement, finalMember);
4148
-
4069
+
4149
4070
  // 如果设置了过期时间
4150
4071
  if (finalSeconds > 0) {
4151
4072
  if (typeof connection.expire === 'function') {
@@ -4154,7 +4075,7 @@ Redis.prototype.zincrby = async function(key, increment, member, seconds = 0) {
4154
4075
  await connection.execute('EXPIRE', [prefixedKey, finalSeconds]);
4155
4076
  }
4156
4077
  }
4157
-
4078
+
4158
4079
  // 触发后置事件
4159
4080
  this.emit('zincrby_after', {
4160
4081
  key: finalKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_redis",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "高效的Redis客户端封装库,提供连接池管理、数据类型操作、错误处理和批量操作优化",
5
5
  "main": "index.js",
6
6
  "scripts": {
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
  });