mm_cachebase 1.8.8 → 1.9.0
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 +152 -89
- package/package.json +2 -2
- package/index_old.js +0 -2521
- package/simple_test.js +0 -21
- package/simple_test_direct.js +0 -27
- package/test_cache/test_file_operations.json +0 -1
- package/test_cache/undefined.cache.json +0 -12
- package/test_new_methods.js +0 -170
package/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* CacheBase 是一个通用缓存基类,支持内存缓存和持久化缓存
|
|
3
3
|
* @class CacheBase
|
|
4
|
-
* @extends
|
|
4
|
+
* @extends Base
|
|
5
5
|
*/
|
|
6
6
|
const {
|
|
7
|
-
|
|
8
|
-
} = require('
|
|
7
|
+
Base
|
|
8
|
+
} = require('mm_expand');
|
|
9
9
|
|
|
10
|
-
class CacheBase extends
|
|
10
|
+
class CacheBase extends Base {
|
|
11
11
|
/**
|
|
12
12
|
* 默认配置静态变量
|
|
13
13
|
*/
|
|
@@ -53,6 +53,24 @@ class CacheBase extends BaseService {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
* 初始化服务
|
|
58
|
+
* @returns {Promise<void>}
|
|
59
|
+
*/
|
|
60
|
+
CacheBase.prototype.init = async function() {
|
|
61
|
+
if (this.is_inited || this.is_destroyed) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
await this._initService();
|
|
67
|
+
this.is_inited = true;
|
|
68
|
+
this.logger('info', 'CacheBase service initialized successfully');
|
|
69
|
+
} catch (error) {
|
|
70
|
+
this.logger('error', 'Failed to initialize CacheBase service:', error);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
56
74
|
/**
|
|
57
75
|
* 初始化服务
|
|
58
76
|
* @private
|
|
@@ -87,7 +105,7 @@ CacheBase.prototype._initService = async function() {
|
|
|
87
105
|
// 设置初始化完成状态
|
|
88
106
|
this.is_inited = true;
|
|
89
107
|
} catch (error) {
|
|
90
|
-
|
|
108
|
+
this.logger('error', 'Failed to initialize service:', error);
|
|
91
109
|
}
|
|
92
110
|
};
|
|
93
111
|
|
|
@@ -142,8 +160,7 @@ CacheBase.prototype._loadData = function() {
|
|
|
142
160
|
$.log.info(`[CacheBase] [_loadData] 持久化未启用或文件路径未设置`);
|
|
143
161
|
}
|
|
144
162
|
} catch (error) {
|
|
145
|
-
|
|
146
|
-
this.emit('error', new Error('Failed to load data: ' + error.message));
|
|
163
|
+
this.logger('error', '加载数据失败:', error);
|
|
147
164
|
}
|
|
148
165
|
};
|
|
149
166
|
|
|
@@ -153,7 +170,7 @@ CacheBase.prototype._loadData = function() {
|
|
|
153
170
|
CacheBase.prototype.save = function() {
|
|
154
171
|
// 检查服务状态
|
|
155
172
|
if (!this.is_inited || this.is_destroyed) {
|
|
156
|
-
this.
|
|
173
|
+
this.logger('error', 'Service is not initialized or has been destroyed');
|
|
157
174
|
return;
|
|
158
175
|
}
|
|
159
176
|
|
|
@@ -162,7 +179,7 @@ CacheBase.prototype.save = function() {
|
|
|
162
179
|
this._saveData();
|
|
163
180
|
this.emit('save_after');
|
|
164
181
|
} catch (error) {
|
|
165
|
-
this.
|
|
182
|
+
this.logger('error', 'Failed to save data:', error);
|
|
166
183
|
}
|
|
167
184
|
};
|
|
168
185
|
|
|
@@ -190,7 +207,7 @@ CacheBase.prototype._saveData = function() {
|
|
|
190
207
|
file_path.saveJson(data_to_save, null, 2);
|
|
191
208
|
}
|
|
192
209
|
} catch (error) {
|
|
193
|
-
|
|
210
|
+
this.logger('error', 'Failed to save data:', error);
|
|
194
211
|
}
|
|
195
212
|
};
|
|
196
213
|
|
|
@@ -233,7 +250,7 @@ CacheBase.prototype.destroy = async function() {
|
|
|
233
250
|
// 触发自定义销毁事件
|
|
234
251
|
this.emit('destroy_after');
|
|
235
252
|
} catch (error) {
|
|
236
|
-
this.
|
|
253
|
+
this.logger('error', 'Failed to destroy service:', error);
|
|
237
254
|
}
|
|
238
255
|
};
|
|
239
256
|
|
|
@@ -247,15 +264,16 @@ CacheBase.prototype.destroy = async function() {
|
|
|
247
264
|
CacheBase.prototype.set = function(key, value, ttl) {
|
|
248
265
|
// 检查服务状态
|
|
249
266
|
if (!this.is_inited || this.is_destroyed) {
|
|
250
|
-
this.
|
|
267
|
+
this.logger('error', 'Service is not initialized or has been destroyed');
|
|
251
268
|
return false;
|
|
252
269
|
}
|
|
253
270
|
|
|
271
|
+
// 参数验证
|
|
272
|
+
if (!key || typeof key !== 'string') {
|
|
273
|
+
throw new TypeError('key必须是非空字符串');
|
|
274
|
+
}
|
|
275
|
+
|
|
254
276
|
try {
|
|
255
|
-
// 参数验证
|
|
256
|
-
if (!key || typeof key !== 'string') {
|
|
257
|
-
return false;
|
|
258
|
-
}
|
|
259
277
|
|
|
260
278
|
// 触发操作前事件
|
|
261
279
|
const event_data = {
|
|
@@ -294,7 +312,7 @@ CacheBase.prototype.set = function(key, value, ttl) {
|
|
|
294
312
|
|
|
295
313
|
return true;
|
|
296
314
|
} catch (error) {
|
|
297
|
-
this.
|
|
315
|
+
this.logger('error', `Failed to set key ${key}:`, error);
|
|
298
316
|
return false;
|
|
299
317
|
}
|
|
300
318
|
};
|
|
@@ -307,7 +325,7 @@ CacheBase.prototype.set = function(key, value, ttl) {
|
|
|
307
325
|
CacheBase.prototype.get = function(key) {
|
|
308
326
|
// 检查服务状态
|
|
309
327
|
if (!this.is_inited || this.is_destroyed) {
|
|
310
|
-
this.
|
|
328
|
+
this.logger('error', 'Service is not initialized or has been destroyed');
|
|
311
329
|
return null;
|
|
312
330
|
}
|
|
313
331
|
|
|
@@ -320,23 +338,40 @@ CacheBase.prototype.get = function(key) {
|
|
|
320
338
|
|
|
321
339
|
return this._data[key];
|
|
322
340
|
} catch (error) {
|
|
323
|
-
this.
|
|
341
|
+
this.logger('error', `Failed to get key ${key}:`, error);
|
|
324
342
|
return null;
|
|
325
343
|
}
|
|
326
344
|
};
|
|
327
345
|
|
|
328
346
|
/**
|
|
329
347
|
* 删除键
|
|
330
|
-
* @param {string} key -
|
|
348
|
+
* @param {string|Array<string>} key - 键名或键名数组
|
|
331
349
|
* @returns {boolean} 是否成功删除
|
|
332
350
|
*/
|
|
333
351
|
CacheBase.prototype.del = function(key) {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
352
|
+
// 参数验证
|
|
353
|
+
if (!key) {
|
|
354
|
+
throw new TypeError('key必须是非空字符串或非空字符串数组');
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// 如果是单个键,验证是否为非空字符串
|
|
358
|
+
if (typeof key === 'string') {
|
|
359
|
+
if (!key) {
|
|
360
|
+
throw new TypeError('key必须是非空字符串');
|
|
338
361
|
}
|
|
362
|
+
}
|
|
363
|
+
// 如果是数组,验证每个元素是否为非空字符串
|
|
364
|
+
else if (Array.isArray(key)) {
|
|
365
|
+
for (const k of key) {
|
|
366
|
+
if (!k || typeof k !== 'string') {
|
|
367
|
+
throw new TypeError('key必须是非空字符串或非空字符串数组');
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
} else {
|
|
371
|
+
throw new TypeError('key必须是非空字符串或非空字符串数组');
|
|
372
|
+
}
|
|
339
373
|
|
|
374
|
+
try {
|
|
340
375
|
// 触发操作前事件
|
|
341
376
|
const event_data = {
|
|
342
377
|
key,
|
|
@@ -348,30 +383,56 @@ CacheBase.prototype.del = function(key) {
|
|
|
348
383
|
return false;
|
|
349
384
|
}
|
|
350
385
|
|
|
351
|
-
//
|
|
352
|
-
|
|
353
|
-
return false;
|
|
354
|
-
}
|
|
386
|
+
// 使用可能被修改的值
|
|
387
|
+
const finalKey = event_data.key;
|
|
355
388
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
389
|
+
let success = true;
|
|
390
|
+
let deleted = [];
|
|
391
|
+
|
|
392
|
+
if (Array.isArray(finalKey)) {
|
|
393
|
+
// 批量删除
|
|
394
|
+
for (const k of finalKey) {
|
|
395
|
+
// 检查键是否存在
|
|
396
|
+
if (k in this._data) {
|
|
397
|
+
// 删除键
|
|
398
|
+
delete this._data[k];
|
|
399
|
+
delete this._types[k];
|
|
400
|
+
delete this._expires[k];
|
|
401
|
+
deleted.push(k);
|
|
402
|
+
} else {
|
|
403
|
+
success = false;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
// 单个删除
|
|
408
|
+
// 检查键是否存在
|
|
409
|
+
if (!(finalKey in this._data)) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// 删除键
|
|
414
|
+
delete this._data[finalKey];
|
|
415
|
+
delete this._types[finalKey];
|
|
416
|
+
delete this._expires[finalKey];
|
|
417
|
+
deleted.push(finalKey);
|
|
418
|
+
}
|
|
360
419
|
|
|
361
420
|
// 触发操作后事件
|
|
362
421
|
this.emit('del_after', {
|
|
363
|
-
key
|
|
422
|
+
key: finalKey,
|
|
423
|
+
success,
|
|
424
|
+
deleted
|
|
364
425
|
});
|
|
365
426
|
|
|
366
|
-
return
|
|
427
|
+
return success;
|
|
367
428
|
} catch (error) {
|
|
368
|
-
this.
|
|
429
|
+
this.logger('error', 'Failed to del:', error);
|
|
369
430
|
return false;
|
|
370
431
|
}
|
|
371
432
|
};
|
|
372
433
|
|
|
373
434
|
/**
|
|
374
|
-
*
|
|
435
|
+
* 批量删除键(兼容方法,推荐使用del方法)
|
|
375
436
|
* @param {Array} keys - 键名数组
|
|
376
437
|
* @returns {number} 删除的键数量
|
|
377
438
|
*/
|
|
@@ -390,7 +451,7 @@ CacheBase.prototype.delBatch = function(keys) {
|
|
|
390
451
|
|
|
391
452
|
return deletedCount;
|
|
392
453
|
} catch (error) {
|
|
393
|
-
this.
|
|
454
|
+
this.logger('error', 'Failed to delBatch:', error);
|
|
394
455
|
return 0;
|
|
395
456
|
}
|
|
396
457
|
};
|
|
@@ -409,7 +470,7 @@ CacheBase.prototype.exists = function(key) {
|
|
|
409
470
|
|
|
410
471
|
return key in this._data;
|
|
411
472
|
} catch (error) {
|
|
412
|
-
this.
|
|
473
|
+
this.logger('error', `Failed to check key ${key}:`, error);
|
|
413
474
|
return false;
|
|
414
475
|
}
|
|
415
476
|
};
|
|
@@ -435,7 +496,7 @@ CacheBase.prototype.clearExpire = function() {
|
|
|
435
496
|
}
|
|
436
497
|
}
|
|
437
498
|
} catch (error) {
|
|
438
|
-
this.
|
|
499
|
+
this.logger('error', 'Failed to clear expired data:', error);
|
|
439
500
|
}
|
|
440
501
|
};
|
|
441
502
|
|
|
@@ -494,7 +555,7 @@ CacheBase.prototype.expire = function(key, seconds) {
|
|
|
494
555
|
return false;
|
|
495
556
|
}
|
|
496
557
|
|
|
497
|
-
if (
|
|
558
|
+
if (typeof seconds !== 'number' || seconds <= 0) {
|
|
498
559
|
return false;
|
|
499
560
|
}
|
|
500
561
|
|
|
@@ -527,7 +588,7 @@ CacheBase.prototype.expire = function(key, seconds) {
|
|
|
527
588
|
|
|
528
589
|
return true;
|
|
529
590
|
} catch (error) {
|
|
530
|
-
this.
|
|
591
|
+
this.logger('error', `Failed to set expire for key ${key}:`, error);
|
|
531
592
|
return false;
|
|
532
593
|
}
|
|
533
594
|
};
|
|
@@ -582,9 +643,9 @@ CacheBase.prototype.hset = function(key, field, value) {
|
|
|
582
643
|
success: true
|
|
583
644
|
});
|
|
584
645
|
|
|
585
|
-
return is_new_field ? 1 : 0;
|
|
646
|
+
return is_new_field ? 1 : 0; // 返回1表示新增,0表示更新
|
|
586
647
|
} catch (error) {
|
|
587
|
-
this.
|
|
648
|
+
this.logger('error', `Failed to hset for key ${key}:`, error);
|
|
588
649
|
return 0;
|
|
589
650
|
}
|
|
590
651
|
};
|
|
@@ -599,24 +660,23 @@ CacheBase.prototype.hget = function(key, field) {
|
|
|
599
660
|
try {
|
|
600
661
|
// 参数验证
|
|
601
662
|
if (!key || typeof key !== 'string' || !field || typeof field !== 'string') {
|
|
602
|
-
return
|
|
663
|
+
return;
|
|
603
664
|
}
|
|
604
665
|
|
|
605
666
|
// 检查是否过期
|
|
606
667
|
if (this._isExpired(key)) {
|
|
607
668
|
this.del(key);
|
|
608
|
-
return
|
|
669
|
+
return;
|
|
609
670
|
}
|
|
610
671
|
|
|
611
672
|
// 检查键是否存在且类型正确
|
|
612
673
|
if (!(key in this._data) || this._types[key] !== 'hash') {
|
|
613
|
-
return
|
|
674
|
+
return;
|
|
614
675
|
}
|
|
615
676
|
|
|
616
677
|
return this._data[key][field];
|
|
617
678
|
} catch (error) {
|
|
618
|
-
this.
|
|
619
|
-
return undefined;
|
|
679
|
+
this.logger('error', `Failed to hget for key ${key}:`, error);
|
|
620
680
|
}
|
|
621
681
|
};
|
|
622
682
|
|
|
@@ -656,7 +716,7 @@ CacheBase.prototype.hgetall = function(key) {
|
|
|
656
716
|
...this._data[key]
|
|
657
717
|
};
|
|
658
718
|
} catch (error) {
|
|
659
|
-
this.
|
|
719
|
+
this.logger('error', `Failed to hgetall for key ${key}:`, error);
|
|
660
720
|
return {};
|
|
661
721
|
}
|
|
662
722
|
};
|
|
@@ -665,7 +725,7 @@ CacheBase.prototype.hgetall = function(key) {
|
|
|
665
725
|
* 删除哈希表字段
|
|
666
726
|
* @param {string} key - 哈希表键名
|
|
667
727
|
* @param {string|Array<string>} field - 字段名或字段名数组
|
|
668
|
-
* @returns {boolean}
|
|
728
|
+
* @returns {boolean} 删除成功返回true,失败返回false
|
|
669
729
|
*/
|
|
670
730
|
CacheBase.prototype.hdel = function(key, field) {
|
|
671
731
|
try {
|
|
@@ -709,18 +769,18 @@ CacheBase.prototype.hdel = function(key, field) {
|
|
|
709
769
|
|
|
710
770
|
// 处理字段参数,支持单个字段或字段数组
|
|
711
771
|
const fields = Array.isArray(field) ? field : [field];
|
|
712
|
-
let
|
|
772
|
+
let deletedCount = 0;
|
|
713
773
|
|
|
714
774
|
// 遍历所有字段进行删除
|
|
715
775
|
for (const f of fields) {
|
|
716
776
|
if (typeof f === 'string' && f && f in this._data[key]) {
|
|
717
777
|
delete this._data[key][f];
|
|
718
|
-
|
|
778
|
+
deletedCount++;
|
|
719
779
|
}
|
|
720
780
|
}
|
|
721
781
|
|
|
722
782
|
// 如果哈希表为空,删除整个键
|
|
723
|
-
if (
|
|
783
|
+
if (deletedCount > 0 && Object.keys(this._data[key]).length === 0) {
|
|
724
784
|
delete this._data[key];
|
|
725
785
|
delete this._types[key];
|
|
726
786
|
delete this._expires[key];
|
|
@@ -730,17 +790,17 @@ CacheBase.prototype.hdel = function(key, field) {
|
|
|
730
790
|
this.emit('hdel_after', {
|
|
731
791
|
key,
|
|
732
792
|
field,
|
|
733
|
-
success:
|
|
793
|
+
success: deletedCount > 0
|
|
734
794
|
});
|
|
735
795
|
|
|
736
|
-
return
|
|
796
|
+
return deletedCount > 0;
|
|
737
797
|
} catch (error) {
|
|
738
798
|
this.emit('hdel_error', {
|
|
739
799
|
key,
|
|
740
800
|
field,
|
|
741
801
|
error
|
|
742
802
|
});
|
|
743
|
-
this.
|
|
803
|
+
this.logger('error', 'Failed to hdel:', error);
|
|
744
804
|
return false;
|
|
745
805
|
}
|
|
746
806
|
};
|
|
@@ -959,12 +1019,15 @@ CacheBase.prototype.decr = function(key, seconds = 0) {
|
|
|
959
1019
|
* @returns {Promise<number>} - 返回增加后的值
|
|
960
1020
|
*/
|
|
961
1021
|
CacheBase.prototype.addInt = function(key, value, seconds = 0) {
|
|
1022
|
+
// 参数验证
|
|
1023
|
+
if (!key || typeof key !== 'string') {
|
|
1024
|
+
throw new TypeError('key必须是非空字符串');
|
|
1025
|
+
}
|
|
1026
|
+
if (typeof value !== 'number') {
|
|
1027
|
+
throw new TypeError('value必须是数字');
|
|
1028
|
+
}
|
|
1029
|
+
|
|
962
1030
|
try {
|
|
963
|
-
// 参数验证
|
|
964
|
-
if (!key || typeof key !== 'string' || typeof value !== 'number') {
|
|
965
|
-
$.log.error('[cachebase] addInt参数错误: key必须是非空字符串,value必须是数字');
|
|
966
|
-
return 0;
|
|
967
|
-
}
|
|
968
1031
|
|
|
969
1032
|
this.emit('addInt_before', {
|
|
970
1033
|
key,
|
|
@@ -1044,7 +1107,7 @@ CacheBase.prototype.append = function(key, append) {
|
|
|
1044
1107
|
|
|
1045
1108
|
return current.length;
|
|
1046
1109
|
} catch (error) {
|
|
1047
|
-
this.
|
|
1110
|
+
this.logger('error', `Failed to append for key ${key}:`, error);
|
|
1048
1111
|
return null;
|
|
1049
1112
|
}
|
|
1050
1113
|
};
|
|
@@ -1079,7 +1142,7 @@ CacheBase.prototype.strlen = function(key) {
|
|
|
1079
1142
|
|
|
1080
1143
|
return this._data[key].length;
|
|
1081
1144
|
} catch (error) {
|
|
1082
|
-
this.
|
|
1145
|
+
this.logger('error', `Failed to strlen for key ${key}:`, error);
|
|
1083
1146
|
return 0;
|
|
1084
1147
|
}
|
|
1085
1148
|
};
|
|
@@ -1112,7 +1175,7 @@ CacheBase.prototype.flushdb = function() {
|
|
|
1112
1175
|
|
|
1113
1176
|
return true;
|
|
1114
1177
|
} catch (error) {
|
|
1115
|
-
this.
|
|
1178
|
+
this.logger('error', 'Failed to flushdb:', error);
|
|
1116
1179
|
return false;
|
|
1117
1180
|
}
|
|
1118
1181
|
};
|
|
@@ -1135,7 +1198,7 @@ CacheBase.prototype.keys = function(pattern) {
|
|
|
1135
1198
|
}
|
|
1136
1199
|
return result;
|
|
1137
1200
|
} catch (error) {
|
|
1138
|
-
this.
|
|
1201
|
+
this.logger('error', 'Failed to get keys:', error);
|
|
1139
1202
|
return [];
|
|
1140
1203
|
}
|
|
1141
1204
|
};
|
|
@@ -1176,7 +1239,7 @@ CacheBase.prototype.dbsize = function() {
|
|
|
1176
1239
|
}
|
|
1177
1240
|
return count;
|
|
1178
1241
|
} catch (error) {
|
|
1179
|
-
this.
|
|
1242
|
+
this.logger('error', 'Failed to get dbsize:', error);
|
|
1180
1243
|
return 0;
|
|
1181
1244
|
}
|
|
1182
1245
|
};
|
|
@@ -1269,7 +1332,7 @@ CacheBase.prototype.getset = function(key, value, expire) {
|
|
|
1269
1332
|
|
|
1270
1333
|
return oldValue;
|
|
1271
1334
|
} catch (error) {
|
|
1272
|
-
this.
|
|
1335
|
+
this.logger('error', 'Failed to getset:', error);
|
|
1273
1336
|
return null;
|
|
1274
1337
|
}
|
|
1275
1338
|
};
|
|
@@ -1563,8 +1626,8 @@ CacheBase.prototype.mset = function(...args) {
|
|
|
1563
1626
|
args,
|
|
1564
1627
|
error
|
|
1565
1628
|
});
|
|
1566
|
-
this.
|
|
1567
|
-
|
|
1629
|
+
this.logger('error', 'Failed to mset:', error);
|
|
1630
|
+
return false;
|
|
1568
1631
|
}
|
|
1569
1632
|
};
|
|
1570
1633
|
|
|
@@ -1582,7 +1645,7 @@ CacheBase.prototype.ttl = function(key, seconds) {
|
|
|
1582
1645
|
}
|
|
1583
1646
|
|
|
1584
1647
|
// 判断是否为设置过期时间模式
|
|
1585
|
-
if (arguments.length > 1) {
|
|
1648
|
+
if (arguments.length > 1 && seconds !== undefined) {
|
|
1586
1649
|
// 设置过期时间模式
|
|
1587
1650
|
if (typeof seconds !== 'number' || seconds < 0) {
|
|
1588
1651
|
return false;
|
|
@@ -1633,12 +1696,12 @@ CacheBase.prototype.ttl = function(key, seconds) {
|
|
|
1633
1696
|
return Math.max(0, Math.ceil(remaining / 1000));
|
|
1634
1697
|
}
|
|
1635
1698
|
} catch (error) {
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1699
|
+
const errorMsg = arguments.length > 1 && seconds !== undefined
|
|
1700
|
+
? `Failed to set ttl for key ${key}: ${error.message}`
|
|
1701
|
+
: `Failed to get ttl for key ${key}: ${error.message}`;
|
|
1702
|
+
this.logger('error', errorMsg, error);
|
|
1703
|
+
return arguments.length > 1 && seconds !== undefined ? false : -2;
|
|
1704
|
+
}
|
|
1642
1705
|
};
|
|
1643
1706
|
|
|
1644
1707
|
/**
|
|
@@ -1688,7 +1751,7 @@ CacheBase.prototype.persist = function(key) {
|
|
|
1688
1751
|
|
|
1689
1752
|
return false; // 键没有设置过期时间
|
|
1690
1753
|
} catch (error) {
|
|
1691
|
-
this.
|
|
1754
|
+
this.logger('error', `Failed to persist key ${key}:`, error);
|
|
1692
1755
|
return false;
|
|
1693
1756
|
}
|
|
1694
1757
|
};
|
|
@@ -1717,7 +1780,7 @@ CacheBase.prototype.mget = function(keys) {
|
|
|
1717
1780
|
|
|
1718
1781
|
return results;
|
|
1719
1782
|
} catch (error) {
|
|
1720
|
-
this.
|
|
1783
|
+
this.logger('error', 'Failed to mget:', error);
|
|
1721
1784
|
return [];
|
|
1722
1785
|
}
|
|
1723
1786
|
};
|
|
@@ -1746,7 +1809,7 @@ CacheBase.prototype.dispose = function() {
|
|
|
1746
1809
|
|
|
1747
1810
|
return true;
|
|
1748
1811
|
} catch (error) {
|
|
1749
|
-
this.
|
|
1812
|
+
this.logger('error', 'Failed to dispose:', error);
|
|
1750
1813
|
return false;
|
|
1751
1814
|
}
|
|
1752
1815
|
};
|
|
@@ -1794,7 +1857,7 @@ CacheBase.prototype.setForList = async function(key, value, maxAge = 0) {
|
|
|
1794
1857
|
try {
|
|
1795
1858
|
// 参数验证
|
|
1796
1859
|
if (!key || typeof key !== 'string') {
|
|
1797
|
-
|
|
1860
|
+
return 0;
|
|
1798
1861
|
}
|
|
1799
1862
|
|
|
1800
1863
|
// 确保value是数组
|
|
@@ -1859,7 +1922,7 @@ CacheBase.prototype.setForList = async function(key, value, maxAge = 0) {
|
|
|
1859
1922
|
error
|
|
1860
1923
|
});
|
|
1861
1924
|
$.log.error('[CacheBase] setForList操作失败:', error);
|
|
1862
|
-
|
|
1925
|
+
return 0;
|
|
1863
1926
|
}
|
|
1864
1927
|
};
|
|
1865
1928
|
|
|
@@ -1874,7 +1937,7 @@ CacheBase.prototype.addForList = async function(key, value, maxAge = 0) {
|
|
|
1874
1937
|
try {
|
|
1875
1938
|
// 参数验证
|
|
1876
1939
|
if (!key || typeof key !== 'string') {
|
|
1877
|
-
|
|
1940
|
+
return 0;
|
|
1878
1941
|
}
|
|
1879
1942
|
|
|
1880
1943
|
// 确保value是字符串
|
|
@@ -1936,7 +1999,7 @@ CacheBase.prototype.addForList = async function(key, value, maxAge = 0) {
|
|
|
1936
1999
|
error
|
|
1937
2000
|
});
|
|
1938
2001
|
$.log.error('[CacheBase] addForList操作失败:', error);
|
|
1939
|
-
|
|
2002
|
+
return 0;
|
|
1940
2003
|
}
|
|
1941
2004
|
};
|
|
1942
2005
|
|
|
@@ -2000,7 +2063,7 @@ CacheBase.prototype.hasForList = async function(key, value) {
|
|
|
2000
2063
|
error
|
|
2001
2064
|
});
|
|
2002
2065
|
$.log.error('[CacheBase] hasForList操作失败:', error);
|
|
2003
|
-
|
|
2066
|
+
return false;
|
|
2004
2067
|
}
|
|
2005
2068
|
};
|
|
2006
2069
|
|
|
@@ -2073,7 +2136,7 @@ CacheBase.prototype.getForList = async function(key, start = 0, stop = -1) {
|
|
|
2073
2136
|
error
|
|
2074
2137
|
});
|
|
2075
2138
|
$.log.error('[CacheBase] getForList操作失败:', error);
|
|
2076
|
-
|
|
2139
|
+
return [];
|
|
2077
2140
|
}
|
|
2078
2141
|
};
|
|
2079
2142
|
|
|
@@ -2146,7 +2209,7 @@ CacheBase.prototype.clearForList = async function(key) {
|
|
|
2146
2209
|
error
|
|
2147
2210
|
});
|
|
2148
2211
|
$.log.error('[CacheBase] clearForList操作失败:', error);
|
|
2149
|
-
|
|
2212
|
+
return false;
|
|
2150
2213
|
}
|
|
2151
2214
|
};
|
|
2152
2215
|
|
|
@@ -2209,7 +2272,7 @@ CacheBase.prototype.hmset = function(key, obj) {
|
|
|
2209
2272
|
obj,
|
|
2210
2273
|
error
|
|
2211
2274
|
});
|
|
2212
|
-
this.
|
|
2275
|
+
this.logger('error', 'Failed to hmset:', error);
|
|
2213
2276
|
resolve(false);
|
|
2214
2277
|
}
|
|
2215
2278
|
});
|
|
@@ -2256,7 +2319,7 @@ CacheBase.prototype.hmget = function(key, fields) {
|
|
|
2256
2319
|
fields,
|
|
2257
2320
|
error
|
|
2258
2321
|
});
|
|
2259
|
-
this.
|
|
2322
|
+
this.logger('error', 'Failed to hmget:', error);
|
|
2260
2323
|
resolve([]);
|
|
2261
2324
|
}
|
|
2262
2325
|
});
|
|
@@ -2295,7 +2358,7 @@ CacheBase.prototype.hlen = function(key) {
|
|
|
2295
2358
|
key,
|
|
2296
2359
|
error
|
|
2297
2360
|
});
|
|
2298
|
-
this.
|
|
2361
|
+
this.logger('error', 'Failed to hlen:', error);
|
|
2299
2362
|
resolve(0);
|
|
2300
2363
|
}
|
|
2301
2364
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_cachebase",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "一个轻量级的本地缓存类库,提供与Redis兼容的API接口,支持过期时间设置、哈希表操作、数值操作、字符串操作等功能,以及完整的事件通知系统和多文件持久化功能。",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
},
|
|
40
40
|
"homepage": "https://gitee.com/qiuwenwu91/mm_cachebase#readme",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"
|
|
42
|
+
"mm_expand": "^1.9.7"
|
|
43
43
|
}
|
|
44
44
|
}
|