mm_cachebase 2.0.6 → 2.0.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.
package/README.md CHANGED
@@ -267,3 +267,12 @@ async function pubsubExample() {
267
267
  - 发布订阅为进程内实现(mm_redis 支持跨进程)
268
268
  - 集合/有序集合用 JS 原生 `Set`/`Map` 实现,无 Redis 的 O(log N) 复杂度保证
269
269
  - 构造函数自动初始化(内存缓存特性),mm_redis 需显式 `open()`
270
+
271
+ ## 工具契约(LLM 可调用)
272
+
273
+ 本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
274
+ 其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
275
+
276
+ | 工具名 | 标题 | 说明 |
277
+ | --- | --- | --- |
278
+ | `cachebase` | 进程内缓存 | 带过期的键值缓存与计数 |
@@ -1 +1 @@
1
- {data:{},expires:{},types:{},timestamp:1793313151555}
1
+ {data:{},expires:{},types:{},timestamp:1794643219183}
@@ -1 +1 @@
1
- {data:{},expires:{},types:{},timestamp:1793313151543}
1
+ {data:{},expires:{},types:{},timestamp:1794643219158}
package/index.js CHANGED
@@ -1,28 +1,27 @@
1
1
  const CacheBase = require('./lib/cachebase');
2
- const Admin = require('mm_corejs').Admin;
2
+ const { reg } = require('mm_admin');
3
3
 
4
4
  /**
5
5
  * 缓存管理器实例(Admin 工厂)
6
6
  */
7
- const admin = new Admin();
7
+ const admin = reg('mm_cachebase');
8
8
  admin.setClass(CacheBase);
9
9
 
10
- /**
11
- * @description CacheBase管理器,用于创建缓存
12
- * @param {String} scope 作用域
13
- * @param {Object} config 配置参数
14
- * @return {Object} 返回一个CacheBase类实例
15
- */
16
- function cachebaseAdmin(scope, config) {
17
- if (!scope) {
18
- scope = 'os';
19
- }
20
- return admin.get(scope, config);
21
- }
22
-
23
10
  // 导出类
24
11
  module.exports = {
25
12
  CacheBase,
26
13
  admin,
27
- cachebaseAdmin
28
- };
14
+ /**
15
+ * @description CacheBase管理器,用于创建缓存
16
+ * @param {String} scope 作用域
17
+ * @param {Object} config 配置参数
18
+ * @return {Object} 返回一个CacheBase类实例
19
+ */
20
+ cachebaseAdmin(scope, config) {
21
+ return admin.get(scope || 'os', config);
22
+ }
23
+ };
24
+
25
+ /* mm-tool-logic-export 工具能力层:其它模块可 require("mm_cachebase").tools.<name> 直接引用 */
26
+ Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
27
+ get: function () { return require('./lib/tools'); } });
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+ /**
3
+ * cachebase —— 进程内缓存工具
4
+ * 复用模块 CacheBase 实例的 Redis 对齐 API,不另建缓存表。
5
+ */
6
+ const { cachebaseAdmin } = require('../../../index.js');
7
+
8
+ async function _ready(scope) {
9
+ const cache = cachebaseAdmin(scope || 'os');
10
+ if (typeof cache.isConnected === 'function' && !cache.isConnected()) {
11
+ await cache.init();
12
+ }
13
+ return cache;
14
+ }
15
+
16
+ async function main(params, ctx, options) {
17
+ void options;
18
+ const p = params || {};
19
+ const action = p.action || 'get';
20
+ const done = (data) => { ctx.output = data; return data; };
21
+
22
+ try {
23
+ const cache = await _ready(p.scope);
24
+
25
+ if (action === 'set') {
26
+ if (!p.key) throw new TypeError('key 不能为空');
27
+ if (Number(p.ttl) > 0) {
28
+ await cache.set(p.key, p.value, { ttl: Number(p.ttl) });
29
+ } else {
30
+ await cache.set(p.key, p.value);
31
+ }
32
+ return done({ code: 0, msg: '写入成功', key: p.key });
33
+ }
34
+
35
+ if (action === 'get') {
36
+ if (!p.key) throw new TypeError('key 不能为空');
37
+ const value = await cache.get(p.key);
38
+ return done({ code: 0, msg: '成功', key: p.key, value, exists: value !== null && value !== undefined });
39
+ }
40
+
41
+ if (action === 'del') {
42
+ if (!p.key) throw new TypeError('key 不能为空');
43
+ const deleted = await cache.del(p.key);
44
+ return done({ code: 0, msg: '删除完成', key: p.key, deleted: Number(deleted) || 0 });
45
+ }
46
+
47
+ if (action === 'has') {
48
+ if (!p.key) throw new TypeError('key 不能为空');
49
+ const exists = await cache.has(p.key);
50
+ return done({ code: 0, msg: '成功', key: p.key, exists: !!exists });
51
+ }
52
+
53
+ if (action === 'keys') {
54
+ const keys = await cache.keys(p.pattern || '*');
55
+ const list = Array.isArray(keys) ? keys : Object.keys(keys || {});
56
+ return done({ code: 0, msg: '成功', count: list.length, keys: list });
57
+ }
58
+
59
+ if (action === 'incr') {
60
+ if (!p.key) throw new TypeError('key 不能为空');
61
+ const value = await cache.incrby(p.key, Number(p.delta || 1));
62
+ return done({ code: 0, msg: '成功', key: p.key, value });
63
+ }
64
+
65
+ if (action === 'ttl') {
66
+ if (!p.key) throw new TypeError('key 不能为空');
67
+ const ttl = await cache.ttl(p.key);
68
+ return done({ code: 0, msg: '成功', key: p.key, ttl });
69
+ }
70
+
71
+ if (action === 'clear') {
72
+ await cache.clear();
73
+ return done({ code: 0, msg: '已清库' });
74
+ }
75
+
76
+ return done({ code: 10001, msg: '不支持的操作: ' + action });
77
+ } catch (error) {
78
+ return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
79
+ }
80
+ }
81
+
82
+ module.exports = { main };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ /** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
5
+ module.exports = new Proxy({}, {
6
+ get(_t, key) {
7
+ if (typeof key !== 'string') return undefined;
8
+ if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
9
+ return require('./' + key);
10
+ },
11
+ has(_t, key) {
12
+ return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
13
+ },
14
+ ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
15
+ getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
16
+ });
package/package.json CHANGED
@@ -1,54 +1,55 @@
1
1
  {
2
- "name": "mm_cachebase",
3
- "version": "2.0.6",
4
- "description": "一个轻量级的本地缓存类库,接口与 mm_redis 完全对齐:全异步API、冒号风格事件、统一签名与返回值,支持字符串、哈希、列表、集合、有序集合、发布订阅。",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://gitee.com/qiuwenwu91/mm_cachebase.git"
12
- },
13
- "keywords": [
14
- "mm_mod",
15
- "mm_port",
16
- "mm_cache",
17
- "cachebase",
18
- "redis",
19
- "cache",
20
- "memory-cache",
21
- "local-cache",
22
- "async",
23
- "await",
24
- "promise",
25
- "set",
26
- "get",
27
- "del",
28
- "hash",
29
- "event",
30
- "ttl",
31
- "expire",
32
- "incr",
33
- "decr",
34
- "persistence",
35
- "persist",
36
- "scope"
37
- ],
38
- "layer": "adapter",
39
- "group": "cache",
40
- "author": "邱文武",
41
- "license": "MIT",
42
- "bugs": {
43
- "url": "https://gitee.com/qiuwenwu91/mm_cachebase/issues"
44
- },
45
- "homepage": "https://gitee.com/qiuwenwu91/mm_cachebase#readme",
46
- "dependencies": {
47
- "mm_corejs": "^1.1.5",
48
- "mm_mod": "^1.0.1",
49
- "mm_helper": "^1.0.1"
50
- },
51
- "devDependencies": {
52
- "mm_eslint": "^1.7.7"
53
- }
2
+ "name": "mm_cachebase",
3
+ "version": "2.0.7",
4
+ "description": "一个轻量级的本地缓存类库,接口与 mm_redis 完全对齐:全异步API、冒号风格事件、统一签名与返回值,支持字符串、哈希、列表、集合、有序集合、发布订阅。",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://gitee.com/qiuwenwu91/mm_cachebase.git"
12
+ },
13
+ "keywords": [
14
+ "mm_mod",
15
+ "mm_port",
16
+ "mm_cache",
17
+ "cachebase",
18
+ "redis",
19
+ "cache",
20
+ "memory-cache",
21
+ "local-cache",
22
+ "async",
23
+ "await",
24
+ "promise",
25
+ "set",
26
+ "get",
27
+ "del",
28
+ "hash",
29
+ "event",
30
+ "ttl",
31
+ "expire",
32
+ "incr",
33
+ "decr",
34
+ "persistence",
35
+ "persist",
36
+ "scope"
37
+ ],
38
+ "layer": "infrastructure",
39
+ "group": "cache",
40
+ "author": "邱文武",
41
+ "license": "MIT",
42
+ "bugs": {
43
+ "url": "https://gitee.com/qiuwenwu91/mm_cachebase/issues"
44
+ },
45
+ "homepage": "https://gitee.com/qiuwenwu91/mm_cachebase#readme",
46
+ "dependencies": {
47
+ "mm_admin": "^1.1.0",
48
+ "mm_eslint": "^1.0.0",
49
+ "mm_helper": "^1.0.1",
50
+ "mm_mod": "^1.0.1"
51
+ },
52
+ "devDependencies": {
53
+ "mm_eslint": "^1.7.7"
54
+ }
54
55
  }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * cachebase —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/cachebase/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_cachebase').tools.cachebase 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/cachebase');
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "cachebase",
3
+ "title": "进程内缓存",
4
+ "description": "基于 mm_cachebase 的本地缓存能力(接口与 Redis 对齐):写入带过期时间的键、读取、删除、列键、自增、查剩余 TTL、清库。适用场景:缓存接口结果、做去重计数、临时限流状态。工作流提示:set 传 key/value/ttl(秒),incr 用于计数。",
5
+ "category": "缓存",
6
+ "group": "cache",
7
+ "type": "function",
8
+ "main": "./index.js",
9
+ "params": {
10
+ "type": "object",
11
+ "properties": {
12
+ "action": {
13
+ "type": "string",
14
+ "description": "操作类型: set(写入缓存)/get(读取)/del(删除)/has(判断存在)/keys(列键)/incr(自增)/ttl(剩余过期)/clear(清库)",
15
+ "enum": [
16
+ "set",
17
+ "get",
18
+ "del",
19
+ "has",
20
+ "keys",
21
+ "incr",
22
+ "ttl",
23
+ "clear"
24
+ ],
25
+ "default": "set"
26
+ },
27
+ "key": {
28
+ "type": "string",
29
+ "description": "缓存键(get/del/has/incr/ttl/set 必填)"
30
+ },
31
+ "value": {
32
+ "description": "缓存值(set 必填)"
33
+ },
34
+ "ttl": {
35
+ "type": "number",
36
+ "description": "过期秒数(set 可选),0 表示不过期"
37
+ },
38
+ "delta": {
39
+ "type": "number",
40
+ "description": "自增步长(incr 可选),缺省 1",
41
+ "default": 1
42
+ },
43
+ "pattern": {
44
+ "type": "string",
45
+ "description": "键匹配模式(keys 可选),缺省 *",
46
+ "default": "*"
47
+ },
48
+ "scope": {
49
+ "type": "string",
50
+ "description": "实例作用域,缺省 os",
51
+ "default": "os"
52
+ }
53
+ },
54
+ "required": [
55
+ "action"
56
+ ]
57
+ },
58
+ "returns": {
59
+ "type": "object",
60
+ "properties": {
61
+ "code": {
62
+ "type": "number",
63
+ "description": "0 表示成功,1xxxx 表示业务/执行异常"
64
+ },
65
+ "msg": {
66
+ "type": "string",
67
+ "description": "结果描述"
68
+ },
69
+ "value": {
70
+ "description": "缓存值(get)"
71
+ },
72
+ "exists": {
73
+ "type": "boolean",
74
+ "description": "是否存在"
75
+ },
76
+ "keys": {
77
+ "type": "array",
78
+ "description": "键列表(keys)"
79
+ },
80
+ "count": {
81
+ "type": "number",
82
+ "description": "数量"
83
+ },
84
+ "ttl": {
85
+ "type": "number",
86
+ "description": "剩余过期秒数,-1 永久,-2 不存在(ttl)"
87
+ },
88
+ "deleted": {
89
+ "type": "number",
90
+ "description": "删除的键数(del)"
91
+ },
92
+ "error": {
93
+ "type": "object",
94
+ "description": "异常对象 { name, message }"
95
+ }
96
+ }
97
+ },
98
+ "handler": "manager"
99
+ }