mm_cachebase 2.0.3 → 2.0.5
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/example/index.js +53 -0
- package/index.js +9 -3141
- package/lib/cachebase.js +3125 -0
- package/package.json +8 -2
- package/test.js +49 -37
package/example/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mm_cachebase 模块最小可运行示例
|
|
3
|
+
* @description 演示 CacheBase 本地内存缓存的核心 API,不依赖任何外部服务
|
|
4
|
+
*/
|
|
5
|
+
// 独立运行时补充全局 $ 对象(mm_os 宿主环境已提供,npm 包运行需自备)
|
|
6
|
+
if (!global.$) {
|
|
7
|
+
global.$ = {
|
|
8
|
+
log: {
|
|
9
|
+
debug: console.debug ? console.debug.bind(console) : console.log.bind(console),
|
|
10
|
+
info: console.info ? console.info.bind(console) : console.log.bind(console),
|
|
11
|
+
warn: console.warn ? console.warn.bind(console) : console.log.bind(console),
|
|
12
|
+
error: console.error ? console.error.bind(console) : console.log.bind(console),
|
|
13
|
+
success: console.log.bind(console)
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const { CacheBase } = require('../index.js');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 主演示流程
|
|
22
|
+
*/
|
|
23
|
+
async function main() {
|
|
24
|
+
const cache = new CacheBase({ scope: 'demo', persist: false });
|
|
25
|
+
await cache.init();
|
|
26
|
+
console.log('已连接:', cache.isConnected());
|
|
27
|
+
|
|
28
|
+
// 基础键值操作
|
|
29
|
+
await cache.set('name', 'mm_cachebase');
|
|
30
|
+
console.log('set/get:', await cache.get('name'));
|
|
31
|
+
|
|
32
|
+
// 过期时间
|
|
33
|
+
await cache.set('temp', '10 秒后过期', 10);
|
|
34
|
+
console.log('ttl:', await cache.ttl('temp'));
|
|
35
|
+
|
|
36
|
+
// 原子自增操作
|
|
37
|
+
console.log('incr:', await cache.incr('counter'));
|
|
38
|
+
|
|
39
|
+
// 哈希表操作
|
|
40
|
+
await cache.hset('user', 'id', '001');
|
|
41
|
+
console.log('hset/hget:', await cache.hget('user', 'id'));
|
|
42
|
+
|
|
43
|
+
// 删除键,返回删除数量
|
|
44
|
+
console.log('del 删除数量:', await cache.del('name'));
|
|
45
|
+
|
|
46
|
+
await cache.close();
|
|
47
|
+
console.log('示例运行完成');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
main().catch((err) => {
|
|
51
|
+
console.error('示例运行失败:', err);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
});
|