mm_cache 1.5.6 → 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.
- package/index.js +7 -7
- package/lib/tools/cache/index.js +70 -0
- package/lib/tools/index.js +16 -0
- package/package.json +48 -46
- package/tool/cache/index.js +7 -0
- package/tool/cache/tool.json +73 -0
package/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
const Cache = require('./lib/cache');
|
|
3
|
-
const
|
|
3
|
+
const { reg } = require('mm_admin');
|
|
4
4
|
|
|
5
|
-
|
|
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
|
-
|
|
16
|
-
scope = 'os';
|
|
17
|
-
}
|
|
18
|
-
return admin.get(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'); } });
|
|
@@ -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,48 +1,50 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
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
|
+
}
|
|
48
50
|
}
|
|
@@ -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
|
+
}
|