mm_cachebase 1.9.0 → 1.9.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.
@@ -0,0 +1,68 @@
1
+ const jsdoc = require('eslint-plugin-jsdoc');
2
+
3
+ module.exports = [
4
+ {
5
+ files: ['lib/**/*.js'],
6
+ languageOptions: {
7
+ ecmaVersion: 2020,
8
+ sourceType: 'script'
9
+ },
10
+ plugins: {
11
+ jsdoc: jsdoc
12
+ },
13
+ rules: {
14
+ // JSDoc相关规则 - 设置为error级别强制要求
15
+ 'jsdoc/require-jsdoc': ['error', {
16
+ require: {
17
+ FunctionDeclaration: true,
18
+ MethodDefinition: true,
19
+ ClassDeclaration: true,
20
+ ArrowFunctionExpression: true
21
+ }
22
+ }],
23
+ 'jsdoc/check-param-names': 'off',
24
+ 'jsdoc/check-tag-names': 'off',
25
+ 'jsdoc/check-types': 'off',
26
+ 'jsdoc/require-param': 'off',
27
+ 'jsdoc/require-param-description': 'off',
28
+ 'jsdoc/require-returns': 'off',
29
+ 'jsdoc/require-returns-description': 'off',
30
+
31
+ // 基础语法规则
32
+ 'no-unused-vars': ['error', { args: 'none', vars: 'all' }],
33
+ 'no-console': 'warn',
34
+
35
+ // 代码风格规则
36
+ 'indent': ['error', 2],
37
+ 'quotes': ['error', 'single'],
38
+ 'semi': ['error', 'always'],
39
+ 'comma-dangle': ['error', 'never'],
40
+ 'no-trailing-spaces': 'error',
41
+ 'eol-last': 'error',
42
+ 'max-len': ['error', { code: 100 }],
43
+ 'max-lines-per-function': ['error', { max: 40 }],
44
+ 'complexity': ['error', 12],
45
+ 'max-depth': ['error', 4],
46
+ 'max-params': ['error', 4],
47
+
48
+ // 命名规则
49
+ 'id-length': ['error', {
50
+ min: 1,
51
+ max: 20,
52
+ properties: 'always'
53
+ }]
54
+ }
55
+ },
56
+ {
57
+ files: ['test/**/*.js'],
58
+ languageOptions: {
59
+ ecmaVersion: 2020,
60
+ sourceType: 'script'
61
+ },
62
+ rules: {
63
+ // 测试文件中允许使用console
64
+ 'no-console': 'off',
65
+ 'no-unused-vars': 'off'
66
+ }
67
+ }
68
+ ];
package/index.js CHANGED
@@ -65,9 +65,9 @@ CacheBase.prototype.init = async function() {
65
65
  try {
66
66
  await this._initService();
67
67
  this.is_inited = true;
68
- this.logger('info', 'CacheBase service initialized successfully');
68
+ this.log('info', 'CacheBase service initialized successfully');
69
69
  } catch (error) {
70
- this.logger('error', 'Failed to initialize CacheBase service:', error);
70
+ this.log('error', 'Failed to initialize CacheBase service:', error);
71
71
  }
72
72
  };
73
73
 
@@ -105,7 +105,7 @@ CacheBase.prototype._initService = async function() {
105
105
  // 设置初始化完成状态
106
106
  this.is_inited = true;
107
107
  } catch (error) {
108
- this.logger('error', 'Failed to initialize service:', error);
108
+ this.log('error', 'Failed to initialize service:', error);
109
109
  }
110
110
  };
111
111
 
@@ -131,19 +131,19 @@ CacheBase.prototype._loadData = function() {
131
131
  if (this.config.persist && this.config.file_path) {
132
132
  // 使用字符串的原型函数获取完整路径
133
133
  const file_path = this.config.file_path.fullname();
134
- $.log.info(`[CacheBase] [_loadData] 尝试加载文件: ${file_path}`);
134
+ this.log('info', `[_loadData] 尝试加载文件: ${file_path}`);
135
135
 
136
136
  if (file_path.hasFile()) {
137
- $.log.info(`[CacheBase] [_loadData] 文件存在,开始读取`);
137
+ this.log('info', `[_loadData] 文件存在,开始读取`);
138
138
  // 使用mm_expand的原型函数读取JSON文件
139
139
  const parsed_data = file_path.loadJson();
140
- $.log.info(`[CacheBase] [_loadData] 文件读取成功,解析后的数据:`, Object.keys(parsed_data || {}));
140
+ this.log('info', `[_loadData] 文件读取成功,解析后的数据:`, Object.keys(parsed_data || {}));
141
141
 
142
142
  if (parsed_data && parsed_data.data) {
143
143
  this._data = parsed_data.data || {};
144
- $.log.info(`[CacheBase] [_loadData] 数据加载成功,键数量:`, Object.keys(this._data).length);
144
+ this.log('info', `[_loadData] 数据加载成功,键数量:`, Object.keys(this._data).length);
145
145
  } else {
146
- $.log.warn(`[CacheBase] [_loadData] 解析后的数据中没有data字段`);
146
+ this.log('warn', `[_loadData] 解析后的数据中没有data字段`);
147
147
  }
148
148
 
149
149
  if (parsed_data && parsed_data.expires) {
@@ -154,13 +154,13 @@ CacheBase.prototype._loadData = function() {
154
154
  this._types = parsed_data.types || {};
155
155
  }
156
156
  } else {
157
- $.log.info(`[CacheBase] [_loadData] 文件不存在: ${file_path}`);
157
+ this.log('info', `[_loadData] 文件不存在: ${file_path}`);
158
158
  }
159
159
  } else {
160
- $.log.info(`[CacheBase] [_loadData] 持久化未启用或文件路径未设置`);
160
+ this.log('info', `[_loadData] 持久化未启用或文件路径未设置`);
161
161
  }
162
162
  } catch (error) {
163
- this.logger('error', '加载数据失败:', error);
163
+ this.log('error', '加载数据失败:', error);
164
164
  }
165
165
  };
