mm_cachebase 1.9.1 → 1.9.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/eslint.config.js +211 -0
- package/index.js +92 -100
- package/package.json +2 -2
- package/test.js +39 -18
package/eslint.config.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const namingConventionPlugin = require('mm_eslint');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 基于个人规则的ESLint配置
|
|
7
|
+
* 符合最新ESLint Flat Config格式
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
module.exports = [
|
|
11
|
+
{
|
|
12
|
+
files: ['**/*.js'],
|
|
13
|
+
plugins: {
|
|
14
|
+
'naming-convention': namingConventionPlugin,
|
|
15
|
+
'jsdoc': require('eslint-plugin-jsdoc')
|
|
16
|
+
},
|
|
17
|
+
languageOptions: {
|
|
18
|
+
ecmaVersion: 'latest',
|
|
19
|
+
sourceType: 'module',
|
|
20
|
+
parserOptions: {
|
|
21
|
+
ecmaVersion: 'latest',
|
|
22
|
+
sourceType: 'module'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
// 命名规范规则 - 由自定义插件处理
|
|
27
|
+
rules: {
|
|
28
|
+
// 自定义命名规范插件规则(优先使用)
|
|
29
|
+
'naming-convention/class-name': 'error',
|
|
30
|
+
'naming-convention/function-name': 'error',
|
|
31
|
+
'naming-convention/method-name': 'error',
|
|
32
|
+
'naming-convention/variable-name': 'warn',
|
|
33
|
+
'naming-convention/constant-name': 'error',
|
|
34
|
+
'naming-convention/private-method-naming': 'warn',
|
|
35
|
+
'naming-convention/private-variable-naming': 'warn',
|
|
36
|
+
'naming-convention/param-name': 'warn',
|
|
37
|
+
'naming-convention/property-name': 'warn',
|
|
38
|
+
'naming-convention/instance-property': 'warn',
|
|
39
|
+
|
|
40
|
+
// 禁用与命名规范插件冲突的默认规则
|
|
41
|
+
'camelcase': 'off',
|
|
42
|
+
'id-match': 'off',
|
|
43
|
+
'new-cap': 'off',
|
|
44
|
+
'id-length': 'off',
|
|
45
|
+
'id-denylist': 'off',
|
|
46
|
+
'id-blacklist': 'off'
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
{
|
|
51
|
+
// 代码风格规则
|
|
52
|
+
rules: {
|
|
53
|
+
// 最大行长度100字符
|
|
54
|
+
'max-len': ['error', {
|
|
55
|
+
code: 100,
|
|
56
|
+
ignoreComments: true,
|
|
57
|
+
ignoreUrls: true,
|
|
58
|
+
ignoreStrings: true,
|
|
59
|
+
ignoreTemplateLiterals: true
|
|
60
|
+
}],
|
|
61
|
+
|
|
62
|
+
// 缩进2空格
|
|
63
|
+
'indent': ['error', 2, {
|
|
64
|
+
SwitchCase: 1,
|
|
65
|
+
VariableDeclarator: 1,
|
|
66
|
+
outerIIFEBody: 1,
|
|
67
|
+
MemberExpression: 1,
|
|
68
|
+
FunctionDeclaration: { parameters: 1, body: 1 },
|
|
69
|
+
FunctionExpression: { parameters: 1, body: 1 },
|
|
70
|
+
CallExpression: { arguments: 1 },
|
|
71
|
+
ArrayExpression: 1,
|
|
72
|
+
ObjectExpression: 1,
|
|
73
|
+
ImportDeclaration: 1,
|
|
74
|
+
flatTernaryExpressions: false,
|
|
75
|
+
ignoreComments: false
|
|
76
|
+
}],
|
|
77
|
+
|
|
78
|
+
// 单引号
|
|
79
|
+
'quotes': ['error', 'single', {
|
|
80
|
+
avoidEscape: true,
|
|
81
|
+
allowTemplateLiterals: true
|
|
82
|
+
}],
|
|
83
|
+
|
|
84
|
+
// 禁止制表符
|
|
85
|
+
'no-tabs': 'error',
|
|
86
|
+
|
|
87
|
+
// 行尾分号
|
|
88
|
+
'semi': ['error', 'always'],
|
|
89
|
+
|
|
90
|
+
// 逗号风格
|
|
91
|
+
'comma-style': ['error', 'last'],
|
|
92
|
+
|
|
93
|
+
// 逗号后必须有空格,逗号前不能有空格
|
|
94
|
+
'comma-spacing': ['error', {
|
|
95
|
+
before: false,
|
|
96
|
+
after: true
|
|
97
|
+
}]
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
{
|
|
102
|
+
// 错误处理规则
|
|
103
|
+
rules: {
|
|
104
|
+
// 必须进行参数校验
|
|
105
|
+
'no-unused-vars': ['error', {
|
|
106
|
+
args: 'all',
|
|
107
|
+
argsIgnorePattern: '^_',
|
|
108
|
+
caughtErrors: 'all',
|
|
109
|
+
caughtErrorsIgnorePattern: '^_',
|
|
110
|
+
destructuredArrayIgnorePattern: '^_',
|
|
111
|
+
varsIgnorePattern: '^_',
|
|
112
|
+
ignoreRestSiblings: true
|
|
113
|
+
}],
|
|
114
|
+
|
|
115
|
+
// 建议使用try-catch
|
|
116
|
+
'no-unsafe-finally': 'warn',
|
|
117
|
+
|
|
118
|
+
// 禁止直接抛出字符串
|
|
119
|
+
'no-throw-literal': 'error'
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
{
|
|
124
|
+
// 最佳实践规则
|
|
125
|
+
rules: {
|
|
126
|
+
// 优先使用async/await
|
|
127
|
+
'prefer-promise-reject-errors': 'error',
|
|
128
|
+
|
|
129
|
+
// 避免副作用
|
|
130
|
+
'no-param-reassign': ['error', {
|
|
131
|
+
props: true,
|
|
132
|
+
ignorePropertyModificationsFor: [
|
|
133
|
+
'acc', // for reduce accumulators
|
|
134
|
+
'accumulator', // for reduce accumulators
|
|
135
|
+
'e', // for e.returnvalue
|
|
136
|
+
'ctx', // for Koa routing
|
|
137
|
+
'context', // for Koa routing
|
|
138
|
+
'req', // for Express requests
|
|
139
|
+
'request', // for Express requests
|
|
140
|
+
'res', // for Express responses
|
|
141
|
+
'response', // for Express responses
|
|
142
|
+
'$scope', // for Angular 1 scopes
|
|
143
|
+
'staticContext' // for ReactRouter context
|
|
144
|
+
]
|
|
145
|
+
}],
|
|
146
|
+
|
|
147
|
+
// 优先链式编程
|
|
148
|
+
'prefer-object-spread': 'error',
|
|
149
|
+
|
|
150
|
+
// 优先函数式编程
|
|
151
|
+
'prefer-arrow-callback': 'error',
|
|
152
|
+
|
|
153
|
+
// 方法长度限制
|
|
154
|
+
'max-lines-per-function': ['warn', {
|
|
155
|
+
max: 40,
|
|
156
|
+
skipBlankLines: true,
|
|
157
|
+
skipComments: true
|
|
158
|
+
}],
|
|
159
|
+
|
|
160
|
+
// 复杂度限制
|
|
161
|
+
'complexity': ['warn', 10]
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
{
|
|
166
|
+
// JSDoc规则
|
|
167
|
+
rules: {
|
|
168
|
+
// 要求公开方法有JSDoc注释
|
|
169
|
+
'jsdoc/require-jsdoc': ['warn', {
|
|
170
|
+
publicOnly: true,
|
|
171
|
+
require: {
|
|
172
|
+
FunctionDeclaration: true,
|
|
173
|
+
MethodDefinition: true,
|
|
174
|
+
ClassDeclaration: true,
|
|
175
|
+
ArrowFunctionExpression: false,
|
|
176
|
+
FunctionExpression: false
|
|
177
|
+
}
|
|
178
|
+
}],
|
|
179
|
+
|
|
180
|
+
// 检查JSDoc语法
|
|
181
|
+
'jsdoc/check-alignment': 'warn',
|
|
182
|
+
'jsdoc/check-indentation': 'warn',
|
|
183
|
+
'jsdoc/check-param-names': 'warn',
|
|
184
|
+
'jsdoc/check-syntax': 'warn',
|
|
185
|
+
'jsdoc/check-tag-names': 'warn',
|
|
186
|
+
'jsdoc/check-types': 'warn',
|
|
187
|
+
'jsdoc/no-undefined-types': 'warn',
|
|
188
|
+
'jsdoc/require-description': 'warn',
|
|
189
|
+
'jsdoc/require-param': 'warn',
|
|
190
|
+
'jsdoc/require-param-description': 'warn',
|
|
191
|
+
'jsdoc/require-param-name': 'warn',
|
|
192
|
+
'jsdoc/require-param-type': 'warn',
|
|
193
|
+
'jsdoc/require-returns': 'warn',
|
|
194
|
+
'jsdoc/require-returns-check': 'warn',
|
|
195
|
+
'jsdoc/require-returns-description': 'warn',
|
|
196
|
+
'jsdoc/require-returns-type': 'warn',
|
|
197
|
+
'jsdoc/valid-types': 'warn'
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
{
|
|
202
|
+
// 忽略规则
|
|
203
|
+
ignores: [
|
|
204
|
+
'node_modules/**',
|
|
205
|
+
'dist/**',
|
|
206
|
+
'build/**',
|
|
207
|
+
'coverage/**',
|
|
208
|
+
'*.min.js'
|
|
209
|
+
]
|
|
210
|
+
}
|
|
211
|
+
];
|
package/index.js
CHANGED
|
@@ -11,10 +11,10 @@ class CacheBase extends Base {
|
|
|
11
11
|
/**
|
|
12
12
|
* 默认配置静态变量
|
|
13
13
|
*/
|
|
14
|
-
static
|
|
14
|
+
static config = {
|
|
15
15
|
enabled: true,
|
|
16
16
|
persist: true,
|
|
17
|
-
file_path: './
|
|
17
|
+
file_path: './cache/base.json',
|
|
18
18
|
debug: false,
|
|
19
19
|
auto_save: true,
|
|
20
20
|
save_interval: 30000,
|
|
@@ -28,7 +28,7 @@ class CacheBase extends Base {
|
|
|
28
28
|
* @param {Object} config - 配置对象
|
|
29
29
|
*/
|
|
30
30
|
constructor(config = {}) {
|
|
31
|
-
super(config);
|
|
31
|
+
super(CacheBase.config);
|
|
32
32
|
// 存储实际数据
|
|
33
33
|
this._data = {};
|
|
34
34
|
|
|
@@ -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.
|
|
68
|
+
this.log('info', 'CacheBase service initialized successfully');
|
|
69
69
|
} catch (error) {
|
|
70
|
-
this.
|
|
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.
|
|
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
|
-
|
|
134
|
+
this.log('info', `[_loadData] 尝试加载文件: ${file_path}`);
|
|
135
135
|
|
|
136
136
|
if (file_path.hasFile()) {
|
|
137
|
-
|
|
137
|
+
this.log('info', `[_loadData] 文件存在,开始读取`);
|
|
138
138
|
// 使用mm_expand的原型函数读取JSON文件
|
|
139
139
|
const parsed_data = file_path.loadJson();
|
|
140
|
-
|
|
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
|
-
|
|
144
|
+
this.log('info', `[_loadData] 数据加载成功,键数量:`, Object.keys(this._data).length);
|
|
145
145
|
} else {
|
|
146
|
-
|
|
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
|
-
|
|
157
|
+
this.log('info', `[_loadData] 文件不存在: ${file_path}`);
|
|
158
158
|
}
|
|
159
159
|
} else {
|
|
160
|
-
|
|
160
|
+
this.log('info', `[_loadData] 持久化未启用或文件路径未设置`);
|
|
161
161
|
}
|
|
162
162
|
} catch (error) {
|
|
163
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1407
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
2302
|
+
this.log('error', 'Failed to hmget:', error);
|
|
2323
2303
|
resolve([]);
|
|
2324
2304
|
}
|
|
2325
2305
|
});
|
|
@@ -2358,15 +2338,20 @@ CacheBase.prototype.hlen = function(key) {
|
|
|
2358
2338
|
key,
|
|
2359
2339
|
error
|
|
2360
2340
|
});
|
|
2361
|
-
this.
|
|
2341
|
+
this.log('error', 'Failed to hlen:', error);
|
|
2362
2342
|
resolve(0);
|
|
2363
2343
|
}
|
|
2364
2344
|
});
|
|
2365
2345
|
};
|
|
2366
2346
|
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2347
|
+
|
|
2348
|
+
/**
|
|
2349
|
+
* @description CacheBase管理器,用于创建缓存
|
|
2350
|
+
* @param {String} scope 作用域
|
|
2351
|
+
* @param {Object} config 配置参数
|
|
2352
|
+
* @return {Object} 返回一个CacheBase类实例
|
|
2353
|
+
*/
|
|
2354
|
+
function cachebaseAdmin(scope, config) {
|
|
2370
2355
|
if (!global.$) {
|
|
2371
2356
|
global.$ = {};
|
|
2372
2357
|
}
|
|
@@ -2381,11 +2366,18 @@ exports.cachebaseAdmin = function(scope, dir) {
|
|
|
2381
2366
|
}
|
|
2382
2367
|
var obj = global.$.pool.cachebase[scope];
|
|
2383
2368
|
if (!obj) {
|
|
2384
|
-
const
|
|
2385
|
-
file_path: dir ? './cachebase_' + scope + '.json' : '/cache/cachebase_' + scope + '.json'
|
|
2369
|
+
const dbConfig = {
|
|
2370
|
+
file_path: config && config.dir ? './cachebase_' + scope + '.json' : '/cache/cachebase_' + scope + '.json',
|
|
2371
|
+
...(typeof config === 'object' && config ? config : {})
|
|
2386
2372
|
};
|
|
2387
|
-
global.$.pool.cachebase[scope] = new CacheBase(
|
|
2373
|
+
global.$.pool.cachebase[scope] = new CacheBase(dbConfig);
|
|
2388
2374
|
obj = global.$.pool.cachebase[scope];
|
|
2389
2375
|
}
|
|
2390
2376
|
return obj;
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
// 导出类
|
|
2380
|
+
module.exports = {
|
|
2381
|
+
CacheBase,
|
|
2382
|
+
cachebaseAdmin,
|
|
2391
2383
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_cachebase",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.3",
|
|
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": "^
|
|
42
|
+
"mm_expand": "^2.0.0"
|
|
43
43
|
}
|
|
44
44
|
}
|
package/test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { CacheBase,
|
|
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
|
-
|
|
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('
|
|
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('
|
|
457
|
-
const cache1 =
|
|
458
|
-
const cache2 =
|
|
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('
|
|
478
|
+
throw new Error('cachebaseAdmin应返回相同实例但没有');
|
|
461
479
|
}
|
|
462
480
|
|
|
463
|
-
const cache3 =
|
|
481
|
+
const cache3 = cachebaseAdmin('another_scope');
|
|
464
482
|
if (cache1 === cache3) {
|
|
465
|
-
throw new Error('
|
|
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
|
|