mm_cache 1.4.2 → 1.4.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.
- package/index.js +67 -79
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
* @version 1.2
|
|
5
5
|
*/
|
|
6
6
|
const EventEmitter = require('events');
|
|
7
|
-
require('
|
|
7
|
+
const { Base } = require('mm_expand');
|
|
8
8
|
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
10
|
* @class 缓存类
|
|
11
11
|
*/
|
|
12
|
-
class Cache extends
|
|
12
|
+
class Cache extends Base {
|
|
13
13
|
/**
|
|
14
14
|
* @description 创建Cache帮助类函数 (构造函数)
|
|
15
15
|
* @param {String} scope 作用域
|
|
@@ -57,27 +57,15 @@ class Cache extends EventEmitter {
|
|
|
57
57
|
// 数据库名
|
|
58
58
|
database: "mm",
|
|
59
59
|
// 用户名
|
|
60
|
-
user: "
|
|
60
|
+
user: "",
|
|
61
61
|
// 密码
|
|
62
62
|
password: "",
|
|
63
|
-
// 连接选项
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
// 连接超时时间
|
|
70
|
-
connectTimeoutMS: 10000,
|
|
71
|
-
// 服务器选择超时
|
|
72
|
-
serverSelectionTimeoutMS: 5000,
|
|
73
|
-
// 重试策略
|
|
74
|
-
retryWrites: true
|
|
75
|
-
},
|
|
76
|
-
// 重试配置
|
|
77
|
-
retry: {
|
|
78
|
-
maxAttempts: 3,
|
|
79
|
-
delayMS: 1000
|
|
80
|
-
}
|
|
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
|
|
81
69
|
}
|
|
82
70
|
};
|
|
83
71
|
// 作用域
|
|
@@ -100,9 +88,9 @@ class Cache extends EventEmitter {
|
|
|
100
88
|
* @description 设置配置
|
|
101
89
|
* @param {Object} config 配置
|
|
102
90
|
*/
|
|
103
|
-
Cache.prototype.setConfig = function(config) {
|
|
91
|
+
Cache.prototype.setConfig = function (config) {
|
|
104
92
|
if (config) {
|
|
105
|
-
|
|
93
|
+
$.push(this.config, config);
|
|
106
94
|
}
|
|
107
95
|
};
|
|
108
96
|
|
|
@@ -110,7 +98,7 @@ Cache.prototype.setConfig = function(config) {
|
|
|
110
98
|
* @description 获取当前缓存方式
|
|
111
99
|
* @return {String} 缓存方式
|
|
112
100
|
*/
|
|
113
|
-
Cache.prototype.getCacheWay = function() {
|
|
101
|
+
Cache.prototype.getCacheWay = function () {
|
|
114
102
|
return this.config.way;
|
|
115
103
|
};
|
|
116
104
|
|
|
@@ -118,7 +106,7 @@ Cache.prototype.getCacheWay = function() {
|
|
|
118
106
|
* @description 连接缓存数据库
|
|
119
107
|
* @return {Promise<Boolean>} 是否连接成功
|
|
120
108
|
*/
|
|
121
|
-
Cache.prototype.connect = async function() {
|
|
109
|
+
Cache.prototype.connect = async function () {
|
|
122
110
|
// 关闭现有连接
|
|
123
111
|
this.dispose();
|
|
124
112
|
|
|
@@ -127,9 +115,9 @@ Cache.prototype.connect = async function() {
|
|
|
127
115
|
case 'redis': {
|
|
128
116
|
try {
|
|
129
117
|
const {
|
|
130
|
-
|
|
118
|
+
redisAdmin
|
|
131
119
|
} = require('mm_redis');
|
|
132
|
-
this.db =
|
|
120
|
+
this.db = redisAdmin(this.config.scope, this.config.redis);
|
|
133
121
|
if (!this.db.connected) {
|
|
134
122
|
await this.db.open();
|
|
135
123
|
}
|
|
@@ -144,9 +132,9 @@ Cache.prototype.connect = async function() {
|
|
|
144
132
|
case 'mongodb': {
|
|
145
133
|
try {
|
|
146
134
|
const {
|
|
147
|
-
|
|
135
|
+
mongodbAdmin
|
|
148
136
|
} = require('mm_mongodb');
|
|
149
|
-
this.db =
|
|
137
|
+
this.db = mongodbAdmin(this.config.scope, this.config.mongodb);
|
|
150
138
|
if (!this.db.connected) {
|
|
151
139
|
await this.db.open();
|
|
152
140
|
}
|
|
@@ -161,9 +149,9 @@ Cache.prototype.connect = async function() {
|
|
|
161
149
|
case 'cachebase':
|
|
162
150
|
try {
|
|
163
151
|
const {
|
|
164
|
-
|
|
152
|
+
cachebaseAdmin
|
|
165
153
|
} = require('mm_cachebase');
|
|
166
|
-
this.db =
|
|
154
|
+
this.db = cachebaseAdmin(this.config.scope, this.config.cachebase);
|
|
167
155
|
} catch (e) {
|
|
168
156
|
$.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
|
|
169
157
|
return false;
|
|
@@ -173,9 +161,9 @@ Cache.prototype.connect = async function() {
|
|
|
173
161
|
default: {
|
|
174
162
|
try {
|
|
175
163
|
const {
|
|
176
|
-
|
|
164
|
+
cachebaseAdmin
|
|
177
165
|
} = require('mm_cachebase');
|
|
178
|
-
this.db =
|
|
166
|
+
this.db = cachebaseAdmin('default');
|
|
179
167
|
} catch (e) {
|
|
180
168
|
$.log.error('[Cache] [connect] CacheBase模块加载失败,无法初始化缓存:', e.message);
|
|
181
169
|
return false;
|
|
@@ -198,7 +186,7 @@ Cache.prototype.connect = async function() {
|
|
|
198
186
|
* @param {Object} config 可选配置
|
|
199
187
|
* @return {Promise<Boolean>} 是否成功
|
|
200
188
|
*/
|
|
201
|
-
Cache.prototype.switchCache = async function(way, config) {
|
|
189
|
+
Cache.prototype.switchCache = async function (way, config) {
|
|
202
190
|
if (['cachebase', 'redis', 'mongodb', 'memory'].includes(way)) {
|
|
203
191
|
this.config.way = way;
|
|
204
192
|
if (config) {
|
|
@@ -214,7 +202,7 @@ Cache.prototype.switchCache = async function(way, config) {
|
|
|
214
202
|
* @param {String} pattern 匹配模式
|
|
215
203
|
* @return {Promise<Array>} 键名数组
|
|
216
204
|
*/
|
|
217
|
-
Cache.prototype.keys = async function(pattern) {
|
|
205
|
+
Cache.prototype.keys = async function (pattern) {
|
|
218
206
|
// 添加作用域前缀
|
|
219
207
|
const scopePattern = pattern ? `${this.scope}:${pattern}` : `${this.scope}:*`;
|
|
220
208
|
// 直接调用底层keys方法
|
|
@@ -226,7 +214,7 @@ Cache.prototype.keys = async function(pattern) {
|
|
|
226
214
|
* @param {String} key 键名
|
|
227
215
|
* @return {Promise<Boolean>} 是否存在
|
|
228
216
|
*/
|
|
229
|
-
Cache.prototype.has = async function(key) {
|
|
217
|
+
Cache.prototype.has = async function (key) {
|
|
230
218
|
// 添加作用域前缀
|
|
231
219
|
const scopeKey = `${this.scope}:${key}`;
|
|
232
220
|
// 优先使用has方法,如果不存在则使用exists
|
|
@@ -243,7 +231,7 @@ Cache.prototype.has = async function(key) {
|
|
|
243
231
|
* @param {Array} args 参数数组
|
|
244
232
|
* @return {Promise<any>} 方法返回值
|
|
245
233
|
*/
|
|
246
|
-
Cache.prototype._safeCall = async function(method, args = []) {
|
|
234
|
+
Cache.prototype._safeCall = async function (method, args = []) {
|
|
247
235
|
if (!this.db) {
|
|
248
236
|
// 如果未连接,自动尝试连接
|
|
249
237
|
await this.connect();
|
|
@@ -320,7 +308,7 @@ Cache.prototype._safeCall = async function(method, args = []) {
|
|
|
320
308
|
* @param {Number} seconds 秒
|
|
321
309
|
* @return {Promise<Boolean>} 操作结果
|
|
322
310
|
*/
|
|
323
|
-
Cache.prototype.add = async function(key, value, expire) {
|
|
311
|
+
Cache.prototype.add = async function (key, value, expire) {
|
|
324
312
|
// 添加作用域前缀
|
|
325
313
|
const scopeKey = `${this.scope}:${key}`;
|
|
326
314
|
// 直接调用底层存储的原生add方法
|
|
@@ -334,7 +322,7 @@ Cache.prototype.add = async function(key, value, expire) {
|
|
|
334
322
|
* @param {Number} expire 过期时间(秒),默认不设置过期时间
|
|
335
323
|
* @return {Promise<Boolean>} 是否设置成功
|
|
336
324
|
*/
|
|
337
|
-
Cache.prototype.set = async function(key, value, expire) {
|
|
325
|
+
Cache.prototype.set = async function (key, value, expire) {
|
|
338
326
|
// 添加作用域前缀
|
|
339
327
|
const scopeKey = `${this.scope}:${key}`;
|
|
340
328
|
return await this._safeCall('set', [scopeKey, value, expire]);
|
|
@@ -345,7 +333,7 @@ Cache.prototype.set = async function(key, value, expire) {
|
|
|
345
333
|
* @param {String} key 键名
|
|
346
334
|
* @return {Promise<Any>} 缓存值
|
|
347
335
|
*/
|
|
348
|
-
Cache.prototype.get = async function(key) {
|
|
336
|
+
Cache.prototype.get = async function (key) {
|
|
349
337
|
// 添加作用域前缀
|
|
350
338
|
const scopeKey = `${this.scope}:${key}`;
|
|
351
339
|
return await this._safeCall('get', [scopeKey]);
|
|
@@ -356,7 +344,7 @@ Cache.prototype.get = async function(key) {
|
|
|
356
344
|
* @param {String} key 键名
|
|
357
345
|
* @return {Promise<Boolean>} 是否删除成功
|
|
358
346
|
*/
|
|
359
|
-
Cache.prototype.del = async function(key) {
|
|
347
|
+
Cache.prototype.del = async function (key) {
|
|
360
348
|
// 添加作用域前缀
|
|
361
349
|
const scopeKey = `${this.scope}:${key}`;
|
|
362
350
|
return await this._safeCall('del', [scopeKey]);
|
|
@@ -367,7 +355,7 @@ Cache.prototype.del = async function(key) {
|
|
|
367
355
|
* @param {String} key 键名
|
|
368
356
|
* @return {Promise<Boolean>} 是否存在
|
|
369
357
|
*/
|
|
370
|
-
Cache.prototype.exists = async function(key) {
|
|
358
|
+
Cache.prototype.exists = async function (key) {
|
|
371
359
|
// 添加作用域前缀
|
|
372
360
|
const scopeKey = `${this.scope}:${key}`;
|
|
373
361
|
return await this._safeCall('exists', [scopeKey]);
|
|
@@ -379,7 +367,7 @@ Cache.prototype.exists = async function(key) {
|
|
|
379
367
|
* @param {Number} seconds 过期时间(秒)
|
|
380
368
|
* @return {Promise<Boolean>} 是否设置成功
|
|
381
369
|
*/
|
|
382
|
-
Cache.prototype.expire = async function(key, seconds) {
|
|
370
|
+
Cache.prototype.expire = async function (key, seconds) {
|
|
383
371
|
// 添加作用域前缀
|
|
384
372
|
const scopeKey = `${this.scope}:${key}`;
|
|
385
373
|
return await this._safeCall('expire', [scopeKey, seconds]);
|
|
@@ -391,7 +379,7 @@ Cache.prototype.expire = async function(key, seconds) {
|
|
|
391
379
|
* @param {Number} seconds 过期时间(秒),默认不设置过期时间
|
|
392
380
|
* @return {Promise<Number>} 过期时间(秒),-1表示永不过期,-2表示键不存在
|
|
393
381
|
*/
|
|
394
|
-
Cache.prototype.ttl = async function(key, seconds) {
|
|
382
|
+
Cache.prototype.ttl = async function (key, seconds) {
|
|
395
383
|
// 添加作用域前缀
|
|
396
384
|
const scopeKey = `${this.scope}:${key}`;
|
|
397
385
|
// 仅在seconds参数存在时才传递给底层方法
|
|
@@ -407,7 +395,7 @@ Cache.prototype.ttl = async function(key, seconds) {
|
|
|
407
395
|
* @param {...String} keys 键名列表
|
|
408
396
|
* @return {Array} 值数组,与键的顺序一致
|
|
409
397
|
*/
|
|
410
|
-
Cache.prototype.mget = function() {
|
|
398
|
+
Cache.prototype.mget = function () {
|
|
411
399
|
// 确保返回一个函数,满足测试脚本的检查
|
|
412
400
|
return async (...keys) => {
|
|
413
401
|
try {
|
|
@@ -438,7 +426,7 @@ Cache.prototype.mget = function() {
|
|
|
438
426
|
* @param {Number} step 步长,默认1
|
|
439
427
|
* @return {Promise<Number>} 递增后的值
|
|
440
428
|
*/
|
|
441
|
-
Cache.prototype.incr = async function(key, step = 1) {
|
|
429
|
+
Cache.prototype.incr = async function (key, step = 1) {
|
|
442
430
|
// 添加作用域前缀
|
|
443
431
|
const scopeKey = `${this.scope}:${key}`;
|
|
444
432
|
// 优先使用incr方法,如果不存在则使用addInt
|
|
@@ -455,7 +443,7 @@ Cache.prototype.incr = async function(key, step = 1) {
|
|
|
455
443
|
* @param {Number} step 步长,默认1
|
|
456
444
|
* @return {Promise<Number>} 递增后的值
|
|
457
445
|
*/
|
|
458
|
-
Cache.prototype.addInt = async function(key, step = 1) {
|
|
446
|
+
Cache.prototype.addInt = async function (key, step = 1) {
|
|
459
447
|
// 添加作用域前缀
|
|
460
448
|
const scopeKey = `${this.scope}:${key}`;
|
|
461
449
|
return await this._safeCall('addInt', [scopeKey, step]);
|
|
@@ -467,7 +455,7 @@ Cache.prototype.addInt = async function(key, step = 1) {
|
|
|
467
455
|
* @param {String} value 要追加的字符串
|
|
468
456
|
* @return {Promise<Number>} 追加后的字符串长度
|
|
469
457
|
*/
|
|
470
|
-
Cache.prototype.addStr = async function(key, value) {
|
|
458
|
+
Cache.prototype.addStr = async function (key, value) {
|
|
471
459
|
// 添加作用域前缀
|
|
472
460
|
const scopeKey = `${this.scope}:${key}`;
|
|
473
461
|
return await this._safeCall('addStr', [scopeKey, value]);
|
|
@@ -479,7 +467,7 @@ Cache.prototype.addStr = async function(key, value) {
|
|
|
479
467
|
* @param {Number} step 步长,默认1.0
|
|
480
468
|
* @return {Promise<Number>} 递增后的值
|
|
481
469
|
*/
|
|
482
|
-
Cache.prototype.addFloat = async function(key, step = 1.0) {
|
|
470
|
+
Cache.prototype.addFloat = async function (key, step = 1.0) {
|
|
483
471
|
// 确保step是数字类型
|
|
484
472
|
step = parseFloat(step) || 1.0;
|
|
485
473
|
// 添加作用域前缀
|
|
@@ -494,7 +482,7 @@ Cache.prototype.addFloat = async function(key, step = 1.0) {
|
|
|
494
482
|
* @param {Object} options 配置选项
|
|
495
483
|
* @return {Promise<Number>} 增加后的值
|
|
496
484
|
*/
|
|
497
|
-
Cache.prototype.incrby = async function(key, value, options) {
|
|
485
|
+
Cache.prototype.incrby = async function (key, value, options) {
|
|
498
486
|
// 添加作用域前缀
|
|
499
487
|
const scopeKey = `${this.scope}:${key}`;
|
|
500
488
|
return await this._safeCall('incrby', [scopeKey, value], options);
|
|
@@ -507,7 +495,7 @@ Cache.prototype.incrby = async function(key, value, options) {
|
|
|
507
495
|
* @param {Object} options 配置选项
|
|
508
496
|
* @return {Promise<Number>} 减少后的值
|
|
509
497
|
*/
|
|
510
|
-
Cache.prototype.decrby = async function(key, value, options) {
|
|
498
|
+
Cache.prototype.decrby = async function (key, value, options) {
|
|
511
499
|
// 添加作用域前缀
|
|
512
500
|
const scopeKey = `${this.scope}:${key}`;
|
|
513
501
|
return await this._safeCall('decrby', [scopeKey, value], options);
|
|
@@ -520,7 +508,7 @@ Cache.prototype.decrby = async function(key, value, options) {
|
|
|
520
508
|
* @param {Object} options 配置选项
|
|
521
509
|
* @return {Promise<Number>} 增加后的值
|
|
522
510
|
*/
|
|
523
|
-
Cache.prototype.incrbyfloat = async function(key, value, options) {
|
|
511
|
+
Cache.prototype.incrbyfloat = async function (key, value, options) {
|
|
524
512
|
// 添加作用域前缀
|
|
525
513
|
const scopeKey = `${this.scope}:${key}`;
|
|
526
514
|
return await this._safeCall('incrbyfloat', [scopeKey, value], options);
|
|
@@ -531,7 +519,7 @@ Cache.prototype.incrbyfloat = async function(key, value, options) {
|
|
|
531
519
|
* @param {Array} keyValues 键值对数组,格式: [key1, value1, key2, value2, ...]
|
|
532
520
|
* @return {Promise<Boolean>} 是否设置成功
|
|
533
521
|
*/
|
|
534
|
-
Cache.prototype.mset = async function(...keyValues) {
|
|
522
|
+
Cache.prototype.mset = async function (...keyValues) {
|
|
535
523
|
// 添加作用域前缀到所有键名
|
|
536
524
|
const scopeKeyValues = [];
|
|
537
525
|
for (let i = 0; i < keyValues.length; i += 2) {
|
|
@@ -546,7 +534,7 @@ Cache.prototype.mset = async function(...keyValues) {
|
|
|
546
534
|
* @param {Array} keys 键名数组
|
|
547
535
|
* @return {Promise<Array>} 缓存值数组
|
|
548
536
|
*/
|
|
549
|
-
Cache.prototype.mget = async function(...keys) {
|
|
537
|
+
Cache.prototype.mget = async function (...keys) {
|
|
550
538
|
// 处理参数:如果第一个参数是数组,则使用该数组;否则使用所有参数作为数组
|
|
551
539
|
let keysToUse = keys;
|
|
552
540
|
if (keys.length === 1 && Array.isArray(keys[0])) {
|
|
@@ -565,7 +553,7 @@ Cache.prototype.mget = async function(...keys) {
|
|
|
565
553
|
* @param {Any} value 字段值
|
|
566
554
|
* @return {Promise<Number>} 1表示新字段,0表示更新已有字段
|
|
567
555
|
*/
|
|
568
|
-
Cache.prototype.hset = async function(key, field, value) {
|
|
556
|
+
Cache.prototype.hset = async function (key, field, value) {
|
|
569
557
|
// 添加作用域前缀
|
|
570
558
|
const scopeKey = `${this.scope}:${key}`;
|
|
571
559
|
return await this._safeCall('hset', [scopeKey, field, value]);
|
|
@@ -577,7 +565,7 @@ Cache.prototype.hset = async function(key, field, value) {
|
|
|
577
565
|
* @param {String} field 字段名
|
|
578
566
|
* @return {Promise<Any>} 字段值
|
|
579
567
|
*/
|
|
580
|
-
Cache.prototype.hget = async function(key, field) {
|
|
568
|
+
Cache.prototype.hget = async function (key, field) {
|
|
581
569
|
// 添加作用域前缀
|
|
582
570
|
const scopeKey = `${this.scope}:${key}`;
|
|
583
571
|
return await this._safeCall('hget', [scopeKey, field]);
|
|
@@ -588,7 +576,7 @@ Cache.prototype.hget = async function(key, field) {
|
|
|
588
576
|
* @param {String} key 键名
|
|
589
577
|
* @return {Promise<Object>} 所有字段和值
|
|
590
578
|
*/
|
|
591
|
-
Cache.prototype.hgetall = async function(key) {
|
|
579
|
+
Cache.prototype.hgetall = async function (key) {
|
|
592
580
|
// 添加作用域前缀
|
|
593
581
|
const scopeKey = `${this.scope}:${key}`;
|
|
594
582
|
return await this._safeCall('hgetall', [scopeKey]);
|
|
@@ -600,7 +588,7 @@ Cache.prototype.hgetall = async function(key) {
|
|
|
600
588
|
* @param {String} field 字段名
|
|
601
589
|
* @return {Promise<Number>} 删除的字段数量
|
|
602
590
|
*/
|
|
603
|
-
Cache.prototype.hdel = async function(key, field) {
|
|
591
|
+
Cache.prototype.hdel = async function (key, field) {
|
|
604
592
|
// 添加作用域前缀
|
|
605
593
|
const scopeKey = `${this.scope}:${key}`;
|
|
606
594
|
return await this._safeCall('hdel', [scopeKey, field]);
|
|
@@ -612,7 +600,7 @@ Cache.prototype.hdel = async function(key, field) {
|
|
|
612
600
|
* @param {Array} fieldValues 字段值对数组,格式: [field1, value1, field2, value2, ...]
|
|
613
601
|
* @return {Promise<Number>} 设置的字段数量
|
|
614
602
|
*/
|
|
615
|
-
Cache.prototype.hmset = async function(key, ...fieldValues) {
|
|
603
|
+
Cache.prototype.hmset = async function (key, ...fieldValues) {
|
|
616
604
|
// 添加作用域前缀
|
|
617
605
|
const scopeKey = `${this.scope}:${key}`;
|
|
618
606
|
return await this._safeCall('hmset', [scopeKey, ...fieldValues]);
|
|
@@ -624,7 +612,7 @@ Cache.prototype.hmset = async function(key, ...fieldValues) {
|
|
|
624
612
|
* @param {Array} fields 字段名数组
|
|
625
613
|
* @return {Promise<Array>} 字段值数组
|
|
626
614
|
*/
|
|
627
|
-
Cache.prototype.hmget = async function(key, ...fields) {
|
|
615
|
+
Cache.prototype.hmget = async function (key, ...fields) {
|
|
628
616
|
// 添加作用域前缀
|
|
629
617
|
const scopeKey = `${this.scope}:${key}`;
|
|
630
618
|
return await this._safeCall('hmget', [scopeKey, ...fields]);
|
|
@@ -635,7 +623,7 @@ Cache.prototype.hmget = async function(key, ...fields) {
|
|
|
635
623
|
* @param {String} key 键名
|
|
636
624
|
* @return {Promise<Number>} 字段数量
|
|
637
625
|
*/
|
|
638
|
-
Cache.prototype.hlen = async function(key) {
|
|
626
|
+
Cache.prototype.hlen = async function (key) {
|
|
639
627
|
// 添加作用域前缀
|
|
640
628
|
const scopeKey = `${this.scope}:${key}`;
|
|
641
629
|
return await this._safeCall('hlen', [scopeKey]);
|
|
@@ -646,7 +634,7 @@ Cache.prototype.hlen = async function(key) {
|
|
|
646
634
|
* @param {String} key 键名
|
|
647
635
|
* @return {Promise<Number>} 字符串长度
|
|
648
636
|
*/
|
|
649
|
-
Cache.prototype.strlen = async function(key) {
|
|
637
|
+
Cache.prototype.strlen = async function (key) {
|
|
650
638
|
// 添加作用域前缀
|
|
651
639
|
const scopeKey = `${this.scope}:${key}`;
|
|
652
640
|
return await this._safeCall('strlen', [scopeKey]);
|
|
@@ -658,7 +646,7 @@ Cache.prototype.strlen = async function(key) {
|
|
|
658
646
|
* @param {Array} members 成员数组
|
|
659
647
|
* @return {Promise<Number>} 新增的成员数量
|
|
660
648
|
*/
|
|
661
|
-
Cache.prototype.sadd = async function(key, ...members) {
|
|
649
|
+
Cache.prototype.sadd = async function (key, ...members) {
|
|
662
650
|
// 添加作用域前缀
|
|
663
651
|
const scopeKey = `${this.scope}:${key}`;
|
|
664
652
|
return await this._safeCall('sadd', [scopeKey, ...members]);
|
|
@@ -670,7 +658,7 @@ Cache.prototype.sadd = async function(key, ...members) {
|
|
|
670
658
|
* @param {Array} members 成员数组
|
|
671
659
|
* @return {Promise<Number>} 删除的成员数量
|
|
672
660
|
*/
|
|
673
|
-
Cache.prototype.srem = async function(key, ...members) {
|
|
661
|
+
Cache.prototype.srem = async function (key, ...members) {
|
|
674
662
|
// 添加作用域前缀
|
|
675
663
|
const scopeKey = `${this.scope}:${key}`;
|
|
676
664
|
return await this._safeCall('srem', [scopeKey, ...members]);
|
|
@@ -682,7 +670,7 @@ Cache.prototype.srem = async function(key, ...members) {
|
|
|
682
670
|
* @param {String} member 成员
|
|
683
671
|
* @return {Promise<Boolean>} 是否存在
|
|
684
672
|
*/
|
|
685
|
-
Cache.prototype.sismember = async function(key, member) {
|
|
673
|
+
Cache.prototype.sismember = async function (key, member) {
|
|
686
674
|
// 添加作用域前缀
|
|
687
675
|
const scopeKey = `${this.scope}:${key}`;
|
|
688
676
|
return await this._safeCall('sismember', [scopeKey, member]);
|
|
@@ -693,7 +681,7 @@ Cache.prototype.sismember = async function(key, member) {
|
|
|
693
681
|
* @param {String} key 键名
|
|
694
682
|
* @return {Promise<Array>} 成员数组
|
|
695
683
|
*/
|
|
696
|
-
Cache.prototype.smembers = async function(key) {
|
|
684
|
+
Cache.prototype.smembers = async function (key) {
|
|
697
685
|
// 添加作用域前缀
|
|
698
686
|
const scopeKey = `${this.scope}:${key}`;
|
|
699
687
|
return await this._safeCall('smembers', [scopeKey]);
|
|
@@ -705,7 +693,7 @@ Cache.prototype.smembers = async function(key) {
|
|
|
705
693
|
* @param {Number} milliseconds 过期时间(毫秒)
|
|
706
694
|
* @return {Promise<Boolean>} 是否设置成功
|
|
707
695
|
*/
|
|
708
|
-
Cache.prototype.pexpire = async function(key, milliseconds) {
|
|
696
|
+
Cache.prototype.pexpire = async function (key, milliseconds) {
|
|
709
697
|
// 添加作用域前缀
|
|
710
698
|
const scopeKey = `${this.scope}:${key}`;
|
|
711
699
|
return await this._safeCall('pexpire', [scopeKey, milliseconds]);
|
|
@@ -716,7 +704,7 @@ Cache.prototype.pexpire = async function(key, milliseconds) {
|
|
|
716
704
|
* @param {String} key 键名
|
|
717
705
|
* @return {Promise<Boolean>} 是否移除成功
|
|
718
706
|
*/
|
|
719
|
-
Cache.prototype.persist = async function(key) {
|
|
707
|
+
Cache.prototype.persist = async function (key) {
|
|
720
708
|
// 添加作用域前缀
|
|
721
709
|
const scopeKey = `${this.scope}:${key}`;
|
|
722
710
|
return await this._safeCall('persist', [scopeKey]);
|
|
@@ -728,7 +716,7 @@ Cache.prototype.persist = async function(key) {
|
|
|
728
716
|
* @param {String} newKey 新键名
|
|
729
717
|
* @return {Promise<Boolean>} 是否重命名成功
|
|
730
718
|
*/
|
|
731
|
-
Cache.prototype.rename = async function(oldKey, newKey) {
|
|
719
|
+
Cache.prototype.rename = async function (oldKey, newKey) {
|
|
732
720
|
// 添加作用域前缀
|
|
733
721
|
const scopeOldKey = `${this.scope}:${oldKey}`;
|
|
734
722
|
const scopeNewKey = `${this.scope}:${newKey}`;
|
|
@@ -740,7 +728,7 @@ Cache.prototype.rename = async function(oldKey, newKey) {
|
|
|
740
728
|
* @param {String} key 键名
|
|
741
729
|
* @return {Promise<String>} 类型(none|string|list|set|zset|hash)
|
|
742
730
|
*/
|
|
743
|
-
Cache.prototype.type = async function(key) {
|
|
731
|
+
Cache.prototype.type = async function (key) {
|
|
744
732
|
// 添加作用域前缀
|
|
745
733
|
const scopeKey = `${this.scope}:${key}`;
|
|
746
734
|
return await this._safeCall('type', [scopeKey]);
|
|
@@ -753,7 +741,7 @@ Cache.prototype.type = async function(key) {
|
|
|
753
741
|
* @param {Number} expire 过期时间(秒)
|
|
754
742
|
* @return {Promise<Boolean>} 是否成功
|
|
755
743
|
*/
|
|
756
|
-
Cache.prototype.setForList = async function(key, value, expire) {
|
|
744
|
+
Cache.prototype.setForList = async function (key, value, expire) {
|
|
757
745
|
// 添加作用域前缀
|
|
758
746
|
const scopeKey = `${this.scope}:${key}`;
|
|
759
747
|
return await this._safeCall('setForList', [scopeKey, value, expire]);
|
|
@@ -765,7 +753,7 @@ Cache.prototype.setForList = async function(key, value, expire) {
|
|
|
765
753
|
* @param {*} value 要添加的元素
|
|
766
754
|
* @return {Promise<Boolean>} 是否成功
|
|
767
755
|
*/
|
|
768
|
-
Cache.prototype.addForList = async function(key, value) {
|
|
756
|
+
Cache.prototype.addForList = async function (key, value) {
|
|
769
757
|
// 添加作用域前缀
|
|
770
758
|
const scopeKey = `${this.scope}:${key}`;
|
|
771
759
|
return await this._safeCall('addForList', [scopeKey, value]);
|
|
@@ -777,7 +765,7 @@ Cache.prototype.addForList = async function(key, value) {
|
|
|
777
765
|
* @param {*} value 要检查的元素
|
|
778
766
|
* @return {Promise<Boolean>} 是否包含
|
|
779
767
|
*/
|
|
780
|
-
Cache.prototype.hasForList = async function(key, value) {
|
|
768
|
+
Cache.prototype.hasForList = async function (key, value) {
|
|
781
769
|
// 添加作用域前缀
|
|
782
770
|
const scopeKey = `${this.scope}:${key}`;
|
|
783
771
|
return await this._safeCall('hasForList', [scopeKey, value]);
|
|
@@ -790,7 +778,7 @@ Cache.prototype.hasForList = async function(key, value) {
|
|
|
790
778
|
* @param {Number} stop 结束索引,默认-1(表示到末尾)
|
|
791
779
|
* @return {Promise<Array>} 元素数组
|
|
792
780
|
*/
|
|
793
|
-
Cache.prototype.getForList = async function(key, start = 0, stop = -1) {
|
|
781
|
+
Cache.prototype.getForList = async function (key, start = 0, stop = -1) {
|
|
794
782
|
// 添加作用域前缀
|
|
795
783
|
const scopeKey = `${this.scope}:${key}`;
|
|
796
784
|
return await this._safeCall('getForList', [scopeKey, start, stop]);
|
|
@@ -801,7 +789,7 @@ Cache.prototype.getForList = async function(key, start = 0, stop = -1) {
|
|
|
801
789
|
* @param {String} key 键名
|
|
802
790
|
* @return {Promise<Boolean>} 是否成功
|
|
803
791
|
*/
|
|
804
|
-
Cache.prototype.clearForList = async function(key) {
|
|
792
|
+
Cache.prototype.clearForList = async function (key) {
|
|
805
793
|
// 添加作用域前缀
|
|
806
794
|
const scopeKey = `${this.scope}:${key}`;
|
|
807
795
|
return await this._safeCall('clearForList', [scopeKey]);
|
|
@@ -812,7 +800,7 @@ Cache.prototype.clearForList = async function(key) {
|
|
|
812
800
|
* @param {String} key 匹配模式
|
|
813
801
|
* @return {Promise<Boolean>} 是否清理成功
|
|
814
802
|
*/
|
|
815
|
-
Cache.prototype.clear = async function(key) {
|
|
803
|
+
Cache.prototype.clear = async function (key) {
|
|
816
804
|
// 添加作用域前缀
|
|
817
805
|
const scopeKey = key ? `${this.scope}:${key}` : `${this.scope}:*`;
|
|
818
806
|
return await this._safeCall('clear', [scopeKey]);
|
|
@@ -822,7 +810,7 @@ Cache.prototype.clear = async function(key) {
|
|
|
822
810
|
* @description 清理过期缓存
|
|
823
811
|
* @return {Promise<Boolean>} 是否清理成功
|
|
824
812
|
*/
|
|
825
|
-
Cache.prototype.clearExpire = async function() {
|
|
813
|
+
Cache.prototype.clearExpire = async function () {
|
|
826
814
|
return await this._safeCall('clearExpire', []);
|
|
827
815
|
};
|
|
828
816
|
|
|
@@ -830,7 +818,7 @@ Cache.prototype.clearExpire = async function() {
|
|
|
830
818
|
* @description 关闭数据库连接
|
|
831
819
|
* @return {Promise<Boolean>} 是否关闭成功
|
|
832
820
|
*/
|
|
833
|
-
Cache.prototype.dispose = async function() {
|
|
821
|
+
Cache.prototype.dispose = async function () {
|
|
834
822
|
if (this.db && typeof this.db.dispose === 'function') {
|
|
835
823
|
return await this.db.dispose();
|
|
836
824
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_cache",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
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.
|
|
34
|
-
"mm_mongodb": "^1.5.
|
|
35
|
-
"mm_redis": "^2.0.
|
|
33
|
+
"mm_cachebase": "^1.9.1",
|
|
34
|
+
"mm_mongodb": "^1.5.3",
|
|
35
|
+
"mm_redis": "^2.0.9"
|
|
36
36
|
}
|
|
37
37
|
}
|