166
166
 
@@ -170,7 +170,7 @@ CacheBase.prototype._loadData = function() {
170
170
  CacheBase.prototype.save = function() {
171
171
  // 检查服务状态
172
172
  if (!this.is_inited || this.is_destroyed) {
173
- this.logger('error', 'Service is not initialized or has been destroyed');
173
+ this.log('error', 'Service is not initialized or has been destroyed');
174
174
  return;
175
175
  }
176
176
 
@@ -179,7 +179,7 @@ CacheBase.prototype.save = function() {
179
179
  this._saveData();
180
180
  this.emit('save_after');
181
181
  } catch (error) {
182
- this.logger('error', 'Failed to save data:', error);
182
+ this.log('error', 'Failed to save data:', error);
183
183
  }
184
184
  };
185
185
 
@@ -207,7 +207,7 @@ CacheBase.prototype._saveData = function() {
207
207
  file_path.saveJson(data_to_save, null, 2);
208
208
  }
209
209
  } catch (error) {
210
- this.logger('error', 'Failed to save data:', error);
210
+ this.log('error', 'Failed to save data:', error);
211
211
  }
212
212
  };
213
213
 
@@ -250,7 +250,7 @@ CacheBase.prototype.destroy = async function() {
250
250
  // 触发自定义销毁事件
251
251
  this.emit('destroy_after');
252
252
  } catch (error) {
253
- this.logger('error', 'Failed to destroy service:', error);
253
+ this.log('error', 'Failed to destroy service:', error);
254
254
  }
255
255
  };
256
256
 
@@ -264,7 +264,7 @@ CacheBase.prototype.destroy = async function() {
264
264
  CacheBase.prototype.set = function(key, value, ttl) {
265
265
  // 检查服务状态
266
266
  if (!this.is_inited || this.is_destroyed) {
267
- this.logger('error', 'Service is not initialized or has been destroyed');
267
+ this.log('error', 'Service is not initialized or has been destroyed');
268
268
  return false;
269
269
  }
270
270
 
@@ -312,7 +312,7 @@ CacheBase.prototype.set = function(key, value, ttl) {
312
312
 
313
313
  return true;
314
314
  } catch (error) {
315
- this.logger('error', `Failed to set key ${key}:`, error);
315
+ this.log('error', `Failed to set key ${key}:`, error);
316
316
  return false;
317
317
  }
318
318
  };
@@ -325,7 +325,7 @@ CacheBase.prototype.set = function(key, value, ttl) {
325
325
  CacheBase.prototype.get = function(key) {
326
326
  // 检查服务状态
327
327
  if (!this.is_inited || this.is_destroyed) {
328
- this.logger('error', 'Service is not initialized or has been destroyed');
328
+ this.log('error', 'Service is not initialized or has been destroyed');
329
329
  return null;
330
330
  }
331
331
 
@@ -338,7 +338,7 @@ CacheBase.prototype.get = function(key) {
338
338
 
339
339
  return this._data[key];
340
340
  } catch (error) {
341
- this.logger('error', `Failed to get key ${key}:`, error);
341
+ this.log('error', `Failed to get key ${key}:`, error);
342
342
  return null;
343
343
  }
344
344
  };
@@ -426,7 +426,7 @@ CacheBase.prototype.del = function(key) {
426
426
 
427
427
  return success;
428
428
  } catch (error) {
429
- this.logger('error', 'Failed to del:', error);
429
+ this.log('error', 'Failed to del:', error);
430
430
  return false;
431
431
  }
432
432
  };
@@ -451,7 +451,7 @@ CacheBase.prototype.delBatch = function(keys) {
451
451
 
452
452
  return deletedCount;
453
453
  } catch (error) {
454
- this.logger('error', 'Failed to delBatch:', error);
454
+ this.log('error', 'Failed to delBatch:', error);
455
455
  return 0;
456
456
  }
457
457
  };
@@ -470,7 +470,7 @@ CacheBase.prototype.exists = function(key) {
470
470
 
471
471
  return key in this._data;
472
472
  } catch (error) {
473
- this.logger('error', `Failed to check key ${key}:`, error);
473
+ this.log('error', `Failed to check key ${key}:`, error);
474
474
  return false;
475
475
  }
476
476
  };
@@ -496,7 +496,7 @@ CacheBase.prototype.clearExpire = function() {
496
496
  }
