mm_cache 1.4.4 → 1.4.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/eslint.config.js +210 -67
- package/index.js +106 -94
- package/package.json +4 -4
- package/test.js +20 -3
package/eslint.config.js
CHANGED
|
@@ -1,68 +1,211 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
'jsdoc
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const namingConventionPlugin = require('mm_eslint');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 基于个人规则的ESLint配置
|
|
7
|
+
* 符合最新ESLint Flat Config格式
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
module.exports = [
|
|
11
|
+
{
|
|
12
|
+
files: ['**/*.js'],
|
|
13
|
+
plugins: {
|
|
14
|
+
'naming-convention': namingConventionPlugin,
|
|
15
|
+
'jsdoc': require('eslint-plugin-jsdoc')
|
|
16
|
+
},
|
|
17
|
+
languageOptions: {
|
|
18
|
+
ecmaVersion: 'latest',
|
|
19
|
+
sourceType: 'module',
|
|
20
|
+
parserOptions: {
|
|
21
|
+
ecmaVersion: 'latest',
|
|
22
|
+
sourceType: 'module'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
// 命名规范规则 - 由自定义插件处理
|
|
27
|
+
rules: {
|
|
28
|
+
// 自定义命名规范插件规则(优先使用)
|
|
29
|
+
'naming-convention/class-name': 'error',
|
|
30
|
+
'naming-convention/function-name': 'error',
|
|
31
|
+
'naming-convention/method-name': 'error',
|
|
32
|
+
'naming-convention/variable-name': 'warn',
|
|
33
|
+
'naming-convention/constant-name': 'error',
|
|
34
|
+
'naming-convention/private-method-naming': 'warn',
|
|
35
|
+
'naming-convention/private-variable-naming': 'warn',
|
|
36
|
+
'naming-convention/param-name': 'warn',
|
|
37
|
+
'naming-convention/property-name': 'warn',
|
|
38
|
+
'naming-convention/instance-property': 'warn',
|
|
39
|
+
|
|
40
|
+
// 禁用与命名规范插件冲突的默认规则
|
|
41
|
+
'camelcase': 'off',
|
|
42
|
+
'id-match': 'off',
|
|
43
|
+
'new-cap': 'off',
|
|
44
|
+
'id-length': 'off',
|
|
45
|
+
'id-denylist': 'off',
|
|
46
|
+
'id-blacklist': 'off'
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
{
|
|
51
|
+
// 代码风格规则
|
|
52
|
+
rules: {
|
|
53
|
+
// 最大行长度100字符
|
|
54
|
+
'max-len': ['error', {
|
|
55
|
+
code: 100,
|
|
56
|
+
ignoreComments: true,
|
|
57
|
+
ignoreUrls: true,
|
|
58
|
+
ignoreStrings: true,
|
|
59
|
+
ignoreTemplateLiterals: true
|
|
60
|
+
}],
|
|
61
|
+
|
|
62
|
+
// 缩进2空格
|
|
63
|
+
'indent': ['error', 2, {
|
|
64
|
+
SwitchCase: 1,
|
|
65
|
+
VariableDeclarator: 1,
|
|
66
|
+
outerIIFEBody: 1,
|
|
67
|
+
MemberExpression: 1,
|
|
68
|
+
FunctionDeclaration: { parameters: 1, body: 1 },
|
|
69
|
+
FunctionExpression: { parameters: 1, body: 1 },
|
|
70
|
+
CallExpression: { arguments: 1 },
|
|
71
|
+
ArrayExpression: 1,
|
|
72
|
+
ObjectExpression: 1,
|
|
73
|
+
ImportDeclaration: 1,
|
|
74
|
+
flatTernaryExpressions: false,
|
|
75
|
+
ignoreComments: false
|
|
76
|
+
}],
|
|
77
|
+
|
|
78
|
+
// 单引号
|
|
79
|
+
'quotes': ['error', 'single', {
|
|
80
|
+
avoidEscape: true,
|
|
81
|
+
allowTemplateLiterals: true
|
|
82
|
+
}],
|
|
83
|
+
|
|
84
|
+
// 禁止制表符
|
|
85
|
+
'no-tabs': 'error',
|
|
86
|
+
|
|
87
|
+
// 行尾分号
|
|
88
|
+
'semi': ['error', 'always'],
|
|
89
|
+
|
|
90
|
+
// 逗号风格
|
|
91
|
+
'comma-style': ['error', 'last'],
|
|
92
|
+
|
|
93
|
+
// 逗号后必须有空格,逗号前不能有空格
|
|
94
|
+
'comma-spacing': ['error', {
|
|
95
|
+
before: false,
|
|
96
|
+
after: true
|
|
97
|
+
}]
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
{
|
|
102
|
+
// 错误处理规则
|
|
103
|
+
rules: {
|
|
104
|
+
// 必须进行参数校验
|
|
105
|
+
'no-unused-vars': ['error', {
|
|
106
|
+
args: 'all',
|
|
107
|
+
argsIgnorePattern: '^_',
|
|
108
|
+
caughtErrors: 'all',
|
|
109
|
+
caughtErrorsIgnorePattern: '^_',
|
|
110
|
+
destructuredArrayIgnorePattern: '^_',
|
|
111
|
+
varsIgnorePattern: '^_',
|
|
112
|
+
ignoreRestSiblings: true
|
|
113
|
+
}],
|
|
114
|
+
|
|
115
|
+
// 建议使用try-catch
|
|
116
|
+
'no-unsafe-finally': 'warn',
|
|
117
|
+
|
|
118
|
+
// 禁止直接抛出字符串
|
|
119
|
+
'no-throw-literal': 'error'
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
{
|
|
124
|
+
// 最佳实践规则
|
|
125
|
+
rules: {
|
|
126
|
+
// 优先使用async/await
|
|
127
|
+
'prefer-promise-reject-errors': 'error',
|
|
128
|
+
|
|
129
|
+
// 避免副作用
|
|
130
|
+
'no-param-reassign': ['error', {
|
|
131
|
+
props: true,
|
|
132
|
+
ignorePropertyModificationsFor: [
|
|
133
|
+
'acc', // for reduce accumulators
|
|
134
|
+
'accumulator', // for reduce accumulators
|
|
135
|
+
'e', // for e.returnvalue
|
|
136
|
+
'ctx', // for Koa routing
|
|
137
|
+
'context', // for Koa routing
|
|
138
|
+
'req', // for Express requests
|
|
139
|
+
'request', // for Express requests
|
|
140
|
+
'res', // for Express responses
|
|
141
|
+
'response', // for Express responses
|
|
142
|
+
'$scope', // for Angular 1 scopes
|
|
143
|
+
'staticContext' // for ReactRouter context
|
|
144
|
+
]
|
|
145
|
+
}],
|
|
146
|
+
|
|
147
|
+
// 优先链式编程
|
|
148
|
+
'prefer-object-spread': 'error',
|
|
149
|
+
|
|
150
|
+
// 优先函数式编程
|
|
151
|
+
'prefer-arrow-callback': 'error',
|
|
152
|
+
|
|
153
|
+
// 方法长度限制
|
|
154
|
+
'max-lines-per-function': ['warn', {
|
|
155
|
+
max: 40,
|
|
156
|
+
skipBlankLines: true,
|
|
157
|
+
skipComments: true
|
|
158
|
+
}],
|
|
159
|
+
|
|
160
|
+
// 复杂度限制
|
|
161
|
+
'complexity': ['warn', 10]
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
{
|
|
166
|
+
// JSDoc规则
|
|
167
|
+
rules: {
|
|
168
|
+
// 要求公开方法有JSDoc注释
|
|
169
|
+
'jsdoc/require-jsdoc': ['warn', {
|
|
170
|
+
publicOnly: true,
|
|
171
|
+
require: {
|
|
172
|
+
FunctionDeclaration: true,
|
|
173
|
+
MethodDefinition: true,
|
|
174
|
+
ClassDeclaration: true,
|
|
175
|
+
ArrowFunctionExpression: false,
|
|
176
|
+
FunctionExpression: false
|
|
177
|
+
}
|
|
178
|
+
}],
|
|
179
|
+
|
|
180
|
+
// 检查JSDoc语法
|
|
181
|
+
'jsdoc/check-alignment': 'warn',
|
|
182
|
+
'jsdoc/check-indentation': 'warn',
|
|
183
|
+
'jsdoc/check-param-names': 'warn',
|
|
184
|
+
'jsdoc/check-syntax': 'warn',
|
|
185
|
+
'jsdoc/check-tag-names': 'warn',
|
|
186
|
+
'jsdoc/check-types': 'warn',
|
|
187
|
+
'jsdoc/no-undefined-types': 'warn',
|
|
188
|
+
'jsdoc/require-description': 'warn',
|
|
189
|
+
'jsdoc/require-param': 'warn',
|
|
190
|
+
'jsdoc/require-param-description': 'warn',
|
|
191
|
+
'jsdoc/require-param-name': 'warn',
|
|
192
|
+
'jsdoc/require-param-type': 'warn',
|
|
193
|
+
'jsdoc/require-returns': 'warn',
|
|
194
|
+
'jsdoc/require-returns-check': 'warn',
|
|
195
|
+
'jsdoc/require-returns-description': 'warn',
|
|
196
|
+
'jsdoc/require-returns-type': 'warn',
|
|
197
|
+
'jsdoc/valid-types': 'warn'
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
{
|
|
202
|
+
// 忽略规则
|
|
203
|
+
ignores: [
|
|
204
|
+
'node_modules/**',
|
|
205
|
+
'dist/**',
|
|
206
|
+
'build/**',
|
|
207
|
+
'coverage/**',
|
|
208
|
+
'*.min.js'
|
|
209
|
+
]
|
|
210
|
+
}
|
|
68
211
|
];
|
package/index.js
CHANGED
|
@@ -3,80 +3,74 @@
|
|
|
3
3
|
* @author <a href="http://qww.elins.cn">邱文武</a>
|
|
4
4
|
* @version 1.2
|
|
5
5
|
*/
|
|
6
|
-
const EventEmitter = require('events');
|
|
7
6
|
const { Base } = require('mm_expand');
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* @class 缓存类
|
|
11
10
|
*/
|
|
12
11
|
class Cache extends Base {
|
|
12
|
+
static config = {
|
|
13
|
+
scope: 'sys',
|
|
14
|
+
way: 'memory',
|
|
15
|
+
// 缓存基础配置
|
|
16
|
+
cachebase: {
|
|
17
|
+
dir: './cache/'.fullname(),
|
|
18
|
+
expire: 0,
|
|
19
|
+
max: 0,
|
|
20
|
+
persist: false, // 是否启用持久化
|
|
21
|
+
saveInterval: 60000 // 保存间隔(毫秒)
|
|
22
|
+
},
|
|
23
|
+
// 缓存配置
|
|
24
|
+
redis: {
|
|
25
|
+
// 服务器地址
|
|
26
|
+
host: "localhost",
|
|
27
|
+
// 端口号
|
|
28
|
+
port: 6379,
|
|
29
|
+
// 密码
|
|
30
|
+
password: "asd159357",
|
|
31
|
+
// 数据库
|
|
32
|
+
database: 0,
|
|
33
|
+
// 前缀
|
|
34
|
+
prefix: "",
|
|
35
|
+
// 连接超时时间(ms)
|
|
36
|
+
connect_timeout: 10000,
|
|
37
|
+
// 重试次数
|
|
38
|
+
retry_count: 3,
|
|
39
|
+
// 重试间隔(ms)
|
|
40
|
+
retry_delay: 1000
|
|
41
|
+
},
|
|
42
|
+
// 数据库配置
|
|
43
|
+
mongodb: {
|
|
44
|
+
// 服务器地址
|
|
45
|
+
host: "localhost",
|
|
46
|
+
// 端口号
|
|
47
|
+
port: 27017,
|
|
48
|
+
// 数据库名
|
|
49
|
+
database: "mm",
|
|
50
|
+
// 用户名
|
|
51
|
+
user: "",
|
|
52
|
+
// 密码
|
|
53
|
+
password: "",
|
|
54
|
+
// 连接选项 (MongoDB驱动7.x版本支持的参数)
|
|
55
|
+
min_pool_size: 10,
|
|
56
|
+
max_pool_size: 100,
|
|
57
|
+
connect_timeout_ms: 10000,
|
|
58
|
+
server_selection_timeout_ms: 5000,
|
|
59
|
+
retry_writes: true
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
13
63
|
/**
|
|
14
64
|
* @description 创建Cache帮助类函数 (构造函数)
|
|
15
|
-
* @param {String} scope 作用域
|
|
16
65
|
* @param {Object} config 配置
|
|
17
66
|
* @constructor
|
|
18
67
|
*/
|
|
19
|
-
constructor(
|
|
20
|
-
super();
|
|
21
|
-
this.config = {
|
|
22
|
-
scope: 'sys',
|
|
23
|
-
way: 'memory',
|
|
24
|
-
// 缓存基础配置
|
|
25
|
-
cachebase: {
|
|
26
|
-
dir: './cache/'.fullname(),
|
|
27
|
-
expire: 0,
|
|
28
|
-
max: 0,
|
|
29
|
-
persist: false, // 是否启用持久化
|
|
30
|
-
saveInterval: 60000 // 保存间隔(毫秒)
|
|
31
|
-
},
|
|
32
|
-
// 缓存配置
|
|
33
|
-
redis: {
|
|
34
|
-
// 服务器地址
|
|
35
|
-
host: "localhost",
|
|
36
|
-
// 端口号
|
|
37
|
-
port: 6379,
|
|
38
|
-
// 密码
|
|
39
|
-
password: "asd159357",
|
|
40
|
-
// 数据库
|
|
41
|
-
database: 0,
|
|
42
|
-
// 前缀
|
|
43
|
-
prefix: "",
|
|
44
|
-
// 连接超时时间(ms)
|
|
45
|
-
connect_timeout: 10000,
|
|
46
|
-
// 重试次数
|
|
47
|
-
retry_count: 3,
|
|
48
|
-
// 重试间隔(ms)
|
|
49
|
-
retry_delay: 1000
|
|
50
|
-
},
|
|
51
|
-
// 数据库配置
|
|
52
|
-
mongodb: {
|
|
53
|
-
// 服务器地址
|
|
54
|
-
host: "localhost",
|
|
55
|
-
// 端口号
|
|
56
|
-
port: 27017,
|
|
57
|
-
// 数据库名
|
|
58
|
-
database: "mm",
|
|
59
|
-
// 用户名
|
|
60
|
-
user: "",
|
|
61
|
-
// 密码
|
|
62
|
-
password: "",
|
|
63
|
-
// 连接选项 (MongoDB驱动7.x版本支持的参数)
|
|
64
|
-
min_pool_size: 10,
|
|
65
|
-
max_pool_size: 100,
|
|
66
|
-
connect_timeout_ms: 10000,
|
|
67
|
-
server_selection_timeout_ms: 5000,
|
|
68
|
-
retry_writes: true
|
|
69
|
-
}
|
|
70
|
-
};
|
|
68
|
+
constructor(config) {
|
|
69
|
+
super(Cache.config);
|
|
71
70
|
// 作用域
|
|
72
71
|
this.scope;
|
|
73
|
-
if (scope) {
|
|
74
|
-
this.scope = scope;
|
|
75
|
-
} else {
|
|
76
|
-
this.scope = $.val.scope + '';
|
|
77
|
-
}
|
|
78
72
|
// 当前连接库
|
|
79
|
-
this.
|
|
73
|
+
this._client = null;
|
|
80
74
|
// 初始化配置
|
|
81
75
|
this.setConfig(config);
|
|
82
76
|
// 注意:不再在构造函数中直接调用异步的connect()
|
|
@@ -89,9 +83,26 @@ class Cache extends Base {
|
|
|
89
83
|
* @param {Object} config 配置
|
|
90
84
|
*/
|
|
91
85
|
Cache.prototype.setConfig = function (config) {
|
|
86
|
+
// 确保this.config存在
|
|
87
|
+
if (!this.config) {
|
|
88
|
+
this.config = {};
|
|
89
|
+
}
|
|
90
|
+
|
|
92
91
|
if (config) {
|
|
92
|
+
// 使用$.push合并配置
|
|
93
93
|
$.push(this.config, config);
|
|
94
94
|
}
|
|
95
|
+
|
|
96
|
+
// 确保way属性存在
|
|
97
|
+
if (!this.config.way) {
|
|
98
|
+
this.config.way = 'memory'; // 默认缓存方式
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (this.config.scope) {
|
|
102
|
+
this.scope = this.config.scope;
|
|
103
|
+
} else {
|
|
104
|
+
this.scope = $.val.scope + '';
|
|
105
|
+
}
|
|
95
106
|
};
|
|
96
107
|
|
|
97
108
|
/**
|
|
@@ -117,14 +128,14 @@ Cache.prototype.connect = async function () {
|
|
|
117
128
|
const {
|
|
118
129
|
redisAdmin
|
|
119
130
|
} = require('mm_redis');
|
|
120
|
-
this.
|
|
121
|
-
if (!this.
|
|
122
|
-
await this.
|
|
131
|
+
this._client = redisAdmin(this.config.scope, this.config.redis);
|
|
132
|
+
if (!this._client.connected) {
|
|
133
|
+
await this._client.open();
|
|
123
134
|
}
|
|
124
135
|
} catch (e) {
|
|
125
136
|
$.log.warn('[Cache] [connect] Cache模块加载失败,回退到memory:', e.message);
|
|
126
137
|
this.config.way = 'memory';
|
|
127
|
-
await this.
|
|
138
|
+
await this._clientect(); // 递归调用,使用memory
|
|
128
139
|
return false;
|
|
129
140
|
}
|
|
130
141
|
break;
|
|
@@ -134,14 +145,14 @@ Cache.prototype.connect = async function () {
|
|
|
134
145
|
const {
|
|
135
146
|
mongodbAdmin
|
|
136
147
|
} = require('mm_mongodb');
|
|
137
|
-
this.
|
|
138
|
-
if (!this.
|
|
139
|
-
await this.
|
|
148
|
+
this._client = mongodbAdmin(this.config.scope, this.config.mongodb);
|
|
149
|
+
if (!this._client.connected) {
|
|
150
|
+
await this._client.open();
|
|
140
151
|
}
|
|
141
152
|
} catch (e) {
|
|
142
153
|
$.log.warn('[Cache] [connect] MongoDB模块加载失败,回退到memory:', e.message);
|
|
143
154
|
this.config.way = 'memory';
|
|
144
|
-
await this.
|
|
155
|
+
await this._clientect(); // 递归调用,使用cachebase memory
|
|
145
156
|
return false;
|
|
146
157
|
}
|
|
147
158
|
break;
|
|
@@ -151,7 +162,7 @@ Cache.prototype.connect = async function () {
|
|
|
151
162
|
const {
|
|
152
163
|
cachebaseAdmin
|
|
153
164
|
} = require('mm_cachebase');
|
|
154
|
-
this.
|
|
165
|
+
this._client = cachebaseAdmin(this.config.scope, this.config.cachebase);
|
|
155
166
|
} catch (e) {
|
|
156
167
|
$.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
|
|
157
168
|
return false;
|
|
@@ -163,7 +174,7 @@ Cache.prototype.connect = async function () {
|
|
|
163
174
|
const {
|
|
164
175
|
cachebaseAdmin
|
|
165
176
|
} = require('mm_cachebase');
|
|
166
|
-
this.
|
|
177
|
+
this._client = cachebaseAdmin('default');
|
|
167
178
|
} catch (e) {
|
|
168
179
|
$.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
|
|
169
180
|
return false;
|
|
@@ -192,7 +203,7 @@ Cache.prototype.switchCache = async function (way, config) {
|
|
|
192
203
|
if (config) {
|
|
193
204
|
this.config[way] = Object.assign(this.config[way], config);
|
|
194
205
|
}
|
|
195
|
-
return await this.
|
|
206
|
+
return await this._clientect();
|
|
196
207
|
}
|
|
197
208
|
return false;
|
|
198
209
|
};
|
|
@@ -218,8 +229,8 @@ Cache.prototype.has = async function (key) {
|
|
|
218
229
|
// 添加作用域前缀
|
|
219
230
|
const scopeKey = `${this.scope}:${key}`;
|
|
220
231
|
// 优先使用has方法,如果不存在则使用exists
|
|
221
|
-
if (this.
|
|
222
|
-
return await this.
|
|
232
|
+
if (this._client && typeof this._client.has === 'function') {
|
|
233
|
+
return await this._client.has(scopeKey);
|
|
223
234
|
} else {
|
|
224
235
|
return await this._safeCall('exists', [scopeKey]);
|
|
225
236
|
}
|
|
@@ -232,18 +243,18 @@ Cache.prototype.has = async function (key) {
|
|
|
232
243
|
* @return {Promise<any>} 方法返回值
|
|
233
244
|
*/
|
|
234
245
|
Cache.prototype._safeCall = async function (method, args = []) {
|
|
235
|
-
if (!this.
|
|
246
|
+
if (!this._client) {
|
|
236
247
|
// 如果未连接,自动尝试连接
|
|
237
|
-
await this.
|
|
238
|
-
if (!this.
|
|
248
|
+
await this._clientect();
|
|
249
|
+
if (!this._client) {
|
|
239
250
|
$.log.error('[Cache] [_safeCall] 数据库未初始化');
|
|
240
251
|
return null;
|
|
241
252
|
}
|
|
242
253
|
}
|
|
243
254
|
|
|
244
255
|
// 如果方法存在,直接调用
|
|
245
|
-
if (typeof this.
|
|
246
|
-
return await this.
|
|
256
|
+
if (typeof this._client[method] === 'function') {
|
|
257
|
+
return await this._client[method](...args);
|
|
247
258
|
}
|
|
248
259
|
|
|
249
260
|
// 降级处理:仅为确实不存在的方法提供内置实现
|
|
@@ -251,7 +262,7 @@ Cache.prototype._safeCall = async function (method, args = []) {
|
|
|
251
262
|
case 'strlen': {
|
|
252
263
|
const key = args[0];
|
|
253
264
|
// 获取值并计算长度
|
|
254
|
-
const value = await this.
|
|
265
|
+
const value = await this._client.get(key);
|
|
255
266
|
if (value === null || value === undefined) {
|
|
256
267
|
return 0;
|
|
257
268
|
}
|
|
@@ -262,14 +273,14 @@ Cache.prototype._safeCall = async function (method, args = []) {
|
|
|
262
273
|
const [oldKey, newKey] = args;
|
|
263
274
|
try {
|
|
264
275
|
// 获取原键值
|
|
265
|
-
const value = await this.
|
|
276
|
+
const value = await this._client.get(oldKey);
|
|
266
277
|
if (value === null || value === undefined) {
|
|
267
278
|
return false;
|
|
268
279
|
}
|
|
269
280
|
// 设置新键
|
|
270
|
-
await this.
|
|
281
|
+
await this._client.set(newKey, value);
|
|
271
282
|
// 删除原键
|
|
272
|
-
await this.
|
|
283
|
+
await this._client.del(oldKey);
|
|
273
284
|
return 'OK';
|
|
274
285
|
} catch (error) {
|
|
275
286
|
$.log.error(`[Cache] [_safeCall] rename操作失败:`, error);
|
|
@@ -279,7 +290,7 @@ Cache.prototype._safeCall = async function (method, args = []) {
|
|
|
279
290
|
case 'type': {
|
|
280
291
|
const key = args[0];
|
|
281
292
|
// 获取值并判断类型
|
|
282
|
-
const value = await this.
|
|
293
|
+
const value = await this._client.get(key);
|
|
283
294
|
if (value === null || value === undefined) {
|
|
284
295
|
return 'none';
|
|
285
296
|
}
|
|
@@ -406,7 +417,7 @@ Cache.prototype.mget = function () {
|
|
|
406
417
|
const promises = scopeKeys.map(async (key) => {
|
|
407
418
|
try {
|
|
408
419
|
// 直接使用db.get方法获取单个键的值
|
|
409
|
-
return await this.
|
|
420
|
+
return await this._client.get(key);
|
|
410
421
|
} catch (error) {
|
|
411
422
|
return null;
|
|
412
423
|
}
|
|
@@ -430,8 +441,8 @@ Cache.prototype.incr = async function (key, step = 1) {
|
|
|
430
441
|
// 添加作用域前缀
|
|
431
442
|
const scopeKey = `${this.scope}:${key}`;
|
|
432
443
|
// 优先使用incr方法,如果不存在则使用addInt
|
|
433
|
-
if (this.
|
|
434
|
-
return await this.
|
|
444
|
+
if (this._client && typeof this._client.incr === 'function') {
|
|
445
|
+
return await this._client.incr(scopeKey, step);
|
|
435
446
|
} else {
|
|
436
447
|
return await this._safeCall('addInt', [scopeKey, step]);
|
|
437
448
|
}
|
|
@@ -819,17 +830,14 @@ Cache.prototype.clearExpire = async function () {
|
|
|
819
830
|
* @return {Promise<Boolean>} 是否关闭成功
|
|
820
831
|
*/
|
|
821
832
|
Cache.prototype.dispose = async function () {
|
|
822
|
-
if (this.
|
|
823
|
-
return await this.
|
|
833
|
+
if (this._client && typeof this._client.dispose === 'function') {
|
|
834
|
+
return await this._client.dispose();
|
|
824
835
|
}
|
|
825
836
|
return true;
|
|
826
837
|
};
|
|
827
838
|
|
|
828
839
|
|
|
829
840
|
|
|
830
|
-
// 修改导出方式以支持多种导入格式
|
|
831
|
-
exports.Cache = Cache;
|
|
832
|
-
|
|
833
841
|
/**
|
|
834
842
|
* 确保连接池对象存在
|
|
835
843
|
*/
|
|
@@ -846,13 +854,14 @@ if (!$.pool.cache) {
|
|
|
846
854
|
* @param {Object} config 配置参数
|
|
847
855
|
* @return {Object} 返回一个Cache类实例
|
|
848
856
|
*/
|
|
849
|
-
function cacheAdmin(scope, config) {
|
|
857
|
+
function cacheAdmin(scope, config = {}) {
|
|
850
858
|
if (!scope) {
|
|
851
859
|
scope = 'sys';
|
|
852
860
|
}
|
|
853
861
|
var obj = $.pool.cache[scope];
|
|
854
862
|
if (!obj) {
|
|
855
|
-
|
|
863
|
+
config.scope = scope;
|
|
864
|
+
$.pool.cache[scope] = new Cache(config);
|
|
856
865
|
obj = $.pool.cache[scope];
|
|
857
866
|
}
|
|
858
867
|
return obj;
|
|
@@ -861,4 +870,7 @@ function cacheAdmin(scope, config) {
|
|
|
861
870
|
/**
|
|
862
871
|
* @module 导出Cache管理器
|
|
863
872
|
*/
|
|
864
|
-
exports
|
|
873
|
+
module.exports = {
|
|
874
|
+
Cache,
|
|
875
|
+
cacheAdmin
|
|
876
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_cache",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.6",
|
|
4
4
|
"description": "一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://gitee.com/qiuwenwu/mm_cache#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"mm_cachebase": "^1.9.
|
|
34
|
-
"mm_mongodb": "^1.5.
|
|
35
|
-
"mm_redis": "^2.
|
|
33
|
+
"mm_cachebase": "^1.9.6",
|
|
34
|
+
"mm_mongodb": "^1.5.5",
|
|
35
|
+
"mm_redis": "^2.1.1"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/test.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
const Cache = require('./index.js')
|
|
2
|
-
const cache_admin = require('./index.js').cache_admin;
|
|
1
|
+
const { Cache, cacheAdmin } = require('./index.js');
|
|
3
2
|
|
|
4
3
|
// 测试统计变量
|
|
5
4
|
const testStats = {
|
|
@@ -60,6 +59,16 @@ function isMethodUnsupported(errorMessage, methodName) {
|
|
|
60
59
|
async function recordTestResult(cache, methodName, testFunction) {
|
|
61
60
|
const cacheWay = cache.getCacheWay();
|
|
62
61
|
|
|
62
|
+
// 确保方法结果对象存在
|
|
63
|
+
if (!testStats.methodResults[methodName]) {
|
|
64
|
+
testStats.methodResults[methodName] = {
|
|
65
|
+
total: 0,
|
|
66
|
+
passed: 0,
|
|
67
|
+
unsupported: 0,
|
|
68
|
+
cacheWays: {}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
63
72
|
// 初始化方法的缓存方式结果
|
|
64
73
|
if (!testStats.methodResults[methodName].cacheWays[cacheWay]) {
|
|
65
74
|
testStats.methodResults[methodName].cacheWays[cacheWay] = {
|
|
@@ -109,6 +118,11 @@ async function recordTestResult(cache, methodName, testFunction) {
|
|
|
109
118
|
|
|
110
119
|
return false;
|
|
111
120
|
} finally {
|
|
121
|
+
// 确保缓存方式结果对象存在
|
|
122
|
+
if (!testStats.results[cacheWay]) {
|
|
123
|
+
testStats.results[cacheWay] = { total: 0, passed: 0, unsupported: 0 };
|
|
124
|
+
}
|
|
125
|
+
|
|
112
126
|
// 增加总数计数
|
|
113
127
|
testStats.results[cacheWay].total++;
|
|
114
128
|
testStats.methodResults[methodName].total++;
|
|
@@ -453,7 +467,10 @@ async function testCacheWay(cacheWay) {
|
|
|
453
467
|
$.log.debug(`[Cache] ==============================`);
|
|
454
468
|
|
|
455
469
|
// 直接使用指定的缓存方式创建Cache实例
|
|
456
|
-
const cache = new Cache(
|
|
470
|
+
const cache = new Cache({
|
|
471
|
+
way: cacheWay,
|
|
472
|
+
scope: `test_${cacheWay}`
|
|
473
|
+
});
|
|
457
474
|
const connected = await cache.connect();
|
|
458
475
|
|
|
459
476
|
if (connected) {
|