mm_cachebase 1.8.4 → 1.8.6

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,272 @@
1
+ const { CacheBase } = require('./index');
2
+
3
+ /**
4
+ * 测试CacheBase方法与Redis的兼容性
5
+ */
6
+ async function testCompatibilityFixes() {
7
+ console.log('开始测试兼容性修复...');
8
+
9
+ // 创建缓存实例
10
+ const cache = new CacheBase('test_compatibility', { persist: false });
11
+
12
+ // 测试计数器
13
+ let passed = 0;
14
+ let total = 0;
15
+
16
+ // 测试函数
17
+ const test = async (name, testFn) => {
18
+ total++;
19
+ console.log(`\n测试 ${name}...`);
20
+ try {
21
+ await testFn();
22
+ passed++;
23
+ console.log(`✅ ${name} 通过`);
24
+ } catch (error) {
25
+ console.error(`❌ ${name} 失败:`, error);
26
+ }
27
+ };
28
+
29
+ try {
30
+ // 测试set方法
31
+ await test('set方法兼容性', async () => {
32
+ // 测试基本设置
33
+ const result = await cache.set('test:set', 'value');
34
+ if (result !== 'OK') {
35
+ throw new Error(`set应返回'OK',但得到了${result}`);
36
+ }
37
+
38
+ const value = await cache.get('test:set');
39
+ if (value !== 'value') {
40
+ throw new Error(`设置的值与获取的值不匹配: ${value}`);
41
+ }
42
+
43
+ // 测试带过期时间
44
+ await cache.set('test:set:expire', 'expire_value', 1);
45
+ const expireValue = await cache.get('test:set:expire');
46
+ if (expireValue !== 'expire_value') {
47
+ throw new Error(`带过期时间的set失败: ${expireValue}`);
48
+ }
49
+ });
50
+
51
+ // 测试del方法
52
+ await test('del方法兼容性', async () => {
53
+ // 设置测试键
54
+ await cache.set('test:del', 'del_value');
55
+
56
+ // 删除存在的键
57
+ const deleted = await cache.del('test:del');
58
+ if (deleted !== 1) {
59
+ throw new Error(`删除存在的键应返回1,但得到了${deleted}`);
60
+ }
61
+
62
+ // 验证键已删除
63
+ const value = await cache.get('test:del');
64
+ if (value !== undefined) {
65
+ throw new Error(`键应已删除,但仍然存在: ${value}`);
66
+ }
67
+
68
+ // 删除不存在的键
69
+ const deletedNonExistent = await cache.del('test:del:non_existent');
70
+ if (deletedNonExistent !== 0) {
71
+ throw new Error(`删除不存在的键应返回0,但得到了${deletedNonExistent}`);
72
+ }
73
+ });
74
+
75
+ // 测试clear方法
76
+ await test('clear方法兼容性', async () => {
77
+ // 设置测试键
78
+ await cache.set('test:clear:1', 'value1');
79
+ await cache.set('test:clear:2', 'value2');
80
+ await cache.set('other:key', 'other_value');
81
+
82
+ // 测试带模式的clear
83
+ const result1 = await cache.clear('test:clear:*');
84
+ if (result1 !== 'OK') {
85
+ throw new Error(`带模式的clear应返回'OK',但得到了${result1}`);
86
+ }
87
+
88
+ // 验证匹配的键已删除,其他键保留
89
+ const value1 = await cache.get('test:clear:1');
90
+ const value2 = await cache.get('test:clear:2');
91
+ const otherValue = await cache.get('other:key');
92
+
93
+ if (value1 !== undefined || value2 !== undefined || otherValue !== 'other_value') {
94
+ throw new Error(`clear模式匹配失败: value1=${value1}, value2=${value2}, otherValue=${otherValue}`);
95
+ }
96
+
97
+ // 测试清空所有
98
+ const result2 = await cache.clear();
99
+ if (result2 !== 'OK') {
100
+ throw new Error(`清空所有的clear应返回'OK',但得到了${result2}`);
101
+ }
102
+
103
+ // 验证所有键已删除
104
+ const afterClear = await cache.get('other:key');
105
+ if (afterClear !== undefined) {
106
+ throw new Error(`clear应清空所有键,但键仍然存在: ${afterClear}`);
107
+ }
108
+ });
109
+
110
+ // 测试exists方法
111
+ await test('exists方法兼容性', async () => {
112
+ // 设置测试键
113
+ await cache.set('test:exists:1', 'value1');
114
+ await cache.set('test:exists:2', 'value2');
115
+
116
+ // 测试单键存在
117
+ const exists1 = await cache.exists('test:exists:1');
118
+ if (exists1 !== 1) {
119
+ throw new Error(`exists应返回1表示键存在,但得到了${exists1}`);
120
+ }
121
+
122
+ // 测试单键不存在
123
+ const existsNonExistent = await cache.exists('test:exists:non_existent');
124
+ if (existsNonExistent !== 0) {
125
+ throw new Error(`exists应返回0表示键不存在,但得到了${existsNonExistent}`);
126
+ }
127
+
128
+ // 测试多键存在
129
+ const existsMultiple = await cache.exists(['test:exists:1', 'test:exists:2', 'test:exists:non_existent']);
130
+ if (existsMultiple !== 2) {
131
+ throw new Error(`多键exists应返回存在的键数量2,但得到了${existsMultiple}`);
132
+ }
133
+ });
134
+
135
+ // 测试hset方法
136
+ await test('hset方法兼容性', async () => {
137
+ // 测试创建新字段
138
+ const newFieldResult = await cache.hset('test:hash', 'field1', 'value1');
139
+ if (newFieldResult !== 1) {
140
+ throw new Error(`hset创建新字段应返回1,但得到了${newFieldResult}`);
141
+ }
142
+
143
+ // 验证字段值
144
+ const field1Value = await cache.hget('test:hash', 'field1');
145
+ if (field1Value !== 'value1') {
146
+ throw new Error(`hset设置的字段值与获取的值不匹配: ${field1Value}`);
147
+ }
148
+
149
+ // 测试更新现有字段
150
+ const updateFieldResult = await cache.hset('test:hash', 'field1', 'updated_value');
151
+ if (updateFieldResult !== 0) {
152
+ throw new Error(`hset更新字段应返回0,但得到了${updateFieldResult}`);
153
+ }
154
+
155
+ // 验证更新后的值
156
+ const updatedValue = await cache.hget('test:hash', 'field1');
157
+ if (updatedValue !== 'updated_value') {
158
+ throw new Error(`hset更新的值与获取的值不匹配: ${updatedValue}`);
159
+ }
160
+ });
161
+
162
+ // 测试ttl方法
163
+ await test('ttl方法兼容性', async () => {
164
+ // 测试永不过期的键
165
+ await cache.set('test:ttl:never', 'never_expire');
166
+ const ttlNever = await cache.ttl('test:ttl:never');
167
+ if (ttlNever !== -1) {
168
+ throw new Error(`永不过期的键ttl应返回-1,但得到了${ttlNever}`);
169
+ }
170
+
171
+ // 测试带过期时间的键
172
+ await cache.set('test:ttl:expire', 'expire_value', 5);
173
+ const ttlExpire = await cache.ttl('test:ttl:expire');
174
+ if (ttlExpire < 0 || ttlExpire > 5) {
175
+ throw new Error(`带过期时间的键ttl应在0-5之间,但得到了${ttlExpire}`);
176
+ }
177
+
178
+ // 测试不存在的键
179
+ const ttlNonExistent = await cache.ttl('test:ttl:non_existent');
180
+ if (ttlNonExistent !== -2) {
181
+ throw new Error(`不存在的键ttl应返回-2,但得到了${ttlNonExistent}`);
182
+ }
183
+ });
184
+
185
+ // 测试decr方法
186
+ await test('decr方法兼容性', async () => {
187
+ // 测试不存在的键(初始化为0后减1)
188
+ const result1 = await cache.decr('test:decr:new');
189
+ if (result1 !== -1) {
190
+ throw new Error(`decr不存在的键应返回-1,但得到了${result1}`);
191
+ }
192
+
193
+ // 测试已存在的数字键
194
+ await cache.set('test:decr:number', 5);
195
+ const result2 = await cache.decr('test:decr:number');
196
+ if (result2 !== 4) {
197
+ throw new Error(`decr数字键应返回4,但得到了${result2}`);
198
+ }
199
+
200
+ // 测试非数字值(应视为0)
201
+ await cache.set('test:decr:string', 'not_a_number');
202
+ const result3 = await cache.decr('test:decr:string');
203
+ if (result3 !== -1) {
204
+ throw new Error(`decr非数字值应返回-1,但得到了${result3}`);
205
+ }
206
+ });
207
+
208
+ // 测试mset方法
209
+ await test('mset方法兼容性', async () => {
210
+ // 测试批量设置
211
+ const result = await cache.mset({
212
+ 'test:mset:1': 'value1',
213
+ 'test:mset:2': 'value2',
214
+ 'test:mset:3': 'value3'
215
+ });
216
+
217
+ if (result !== 'OK') {
218
+ throw new Error(`mset应返回'OK',但得到了${result}`);
219
+ }
220
+
221
+ // 验证设置的值
222
+ const value1 = await cache.get('test:mset:1');
223
+ const value2 = await cache.get('test:mset:2');
224
+ const value3 = await cache.get('test:mset:3');
225
+
226
+ if (value1 !== 'value1' || value2 !== 'value2' || value3 !== 'value3') {
227
+ throw new Error(`mset设置的值与获取的值不匹配: value1=${value1}, value2=${value2}, value3=${value3}`);
228
+ }
229
+ });
230
+
231
+ // 测试mget方法
232
+ await test('mget方法兼容性', async () => {
233
+ // 确保测试键存在
234
+ await cache.set('test:mget:1', 'value1');
235
+ await cache.set('test:mget:2', 'value2');
236
+
237
+ // 测试批量获取
238
+ const results = await cache.mget(['test:mget:1', 'test:mget:2', 'test:mget:non_existent']);
239
+
240
+ if (!Array.isArray(results)) {
241
+ throw new Error(`mget应返回数组,但得到了${typeof results}`);
242
+ }
243
+
244
+ if (results.length !== 3) {
245
+ throw new Error(`mget应返回长度为3的数组,但得到了长度${results.length}`);
246
+ }
247
+
248
+ if (results[0] !== 'value1' || results[1] !== 'value2' || results[2] !== undefined) {
249
+ throw new Error(`mget返回的值与预期不符: ${JSON.stringify(results)}`);
250
+ }
251
+ });
252
+
253
+ // 清理测试数据
254
+ await cache.clear();
255
+ await cache.dispose();
256
+
257
+ // 输出测试结果
258
+ console.log(`\n测试完成: ${passed}/${total} 测试通过`);
259
+ if (passed === total) {
260
+ console.log('🎉 所有兼容性修复测试通过!');
261
+ } else {
262
+ console.log(`❌ 有 ${total - passed} 个测试失败`);
263
+ }
264
+
265
+ } catch (error) {
266
+ console.error('测试过程中发生错误:', error);
267
+ await cache.dispose();
268
+ }
269
+ }
270
+
271
+ // 运行测试
272
+ testCompatibilityFixes();
@@ -0,0 +1,147 @@
1
+ const assert = require('assert');
2
+ const { CacheBase } = require('./index');
3
+ async function testCompatibility() {
4
+ console.log('开始测试兼容性改进...');
5
+
6
+ // 创建缓存实例
7
+ const cache = new CacheBase('test_compatibility');
8
+
9
+ try {
10
+ // 清空之前的测试数据
11
+ await cache.clear();
12
+
13
+ console.log('\n1. 测试 set 方法兼容性');
14
+ // 测试set方法
15
+ const setResult = await cache.set('test_key', 'test_value');
16
+ assert.strictEqual(setResult, 'OK', 'set方法应返回OK');
17
+ const getResult = await cache.get('test_key');
18
+ assert.strictEqual(getResult, 'test_value', 'set方法设置的值应正确');
19
+ console.log('✓ set 方法测试通过');
20
+
21
+ console.log('\n2. 测试 del 方法兼容性');
22
+ // 测试del方法删除存在的键
23
+ const delResult1 = await cache.del('test_key');
24
+ assert.strictEqual(delResult1, 1, 'del方法应返回删除的键数量1');
25
+ const exists1 = await cache.exists('test_key');
26
+ assert.strictEqual(exists1, 0, '删除的键不应再存在');
27
+
28
+ // 测试del方法删除不存在的键
29
+ const delResult2 = await cache.del('non_existent_key');
30
+ assert.strictEqual(delResult2, 0, '删除不存在的键应返回0');
31
+ console.log('✓ del 方法测试通过');
32
+
33
+ console.log('\n3. 测试 exists 方法兼容性');
34
+ // 设置一些测试键
35
+ await cache.set('exists_key1', 'value1');
36
+ await cache.set('exists_key2', 'value2', 1); // 1秒过期
37
+
38
+ // 测试单个键存在
39
+ const existsResult1 = await cache.exists('exists_key1');
40
+ assert.strictEqual(existsResult1, 1, 'exists方法应返回存在的键数量1');
41
+
42
+ // 测试多个键存在
43
+ const existsResult2 = await cache.exists(['exists_key1', 'exists_key2', 'non_existent']);
44
+ assert.strictEqual(existsResult2, 2, 'exists方法应返回存在的键数量2');
45
+
46
+ // 测试不存在的键
47
+ const existsResult3 = await cache.exists('non_existent');
48
+ assert.strictEqual(existsResult3, 0, '不存在的键应返回0');
49
+ console.log('✓ exists 方法测试通过');
50
+
51
+ console.log('\n4. 测试 ttl 方法兼容性');
52
+ // 设置带过期时间的键
53
+ await cache.set('ttl_key', 'ttl_value', 10); // 10秒过期
54
+
55
+ // 测试永不过期的键
56
+ await cache.set('ttl_key_forever', 'forever_value');
57
+ const ttlForever = await cache.ttl('ttl_key_forever');
58
+ assert.strictEqual(ttlForever, -1, '永不过期的键ttl应返回-1');
59
+
60
+ // 测试存在但已过期的键
61
+ await cache.set('ttl_key_expire', 'expire_value', 1); // 1秒过期
62
+ await new Promise(resolve => setTimeout(resolve, 1500)); // 等待过期
63
+ const ttlExpired = await cache.ttl('ttl_key_expire');
64
+ assert.strictEqual(ttlExpired, -2, '已过期的键ttl应返回-2');
65
+
66
+ // 测试不存在的键
67
+ const ttlNonExistent = await cache.ttl('non_existent_ttl');
68
+ assert.strictEqual(ttlNonExistent, -2, '不存在的键ttl应返回-2');
69
+
70
+ // 测试有过期时间的键
71
+ await cache.set('ttl_key_with_ttl', 'value_with_ttl', 2); // 2秒过期
72
+ const ttlWithTtl = await cache.ttl('ttl_key_with_ttl');
73
+ assert.ok(ttlWithTtl >= 0 && ttlWithTtl <= 2, '有过期时间的键应返回剩余秒数');
74
+ console.log('✓ ttl 方法测试通过');
75
+
76
+ console.log('\n5. 测试 clear 方法兼容性');
77
+ // 设置一些测试键
78
+ await cache.set('clear_key1', 'value1');
79
+ await cache.set('clear_key2', 'value2');
80
+ await cache.set('test_pattern1', 'pattern1');
81
+ await cache.set('test_pattern2', 'pattern2');
82
+
83
+ // 测试模式匹配清除
84
+ await cache.clear('test_pattern*');
85
+ const existsPattern1 = await cache.exists('test_pattern1');
86
+ const existsPattern2 = await cache.exists('test_pattern2');
87
+ const existsClear1 = await cache.exists('clear_key1');
88
+ assert.strictEqual(existsPattern1, 0, '模式匹配的键应被清除');
89
+ assert.strictEqual(existsPattern2, 0, '模式匹配的键应被清除');
90
+ assert.strictEqual(existsClear1, 1, '非模式匹配的键应保留');
91
+
92
+ // 测试全部清除
93
+ await cache.clear();
94
+ const existsAfterClear = await cache.exists(['clear_key1', 'clear_key2']);
95
+ assert.strictEqual(existsAfterClear, 0, 'clear方法应清除所有键');
96
+ console.log('✓ clear 方法测试通过');
97
+
98
+ // 新添加的测试:addInt、addStr、addFloat、mset、mget
99
+ console.log('\n6. 测试 addInt 方法兼容性');
100
+ const intResult1 = await cache.addInt('int_key', 10);
101
+ assert.strictEqual(intResult1, 10, 'addInt初始值应为10');
102
+ const intResult2 = await cache.addInt('int_key', 5);
103
+ assert.strictEqual(intResult2, 15, 'addInt增加后应为15');
104
+ console.log('✓ addInt 方法测试通过');
105
+
106
+ console.log('\n7. 测试 addStr 方法兼容性');
107
+ const strResult1 = await cache.addStr('str_key', 'hello');
108
+ assert.strictEqual(strResult1, 5, '初始字符串长度应为5');
109
+ const strResult2 = await cache.addStr('str_key', ' world');
110
+ assert.strictEqual(strResult2, 11, '追加后字符串长度应为11');
111
+ console.log('✓ addStr 方法测试通过');
112
+
113
+ console.log('\n8. 测试 addFloat 方法兼容性');
114
+ const floatResult1 = await cache.addFloat('float_key', 10.5);
115
+ assert.strictEqual(floatResult1, 10.5, '初始浮点数应为10.5');
116
+ const floatResult2 = await cache.addFloat('float_key', 2.5);
117
+ assert.strictEqual(floatResult2, 13.0, '增加后浮点数应为13.0');
118
+ console.log('✓ addFloat 方法测试通过');
119
+
120
+ console.log('\n9. 测试 mset 方法兼容性');
121
+ const msetResult = await cache.mset({ mkey1: 'value1', mkey2: 'value2' });
122
+ assert.strictEqual(msetResult, 'OK', 'mset应返回OK');
123
+ assert.strictEqual(await cache.get('mkey1'), 'value1', 'mset应正确设置值1');
124
+ assert.strictEqual(await cache.get('mkey2'), 'value2', 'mset应正确设置值2');
125
+ console.log('✓ mset 方法测试通过');
126
+
127
+ console.log('\n10. 测试 mget 方法兼容性');
128
+ const mgetResult = await cache.mget(['mkey1', 'mkey2', 'nonexistent']);
129
+ assert.deepStrictEqual(mgetResult, ['value1', 'value2', undefined], 'mget应正确获取多个值');
130
+ console.log('✓ mget 方法测试通过');
131
+
132
+ console.log('\n所有兼容性测试通过!');
133
+ return true;
134
+ } catch (error) {
135
+ console.error('测试失败:', error);
136
+ return false;
137
+ } finally {
138
+ // 清理测试数据
139
+ await cache.clear();
140
+ await cache.save();
141
+ }
142
+ }
143
+
144
+ // 运行测试
145
+ testCompatibility().then(success => {
146
+ process.exit(success ? 0 : 1);
147
+ });
@@ -0,0 +1,99 @@
1
+ const assert = require('assert');
2
+ const { CacheBase } = require('./index');
3
+ async function testCompatibilityImprovements() {
4
+ console.log('开始测试兼容性改进方法...');
5
+
6
+ // 创建缓存实例
7
+ const cache = new CacheBase('test_compatibility_methods');
8
+
9
+ try {
10
+ // 清空之前的测试数据
11
+ await cache.clear();
12
+
13
+ console.log('\n1. 测试 decr 方法兼容性');
14
+ // 测试decr方法初始值
15
+ const decrResult1 = await cache.decr('decr_key');
16
+ assert.strictEqual(decrResult1, -1, 'decr初始值应为-1');
17
+
18
+ // 测试decr方法增加值
19
+ const decrResult2 = await cache.decr('decr_key');
20
+ assert.strictEqual(decrResult2, -2, 'decr减少后应为-2');
21
+
22
+ // 测试已有数值的decr
23
+ await cache.set('decr_existing', 5);
24
+ const decrResult3 = await cache.decr('decr_existing');
25
+ assert.strictEqual(decrResult3, 4, '现有数值decr后应为4');
26
+ console.log('✓ decr 方法测试通过');
27
+
28
+ console.log('\n2. 测试 list_set 方法兼容性');
29
+ // 测试list_set返回列表长度
30
+ const listSetResult1 = await cache.list_set('list_key', ['item1', 'item2']);
31
+ assert.strictEqual(listSetResult1, 2, 'list_set应返回列表长度');
32
+
33
+ // 测试非数组值
34
+ const listSetResult2 = await cache.list_set('list_key_single', 'single_item');
35
+ assert.strictEqual(listSetResult2, 1, '非数组值list_set应返回1');
36
+ const listContent = await cache.list_get('list_key_single');
37
+ assert.deepStrictEqual(listContent, ['single_item'], '非数组值应被包装为数组');
38
+ console.log('✓ list_set 方法测试通过');
39
+
40
+ console.log('\n3. 测试 list_add 方法兼容性');
41
+ // 测试list_add返回列表长度
42
+ await cache.list_set('add_list', ['initial']);
43
+ const listAddResult1 = await cache.list_add('add_list', 'new_item');
44
+ assert.strictEqual(listAddResult1, 2, 'list_add应返回添加后列表长度');
45
+
46
+ // 测试空列表添加
47
+ const listAddResult2 = await cache.list_add('new_list', 'first_item');
48
+ assert.strictEqual(listAddResult2, 1, '空列表添加应返回1');
49
+ console.log('✓ list_add 方法测试通过');
50
+
51
+ console.log('\n4. 测试 list_has 方法兼容性');
52
+ // 设置测试列表
53
+ await cache.list_set('has_list', ['item1', 'item2', 'item3']);
54
+
55
+ // 测试存在的项
56
+ const hasResult1 = await cache.list_has('has_list', 'item2');
57
+ assert.strictEqual(hasResult1, 1, '存在的项应返回1');
58
+
59
+ // 测试不存在的项
60
+ const hasResult2 = await cache.list_has('has_list', 'item4');
61
+ assert.strictEqual(hasResult2, 0, '不存在的项应返回0');
62
+
63
+ // 测试不存在的键
64
+ const hasResult3 = await cache.list_has('non_existent_list', 'anything');
65
+ assert.strictEqual(hasResult3, 0, '不存在的键应返回0');
66
+ console.log('✓ list_has 方法测试通过');
67
+
68
+ console.log('\n5. 测试 list_get 方法兼容性');
69
+ // 设置测试列表
70
+ await cache.list_set('range_list', ['item0', 'item1', 'item2', 'item3', 'item4']);
71
+
72
+ // 测试全量获取
73
+ const getResult1 = await cache.list_get('range_list');
74
+ assert.deepStrictEqual(getResult1, ['item0', 'item1', 'item2', 'item3', 'item4'], '默认应返回全部元素');
75
+
76
+ // 测试范围获取
77
+ const getResult2 = await cache.list_get('range_list', 1, 3);
78
+ assert.deepStrictEqual(getResult2, ['item1', 'item2', 'item3'], '范围获取应返回指定元素');
79
+
80
+ // 测试起始索引获取
81
+ const getResult3 = await cache.list_get('range_list', 2);
82
+ assert.deepStrictEqual(getResult3, ['item2', 'item3', 'item4'], '只指定起始索引应返回从该索引到末尾');
83
+
84
+ // 测试不存在的键
85
+ const getResult4 = await cache.list_get('non_existent_range');
86
+ assert.deepStrictEqual(getResult4, [], '不存在的键应返回空数组');
87
+ console.log('✓ list_get 方法测试通过');
88
+
89
+ // 清理测试数据
90
+ await cache.clear();
91
+ console.log('\n所有兼容性改进方法测试通过!');
92
+
93
+ } catch (error) {
94
+ console.error('兼容性改进方法测试失败:', error);
95
+ process.exit(1);
96
+ }
97
+ }
98
+
99
+ testCompatibilityImprovements();
@@ -0,0 +1,57 @@
1
+ const assert = require('assert');
2
+ const { CacheBase } = require('./index');
3
+
4
+ async function testUndocumentedMethods() {
5
+ console.log('开始测试文档与实现不一致的方法...');
6
+
7
+ // 创建缓存实例
8
+ const cache = new CacheBase('test_undocumented');
9
+
10
+ try {
11
+ // 清空之前的测试数据
12
+ await cache.clear();
13
+
14
+ console.log('\n1. 测试 addInt 方法');
15
+ const intResult1 = await cache.addInt('int_key', 10);
16
+ assert.strictEqual(intResult1, 10, 'addInt初始值应为10');
17
+ const intResult2 = await cache.addInt('int_key', 5);
18
+ assert.strictEqual(intResult2, 15, 'addInt增加后应为15');
19
+ console.log('✓ addInt 方法测试通过');
20
+
21
+ console.log('\n2. 测试 addStr 方法');
22
+ const strResult1 = await cache.addStr('str_key', 'hello');
23
+ assert.strictEqual(strResult1, 5, '初始字符串长度应为5');
24
+ const strResult2 = await cache.addStr('str_key', ' world');
25
+ assert.strictEqual(strResult2, 11, '追加后字符串长度应为11');
26
+ console.log('✓ addStr 方法测试通过');
27
+
28
+ console.log('\n3. 测试 addFloat 方法');
29
+ const floatResult1 = await cache.addFloat('float_key', 10.5);
30
+ assert.strictEqual(floatResult1, 10.5, '初始浮点数应为10.5');
31
+ const floatResult2 = await cache.addFloat('float_key', 2.5);
32
+ assert.strictEqual(floatResult2, 13.0, '增加后浮点数应为13.0');
33
+ console.log('✓ addFloat 方法测试通过');
34
+
35
+ console.log('\n4. 测试 mset 方法');
36
+ const msetResult = await cache.mset({ mkey1: 'value1', mkey2: 'value2' });
37
+ assert.strictEqual(msetResult, 'OK', 'mset应返回OK');
38
+ assert.strictEqual(await cache.get('mkey1'), 'value1', 'mset应正确设置值1');
39
+ assert.strictEqual(await cache.get('mkey2'), 'value2', 'mset应正确设置值2');
40
+ console.log('✓ mset 方法测试通过');
41
+
42
+ console.log('\n5. 测试 mget 方法');
43
+ const mgetResult = await cache.mget(['mkey1', 'mkey2', 'nonexistent']);
44
+ assert.deepStrictEqual(mgetResult, ['value1', 'value2', undefined], 'mget应正确获取多个值');
45
+ console.log('✓ mget 方法测试通过');
46
+
47
+ // 清理测试数据
48
+ await cache.clear();
49
+ console.log('\n所有测试通过!');
50
+
51
+ } catch (error) {
52
+ console.error('测试失败:', error);
53
+ process.exit(1);
54
+ }
55
+ }
56
+
57
+ testUndocumentedMethods();