497
497
  }
498
498
  } catch (error) {
499
- this.logger('error', 'Failed to clear expired data:', error);
499
+ this.log('error', 'Failed to clear expired data:', error);
500
500
  }
501
501
  };
502
502
 
@@ -588,7 +588,7 @@ CacheBase.prototype.expire = function(key, seconds) {
588
588
 
589
589
  return true;
590
590
  } catch (error) {
591
- this.logger('error', `Failed to set expire for key ${key}:`, error);
591
+ this.log('error', `Failed to set expire for key ${key}:`, error);
592
592
  return false;
593
593
  }
594
594
  };
@@ -645,7 +645,7 @@ CacheBase.prototype.hset = function(key, field, value) {
645
645
 
646
646
  return is_new_field ? 1 : 0; // 返回1表示新增,0表示更新
647
647
  } catch (error) {
648
- this.logger('error', `Failed to hset for key ${key}:`, error);
648
+ this.log('error', `Failed to hset for key ${key}:`, error);
649
649
  return 0;
650
650
  }
651
651
  };
@@ -676,7 +676,7 @@ CacheBase.prototype.hget = function(key, field) {
676
676
 
677
677
  return this._data[key][field];
678
678
  } catch (error) {
679
- this.logger('error', `Failed to hget for key ${key}:`, error);
679
+ this.log('error', `Failed to hget for key ${key}:`, error);
680
680
  }
681
681
  };
682
682
 
@@ -716,7 +716,7 @@ CacheBase.prototype.hgetall = function(key) {
716
716
  ...this._data[key]
717
717
  };
718
718
  } catch (error) {
719
- this.logger('error', `Failed to hgetall for key ${key}:`, error);
719
+ this.log('error', `Failed to hgetall for key ${key}:`, error);
720
720
  return {};
721
721
  }
722
722
  };
@@ -800,7 +800,7 @@ CacheBase.prototype.hdel = function(key, field) {
800
800
  field,
801
801
  error
802
802
  });
803
- this.logger('error', 'Failed to hdel:', error);
803
+ this.log('error', 'Failed to hdel:', error);
804
804
  return false;
805
805
  }
806
806
  };
@@ -859,7 +859,7 @@ CacheBase.prototype.incr = function(key, seconds = 0) {
859
859
  try {
860
860
  // 参数验证
861
861
  if (!key || typeof key !== 'string') {
862
- $.log.error('[cachebase] incr参数错误: key必须是非空字符串');
862
+ this.log('error', 'incr参数错误: key必须是非空字符串');
863
863
  return 0;
864
864
  }
865
865
 
@@ -906,7 +906,7 @@ CacheBase.prototype.incr = function(key, seconds = 0) {
906
906
  ttl: seconds,
907
907
  error
908
908
  });
909
- $.log.error('[cachebase] incr操作失败:', error);
909
+ this.log('error', 'incr操作失败:', error);
910
910
  return 0;
911
911
  }
912
912
  };
@@ -959,7 +959,7 @@ CacheBase.prototype.decr = function(key, seconds = 0) {
959
959
  try {
960
960
  // 参数验证
961
961
  if (!key || typeof key !== 'string') {
962
- $.log.error('[cachebase] decr参数错误: key必须是非空字符串');
962
+ this.log('error', 'decr参数错误: key必须是非空字符串');
963
963
  return 0;
964
964
  }
965
965
 
@@ -1006,7 +1006,7 @@ CacheBase.prototype.decr = function(key, seconds = 0) {
1006
1006
  ttl: seconds,
1007
1007
  error
1008
1008
  });
1009
- $.log.error('[cachebase] decr操作失败:', error);
1009
+ this.log('error', 'decr操作失败:', error);
1010
1010
  return 0;
1011
1011
  }
1012
1012
  };
@@ -1068,7 +1068,7 @@ CacheBase.prototype.addInt = function(key, value, seconds = 0) {
1068
1068
  ttl: seconds,
1069
1069
  error
1070
1070
  });
1071
- $.log.error('[cachebase] addInt操作失败:', error);
1071
+ this.log('error', 'addInt操作失败:', error);
1072
1072
  return 0;
1073
1073
  }
1074
1074
  };
@@ -1107,7 +1107,7 @@ CacheBase.prototype.append = function(key, append) {
1107
1107
 
1108
1108
  return current.length;
1109
1109
  } catch (error) {
1110
- this.logger('error', `Failed to append for key ${key}:`, error);
1110
+ this.log('error', `Failed to append for key ${key}:`, error);
1111
1111
  return null;
1112
1112
  }
1113
1113
  };
@@ -1142,7 +1142,7 @@ CacheBase.prototype.strlen = function(key) {
1142
1142
 
1143
1143
  return this._data[key].length;
1144
1144
  } catch (error) {
1145
- this.logger('error', `Failed to strlen for key ${key}:`, error);
1145
+ this.log('error', `Failed to strlen for key ${key}:`, error);
1146
1146
  return 0;
1147
1147
  }
1148
1148
  };
@@ -1175,7 +1175,7 @@ CacheBase.prototype.flushdb = function() {
1175
1175
 
1176
1176
  return true;
1177
1177
  } catch (error) {
1178
- this.logger('error', 'Failed to flushdb:', error);
1178
+ this.log('error', 'Failed to flushdb:', error);
1179
1179
  return false;
1180
1180
  }
