mm_cachebase 1.8.6 → 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,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();