mm_cachebase 1.9.2 → 1.9.3

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.
Files changed (3) hide show
  1. package/eslint.config.js +210 -67
  2. package/index.js +21 -9
  3. package/package.json +1 -1
package/eslint.config.js CHANGED
@@ -1,68 +1,211 @@
1
- const jsdoc = require('eslint-plugin-jsdoc');
2
-
3
- module.exports = [
4
- {
5
- files: ['lib/**/*.js'],
6
- languageOptions: {
7
- ecmaVersion: 2020,
8
- sourceType: 'script'
9
- },
10
- plugins: {
11
- jsdoc: jsdoc
12
- },
13
- rules: {
14
- // JSDoc相关规则 - 设置为error级别强制要求
15
- 'jsdoc/require-jsdoc': ['error', {
16
- require: {
17
- FunctionDeclaration: true,
18
- MethodDefinition: true,
19
- ClassDeclaration: true,
20
- ArrowFunctionExpression: true
21
- }
22
- }],
23
- 'jsdoc/check-param-names': 'off',
24
- 'jsdoc/check-tag-names': 'off',
25
- 'jsdoc/check-types': 'off',
26
- 'jsdoc/require-param': 'off',
27
- 'jsdoc/require-param-description': 'off',
28
- 'jsdoc/require-returns': 'off',
29
- 'jsdoc/require-returns-description': 'off',
30
-
31
- // 基础语法规则
32
- 'no-unused-vars': ['error', { args: 'none', vars: 'all' }],
33
- 'no-console': 'warn',
34
-
35
- // 代码风格规则
36
- 'indent': ['error', 2],
37
- 'quotes': ['error', 'single'],
38
- 'semi': ['error', 'always'],
39
- 'comma-dangle': ['error', 'never'],
40
- 'no-trailing-spaces': 'error',
41
- 'eol-last': 'error',
42
- 'max-len': ['error', { code: 100 }],
43
- 'max-lines-per-function': ['error', { max: 40 }],
44
- 'complexity': ['error', 12],
45
- 'max-depth': ['error', 4],
46
- 'max-params': ['error', 4],
47
-
48
- // 命名规则
49
- 'id-length': ['error', {
50
- min: 1,
51
- max: 20,
52
- properties: 'always'
53
- }]
54
- }
55
- },
56
- {
57
- files: ['test/**/*.js'],
58
- languageOptions: {
59
- ecmaVersion: 2020,
60
- sourceType: 'script'
61
- },
62
- rules: {
63
- // 测试文件中允许使用console
64
- 'no-console': 'off',
65
- 'no-unused-vars': 'off'
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
@@ -11,10 +11,10 @@ class CacheBase extends Base {
11
11
  /**
12
12
  * 默认配置静态变量
13
13
  */
14
- static default_config = {
14
+ static config = {
15
15
  enabled: true,
16
16
  persist: true,
17
- file_path: './test_cache/undefined.cache.json',
17
+ file_path: './cache/base.json',
18
18
  debug: false,
19
19
  auto_save: true,
20
20
  save_interval: 30000,
@@ -28,7 +28,7 @@ class CacheBase extends Base {
28
28
  * @param {Object} config - 配置对象
29
29
  */
30
30
  constructor(config = {}) {
31
- super(config);
31
+ super(CacheBase.config);
32
32
  // 存储实际数据
33
33
  this._data = {};
34
34
 
@@ -2344,9 +2344,14 @@ CacheBase.prototype.hlen = function(key) {
2344
2344
  });
2345
2345
  };
2346
2346
 
2347
- // 导出类
2348
- exports.CacheBase = CacheBase;
2349
- exports.cachebaseAdmin = function(scope, dir) {
2347
+
2348
+ /**
2349
+ * @description CacheBase管理器,用于创建缓存
2350
+ * @param {String} scope 作用域
2351
+ * @param {Object} config 配置参数
2352
+ * @return {Object} 返回一个CacheBase类实例
2353
+ */
2354
+ function cachebaseAdmin(scope, config) {
2350
2355
  if (!global.$) {
2351
2356
  global.$ = {};
2352
2357
  }
@@ -2361,11 +2366,18 @@ exports.cachebaseAdmin = function(scope, dir) {
2361
2366
  }
2362
2367
  var obj = global.$.pool.cachebase[scope];
2363
2368
  if (!obj) {
2364
- const config = {
2365
- file_path: dir ? './cachebase_' + scope + '.json' : '/cache/cachebase_' + scope + '.json'
2369
+ const dbConfig = {
2370
+ file_path: config && config.dir ? './cachebase_' + scope + '.json' : '/cache/cachebase_' + scope + '.json',
2371
+ ...(typeof config === 'object' && config ? config : {})
2366
2372
  };
2367
- global.$.pool.cachebase[scope] = new CacheBase(config);
2373
+ global.$.pool.cachebase[scope] = new CacheBase(dbConfig);
2368
2374
  obj = global.$.pool.cachebase[scope];
2369
2375
  }
2370
2376
  return obj;
2377
+ }
2378
+
2379
+ // 导出类
2380
+ module.exports = {
2381
+ CacheBase,
2382
+ cachebaseAdmin,
2371
2383
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_cachebase",
3
- "version": "1.9.2",
3
+ "version": "1.9.3",
4
4
  "description": "一个轻量级的本地缓存类库,提供与Redis兼容的API接口,支持过期时间设置、哈希表操作、数值操作、字符串操作等功能,以及完整的事件通知系统和多文件持久化功能。",
5
5
  "main": "index.js",
6
6
  "scripts": {