1181
1181
  };
@@ -1198,7 +1198,7 @@ CacheBase.prototype.keys = function(pattern) {
1198
1198
  }
1199
1199
  return result;
1200
1200
  } catch (error) {
1201
- this.logger('error', 'Failed to get keys:', error);
1201
+ this.log('error', 'Failed to get keys:', error);
1202
1202
  return [];
1203
1203
  }
1204
1204
  };
@@ -1239,7 +1239,7 @@ CacheBase.prototype.dbsize = function() {
1239
1239
  }
1240
1240
  return count;
1241
1241
  } catch (error) {
1242
- this.logger('error', 'Failed to get dbsize:', error);
1242
+ this.log('error', 'Failed to get dbsize:', error);
1243
1243
  return 0;
1244
1244
  }
1245
1245
  };
@@ -1255,7 +1255,7 @@ CacheBase.prototype.incrbyfloat = function(key, value = 1.0, seconds = 0) {
1255
1255
  try {
1256
1256
  // 参数验证
1257
1257
  if (!key || typeof key !== 'string' || typeof value !== 'number') {
1258
- $.log.error('[cachebase] incrbyfloat参数错误: key必须是非空字符串,value必须是数字');
1258
+ this.log('error', 'incrbyfloat参数错误: key必须是非空字符串,value必须是数字');
1259
1259
  return 0;
1260
1260
  }
1261
1261
 
@@ -1305,7 +1305,7 @@ CacheBase.prototype.incrbyfloat = function(key, value = 1.0, seconds = 0) {
1305
1305
  ttl: seconds,
1306
1306
  error
1307
1307
  });
1308
- $.log.error('[cachebase] incrbyfloat操作失败:', error);
1308
+ this.log('error', 'incrbyfloat操作失败:', error);
1309
1309
  return 0;
1310
1310
  }
1311
1311
  };
@@ -1332,7 +1332,7 @@ CacheBase.prototype.getset = function(key, value, expire) {
1332
1332
 
1333
1333
  return oldValue;
1334
1334
  } catch (error) {
1335
- this.logger('error', 'Failed to getset:', error);
1335
+ this.log('error', 'Failed to getset:', error);
1336
1336
  return null;
1337
1337
  }
1338
1338
  };
