mm_cache 1.5.4 → 1.5.6
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 +57 -0
- package/index.js +14 -868
- package/lib/cache.js +832 -0
- package/package.json +17 -6
- package/test.js +13 -0
package/example/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mm_cache 模块最小可运行示例
|
|
3
|
+
* @description 演示 Cache 统一缓存框架(cachebase 内存方式),
|
|
4
|
+
* 无需连接 Redis / MongoDB 等外部缓存服务
|
|
5
|
+
*/
|
|
6
|
+
// 独立运行时补充全局 $ 对象(mm_os 宿主环境已提供,npm 包运行需自备)
|
|
7
|
+
if (!global.$) {
|
|
8
|
+
global.$ = {
|
|
9
|
+
log: {
|
|
10
|
+
debug: console.debug ? console.debug.bind(console) : console.log.bind(console),
|
|
11
|
+
info: console.info ? console.info.bind(console) : console.log.bind(console),
|
|
12
|
+
warn: console.warn ? console.warn.bind(console) : console.log.bind(console),
|
|
13
|
+
error: console.error ? console.error.bind(console) : console.log.bind(console),
|
|
14
|
+
success: console.log.bind(console)
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { Cache } = require('../index.js');
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 主演示流程
|
|
23
|
+
*/
|
|
24
|
+
async function main() {
|
|
25
|
+
// 使用 cachebase 内存缓存方式,不连接任何外部缓存服务
|
|
26
|
+
const cache = new Cache({
|
|
27
|
+
way: 'cachebase',
|
|
28
|
+
scope: 'demo',
|
|
29
|
+
cachebase: { persist: false }
|
|
30
|
+
});
|
|
31
|
+
const connected = await cache.connect();
|
|
32
|
+
console.log('缓存方式:', cache.getCacheWay(), '| 连接结果:', connected);
|
|
33
|
+
|
|
34
|
+
// 基础键值操作
|
|
35
|
+
await cache.set('name', 'mm_cache');
|
|
36
|
+
console.log('set/get:', await cache.get('name'));
|
|
37
|
+
|
|
38
|
+
// 原子自增操作
|
|
39
|
+
await cache.set('counter', 10);
|
|
40
|
+
console.log('incr:', await cache.incr('counter'));
|
|
41
|
+
|
|
42
|
+
// 哈希表操作
|
|
43
|
+
await cache.hset('user', 'id', '001');
|
|
44
|
+
console.log('hset/hget:', await cache.hget('user', 'id'));
|
|
45
|
+
|
|
46
|
+
// 删除键
|
|
47
|
+
await cache.del('name');
|
|
48
|
+
console.log('del 后 has:', await cache.has('name'));
|
|
49
|
+
|
|
50
|
+
await cache.dispose();
|
|
51
|
+
console.log('示例运行完成');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
main().catch((err) => {
|
|
55
|
+
console.error('示例运行失败:', err);
|
|
56
|
+
process.exit(1);
|
|
57
|
+
});
|