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.
@@ -0,0 +1 @@
1
+ {"data":{"test_key1":"test_value1","test_key2":{"complex":"data"}},"expires":{},"types":{"test_key1":"string","test_key2":"object"},"timestamp":1760329459329}
@@ -0,0 +1,12 @@
1
+ {
2
+ "data": {
3
+ "test_key": {
4
+ "value": "test_value",
5
+ "expire": 0
6
+ }
7
+ },
8
+ "list": [
9
+ "test_key"
10
+ ],
11
+ "timestamp": 1760319208888
12
+ }
@@ -0,0 +1,170 @@
1
+ const { CacheBase } = require('./index.js');
2
+
3
+ // 扩展测试脚本,测试新添加的方法
4
+ async function testNewMethods() {
5
+ $.log.debug('[CacheBase] \n========== 测试新添加的方法 ==========');
6
+
7
+ try {
8
+ const runner = new TestRunner();
9
+ const cache = new CacheBase();
10
+
11
+ // 清空缓存,确保测试环境干净
12
+ await cache.clear();
13
+
14
+ // 测试列表操作方法
15
+ $.log.debug('[CacheBase] \n---------- 测试列表操作方法 ----------');
16
+
17
+ await runner.runTest('setForList方法', async () => {
18
+ const result = await cache.setForList('list1', ['item1', 'item2', 'item3']);
19
+ if (result !== 3) {
20
+ throw new Error(`setForList失败,预期长度: 3, 实际: ${result}`);
21
+ }
22
+
23
+ const list = await cache.getForList('list1');
24
+ if (!Array.isArray(list) || list.length !== 3 || !list.includes('item1') || !list.includes('item2') || !list.includes('item3')) {
25
+ throw new Error(`setForList后数据错误,实际: ${JSON.stringify(list)}`);
26
+ }
27
+ });
28
+
29
+ await runner.runTest('addForList方法', async () => {
30
+ const result = await cache.addForList('list1', 'item4');
31
+ if (result !== 4) {
32
+ throw new Error(`addForList失败,预期长度: 4, 实际: ${result}`);
33
+ }
34
+
35
+ const list = await cache.getForList('list1');
36
+ if (list.length !== 4 || list[3] !== 'item4') {
37
+ throw new Error(`addForList后数据错误,实际: ${JSON.stringify(list)}`);
38
+ }
39
+ });
40
+
41
+ await runner.runTest('hasForList方法', async () => {
42
+ const hasItem = await cache.hasForList('list1', 'item2');
43
+ if (!hasItem) {
44
+ throw new Error(`hasForList应返回true但返回了false`);
45
+ }
46
+
47
+ const hasNonExistent = await cache.hasForList('list1', 'nonExistent');
48
+ if (hasNonExistent) {
49
+ throw new Error(`hasForList应返回false但返回了true`);
50
+ }
51
+ });
52
+
53
+ await runner.runTest('getForList方法(索引)', async () => {
54
+ const partialList = await cache.getForList('list1', 1, 2);
55
+ if (!Array.isArray(partialList) || partialList.length !== 2 || partialList[0] !== 'item2' || partialList[1] !== 'item3') {
56
+ throw new Error(`getForList索引查询失败,实际: ${JSON.stringify(partialList)}`);
57
+ }
58
+ });
59
+
60
+ await runner.runTest('clearForList方法', async () => {
61
+ const result = await cache.clearForList('list1');
62
+ if (!result) {
63
+ throw new Error(`clearForList失败,预期: true, 实际: ${result}`);
64
+ }
65
+
66
+ const list = await cache.getForList('list1');
67
+ if (!Array.isArray(list) || list.length !== 0) {
68
+ throw new Error(`clearForList后列表不为空,实际: ${JSON.stringify(list)}`);
69
+ }
70
+ });
71
+
72
+ // 测试哈希表新增方法
73
+ $.log.debug('[CacheBase] \n---------- 测试哈希表新增方法 ----------');
74
+
75
+ await runner.runTest('hmset方法', async () => {
76
+ const result = await cache.hmset('hash2', { field1: 'value1', field2: 'value2', field3: 'value3' });
77
+ if (!result) {
78
+ throw new Error(`hmset失败,预期: true, 实际: ${result}`);
79
+ }
80
+
81
+ const hash = await cache.hgetall('hash2');
82
+ if (hash.field1 !== 'value1' || hash.field2 !== 'value2' || hash.field3 !== 'value3') {
83
+ throw new Error(`hmset后数据错误,实际: ${JSON.stringify(hash)}`);
84
+ }
85
+ });
86
+
87
+ await runner.runTest('hmget方法', async () => {
88
+ const values = await cache.hmget('hash2', ['field1', 'field2', 'field3', 'nonExistent']);
89
+ if (JSON.stringify(values) !== JSON.stringify(['value1', 'value2', 'value3', null])) {
90
+ throw new Error(`hmget失败,预期: ["value1","value2","value3",null], 实际: ${JSON.stringify(values)}`);
91
+ }
92
+ });
93
+
94
+ await runner.runTest('hlen方法', async () => {
95
+ const length = await cache.hlen('hash2');
96
+ if (length !== 3) {
97
+ throw new Error(`hlen失败,预期: 3, 实际: ${length}`);
98
+ }
99
+
100
+ // 不存在的键
101
+ const nonExistentLength = await cache.hlen('nonExistentHash');
102
+ if (nonExistentLength !== 0) {
103
+ throw new Error(`hlen不存在键失败,预期: 0, 实际: ${nonExistentLength}`);
104
+ }
105
+ });
106
+
107
+ // 显示测试结果
108
+ const passRate = runner.displayResults();
109
+ $.log.debug(`[CacheBase] \n新方法测试完成!通过率: ${passRate}%`);
110
+
111
+ return runner.passedTests === runner.totalTests;
112
+
113
+ } catch (error) {
114
+ $.log.error('[CacheBase] 新方法测试过程中出现错误:', error);
115
+ return false;
116
+ }
117
+ }
118
+
119
+ // 复用TestRunner类
120
+ class TestRunner {
121
+ constructor() {
122
+ this.totalTests = 0;
123
+ this.passedTests = 0;
124
+ this.results = [];
125
+ }
126
+
127
+ async runTest(name, testFunction) {
128
+ this.totalTests++;
129
+ try {
130
+ await testFunction();
131
+ this.passedTests++;
132
+ this.results.push({ name, passed: true });
133
+ $.log.debug(`✅ ${name}`);
134
+ } catch (error) {
135
+ this.results.push({ name, passed: false, error: error.message });
136
+ $.log.debug(`❌ ${name} - ${error.message}`);
137
+ }
138
+ }
139
+
140
+ displayResults() {
141
+ $.log.debug('\n========== 新方法测试结果 ==========');
142
+ const passRate = (this.passedTests / this.totalTests * 100).toFixed(2);
143
+ $.log.debug(`通过: ${this.passedTests}/${this.totalTests} (${passRate}%)`);
144
+
145
+ // 显示失败的测试
146
+ const failedTests = this.results.filter(r => !r.passed);
147
+ if (failedTests.length > 0) {
148
+ $.log.debug('\n失败的测试:');
149
+ failedTests.forEach(test => {
150
+ $.log.debug(` - ${test.name}: ${test.error}`);
151
+ });
152
+ }
153
+
154
+ return passRate;
155
+ }
156
+ }
157
+
158
+ // 运行测试
159
+ testNewMethods().then(success => {
160
+ if (success) {
161
+ $.log.debug('[CacheBase] 新方法测试全部通过!');
162
+ process.exit(0);
163
+ } else {
164
+ $.log.error('[CacheBase] 新方法测试失败!');
165
+ process.exit(1);
166
+ }
167
+ }).catch(err => {
168
+ $.log.error('[CacheBase] 新方法测试未捕获的错误:', err);
169
+ process.exit(1);
170
+ });
@@ -1,101 +0,0 @@
1
- # 四种缓存方式方法兼容性对比表格
2
-
3
- ## 核心缓存方法
4
-
5
- | 方法名 | Redis | MongoDB | CacheBase | Memory | 状态说明 |
6
- |-------|-------|---------|-----------|--------|----------|
7
- | set | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
8
- | get | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
9
- | del | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
10
- | has | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
11
- | clear | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
12
- | keys | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
13
- | ttl | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
14
- | add | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
15
- | addInt | ✅ | ❌ | ✅ | ✅ | CacheBase和Memory支持,Redis支持,MongoDB不支持 |
16
- | addStr | ✅ | ❌ | ✅ | ✅ | CacheBase和Memory支持,Redis支持,MongoDB不支持 |
17
- | sort | ✅ | ✅ | ❌ | ❌ | Redis和MongoDB支持,其他方式不支持 |
18
- | exists | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
19
-
20
- ## Redis兼容方法 - 哈希表操作
21
-
22
- | 方法名 | Redis | MongoDB | CacheBase | Memory | 状态说明 |
23
- |-------|-------|---------|-----------|--------|----------|
24
- | hset | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
25
- | hget | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
26
- | hgetall | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
27
- | hdel | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
28
- | hmset | ✅ | ✅ | ❌ | ❌ | Redis和MongoDB支持,其他方式不支持 |
29
- | hmget | ❌ | ✅ | ❌ | ❌ | 仅MongoDB支持 |
30
- | hlen | ✅ | ✅ | ❌ | ❌ | Redis和MongoDB支持,其他方式不支持 |
31
-
32
- ## Redis兼容方法 - 其他常用命令
33
-
34
- | 方法名 | Redis | MongoDB | CacheBase | Memory | 状态说明 |
35
- |-------|-------|---------|-----------|--------|----------|
36
- | expire | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
37
- | incr | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
38
- | decr | ✅ | ❌ | ✅ | ✅ | Redis、CacheBase和Memory支持,MongoDB不支持 |
39
- | mget | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
40
- | mset | ✅ | ✅ | ✅ | ✅ | 所有方式都支持 |
41
- | pexpire | ❌ | ❌ | ❌ | ❌ | 所有方式都不支持 |
42
- | persist | ✅ | ❌ | ✅ | ✅ | Redis、CacheBase和Memory支持,MongoDB不支持 |
43
- | strlen | ✅ | ❌ | ✅ | ❌ | Redis和CacheBase支持,其他方式不支持 |
44
- | addFloat | ✅ | ❌ | ✅ | ✅ | Redis、CacheBase和Memory支持,MongoDB不支持 |
45
- | type | ❌ | ❌ | ❌ | ❌ | 所有方式都不支持 |
46
- | rename | ❌ | ❌ | ❌ | ❌ | 所有方式都不支持 |
47
-
48
- ## Redis兼容方法 - 集合操作
49
-
50
- | 方法名 | Redis | MongoDB | CacheBase | Memory | 状态说明 |
51
- |-------|-------|---------|-----------|--------|----------|
52
- | sadd | ✅ | ❌ | ❌ | ❌ | 仅Redis支持 |
53
- | srem | ✅ | ❌ | ❌ | ❌ | 仅Redis支持 |
54
- | sismember | ✅ | ✅ | ❌ | ❌ | Redis和MongoDB支持,其他方式不支持 |
55
- | smembers | ✅ | ✅ | ❌ | ❌ | Redis和MongoDB支持,其他方式不支持 |
56
-
57
- ## 数组/列表操作方法
58
-
59
- | 方法名 | Redis | MongoDB | CacheBase | Memory | 状态说明 |
60
- |-------|-------|---------|-----------|--------|----------|
61
- | list_set | ✅ | ❌ | ✅ | ✅ | Redis、CacheBase和Memory支持,返回列表长度,MongoDB不支持 |
62
- | list_add | ✅ | ❌ | ✅ | ✅ | Redis、CacheBase和Memory支持,返回列表长度,MongoDB不支持 |
63
- | list_has | ✅ | ❌ | ✅ | ✅ | Redis、CacheBase和Memory支持,返回1或0,MongoDB不支持 |
64
- | list_get | ✅ | ❌ | ✅ | ✅ | Redis、CacheBase和Memory支持,支持范围获取,MongoDB不支持 |
65
- | list_clear | ✅ | ❌ | ✅ | ✅ | MongoDB不支持,其他方式支持 |
66
-
67
- ## 兼容性报告总结
68
-
69
- 基于实际功能测试,以下是四种缓存方式的兼容性报告:
70
-
71
- | 方法类别 | CacheBase | Redis | MongoDB | Memory | 状态说明 |
72
- |---------|-----------|-------|---------|--------|----------|
73
- | 基础键值操作 | ✅ | ✅ | ✅ | ✅ | 所有缓存方式均正常工作 |
74
- | 数组操作 | ✅ | ✅ | ❌ | ✅ | Redis、CacheBase和Memory支持,MongoDB完全不支持 |
75
- | 过期时间操作 | ✅ | ✅ | ✅ | ✅ | 基本过期时间操作支持,但MongoDB不支持persist |
76
- | 原子递增操作 | ✅ | ✅ | ✅ | ✅ | incr和decr都支持,但MongoDB不支持decr |
77
- | 批量操作 | ✅ | ✅ | ✅ | ✅ | 所有方式都支持mset和mget方法 |
78
- | 哈希表基本操作 | ✅ | ✅ | ✅ | ✅ | 基本操作均支持 |
79
- | 哈希表批量操作 | ❌ | ✅ | ✅ | ❌ | Redis和MongoDB支持,其他方式不支持 |
80
- | 集合操作 | ❌ | ✅ | ⚠️ | ❌ | Redis完整支持,MongoDB支持部分方法,其他方式不支持 |
81
- | 字符串操作 | ✅ | ✅ | ❌ | ✅ | Redis、CacheBase和Memory支持字符串操作方法 |
82
- | 类型检测 | ❌ | ❌ | ❌ | ❌ | 所有方式都不支持 |
83
- | 键重命名 | ❌ | ❌ | ❌ | ❌ | 所有方式都不支持 |
84
- | 清理操作 | ✅ | ✅ | ✅ | ✅ | clear操作正常工作 |
85
-
86
- ### 测试统计
87
-
88
- - **总测试用例**:39个方法(增加了addFloat方法)
89
- - **通过数**:不同缓存方式通过数量不同(Redis最多)
90
- - **整体支持情况**:
91
- - Redis:支持31个方法,不支持7个,行为不一致0个
92
- - MongoDB:支持23个方法,不支持15个,行为不一致0个
93
- - CacheBase:支持31个方法,不支持7个,行为不一致0个
94
- - Memory:支持31个方法,不支持7个,行为不一致0个
95
- - **注意事项**:
96
- 1. CacheBase和Memory的行为完全一致
97
- 2. Redis、CacheBase和Memory在所有实现的方法上表现一致
98
- 3. MongoDB完全不支持列表操作方法(list_*)
99
- 4. 大部分核心功能在Redis、CacheBase和Memory中表现一致
100
- 5. 所有缓存方式在基本功能上表现稳定可靠
101
- 6. 集合操作只有Redis完整支持
@@ -1,60 +0,0 @@
1
- // 测试add方法直接保存任意值的行为
2
- const $ = require('./index');
3
-
4
- async function testAddMethod() {
5
- console.log('开始测试add方法 - 直接保存任意值');
6
- const cache = new $.CacheBase();
7
-
8
- try {
9
- // 测试1: 保存数字值
10
- console.log('测试1: 保存数字值');
11
- const result1 = await cache.add('numKey', 10);
12
- console.log(` 保存数字10,返回值: ${result1}`);
13
- const value1 = await cache.get('numKey');
14
- console.log(` 获取值: ${value1}`);
15
-
16
- // 测试2: 再次保存数字值(覆盖)
17
- console.log('测试2: 再次保存数字值(覆盖)');
18
- const result2 = await cache.add('numKey', 20);
19
- console.log(` 保存数字20,返回值: ${result2}`);
20
- const value2 = await cache.get('numKey');
21
- console.log(` 获取值: ${value2} (应该是20,不是30)`);
22
-
23
- // 测试3: 保存字符串
24
- console.log('测试3: 保存字符串');
25
- const result3 = await cache.add('strKey', 'hello');
26
- console.log(` 保存字符串'hello',返回值: ${result3}`);
27
- const value3 = await cache.get('strKey');
28
- console.log(` 获取值: ${value3}`);
29
-
30
- // 测试4: 保存对象
31
- console.log('测试4: 保存对象');
32
- const objValue = { name: 'test', age: 25 };
33
- const result4 = await cache.add('objKey', objValue);
34
- console.log(` 保存对象,返回值类型: ${typeof result4}`);
35
- const value4 = await cache.get('objKey');
36
- console.log(` 获取对象name属性: ${value4.name}`);
37
-
38
- // 测试5: 保存数组
39
- console.log('测试5: 保存数组');
40
- const arrValue = [1, 2, 3, 4, 5];
41
- const result5 = await cache.add('arrKey', arrValue);
42
- console.log(` 保存数组,返回值长度: ${result5.length}`);
43
- const value5 = await cache.get('arrKey');
44
- console.log(` 获取数组第一个元素: ${value5[0]}`);
45
-
46
- // 测试6: 保存布尔值
47
- console.log('测试6: 保存布尔值');
48
- const result6 = await cache.add('boolKey', true);
49
- console.log(` 保存布尔值true,返回值: ${result6}`);
50
- const value6 = await cache.get('boolKey');
51
- console.log(` 获取值: ${value6}`);
52
-
53
- console.log('\n所有测试完成!add方法现在直接保存任意值,不执行递增操作。');
54
- console.log('注意:这会导致原始测试套件中的add方法测试失败,因为它们期望递增行为。');
55
- } catch (error) {
56
- console.error('测试失败:', error);
57
- }
58
- }
59
-
60
- testAddMethod();
@@ -1,50 +0,0 @@
1
- // 测试add方法 - 如果不存在则添加
2
- const $ = require('./index');
3
-
4
- async function testAddMethod() {
5
- console.log('开始测试add方法 - 如果不存在则添加');
6
- const cache = new $.CacheBase();
7
-
8
- try {
9
- // 测试1: 添加不存在的键
10
- console.log('测试1: 添加不存在的键');
11
- const result1 = await cache.add('newKey', 'newValue');
12
- console.log(` 添加结果: ${result1} (应该是true)`);
13
- const value1 = await cache.get('newKey');
14
- console.log(` 获取值: ${value1} (应该是newValue)`);
15
-
16
- // 测试2: 再次添加同一个键(应该失败)
17
- console.log('测试2: 再次添加同一个键(应该失败)');
18
- const result2 = await cache.add('newKey', 'updatedValue');
19
- console.log(` 添加结果: ${result2} (应该是false)`);
20
- const value2 = await cache.get('newKey');
21
- console.log(` 获取值: ${value2} (应该仍然是newValue)`);
22
-
23
- // 测试3: 添加不同类型的值
24
- console.log('测试3: 添加不同类型的值');
25
- const result3 = await cache.add('numKey', 123);
26
- console.log(` 添加数字结果: ${result3} (应该是true)`);
27
- const result4 = await cache.add('objKey', { name: 'test' });
28
- console.log(` 添加对象结果: ${result4} (应该是true)`);
29
-
30
- // 测试4: 带过期时间的添加
31
- console.log('测试4: 带过期时间的添加');
32
- const result5 = await cache.add('expireKey', 'expireValue', 1); // 1秒过期
33
- console.log(` 添加带过期时间结果: ${result5} (应该是true)`);
34
-
35
- // 测试5: 过期后再次添加
36
- console.log('测试5: 过期后再次添加');
37
- console.log(' 等待1秒让键过期...');
38
- await new Promise(resolve => setTimeout(resolve, 1100));
39
- const result6 = await cache.add('expireKey', 'newExpireValue');
40
- console.log(` 过期后添加结果: ${result6} (应该是true)`);
41
- const value3 = await cache.get('expireKey');
42
- console.log(` 获取过期后重新添加的值: ${value3} (应该是newExpireValue)`);
43
-
44
- console.log('\n所有测试完成!add方法现在正确实现了"如果不存在则添加"的行为。');
45
- } catch (error) {
46
- console.error('测试失败:', error);
47
- }
48
- }
49
-
50
- testAddMethod();
@@ -1,44 +0,0 @@
1
- const { CacheBase } = require('./index.js');
2
-
3
- async function testAddMethod() {
4
- console.log('测试add方法行为...');
5
- const cache = new CacheBase();
6
-
7
- // 测试1: 键不存在时设置值
8
- console.log('\n测试1: 键不存在时设置值');
9
- const result1 = await cache.add('test_key1', 10);
10
- console.log(`add('test_key1', 10) = ${result1}`);
11
- console.log(`get('test_key1') = ${await cache.get('test_key1')}`);
12
-
13
- // 测试2: 键存在时增加值
14
- console.log('\n测试2: 键存在时增加值');
15
- const result2 = await cache.add('test_key1', 5);
16
- console.log(`add('test_key1', 5) = ${result2}`);
17
- console.log(`get('test_key1') = ${await cache.get('test_key1')}`);
18
-
19
- // 测试3: 字符串值的处理
20
- console.log('\n测试3: 字符串值的处理');
21
- try {
22
- const result3 = await cache.add('test_key2', 'test');
23
- console.log(`add('test_key2', 'test') = ${result3}`);
24
- console.log(`get('test_key2') = ${await cache.get('test_key2')}`);
25
- } catch (error) {
26
- console.log(`add('test_key2', 'test') 抛出错误: ${error.message}`);
27
- }
28
-
29
- // 测试4: 非数值字符串转换
30
- console.log('\n测试4: 非数值字符串转换');
31
- const result4 = await cache.add('test_key3', '123');
32
- console.log(`add('test_key3', '123') = ${result4}`);
33
- console.log(`get('test_key3') = ${await cache.get('test_key3')}`);
34
-
35
- // 测试5: 带过期时间
36
- console.log('\n测试5: 带过期时间');
37
- const result5 = await cache.add('test_key4', 100, 5); // 5秒过期
38
- console.log(`add('test_key4', 100, 5) = ${result5}`);
39
- console.log(`ttl('test_key4') = ${await cache.ttl('test_key4')}`);
40
-
41
- console.log('\n测试完成!');
42
- }
43
-
44
- testAddMethod().catch(console.error);
@@ -1,5 +0,0 @@
1
- {
2
- "data": {},
3
- "list": [],
4
- "timestamp": 1759920414864
5
- }
@@ -1,5 +0,0 @@
1
- {
2
- "data": {},
3
- "list": [],
4
- "timestamp": 1759920414866
5
- }
@@ -1,5 +0,0 @@
1
- {
2
- "data": {},
3
- "list": [],
4
- "timestamp": 1759920414869
5
- }
@@ -1,5 +0,0 @@
1
- {
2
- "data": {},
3
- "list": [],
4
- "timestamp": 1759920431894
5
- }
@@ -1,109 +0,0 @@
1
- const $ = require('./index');
2
- const assert = require('assert');
3
-
4
- async function runTests() {
5
- console.log('开始测试 CacheBase 兼容方法...');
6
-
7
- // 创建缓存实例
8
- const cache = new $.CacheBase('test_compatibility');
9
-
10
- try {
11
- // 测试 list_* 方法
12
- console.log('\n=== 测试 list_* 方法 ===');
13
-
14
- // 测试 list_set - 确保值始终是数组类型
15
- await cache.list_set('list1', 'single value');
16
- let result = await cache.list_get('list1');
17
- console.log('list_set 单值测试:', Array.isArray(result), result);
18
- assert.strictEqual(Array.isArray(result), true, 'list_set 应确保值是数组类型');
19
- assert.strictEqual(result.length, 1, 'list_set 应将单值包装成数组');
20
-
21
- // 测试 list_set - 数组值
22
- await cache.list_set('list2', [1, 2, 3]);
23
- result = await cache.list_get('list2');
24
- console.log('list_set 数组测试:', Array.isArray(result), result);
25
- assert.strictEqual(Array.isArray(result), true, 'list_set 应保持数组类型');
26
- assert.strictEqual(result.length, 3, 'list_set 应正确存储数组');
27
-
28
- // 测试 list_add - 向空列表添加
29
- await cache.list_add('list3', 'item1');
30
- result = await cache.list_get('list3');
31
- console.log('list_add 空列表测试:', result);
32
- assert.strictEqual(result.length, 1, 'list_add 应向空列表添加元素');
33
-
34
- // 测试 list_add - 向现有列表添加
35
- await cache.list_add('list3', 'item2');
36
- result = await cache.list_get('list3');
37
- console.log('list_add 现有列表测试:', result);
38
- assert.strictEqual(result.length, 2, 'list_add 应向现有列表添加元素');
39
-
40
- // 测试 list_has - 基本类型
41
- let hasValue = await cache.list_has('list3', 'item1');
42
- console.log('list_has 基本类型测试:', hasValue);
43
- assert.strictEqual(hasValue, true, 'list_has 应正确检测基本类型');
44
-
45
- // 测试 list_has - 对象类型
46
- const objValue = { name: 'test', value: 123 };
47
- await cache.list_add('list4', objValue);
48
- hasValue = await cache.list_has('list4', { name: 'test', value: 123 });
49
- console.log('list_has 对象类型测试:', hasValue);
50
- assert.strictEqual(hasValue, true, 'list_has 应正确检测对象类型');
51
-
52
- // 测试 list_clear - 清空现有列表
53
- result = await cache.list_clear('list3');
54
- console.log('list_clear 现有列表测试:', result);
55
- assert.strictEqual(result, true, 'list_clear 应对现有列表返回 true');
56
- result = await cache.list_get('list3');
57
- assert.strictEqual(Array.isArray(result), true, 'list_clear 后应返回数组');
58
- assert.strictEqual(result.length, 0, 'list_clear 应清空列表');
59
-
60
- // 测试 list_clear - 不存在的键
61
- result = await cache.list_clear('nonexistent_list');
62
- console.log('list_clear 不存在键测试:', result);
63
- assert.strictEqual(result, true, 'list_clear 应对不存在的键创建空列表并返回 true');
64
- result = await cache.list_get('nonexistent_list');
65
- assert.strictEqual(Array.isArray(result), true, 'list_clear 后应创建空数组');
66
-
67
- // 测试 hdel 方法
68
- console.log('\n=== 测试 hdel 方法 ===');
69
-
70
- // 创建哈希表
71
- await cache.hset('hash1', 'field1', 'value1');
72
- await cache.hset('hash1', 'field2', 'value2');
73
-
74
- // 测试 hdel - 存在的字段
75
- result = await cache.hdel('hash1', 'field1');
76
- console.log('hdel 存在字段测试:', result);
77
- assert.strictEqual(result, 0, 'hdel 应始终返回 0 以保持兼容性');
78
-
79
- // 验证字段已被删除
80
- let hashValue = await cache.hget('hash1', 'field1');
81
- assert.strictEqual(hashValue, undefined, 'hdel 应实际删除字段');
82
-
83
- // 测试 hdel - 不存在的字段
84
- result = await cache.hdel('hash1', 'nonexistent_field');
85
- console.log('hdel 不存在字段测试:', result);
86
- assert.strictEqual(result, 0, 'hdel 应对不存在的字段返回 0');
87
-
88
- // 测试 hdel - 不存在的键
89
- result = await cache.hdel('nonexistent_hash', 'field');
90
- console.log('hdel 不存在键测试:', result);
91
- assert.strictEqual(result, 0, 'hdel 应对不存在的键返回 0');
92
-
93
- console.log('\n所有测试通过!');
94
- } catch (error) {
95
- console.error('测试失败:', error.message);
96
- throw error;
97
- } finally {
98
- // 清理测试数据
99
- await cache.clear();
100
- await cache.dispose();
101
- console.log('\n测试完成,已清理数据');
102
- }
103
- }
104
-
105
- // 运行测试
106
- runTests().catch(error => {
107
- console.error('测试执行失败:', error);
108
- process.exit(1);
109
- });