@@ -1348,8 +1348,7 @@ CacheBase.prototype.add = function(key, value, maxAge = 0) {
1348
1348
  try {
1349
1349
  // 参数验证
1350
1350
  if (!key || typeof key !== 'string') {
1351
- $.log.error('[cachebase] add参数错误: key必须是非空字符串');
1352
- return false;
1351
+ throw new TypeError('add参数错误: key必须是非空字符串');
1353
1352
  }
1354
1353
 
1355
1354
  // 触发前置事件
@@ -1362,7 +1361,7 @@ CacheBase.prototype.add = function(key, value, maxAge = 0) {
1362
1361
  this.emit('add_before', beforeEventData);
1363
1362
 
1364
1363
  if (beforeEventData.cancel) {
1365
- $.log.debug(`[cachebase] add操作被事件处理器取消 [${key}]`);
1364
+ this.log('debug', `add操作被事件处理器取消 [${key}]`);
1366
1365
  return false;
1367
1366
  }
1368
1367
 
@@ -1403,8 +1402,8 @@ CacheBase.prototype.add = function(key, value, maxAge = 0) {
1403
1402
  success: true
1404
1403
  });
1405
1404
  return true;
1406
- } catch (error) {
1407
- $.log.error(`[cachebase] add操作失败 [${key}]:`, error);
1405
+ } catch (error) {
1406
+ this.log('error', `add操作失败 [${key}]:`, error);
1408
1407
  this.emit('add_error', {
1409
1408
  key,
1410
1409
  value,
@@ -1426,8 +1425,7 @@ CacheBase.prototype.addFloat = function(key, num, seconds = 0) {
1426
1425
  try {
1427
1426
  // 参数验证
1428
1427
  if (!key || typeof key !== 'string' || typeof num !== 'number') {
1429
- $.log.error('[cachebase] addFloat参数错误: key必须是非空字符串,num必须是数字');
1430
- return 0;
1428
+ throw new TypeError('addFloat参数错误: key必须是非空字符串,num必须是数字');
1431
1429
  }
1432
1430
 
1433
1431
  this.emit('addFloat_before', {
@@ -1469,7 +1467,7 @@ CacheBase.prototype.addFloat = function(key, num, seconds = 0) {
1469
1467
  ttl: seconds,
1470
1468
  error
1471
1469
  });
1472
- $.log.error('[cachebase] addFloat操作失败:', error);
1470
+ this.log('error', 'addFloat操作失败:', error);
1473
1471
  return 0;
1474
1472
  }
1475
1473
  };
@@ -1485,8 +1483,7 @@ CacheBase.prototype.addStr = function(key, value, seconds = 0) {
1485
1483
  try {
1486
1484
  // 参数验证
1487
1485
  if (!key || typeof key !== 'string' || typeof value !== 'string') {
1488
- $.log.error('[cachebase] addStr参数错误: key和value必须是非空字符串');
1489
- return 0;
1486
+ throw new TypeError('addStr参数错误: key和value必须是非空字符串');
1490
1487
  }
1491
1488
 
1492
1489
  this.emit('addStr_before', {
@@ -1530,7 +1527,7 @@ CacheBase.prototype.addStr = function(key, value, seconds = 0) {
1530
1527
  ttl: seconds,
1531
1528
  error
1532
1529
  });
1533
- $.log.error('[cachebase] addStr操作失败:', error);
1530
+ this.log('error', 'addStr操作失败:', error);
1534
1531
  return 0;
1535
1532
  }
1536
1533
  };
@@ -1544,23 +1541,12 @@ CacheBase.prototype.mset = function(...args) {
1544
1541
  try {
1545
1542
  // 参数验证 - 特别处理null参数情况
1546
1543
  if (args.length === 1 && args[0] === null) {
1547
- const error = new Error('mset参数必须是对象');
1548
- this.emit('mset_error', {
1549
- args,
1550
- error
1551
- });
1552
- throw error;
1544
+ throw new TypeError('参数数量必须为偶数');
1553
1545
  }
1554
1546
 
1555
1547
  // 常规参数验证
1556
1548
  if (args.length === 0 || args.length % 2 !== 0) {
1557
- $.log.error('[CacheBase] [mset] 参数错误: 参数数量必须为偶数');
1558
- const error = new Error('参数数量必须为偶数');
1559
- this.emit('mset_error', {
1560
- args,
1561
- error
1562
- });
1563
- throw error;
1549
+ throw new TypeError('参数数量必须为偶数');
1564
1550
  }
1565
1551
 
1566
1552
  // 将参数列表转换为键值对对象
@@ -1570,13 +1556,7 @@ CacheBase.prototype.mset = function(...args) {
1570
1556
  const value = args[i + 1];
1571
1557
 
1572
1558
  if (typeof key !== 'string' || !key) {
1573
- $.log.error(`[CacheBase] [mset] 参数错误: 键 ${key} 必须是非空字符串`);
1574
- const error = new Error('键必须是非空字符串');
1575
- this.emit('mset_error', {
1576
- args,
1577
- error
1578
- });
1579
- throw error;
1559
+ throw new TypeError('键必须是非空字符串');
1580
1560
  }
1581
1561
 
1582
1562
  keyValuePairs[key] = value;
@@ -1590,7 +1570,7 @@ CacheBase.prototype.mset = function(...args) {
1590
1570
  this.emit('mset_before', beforeEventData);
1591
1571
 
1592
1572
  if (beforeEventData.cancel) {
1593
- $.log.debug('[CacheBase] [mset] 操作被事件处理器取消');
1573
+ this.log('debug', '[mset] 操作被事件处理器取消');
1594
1574
  return false;
1595
1575
  }
1596
1576
 
@@ -1626,7 +1606,7 @@ CacheBase.prototype.mset = function(...args) {
1626
1606
  args,
1627
1607
  error
1628
1608
  });
1629
- this.logger('error', 'Failed to mset:', error);
1609
+ this.log('error', 'Failed to mset:', error);
1630
1610
  return false;
1631
1611
  }
1632
1612
  };
@@ -1699,7 +1679,7 @@ CacheBase.prototype.ttl = function(key, seconds) {
1699
1679
  const errorMsg = arguments.length > 1 && seconds !== undefined
1700
1680
  ? `Failed to set ttl for key ${key}: ${error.message}`
1701
1681
  : `Failed to get ttl for key ${key}: ${error.message}`;
1702
- this.logger('error', errorMsg, error);
1682
+ this.log('error', errorMsg, error);
1703
1683
  return arguments.length > 1 && seconds !== undefined ? false : -2;
1704
1684
  }
1705
1685
  };
@@ -1751,7 +1731,7 @@ CacheBase.prototype.persist = function(key) {
1751
1731
 
1752
1732
  return false; // 键没有设置过期时间
1753
1733
  } catch (error) {
1754
- this.logger('error', `Failed to persist key ${key}:`, error);
1734
+ this.log('error', `Failed to persist key ${key}:`, error);
1755
1735
  return false;
1756
1736
  }
1757
1737
  };
@@ -1780,7 +1760,7 @@ CacheBase.prototype.mget = function(keys) {
1780
1760
 
1781
1761
  return results;
1782
1762
  } catch (error) {
1783
- this.logger('error', 'Failed to mget:', error);
1763
+ this.log('error', 'Failed to mget:', error);
1784
1764
  return [];
1785
1765
  }
1786
1766
  };
@@ -1809,7 +1789,7 @@ CacheBase.prototype.dispose = function() {
1809
1789
 
1810
1790
  return true;
1811
1791
  } catch (error) {
1812
- this.logger('error', 'Failed to dispose:', error);
1792
+ this.log('error', 'Failed to dispose:', error);
1813
1793
  return false;
1814
1794
  }
1815
1795
  };
@@ -1878,7 +1858,7 @@ CacheBase.prototype.setForList = async function(key, value, maxAge = 0) {
1878
1858
  this.emit('setForList_before', beforeEventData);
1879
1859
 
1880
1860
  if (beforeEventData.cancel) {
1881
- $.log.debug(`[CacheBase] setForList操作被事件处理器取消 [${key}]`);
1861
+ this.log('debug', `setForList操作被事件处理器取消 [${key}]`);
1882
1862
  return 0;
1883
1863
  }
1884
1864
 
@@ -1921,7 +1901,7 @@ CacheBase.prototype.setForList = async function(key, value, maxAge = 0) {
1921
1901
  maxAge,
1922
1902
  error
1923
1903
  });
1924
- $.log.error('[CacheBase] setForList操作失败:', error);
1904
+ this.log('error', 'setForList操作失败:', error);
1925
1905
  return 0;
1926
1906
  }
1927
1907
  };
@@ -1953,7 +1933,7 @@ CacheBase.prototype.addForList = async function(key, value, maxAge = 0) {
1953
1933
  this.emit('addForList_before', beforeEventData);
1954
1934
 
1955
1935
  if (beforeEventData.cancel) {
1956
- $.log.debug(`[CacheBase] addForList操作被事件处理器取消 [${key}]`);
1936
+ this.log('debug', `addForList操作被事件处理器取消 [${key}]`);
1957
1937
  return 0;
1958
1938
  }
1959
1939
 
@@ -1998,7 +1978,7 @@ CacheBase.prototype.addForList = async function(key, value, maxAge = 0) {
1998
1978
  maxAge,
1999
1979
  error
2000
1980
  });
2001
- $.log.error('[CacheBase] addForList操作失败:', error);
1981
+ this.log('error', 'addForList操作失败:', error);
2002
1982
  return 0;
2003
1983
  }
