mm_cache 1.4.5 → 1.4.7
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 +39 -26
- package/package.json +2 -2
- 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.
|
|
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
|
}
|
|
@@ -101,6 +113,7 @@ Cache.prototype.getCacheWay = function () {
|
|
|
101
113
|
return this.config.way;
|
|
102
114
|
};
|
|
103
115
|
|
|
116
|
+
|
|
104
117
|
/**
|
|
105
118
|
* @description 连接缓存数据库
|
|
106
119
|
* @return {Promise<Boolean>} 是否连接成功
|
|
@@ -116,9 +129,9 @@ Cache.prototype.connect = async function () {
|
|
|
116
129
|
const {
|
|
117
130
|
redisAdmin
|
|
118
131
|
} = require('mm_redis');
|
|
119
|
-
this.
|
|
120
|
-
if (!this.
|
|
121
|
-
await this.
|
|
132
|
+
this._client = redisAdmin(this.config.scope, this.config.redis);
|
|
133
|
+
if (!this._client.connected) {
|
|
134
|
+
await this._client.open();
|
|
122
135
|
}
|
|
123
136
|
} catch (e) {
|
|
124
137
|
$.log.warn('[Cache] [connect] Cache模块加载失败,回退到memory:', e.message);
|
|
@@ -133,9 +146,9 @@ Cache.prototype.connect = async function () {
|
|
|
133
146
|
const {
|
|
134
147
|
mongodbAdmin
|
|
135
148
|
} = require('mm_mongodb');
|
|
136
|
-
this.
|
|
137
|
-
if (!this.
|
|
138
|
-
await this.
|
|
149
|
+
this._client = mongodbAdmin(this.config.scope, this.config.mongodb);
|
|
150
|
+
if (!this._client.connected) {
|
|
151
|
+
await this._client.open();
|
|
139
152
|
}
|
|
140
153
|
} catch (e) {
|
|
141
154
|
$.log.warn('[Cache] [connect] MongoDB模块加载失败,回退到memory:', e.message);
|
|
@@ -150,7 +163,7 @@ Cache.prototype.connect = async function () {
|
|
|
150
163
|
const {
|
|
151
164
|
cachebaseAdmin
|
|
152
165
|
} = require('mm_cachebase');
|
|
153
|
-
this.
|
|
166
|
+
this._client = cachebaseAdmin(this.config.scope, this.config.cachebase);
|
|
154
167
|
} catch (e) {
|
|
155
168
|
$.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
|
|
156
169
|
return false;
|
|
@@ -162,7 +175,7 @@ Cache.prototype.connect = async function () {
|
|
|
162
175
|
const {
|
|
163
176
|
cachebaseAdmin
|
|
164
177
|
} = require('mm_cachebase');
|
|
165
|
-
this.
|
|
178
|
+
this._client = cachebaseAdmin('default');
|
|
166
179
|
} catch (e) {
|
|
167
180
|
$.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
|
|
168
181
|
return false;
|
|
@@ -217,8 +230,8 @@ Cache.prototype.has = async function (key) {
|
|
|
217
230
|
// 添加作用域前缀
|
|
218
231
|
const scopeKey = `${this.scope}:${key}`;
|
|
219
232
|
// 优先使用has方法,如果不存在则使用exists
|
|
220
|
-
if (this.
|
|
221
|
-
return await this.
|
|
233
|
+
if (this._client && typeof this._client.has === 'function') {
|
|
234
|
+
return await this._client.has(scopeKey);
|
|
222
235
|
} else {
|
|
223
236
|
return await this._safeCall('exists', [scopeKey]);
|
|
224
237
|
}
|
|
@@ -231,18 +244,18 @@ Cache.prototype.has = async function (key) {
|
|
|
231
244
|
* @return {Promise<any>} 方法返回值
|
|
232
245
|
*/
|
|
233
246
|
Cache.prototype._safeCall = async function (method, args = []) {
|
|
234
|
-
if (!this.
|
|
247
|
+
if (!this._client) {
|
|
235
248
|
// 如果未连接,自动尝试连接
|
|
236
249
|
await this.connect();
|
|
237
|
-
if (!this.
|
|
250
|
+
if (!this._client) {
|
|
238
251
|
$.log.error('[Cache] [_safeCall] 数据库未初始化');
|
|
239
252
|
return null;
|
|
240
253
|
}
|
|
241
254
|
}
|
|
242
255
|
|
|
243
256
|
// 如果方法存在,直接调用
|
|
244
|
-
if (typeof this.
|
|
245
|
-
return await this.
|
|
257
|
+
if (typeof this._client[method] === 'function') {
|
|
258
|
+
return await this._client[method](...args);
|
|
246
259
|
}
|
|
247
260
|
|
|
248
261
|
// 降级处理:仅为确实不存在的方法提供内置实现
|
|
@@ -250,7 +263,7 @@ Cache.prototype._safeCall = async function (method, args = []) {
|
|
|
250
263
|
case 'strlen': {
|
|
251
264
|
const key = args[0];
|
|
252
265
|
// 获取值并计算长度
|
|
253
|
-
const value = await this.
|
|
266
|
+
const value = await this._client.get(key);
|
|
254
267
|
if (value === null || value === undefined) {
|
|
255
268
|
return 0;
|
|
256
269
|
}
|
|
@@ -261,14 +274,14 @@ Cache.prototype._safeCall = async function (method, args = []) {
|
|
|
261
274
|
const [oldKey, newKey] = args;
|
|
262
275
|
try {
|
|
263
276
|
// 获取原键值
|
|
264
|
-
const value = await this.
|
|
277
|
+
const value = await this._client.get(oldKey);
|
|
265
278
|
if (value === null || value === undefined) {
|
|
266
279
|
return false;
|
|
267
280
|
}
|
|
268
281
|
// 设置新键
|
|
269
|
-
await this.
|
|
282
|
+
await this._client.set(newKey, value);
|
|
270
283
|
// 删除原键
|
|
271
|
-
await this.
|
|
284
|
+
await this._client.del(oldKey);
|
|
272
285
|
return 'OK';
|
|
273
286
|
} catch (error) {
|
|
274
287
|
$.log.error(`[Cache] [_safeCall] rename操作失败:`, error);
|
|
@@ -278,7 +291,7 @@ Cache.prototype._safeCall = async function (method, args = []) {
|
|
|
278
291
|
case 'type': {
|
|
279
292
|
const key = args[0];
|
|
280
293
|
// 获取值并判断类型
|
|
281
|
-
const value = await this.
|
|
294
|
+
const value = await this._client.get(key);
|
|
282
295
|
if (value === null || value === undefined) {
|
|
283
296
|
return 'none';
|
|
284
297
|
}
|
|
@@ -405,7 +418,7 @@ Cache.prototype.mget = function () {
|
|
|
405
418
|
const promises = scopeKeys.map(async (key) => {
|
|
406
419
|
try {
|
|
407
420
|
// 直接使用db.get方法获取单个键的值
|
|
408
|
-
return await this.
|
|
421
|
+
return await this._client.get(key);
|
|
409
422
|
} catch (error) {
|
|
410
423
|
return null;
|
|
411
424
|
}
|
|
@@ -429,8 +442,8 @@ Cache.prototype.incr = async function (key, step = 1) {
|
|
|
429
442
|
// 添加作用域前缀
|
|
430
443
|
const scopeKey = `${this.scope}:${key}`;
|
|
431
444
|
// 优先使用incr方法,如果不存在则使用addInt
|
|
432
|
-
if (this.
|
|
433
|
-
return await this.
|
|
445
|
+
if (this._client && typeof this._client.incr === 'function') {
|
|
446
|
+
return await this._client.incr(scopeKey, step);
|
|
434
447
|
} else {
|
|
435
448
|
return await this._safeCall('addInt', [scopeKey, step]);
|
|
436
449
|
}
|
|
@@ -818,8 +831,8 @@ Cache.prototype.clearExpire = async function () {
|
|
|
818
831
|
* @return {Promise<Boolean>} 是否关闭成功
|
|
819
832
|
*/
|
|
820
833
|
Cache.prototype.dispose = async function () {
|
|
821
|
-
if (this.
|
|
822
|
-
return await this.
|
|
834
|
+
if (this._client && typeof this._client.dispose === 'function') {
|
|
835
|
+
return await this._client.dispose();
|
|
823
836
|
}
|
|
824
837
|
return true;
|
|
825
838
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_cache",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.7",
|
|
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.
|
|
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({
|
|
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) {
|