mm_cachebase 1.8.5 → 1.8.7

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.
@@ -1,147 +0,0 @@
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
- });
@@ -1,99 +0,0 @@
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();
@@ -1,160 +0,0 @@
1
- const { CacheBase } = require('./index');
2
- const fs = require('fs');
3
- const path = require('path');
4
-
5
- async function testMultiFilePersistence() {
6
- console.log('====== 测试多文件持久化功能 ======');
7
-
8
- // 测试目录
9
- const testDir = './test_cache';
10
-
11
- // 清理之前的测试文件
12
- console.log('清理之前的测试文件...');
13
- if (fs.existsSync(testDir)) {
14
- const files = fs.readdirSync(testDir);
15
- for (const file of files) {
16
- if (file.endsWith('.cache.json')) {
17
- fs.unlinkSync(path.join(testDir, file));
18
- console.log(`删除文件: ${file}`);
19
- }
20
- }
21
- }
22
-
23
- try {
24
- // 创建多个不同scope的缓存实例
25
- console.log('\n创建多个缓存实例...');
26
-
27
- const cache1 = new CacheBase('scope1', {
28
- persist: true,
29
- dir: testDir
30
- });
31
-
32
- const cache2 = new CacheBase('scope2', {
33
- persist: true,
34
- dir: testDir
35
- });
36
-
37
- const cache3 = new CacheBase('scope3', {
38
- persist: true,
39
- dir: testDir
40
- });
41
-
42
- // 为每个实例设置数据
43
- console.log('\n为每个实例设置数据...');
44
-
45
- // scope1 数据
46
- await cache1.set('scope1_key1', 'scope1_value1');
47
- await cache1.set('scope1_key2', { nested: 'object1' });
48
- await cache1.set('scope1_key3', 100);
49
- console.log('scope1 设置的数据:');
50
- console.log('scope1_key1:', await cache1.get('scope1_key1'));
51
- console.log('scope1_key2:', await cache1.get('scope1_key2'));
52
- console.log('scope1_key3:', await cache1.get('scope1_key3'));
53
-
54
- // scope2 数据
55
- await cache2.set('scope2_key1', 'scope2_value1');
56
- await cache2.set('scope2_key2', { nested: 'object2' });
57
- await cache2.set('scope2_key3', 200);
58
- console.log('\nscope2 设置的数据:');
59
- console.log('scope2_key1:', await cache2.get('scope2_key1'));
60
- console.log('scope2_key2:', await cache2.get('scope2_key2'));
61
- console.log('scope2_key3:', await cache2.get('scope2_key3'));
62
-
63
- // scope3 数据
64
- await cache3.set('scope3_key1', 'scope3_value1');
65
- await cache3.set('scope3_key2', { nested: 'object3' });
66
- await cache3.set('scope3_key3', 300);
67
- console.log('\nscope3 设置的数据:');
68
- console.log('scope3_key1:', await cache3.get('scope3_key1'));
69
- console.log('scope3_key2:', await cache3.get('scope3_key2'));
70
- console.log('scope3_key3:', await cache3.get('scope3_key3'));
71
-
72
- // 手动保存数据
73
- console.log('\n手动保存所有数据...');
74
- await cache1.save();
75
- await cache2.save();
76
- await cache3.save();
77
-
78
- // 检查生成的文件
79
- console.log('\n检查生成的缓存文件:');
80
- const files = fs.readdirSync(testDir);
81
- const cacheFiles = files.filter(file => file.endsWith('.cache.json'));
82
- console.log('生成的缓存文件:', cacheFiles);
83
-
84
- // 销毁所有实例
85
- console.log('\n销毁所有实例...');
86
- await cache1.dispose();
87
- await cache2.dispose();
88
- await cache3.dispose();
89
-
90
- // 重新创建实例并验证数据隔离
91
- console.log('\n重新创建缓存实例并验证数据隔离...');
92
-
93
- // 只应该加载自己scope的数据
94
- const newCache1 = new CacheBase('scope1', {
95
- persist: true,
96
- dir: testDir
97
- });
98
-
99
- const newCache2 = new CacheBase('scope2', {
100
- persist: true,
101
- dir: testDir
102
- });
103
-
104
- const newCache3 = new CacheBase('scope3', {
105
- persist: true,
106
- dir: testDir
107
- });
108
-
109
- // 验证每个实例只加载了自己scope的数据
110
- console.log('\n验证数据隔离:');
111
-
112
- // 验证scope1只加载了自己的数据
113
- const scope1Keys = await newCache1.keys();
114
- console.log('scope1 加载的键数量:', scope1Keys.length);
115
- console.log('scope1 加载的键:', scope1Keys);
116
-
117
- // 验证scope2只加载了自己的数据
118
- const scope2Keys = await newCache2.keys();
119
- console.log('scope2 加载的键数量:', scope2Keys.length);
120
- console.log('scope2 加载的键:', scope2Keys);
121
-
122
- // 验证scope3只加载了自己的数据
123
- const scope3Keys = await newCache3.keys();
124
- console.log('scope3 加载的键数量:', scope3Keys.length);
125
- console.log('scope3 加载的键:', scope3Keys);
126
-
127
- // 验证数据正确性
128
- console.log('\n验证数据正确性:');
129
- console.log('scope1_key1:', await newCache1.get('scope1_key1'));
130
- console.log('scope2_key1:', await newCache2.get('scope2_key1'));
131
- console.log('scope3_key1:', await newCache3.get('scope3_key1'));
132
-
133
- // 验证数据隔离 - scope1不应该能访问scope2的数据
134
- console.log('\n验证数据隔离 - scope1不应访问其他scope数据:');
135
- console.log('scope1能否获取scope2_key1:', await newCache1.get('scope2_key1') === undefined ? '✓ 不能 (正确)' : '✗ 能 (错误)');
136
- console.log('scope1能否获取scope3_key1:', await newCache1.get('scope3_key1') === undefined ? '✓ 不能 (正确)' : '✗ 能 (错误)');
137
-
138
- // 清理测试数据
139
- console.log('\n清理测试数据...');
140
- await newCache1.flushdb();
141
- await newCache2.flushdb();
142
- await newCache3.flushdb();
143
- await newCache1.save();
144
- await newCache2.save();
145
- await newCache3.save();
146
-
147
- // 销毁实例
148
- await newCache1.dispose();
149
- await newCache2.dispose();
150
- await newCache3.dispose();
151
-
152
- console.log('\n多文件持久化测试完成!');
153
-
154
- } catch (error) {
155
- console.error('测试过程中发生错误:', error);
156
- }
157
- }
158
-
159
- // 运行测试
160
- testMultiFilePersistence();
@@ -1,138 +0,0 @@
1
- const { CacheBase } = require('./index');
2
- const fs = require('fs');
3
- const path = require('path');
4
-
5
- async function testPersistence() {
6
- console.log('====== 测试数据持久化功能 ======');
7
-
8
- // 清理之前的测试文件
9
- const testDir = './test_cache';
10
- const testFilePath = path.join(testDir, 'test_scope.cache.json');
11
- if (fs.existsSync(testFilePath)) {
12
- fs.unlinkSync(testFilePath);
13
- console.log(`清理之前的测试文件: ${testFilePath}`);
14
- }
15
-
16
- // 创建缓存实例,启用持久化
17
- // 注意:最新版本的构造函数签名是 (scope, config)
18
- const cache = new CacheBase('test_scope', {
19
- persist: true,
20
- dir: './test_cache',
21
- saveInterval: 5000 // 5秒保存一次
22
- });
23
-
24
- try {
25
- // 启动定时保存
26
- console.log('启动定时保存...');
27
- const connected = await cache.connect();
28
- console.log(`定时保存启动${connected ? '成功' : '失败'}`);
29
-
30
- // 设置一些测试数据
31
- console.log('\n设置测试数据...');
32
- await cache.set('key1', 'value1');
33
- await cache.set('key2', { nested: 'object', array: [1, 2, 3], boolean: true });
34
- await cache.set('key3', 123);
35
- await cache.set('key4', 'expire_value', 30); // 30秒过期
36
-
37
- // 增加更多测试数据
38
- await cache.set('number_key', 42.5);
39
- await cache.set('boolean_key', false);
40
- await cache.set('array_key', [1, 'two', { three: 3 }]);
41
- await cache.set('null_key', null);
42
- await cache.set('undefined_key', undefined);
43
- await cache.set('empty_string', '');
44
- await cache.set('long_string', '这是一个很长的字符串,用于测试不同长度的数据存储。This is a long string for testing data storage with different lengths.');
45
-
46
- // 使用哈希表功能
47
- await cache.hset('hash_key', 'field1', 'hash_value1');
48
- await cache.hset('hash_key', 'field2', 100);
49
- await cache.hset('hash_key', 'field3', { nested: 'data' });
50
-
51
- // 设置一些递增递减的值
52
- await cache.incr('counter');
53
- await cache.incrby('counter_by', 10);
54
- await cache.decr('counter_dec');
55
-
56
- // 检查设置的数据
57
- console.log('设置的数据:');
58
- console.log('key1:', await cache.get('key1'));
59
- console.log('key2:', JSON.stringify(await cache.get('key2')));
60
- console.log('key3:', await cache.get('key3'));
61
- console.log('key4:', await cache.get('key4'));
62
- console.log('新增数据示例:');
63
- console.log('array_key:', JSON.stringify(await cache.get('array_key')));
64
- console.log('hash_key 字段1:', await cache.hget('hash_key', 'field1'));
65
- console.log('counter:', await cache.get('counter'));
66
-
67
- // 获取键数量
68
- const initialKeys = await cache.keys();
69
- console.log(`\n初始设置的键数量: ${initialKeys.length}`);
70
- console.log('键列表:', initialKeys);
71
-
72
- // 手动保存数据
73
- console.log('\n手动保存数据...');
74
- const saved = await cache.save();
75
- console.log(`手动保存${saved ? '成功' : '失败'}`);
76
-
77
- // 销毁当前实例
78
- console.log('\n销毁当前实例...');
79
- await cache.dispose();
80
- console.log('实例已销毁');
81
-
82
- // 创建新实例,应该能加载之前保存的数据
83
- console.log('\n创建新实例,尝试加载数据...');
84
- const newCache = new CacheBase('test_scope', {
85
- persist: true,
86
- dir: './test_cache'
87
- });
88
-
89
- // 检查数据是否被正确加载
90
- console.log('\n加载的数据:');
91
- console.log('key1:', await newCache.get('key1'));
92
- console.log('key2:', JSON.stringify(await newCache.get('key2')));
93
- console.log('key3:', await newCache.get('key3'));
94
- console.log('key4:', await newCache.get('key4'));
95
-
96
- // 验证新加的数据是否正确加载
97
- console.log('\n验证新加的数据:');
98
- console.log('number_key:', await newCache.get('number_key'));
99
- console.log('boolean_key:', await newCache.get('boolean_key'));
100
- console.log('array_key:', JSON.stringify(await newCache.get('array_key')));
101
- console.log('null_key:', await newCache.get('null_key'));
102
- console.log('undefined_key:', await newCache.get('undefined_key'));
103
- console.log('empty_string:', JSON.stringify(await newCache.get('empty_string')));
104
- console.log('long_string 长度:', (await newCache.get('long_string')).length);
105
-
106
- // 验证哈希表数据
107
- console.log('\n验证哈希表数据:');
108
- console.log('hash_key 字段1:', await newCache.hget('hash_key', 'field1'));
109
- console.log('hash_key 字段2:', await newCache.hget('hash_key', 'field2'));
110
- console.log('hash_key 字段3:', JSON.stringify(await newCache.hget('hash_key', 'field3')));
111
-
112
- // 验证计数数据
113
- console.log('\n验证计数数据:');
114
- console.log('counter:', await newCache.get('counter'));
115
- console.log('counter_by:', await newCache.get('counter_by'));
116
- console.log('counter_dec:', await newCache.get('counter_dec'));
117
-
118
- // 验证键的数量
119
- const loadedKeys = await newCache.keys();
120
- console.log('\n加载的键数量:', loadedKeys.length);
121
- console.log('键列表:', loadedKeys);
122
-
123
- // 清理测试数据
124
- console.log('\n清理测试数据...');
125
- await newcache.clear();
126
- await newCache.save();
127
- await newCache.dispose();
128
-
129
- console.log('\n测试完成!');
130
-
131
- } catch (error) {
132
- console.error('测试过程中发生错误:', error);
133
- await cache.dispose().catch(() => {});
134
- }
135
- }
136
-
137
- // 运行测试
138
- testPersistence();
@@ -1,57 +0,0 @@
1
- const { CacheBase } = require('./index.js');
2
-
3
- async function testRedisBehavior() {
4
- console.log('测试Redis兼容行为...');
5
- const cache = new CacheBase();
6
-
7
- // 清空缓存
8
- await cache.clear();
9
-
10
- console.log('\n===== 测试1: 键不存在时设置数值 =====');
11
- const result1 = await cache.add('num_key', 10);
12
- console.log(`add('num_key', 10) = ${result1}`);
13
- console.log(`get('num_key') = ${await cache.get('num_key')}`);
14
-
15
- console.log('\n===== 测试2: 键存在时增加值 =====');
16
- const result2 = await cache.add('num_key', 5);
17
- console.log(`add('num_key', 5) = ${result2}`);
18
- console.log(`get('num_key') = ${await cache.get('num_key')}`);
19
-
20
- console.log('\n===== 测试3: 字符串形式的数字 =====');
21
- const result3 = await cache.add('num_key2', '123');
22
- console.log(`add('num_key2', '123') = ${result3}`);
23
- console.log(`get('num_key2') = ${await cache.get('num_key2')}`);
24
-
25
- console.log('\n===== 测试4: 非数值字符串输入 =====');
26
- try {
27
- await cache.add('num_key3', 'abc');
28
- } catch (error) {
29
- console.log(`预期的错误: ${error.message}`);
30
- }
31
-
32
- console.log('\n===== 测试5: 现有值为非数值时 =====');
33
- // 先设置一个字符串值
34
- await cache.set('str_key', 'hello');
35
- console.log(`set('str_key', 'hello') 后的值: ${await cache.get('str_key')}`);
36
-
37
- // 尝试对字符串值执行add操作(应该抛出错误,符合Redis行为)
38
- try {
39
- await cache.add('str_key', 10);
40
- } catch (error) {
41
- console.log(`预期的错误: ${error.message}`);
42
- }
43
-
44
- console.log('\n===== 测试6: 现有值为数字字符串时 =====');
45
- // 设置一个数字字符串值
46
- await cache.set('num_str_key', '42');
47
- console.log(`set('num_str_key', '42') 后的值: ${await cache.get('num_str_key')}`);
48
-
49
- // 对数字字符串执行add操作(应该成功,符合Redis行为)
50
- const result6 = await cache.add('num_str_key', 8);
51
- console.log(`add('num_str_key', 8) = ${result6}`);
52
- console.log(`get('num_str_key') = ${await cache.get('num_str_key')}`);
53
-
54
- console.log('\n所有测试完成!');
55
- }
56
-
57
- testRedisBehavior().catch(console.error);
@@ -1,57 +0,0 @@
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();