2004
1984
  };
@@ -2028,7 +2008,7 @@ CacheBase.prototype.hasForList = async function(key, value) {
2028
2008
  this.emit('hasForList_before', beforeEventData);
2029
2009
 
2030
2010
  if (beforeEventData.cancel) {
2031
- $.log.debug(`[CacheBase] hasForList操作被事件处理器取消 [${key}]`);
2011
+ this.log('debug', `hasForList操作被事件处理器取消 [${key}]`);
2032
2012
  return false;
2033
2013
  }
2034
2014
 
@@ -2062,7 +2042,7 @@ CacheBase.prototype.hasForList = async function(key, value) {
2062
2042
  value,
2063
2043
  error
2064
2044
  });
2065
- $.log.error('[CacheBase] hasForList操作失败:', error);
2045
+ this.log('error', 'hasForList操作失败:', error);
2066
2046
  return false;
2067
2047
  }
2068
2048
  };
@@ -2091,7 +2071,7 @@ CacheBase.prototype.getForList = async function(key, start = 0, stop = -1) {
2091
2071
  this.emit('getForList_before', beforeEventData);
2092
2072
 
2093
2073
  if (beforeEventData.cancel) {
2094
- $.log.debug(`[CacheBase] getForList操作被事件处理器取消 [${key}]`);
2074
+ this.log('debug', `getForList操作被事件处理器取消 [${key}]`);
2095
2075
  return [];
2096
2076
  }
2097
2077
 
@@ -2135,7 +2115,7 @@ CacheBase.prototype.getForList = async function(key, start = 0, stop = -1) {
2135
2115
  stop,
2136
2116
  error
2137
2117
  });
2138
- $.log.error('[CacheBase] getForList操作失败:', error);
2118
+ this.log('error', 'getForList操作失败:', error);
2139
2119
  return [];
2140
2120
  }
2141
2121
  };
@@ -2160,7 +2140,7 @@ CacheBase.prototype.clearForList = async function(key) {
2160
2140
  this.emit('clearForList_before', beforeEventData);
2161
2141
 
2162
2142
  if (beforeEventData.cancel) {
2163
- $.log.debug(`[CacheBase] clearForList操作被事件处理器取消 [${key}]`);
2143
+ this.log('debug', `clearForList操作被事件处理器取消 [${key}]`);
2164
2144
  return false;
2165
2145
  }
2166
2146
 
@@ -2208,7 +2188,7 @@ CacheBase.prototype.clearForList = async function(key) {
2208
2188
  key,
2209
2189
  error
2210
2190
  });
2211
- $.log.error('[CacheBase] clearForList操作失败:', error);
2191
+ this.log('error', 'clearForList操作失败:', error);
2212
2192
  return false;
2213
2193
  }
2214
2194
  };
@@ -2272,7 +2252,7 @@ CacheBase.prototype.hmset = function(key, obj) {
2272
2252
  obj,
2273
2253
  error
2274
2254
  });
2275
- this.logger('error', 'Failed to hmset:', error);
2255
+ this.log('error', 'Failed to hmset:', error);
2276
2256
  resolve(false);
2277
2257
  }
2278
2258
  });
@@ -2319,7 +2299,7 @@ CacheBase.prototype.hmget = function(key, fields) {
2319
2299
  fields,
2320
2300
  error
2321
2301
  });
2322
- this.logger('error', 'Failed to hmget:', error);
2302
+ this.log('error', 'Failed to hmget:', error);
2323
2303
  resolve([]);
2324
2304
  }
2325
2305
  });
