mm_cache 1.5.5 → 1.8.0

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.
@@ -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
+ });
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  const Cache = require('./lib/cache');
3
- const Admin = require('mm_corejs').Admin;
3
+ const { reg } = require('mm_admin');
4
4
 
5
- let admin = new Admin();
5
+ const admin = reg('mm_cache');
6
6
  admin.setClass(Cache);
7
7
 
8
8
  /**
@@ -12,12 +12,12 @@ module.exports = {
12
12
  Cache,
13
13
  admin,
14
14
  cacheAdmin(scope, config = {}) {
15
- if (!scope) {
16
- scope = 'os';
17
- }
18
- return admin.get(scope, { name: scope, ...config });
15
+ return admin.get(scope || 'os', config);
19
16
  },
20
17
  getCache() {
21
18
  return admin.get('os');
22
19
  }
23
- };
20
+ };
21
+ /* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
22
+ Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
23
+ get: function () { return require('./lib/tools'); } });
package/lib/cache.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * @author <a href="http://qww.elins.cn">邱文武</a>
4
4
  * @version 1.2
5
5
  */
6
- const { Base } = require('mm_corejs');
6
+ const { Base } = require('mm_mod');
7
7
  const { fullname } = require('mm_helper');
8
8
 
9
9
  /**
@@ -0,0 +1,70 @@
1
+ /**
2
+ * 缓存管理器
3
+ * 直接依赖 mm_config 读取配置、mm_cache 的 os 作用域实例读写缓存,支持 TTL
4
+ * @param {object} params 步骤参数
5
+ * @param {object} ctx 上下文对象
6
+ * @param {object} options 模块配置
7
+ * @returns {object} 处理结果
8
+ */
9
+ const { configAdmin } = require('mm_config');
10
+ const mm_cache = require('../../../index.js');
11
+
12
+ /**
13
+ * 从配置存储组装缓存实例配置
14
+ * @returns {Promise<object>} 缓存配置
15
+ */
16
+ async function _getCacheConfig() {
17
+ var cfg_store = configAdmin('os');
18
+ var config = (await cfg_store.get('cache')) || {};
19
+ config.way = config.way || 'memory';
20
+ config.cachebase = (await cfg_store.get('cachebase')) || {};
21
+ config.redis = (await cfg_store.get('redis')) || {};
22
+ config.mongodb = (await cfg_store.get('mongodb')) || {};
23
+ return config;
24
+ }
25
+
26
+ async function main(params, ctx, options) {
27
+ var action = params.action || 'get';
28
+ var key = params.key;
29
+ var value = params.value;
30
+ var ttl = params.ttl || 0;
31
+
32
+ if (!mm_cache.admin.has('os')) {
33
+ var config = await _getCacheConfig();
34
+ mm_cache.cacheAdmin('os', config);
35
+ }
36
+ var cache = mm_cache.getCache();
37
+ switch (action) {
38
+ case 'get': {
39
+ var result_value = await cache.get(key);
40
+ ctx.output = { obj: result_value };
41
+ break;
42
+ }
43
+ case 'set': {
44
+ var ok = await cache.set(key, value, ttl);
45
+ ctx.output = { msg: ok ? '缓存设置成功' : '缓存设置失败' };
46
+ break;
47
+ }
48
+ case 'del': {
49
+ var ok2 = await cache.del(key);
50
+ ctx.output = { msg: ok2 ? '缓存删除成功' : '缓存删除失败' };
51
+ break;
52
+ }
53
+ case 'has': {
54
+ var exists = await cache.has(key);
55
+ ctx.output = { obj: exists };
56
+ break;
57
+ }
58
+ case 'clear': {
59
+ var ok3 = await cache.clear();
60
+ ctx.output = { msg: ok3 ? '缓存已清空' : '缓存清空失败' };
61
+ break;
62
+ }
63
+ default:
64
+ ctx.output = { error: { code: 'MGR_UNKNOWN_ACTION', message: '不支持的操作: ' + action } };
65
+ }
66
+
67
+ return ctx.output;
68
+ }
69
+
70
+ 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,44 +1,50 @@
1
1
  {
2
- "name": "mm_cache",
3
- "version": "1.5.5",
4
- "description": "一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://gitee.com/qiuwenwu/mm_cache.git"
12
- },
13
- "keywords": [
14
- "mm_mod",
15
- "mm_port",
16
- "mm_cache",
17
- "cache",
18
- "async",
19
- "await",
20
- "promise",
21
- "add",
22
- "del",
23
- "set",
24
- "get",
25
- "query",
26
- "run",
27
- "exec"
28
- ],
29
- "layer": "port",
30
- "group": "cache",
31
- "author": "邱文武",
32
- "license": "ISC",
33
- "bugs": {
34
- "url": "https://gitee.com/qiuwenwu/mm_cache/issues"
35
- },
36
- "homepage": "https://gitee.com/qiuwenwu/mm_cache#readme",
37
- "dependencies": {
38
- "mm_cachebase": "^2.0.4",
39
- "mm_mongodb": "^2.0.3",
40
- "mm_redis": "^2.1.7",
41
- "mm_corejs": "^1.1.4",
42
- "mm_helper": "^1.0.0"
43
- }
44
- }
2
+ "name": "mm_cache",
3
+ "version": "1.8.0",
4
+ "description": "一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的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_cache.git"
12
+ },
13
+ "keywords": [
14
+ "mm_mod",
15
+ "mm_port",
16
+ "mm_cache",
17
+ "cache",
18
+ "async",
19
+ "await",
20
+ "promise",
21
+ "add",
22
+ "del",
23
+ "set",
24
+ "get",
25
+ "query",
26
+ "run",
27
+ "exec"
28
+ ],
29
+ "layer": "infrastructure",
30
+ "group": "cache",
31
+ "author": "邱文武",
32
+ "license": "ISC",
33
+ "bugs": {
34
+ "url": "https://gitee.com/qiuwenwu91/mm_cache/issues"
35
+ },
36
+ "homepage": "https://gitee.com/qiuwenwu91/mm_cache#readme",
37
+ "dependencies": {
38
+ "mm_admin": "^1.1.0",
39
+ "mm_cachebase": "^2.0.4",
40
+ "mm_config": "^2.6.0",
41
+ "mm_eslint": "^1.0.0",
42
+ "mm_helper": "^1.0.0",
43
+ "mm_mod": "^1.0.1",
44
+ "mm_mongodb": "^2.0.3",
45
+ "mm_redis": "^2.1.7"
46
+ },
47
+ "devDependencies": {
48
+ "mm_eslint": "^1.7.7"
49
+ }
50
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * cache —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/cache/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_cache').tools.cache 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/cache');
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "cache",
3
+ "handler": "manager",
4
+ "title": "缓存管理器",
5
+ "description": "读写全局缓存,支持 get/set/del/has/clear 五种操作与 TTL 过期。适用场景:需要临时缓存计算结果、会话数据或高频读取数据时。工作流提示:action 与 key 必填,set 时 value 必填、ttl 设过期秒数(0 为不过期),读结果在 obj、写结果在 msg。",
6
+ "type": "function",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "action": {
11
+ "type": "string",
12
+ "description": "操作类型: get/set/del/has/clear",
13
+ "enum": [
14
+ "get",
15
+ "set",
16
+ "del",
17
+ "has",
18
+ "clear"
19
+ ]
20
+ },
21
+ "key": {
22
+ "type": "string",
23
+ "description": "缓存键"
24
+ },
25
+ "value": {
26
+ "description": "缓存值(set时必填)"
27
+ },
28
+ "ttl": {
29
+ "type": "number",
30
+ "description": "过期时间(秒)",
31
+ "min": 0,
32
+ "max": 31536000
33
+ }
34
+ },
35
+ "required": [
36
+ "action",
37
+ "key"
38
+ ]
39
+ },
40
+ "returns": {
41
+ "description": "管理操作结果,读操作返回 obj,写操作返回 msg,异常时返回 error",
42
+ "type": "object",
43
+ "properties": {
44
+ "obj": {
45
+ "description": "读取操作的结果值(get/has 操作)"
46
+ },
47
+ "msg": {
48
+ "type": "string",
49
+ "description": "写操作的结果描述(set/del/clear 操作)"
50
+ }
51
+ }
52
+ },
53
+ "options": {
54
+ "way": {
55
+ "type": "string",
56
+ "description": "缓存后端: memory/redis/mongodb",
57
+ "default": "memory"
58
+ },
59
+ "prefix": {
60
+ "type": "string",
61
+ "description": "缓存键前缀",
62
+ "default": "cache:"
63
+ },
64
+ "default_ttl": {
65
+ "type": "number",
66
+ "description": "默认过期时间(秒)",
67
+ "default": 3600,
68
+ "min": 0,
69
+ "max": 31536000
70
+ }
71
+ },
72
+ "group": "storage"
73
+ }