mm_cache 1.4.5 → 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.
Files changed (3) hide show
  1. package/index.js +42 -30
  2. package/package.json +2 -2
  3. package/test.js +19 -1
package/index.js CHANGED
@@ -70,7 +70,7 @@ class Cache extends Base {
70
70
  // 作用域
71
71
  this.scope;
72
72
  // 当前连接库
73
- this.db = null;
73
+ this._client = null;
74
74
  // 初始化配置
75
75
  this.setConfig(config);
76
76
  // 注意:不再在构造函数中直接调用异步的connect()
@@ -83,11 +83,23 @@ class Cache extends Base {
83
83
  * @param {Object} config 配置
84
84
  */
85
85
  Cache.prototype.setConfig = function (config) {
86
+ // 确保this.config存在
87
+ if (!this.config) {
88
+ this.config = {};
89
+ }
90
+
86
91
  if (config) {
92
+ // 使用$.push合并配置
87
93
  $.push(this.config, config);
88
94
  }
95
+
96
+ // 确保way属性存在
97
+ if (!this.config.way) {
98
+ this.config.way = 'memory'; // 默认缓存方式
99
+ }
100
+
89
101
  if (this.config.scope) {
90
- this.scope = scope;
102
+ this.scope = this.config.scope;
91
103
  } else {
92
104
  this.scope = $.val.scope + '';
93
105
  }
@@ -116,14 +128,14 @@ Cache.prototype.connect = async function () {
116
128
  const {
117
129
  redisAdmin
118
130
  } = require('mm_redis');
119
- this.db = redisAdmin(this.config.scope, this.config.redis);
120
- if (!this.db.connected) {
121
- await this.db.open();
131
+ this._client = redisAdmin(this.config.scope, this.config.redis);
132
+ if (!this._client.connected) {
133
+ await this._client.open();
122
134
  }
123
135
  } catch (e) {
124
136
  $.log.warn('[Cache] [connect] Cache模块加载失败,回退到memory:', e.message);
125
137
  this.config.way = 'memory';
126
- await this.connect(); // 递归调用,使用memory
138
+ await this._clientect(); // 递归调用,使用memory
127
139
  return false;
128
140
  }
129
141
  break;
@@ -133,14 +145,14 @@ Cache.prototype.connect = async function () {
133
145
  const {
134
146
  mongodbAdmin
135
147
  } = require('mm_mongodb');
136
- this.db = mongodbAdmin(this.config.scope, this.config.mongodb);
137
- if (!this.db.connected) {
138
- await this.db.open();
148
+ this._client = mongodbAdmin(this.config.scope, this.config.mongodb);
149
+ if (!this._client.connected) {
150
+ await this._client.open();
139
151
  }
140
152
  } catch (e) {
141
153
  $.log.warn('[Cache] [connect] MongoDB模块加载失败,回退到memory:', e.message);
142
154
  this.config.way = 'memory';
143
- await this.connect(); // 递归调用,使用cachebase memory
155
+ await this._clientect(); // 递归调用,使用cachebase memory
144
156
  return false;
145
157
  }
146
158
  break;
@@ -150,7 +162,7 @@ Cache.prototype.connect = async function () {
150
162
  const {
151
163
  cachebaseAdmin
152
164
  } = require('mm_cachebase');
153
- this.db = cachebaseAdmin(this.config.scope, this.config.cachebase);
165
+ this._client = cachebaseAdmin(this.config.scope, this.config.cachebase);
154
166
  } catch (e) {
155
167
  $.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
156
168
  return false;
@@ -162,7 +174,7 @@ Cache.prototype.connect = async function () {
162
174
  const {
163
175
  cachebaseAdmin
164
176
  } = require('mm_cachebase');
165
- this.db = cachebaseAdmin('default');
177
+ this._client = cachebaseAdmin('default');
166
178
  } catch (e) {
167
179
  $.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
168
180
  return false;
@@ -191,7 +203,7 @@ Cache.prototype.switchCache = async function (way, config) {
191
203
  if (config) {
192
204
  this.config[way] = Object.assign(this.config[way], config);
193
205
  }
194
- return await this.connect();
206
+ return await this._clientect();
195
207
  }
196
208
  return false;
197
209
  };
@@ -217,8 +229,8 @@ Cache.prototype.has = async function (key) {
217
229
  // 添加作用域前缀
218
230
  const scopeKey = `${this.scope}:${key}`;
219
231
  // 优先使用has方法,如果不存在则使用exists
220
- if (this.db && typeof this.db.has === 'function') {
221
- return await this.db.has(scopeKey);
232
+ if (this._client && typeof this._client.has === 'function') {
233
+ return await this._client.has(scopeKey);
222
234
  } else {
223
235
  return await this._safeCall('exists', [scopeKey]);
224
236
  }
@@ -231,18 +243,18 @@ Cache.prototype.has = async function (key) {
231
243
  * @return {Promise<any>} 方法返回值
232
244
  */
233
245
  Cache.prototype._safeCall = async function (method, args = []) {
234
- if (!this.db) {
246
+ if (!this._client) {
235
247
  // 如果未连接,自动尝试连接
236
- await this.connect();
237
- if (!this.db) {
248
+ await this._clientect();
249
+ if (!this._client) {
238
250
  $.log.error('[Cache] [_safeCall] 数据库未初始化');
239
251
  return null;
240
252
  }
241
253
  }
242
254
 
243
255
  // 如果方法存在,直接调用
244
- if (typeof this.db[method] === 'function') {
245
- return await this.db[method](...args);
256
+ if (typeof this._client[method] === 'function') {
257
+ return await this._client[method](...args);
246
258
  }
247
259
 
248
260
  // 降级处理:仅为确实不存在的方法提供内置实现
@@ -250,7 +262,7 @@ Cache.prototype._safeCall = async function (method, args = []) {
250
262
  case 'strlen': {
251
263
  const key = args[0];
252
264
  // 获取值并计算长度
253
- const value = await this.db.get(key);
265
+ const value = await this._client.get(key);
254
266
  if (value === null || value === undefined) {
255
267
  return 0;
256
268
  }
@@ -261,14 +273,14 @@ Cache.prototype._safeCall = async function (method, args = []) {
261
273
  const [oldKey, newKey] = args;
262
274
  try {
263
275
  // 获取原键值
264
- const value = await this.db.get(oldKey);
276
+ const value = await this._client.get(oldKey);
265
277
  if (value === null || value === undefined) {
266
278
  return false;
267
279
  }
268
280
  // 设置新键
269
- await this.db.set(newKey, value);
281
+ await this._client.set(newKey, value);
270
282
  // 删除原键
271
- await this.db.del(oldKey);
283
+ await this._client.del(oldKey);
272
284
  return 'OK';
273
285
  } catch (error) {
274
286
  $.log.error(`[Cache] [_safeCall] rename操作失败:`, error);
@@ -278,7 +290,7 @@ Cache.prototype._safeCall = async function (method, args = []) {
278
290
  case 'type': {
279
291
  const key = args[0];
280
292
  // 获取值并判断类型
281
- const value = await this.db.get(key);
293
+ const value = await this._client.get(key);
282
294
  if (value === null || value === undefined) {
283
295
  return 'none';
284
296
  }
@@ -405,7 +417,7 @@ Cache.prototype.mget = function () {
405
417
  const promises = scopeKeys.map(async (key) => {
406
418
  try {
407
419
  // 直接使用db.get方法获取单个键的值
408
- return await this.db.get(key);
420
+ return await this._client.get(key);
409
421
  } catch (error) {
410
422
  return null;
411
423
  }
@@ -429,8 +441,8 @@ Cache.prototype.incr = async function (key, step = 1) {
429
441
  // 添加作用域前缀
430
442
  const scopeKey = `${this.scope}:${key}`;
431
443
  // 优先使用incr方法,如果不存在则使用addInt
432
- if (this.db && typeof this.db.incr === 'function') {
433
- return await this.db.incr(scopeKey, step);
444
+ if (this._client && typeof this._client.incr === 'function') {
445
+ return await this._client.incr(scopeKey, step);
434
446
  } else {
435
447
  return await this._safeCall('addInt', [scopeKey, step]);
436
448
  }
@@ -818,8 +830,8 @@ Cache.prototype.clearExpire = async function () {
818
830
  * @return {Promise<Boolean>} 是否关闭成功
819
831
  */
820
832
  Cache.prototype.dispose = async function () {
821
- if (this.db && typeof this.db.dispose === 'function') {
822
- return await this.db.dispose();
833
+ if (this._client && typeof this._client.dispose === 'function') {
834
+ return await this._client.dispose();
823
835
  }
824
836
  return true;
825
837
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_cache",
3
- "version": "1.4.5",
3
+ "version": "1.4.6",
4
4
  "description": "一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "homepage": "https://gitee.com/qiuwenwu/mm_cache#readme",
32
32
  "dependencies": {
33
- "mm_cachebase": "^1.9.3",
33
+ "mm_cachebase": "^1.9.6",
34
34
  "mm_mongodb": "^1.5.5",
35
35
  "mm_redis": "^2.1.1"
36
36
  }
package/test.js CHANGED
@@ -59,6 +59,16 @@ function isMethodUnsupported(errorMessage, methodName) {
59
59
  async function recordTestResult(cache, methodName, testFunction) {
60
60
  const cacheWay = cache.getCacheWay();
61
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
+
62
72
  // 初始化方法的缓存方式结果
63
73
  if (!testStats.methodResults[methodName].cacheWays[cacheWay]) {
64
74
  testStats.methodResults[methodName].cacheWays[cacheWay] = {
@@ -108,6 +118,11 @@ async function recordTestResult(cache, methodName, testFunction) {
108
118
 
109
119
  return false;
110
120
  } finally {
121
+ // 确保缓存方式结果对象存在
122
+ if (!testStats.results[cacheWay]) {
123
+ testStats.results[cacheWay] = { total: 0, passed: 0, unsupported: 0 };
124
+ }
125
+
111
126
  // 增加总数计数
112
127
  testStats.results[cacheWay].total++;
113
128
  testStats.methodResults[methodName].total++;
@@ -452,7 +467,10 @@ async function testCacheWay(cacheWay) {
452
467
  $.log.debug(`[Cache] ==============================`);
453
468
 
454
469
  // 直接使用指定的缓存方式创建Cache实例
455
- const cache = new Cache({ way: cacheWay }, `test_${cacheWay}`);
470
+ const cache = new Cache({
471
+ way: cacheWay,
472
+ scope: `test_${cacheWay}`
473
+ });
456
474
  const connected = await cache.connect();
457
475
 
458
476
  if (connected) {