mm_cachebase 1.8.9 → 1.9.1
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/index.js +146 -83
- package/package.json +2 -2
- package/index_old.js +0 -2521
- package/simple_test.js +0 -21
- package/simple_test_direct.js +0 -27
- package/test_cache/test_file_operations.json +0 -1
- package/test_cache/undefined.cache.json +0 -12
- package/test_new_methods.js +0 -170
package/simple_test.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// 简单的模块加载测试
|
|
2
|
-
$.log.debug('尝试加载CacheBase模块...');
|
|
3
|
-
try {
|
|
4
|
-
const { CacheBase } = require('./index.js');
|
|
5
|
-
$.log.debug('成功加载CacheBase模块!');
|
|
6
|
-
const cache = new CacheBase();
|
|
7
|
-
$.log.debug('成功创建CacheBase实例!');
|
|
8
|
-
$.log.debug('模块导出检查:', Object.keys(require('./index.js')));
|
|
9
|
-
} catch (error) {
|
|
10
|
-
$.log.error('模块加载失败:', error);
|
|
11
|
-
// 检查文件是否存在
|
|
12
|
-
const fs = require('fs');
|
|
13
|
-
const stats = fs.statSync('./index.js');
|
|
14
|
-
$.log.debug('文件大小:', stats.size, 'bytes');
|
|
15
|
-
// 检查文件末尾
|
|
16
|
-
const buffer = Buffer.alloc(100);
|
|
17
|
-
const fd = fs.openSync('./index.js', 'r');
|
|
18
|
-
const bytesRead = fs.readSync(fd, buffer, 0, 100, stats.size - 100);
|
|
19
|
-
fs.closeSync(fd);
|
|
20
|
-
$.log.debug('文件末尾内容:', buffer.toString('utf8', 0, bytesRead));
|
|
21
|
-
}
|
package/simple_test_direct.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
// 简单测试脚本
|
|
2
|
-
const { CacheBase } = require('./index.js');
|
|
3
|
-
|
|
4
|
-
async function simpleTest() {
|
|
5
|
-
$.log.debug('开始简单测试...');
|
|
6
|
-
const cache = new CacheBase();
|
|
7
|
-
|
|
8
|
-
// 测试incr方法
|
|
9
|
-
$.log.debug('测试incr方法:');
|
|
10
|
-
const result = cache.incr('test_counter');
|
|
11
|
-
$.log.debug('incr结果:', result);
|
|
12
|
-
$.log.debug('当前_data对象:', cache._data);
|
|
13
|
-
$.log.debug('当前_types对象:', cache._types);
|
|
14
|
-
|
|
15
|
-
// 直接设置一个值再测试
|
|
16
|
-
$.log.debug('\n直接设置值后测试:');
|
|
17
|
-
cache._data.test_counter2 = 5;
|
|
18
|
-
const result2 = cache.incr('test_counter2');
|
|
19
|
-
$.log.debug('incr结果2:', result2);
|
|
20
|
-
$.log.debug('当前_data对象:', cache._data);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
simpleTest().catch($.log.error);
|
|
24
|
-
|
|
25
|
-
setTimeout(() => {
|
|
26
|
-
process.exit(0);
|
|
27
|
-
}, 5000);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"data":{"test_key1":"test_value1","test_key2":{"complex":"data"}},"expires":{},"types":{"test_key1":"string","test_key2":"object"},"timestamp":1760329459329}
|
package/test_new_methods.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
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
|
-
});
|