@@ -2358,7 +2338,7 @@ CacheBase.prototype.hlen = function(key) {
2358
2338
  key,
2359
2339
  error
2360
2340
  });
2361
- this.logger('error', 'Failed to hlen:', error);
2341
+ this.log('error', 'Failed to hlen:', error);
2362
2342
  resolve(0);
2363
2343
  }
2364
2344
  });
@@ -2366,7 +2346,7 @@ CacheBase.prototype.hlen = function(key) {
2366
2346
 
2367
2347
  // 导出类
2368
2348
  exports.CacheBase = CacheBase;
2369
- exports.cachebase_admin = function(scope, dir) {
2349
+ exports.cachebaseAdmin = function(scope, dir) {
2370
2350
  if (!global.$) {
2371
2351
  global.$ = {};
2372
2352
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_cachebase",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "一个轻量级的本地缓存类库,提供与Redis兼容的API接口,支持过期时间设置、哈希表操作、数值操作、字符串操作等功能,以及完整的事件通知系统和多文件持久化功能。",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -39,6 +39,6 @@
39
39
  },
40
40
  "homepage": "https://gitee.com/qiuwenwu91/mm_cachebase#readme",
41
41
  "dependencies": {
42
- "mm_expand": "^1.9.7"
42
+ "mm_expand": "^2.0.0"
43
43
  }
44
44
  }
package/test.js CHANGED
@@ -1,4 +1,4 @@
1
- const { CacheBase, cachebase_admin } = require('./index.js');
1
+ const { CacheBase, cachebaseAdmin } = require('./index.js');
2
2
 
3
3
  class TestRunner {
4
4
  constructor() {
@@ -48,9 +48,9 @@ async function test() {
48
48
 
49
49
  // 测试基础方法
50
50
  $.log.debug('[CacheBase] \n========== 测试基础方法 ==========');
51
- const cache = new CacheBase();
52
51
 
53
52
  await runner.runTest('set和get方法', async () => {
53
+ const cache = new CacheBase({ file_path: './test_cache/test_set_get.cache.json' });
54
54
  await cache.set('key1', 'value1');
55
55
  const value = await cache.get('key1');
56
56
  if (value !== 'value1') {
@@ -59,6 +59,7 @@ async function test() {
59
59
  });
60
60
 
61
61
  await runner.runTest('del方法', async () => {
62
+ const cache = new CacheBase({ file_path: './test_cache/test_del.cache.json' });
62
63
  await cache.set('key2', 'value2');
63
64
  const deleted = await cache.del('key2');
64
65
  if (deleted !== true) {
@@ -71,6 +72,7 @@ async function test() {
71
72
  });
72
73
 
73
74
  await runner.runTest('exists/has方法', async () => {
75
+ const cache = new CacheBase({ file_path: './test_cache/test_exists_has.cache.json' });
74
76
  await cache.set('key3', 'value3');
75
77
  const exists = await cache.exists('key3');
76
78
  if (exists !== true) {
@@ -83,7 +85,10 @@ async function test() {
83
85
  });
84
86
 
85
87
  await runner.runTest('keys方法', async () => {
86
- await cache.clear();
88
+ const cache = new CacheBase({
89
+ persist: true,
90
+ file_path: './test_cache/test_keys.cache.json'
91
+ });
87
92
  await cache.set('key4', 'value4');
88
93
  await cache.set('key5', 'value5');
89
94
  const keys = await cache.keys();
@@ -97,6 +102,12 @@ async function test() {
97
102
  });
98
103
 
99
104
  await runner.runTest('dbsize方法', async () => {
105
+ const cache = new CacheBase({
106
+ persist: true,
107
+ file_path: './test_cache/test_dbsize.cache.json'
108
+ });
109
+ await cache.set('key4', 'value4');
110
+ await cache.set('key5', 'value5');
100
111
  const size = await cache.dbsize();
101
112
  if (size !== 2) {
102
113
  throw new Error(`dbsize失败,预期: 2, 实际: ${size}`);
@@ -104,6 +115,8 @@ async function test() {
104
115
  });
105
116
 
106
117
  await runner.runTest('getset方法', async () => {
118
+ const cache = new CacheBase({ file_path: './test_cache/test_getset.cache.json' });
119
+ await cache.set('key4', 'value4');
107
120
  const oldValue = await cache.getset('key4', 'newvalue4');
108
121
  if (oldValue !== 'value4') {
109
122
  throw new Error(`getset失败,预期旧值: value4, 实际: ${oldValue}`);
@@ -115,6 +128,11 @@ async function test() {
115
128
  });
116
129
 
117
130
  await runner.runTest('flushdb/clear方法', async () => {
131
+ const cache = new CacheBase({
132
+ persist: true,
133
+ file_path: './test_cache/test_flushdb.cache.json'
134
+ });
135
+ await cache.set('key6', 'value6');
118
136
  let result = await cache.clear();
119
137
  if (result !== true) {
120
138
  throw new Error(`flushdb失败,预期: true, 实际: ${result}`);
@@ -125,7 +143,7 @@ async function test() {
125
143
  }
126
144
 
127
145
  // 测试clear方法(兼容)
128
- await cache.set('key6', 'value6');
146
+ await cache.set('key7', 'value7');
129
147
  result = await cache.clear();
130
148
  if (result !== true) {
131
149
  throw new Error(`clear失败,预期: true, 实际: ${result}`);
@@ -138,7 +156,7 @@ async function test() {
138
156
 
139
157
  // 测试哈希表方法
140
158
  $.log.debug('[CacheBase] \n========== 测试哈希表方法 ==========');
141
- const hashCache = new CacheBase();
159
+ const hashCache = new CacheBase({ file_path: './test_cache/test_hash.cache.json' });
142
160
 
143
161
  await runner.runTest('hset和hget方法', async () => {
144
162
  const result1 = await hashCache.hset('hash1', 'field1', 'value1');
@@ -184,7 +202,7 @@ async function test() {
184
202
 
185
203
  // 测试批量操作
186
204
  $.log.debug('[CacheBase] \n========== 测试批量操作 ==========');
187
- const batchCache = new CacheBase();
205
+ const batchCache = new CacheBase({ file_path: './test_cache/test_batch.cache.json' });
188
206
 
189
207
  await runner.runTest('mset方法', async () => {
190
208
  const result = await batchCache.mset('key7', 'value7', 'key8', 'value8');
@@ -207,7 +225,7 @@ async function test() {
207
225
 
208
226
  // 测试过期时间
209
227
  $.log.debug('[CacheBase] \n========== 测试过期时间 ==========');
210
- const expireCache = new CacheBase();
228
+ const expireCache = new CacheBase({ file_path: './test_cache/test_expire.cache.json' });
211
229
 
212
230
  await runner.runTest('set带过期时间', async () => {
213
231
  await expireCache.set('expire1', 'value1', 1); // 1秒过期
@@ -256,7 +274,7 @@ async function test() {
256
274
 
257
275
  // 测试数值操作
258
276
  $.log.debug('[CacheBase] \n========== 测试数值操作 ==========');
259
- const numCache = new CacheBase();
277
+ const numCache = new CacheBase({ file_path: './test_cache/test_num.cache.json' });
260
278
 
261
279
  await runner.runTest('incr方法', async () => {
262
280
  const result1 = await numCache.incr('counter');
@@ -358,7 +376,7 @@ async function test() {
358
376
 
359
377
  // 测试字符串操作
360
378
  $.log.debug('[CacheBase] \n========== 测试字符串操作 ==========');
361
- const strCache = new CacheBase();
379
+ const strCache = new CacheBase({ file_path: './test_cache/test_str.cache.json' });
362
380
 
363
381
  await runner.runTest('append方法', async () => {
364
382
  await strCache.set('str', 'hello');
@@ -398,7 +416,7 @@ async function test() {
398
416
 
399
417
  // 测试事件触发
400
418
  $.log.debug('[CacheBase] \n========== 测试事件触发 ==========');
401
- const eventCache = new CacheBase();
419
+ const eventCache = new CacheBase({ file_path: './test_cache/test_event.cache.json' });
402
420
 
403
421
  await runner.runTest('set事件触发', async () => {
404
422
  return new Promise(async (resolve, reject) => {
@@ -431,7 +449,7 @@ async function test() {
431
449
  await runner.runTest('error事件触发', async () => {
432
450
  return new Promise(async (resolve, reject) => {
433
451
  eventCache.once('mset_error', (data) => {
434
- if (data.error && data.error.message.includes('必须是对象')) {
452
+ if (data.error && data.error.message.includes('参数数量必须为偶数')) {
435
453
  resolve();
436
454
  } else {
437
455
  reject(new Error('mset_error事件触发但消息不正确'));
@@ -453,21 +471,24 @@ async function test() {
453
471
  // 测试管理功能
454
472
  $.log.debug('[CacheBase] \n========== 测试管理功能 ==========');
455
473
 
456
- await runner.runTest('cachebase_admin创建实例', async () => {
457
- const cache1 = cachebase_admin('test_scope');
458
- const cache2 = cachebase_admin('test_scope');
474
+ await runner.runTest('cachebaseAdmin创建实例', async () => {
475
+ const cache1 = cachebaseAdmin('test_scope');
476
+ const cache2 = cachebaseAdmin('test_scope');
459
477
  if (cache1 !== cache2) {
460
- throw new Error('cachebase_admin应返回相同实例但没有');
478
+ throw new Error('cachebaseAdmin应返回相同实例但没有');
461
479
  }
462
480
 
463
- const cache3 = cachebase_admin('another_scope');
481
+ const cache3 = cachebaseAdmin('another_scope');
464
482
  if (cache1 === cache3) {
465
- throw new Error('cachebase_admin应返回不同作用域的不同实例但没有');
483
+ throw new Error('cachebaseAdmin应返回不同作用域的不同实例但没有');
466
484
  }
467
485
  });
468
486
 
469
487
  await runner.runTest('dispose方法', async () => {
470
- const disposeCache = new CacheBase();
488
+ const disposeCache = new CacheBase({
489
+ persist: true,
490
+ file_path: './test_cache/test_dispose.cache.json'
491
+ });
471
492
  await disposeCache.set('key_to_dispose', 'value');
472
493
  disposeCache.dispose();
473
494