mm_cache 1.4.3 → 1.4.5

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.
@@ -6,10 +6,20 @@
6
6
  |-------|-------|---------|-----------|--------|----------|
7
7
  | set | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
8
8
  | get | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
9
- | del | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
9
+ | del | ✅ | ✅ | ✅ | ✅ | function (key_path, overwrite = true) {
10
+ if (typeof this !== 'object' || this === null || Array.isArray(this)) {
11
+ throw new TypeError('del方法只能用于普通对象');
12
+ }
13
+ return del(this, key_path, overwrite);
14
+ } |
10
15
  | has | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
11
16
  | clear | ✅ | ✅ | ✅ | ✅ | 所有方式功能实现效果相同,返回值一致 |
12
- | keys | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
17
+ | keys | ✅ | ✅ | ✅ | ✅ | function (include_proto = false, include_private = false) {
18
+ if (typeof this !== 'object' || this === null) {
19
+ throw new TypeError('keys方法只能用于对象');
20
+ }
21
+ return keys(this, include_proto, include_private);
22
+ } |
13
23
  | ttl | ✅ | ✅ | ✅ | ✅ | 所有方式均支持Redis语义,返回-1表示键存在但无过期时间,-2表示键不存在 |
14
24
 
15
25
  ## 原子操作方法
@@ -0,0 +1,211 @@
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
+ }
211
+ ];
package/index.js CHANGED
@@ -3,78 +3,72 @@
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(scope, config) {
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
73
  this.db = null;
80
74
  // 初始化配置
@@ -92,6 +86,11 @@ Cache.prototype.setConfig = function (config) {
92
86
  if (config) {
93
87
  $.push(this.config, config);
94
88
  }
89
+ if (this.config.scope) {
90
+ this.scope = scope;
91
+ } else {
92
+ this.scope = $.val.scope + '';
93
+ }
95
94
  };
96
95
 
97
96
  /**
@@ -827,9 +826,6 @@ Cache.prototype.dispose = async function () {
827
826
 
828
827
 
829
828
 
830
- // 修改导出方式以支持多种导入格式
831
- exports.Cache = Cache;
832
-
833
829
  /**
834
830
  * 确保连接池对象存在
835
831
  */
@@ -846,13 +842,14 @@ if (!$.pool.cache) {
846
842
  * @param {Object} config 配置参数
847
843
  * @return {Object} 返回一个Cache类实例
848
844
  */
849
- function cacheAdmin(scope, config) {
845
+ function cacheAdmin(scope, config = {}) {
850
846
  if (!scope) {
851
847
  scope = 'sys';
852
848
  }
853
849
  var obj = $.pool.cache[scope];
854
850
  if (!obj) {
855
- $.pool.cache[scope] = new Cache(scope, config);
851
+ config.scope = scope;
852
+ $.pool.cache[scope] = new Cache(config);
856
853
  obj = $.pool.cache[scope];
857
854
  }
858
855
  return obj;
@@ -861,4 +858,7 @@ function cacheAdmin(scope, config) {
861
858
  /**
862
859
  * @module 导出Cache管理器
863
860
  */
864
- exports.cacheAdmin = cacheAdmin;
861
+ module.exports = {
862
+ Cache,
863
+ cacheAdmin
864
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_cache",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
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.1",
34
- "mm_mongodb": "^1.5.3",
35
- "mm_redis": "^2.0.9"
33
+ "mm_cachebase": "^1.9.3",
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').Cache;
2
- const cache_admin = require('./index.js').cache_admin;
1
+ const { Cache, cacheAdmin } = require('./index.js');
3
2
 
4
3
  // 测试统计变量
5
4
  const testStats = {
@@ -453,7 +452,7 @@ async function testCacheWay(cacheWay) {
453
452
  $.log.debug(`[Cache] ==============================`);
454
453
 
455
454
  // 直接使用指定的缓存方式创建Cache实例
456
- const cache = new Cache(`test_${cacheWay}`, { way: cacheWay });
455
+ const cache = new Cache({ way: cacheWay }, `test_${cacheWay}`);
457
456
  const connected = await cache.connect();
458
457
 
459
458
  if (connected) {