mm_redis 1.4.2 → 1.5.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.
Files changed (6) hide show
  1. package/README.md +298 -0
  2. package/index.js +2021 -382
  3. package/package.json +33 -33
  4. package/test.js +83 -87
  5. package/index_new.js +0 -585
  6. package/test2.js +0 -22
package/index_new.js DELETED
@@ -1,585 +0,0 @@
1
- /**
2
- * @fileOverview redis缓存帮助类函数
3
- * @author <a href="http://qww.elins.cn">邱文武</a>
4
- * @version 1.2
5
- * redis: ^4.7.0
6
- */
7
- require('mm_expand');
8
- const {
9
- createClient
10
- } = require('redis');
11
-
12
- /* 创建Redis帮助类函数 */
13
- /// 构造函数
14
- class Redis {
15
- /**
16
- * @description 创建Redis帮助类函数 (构造函数)
17
- * @param {String} scope 作用域
18
- * @param {String} dir 当前路径
19
- * @constructor
20
- */
21
- constructor(scope, dir) {
22
- // 作用域
23
- this.scope;
24
- if (scope) {
25
- this.scope = scope;
26
- } else {
27
- this.scope = $.val.scope + '';
28
- }
29
- // 当前目录
30
- this.dir = __dirname;
31
- if (dir) {
32
- this.dir = dir;
33
- }
34
-
35
- // 配置参数
36
- this.config = {
37
- // 服务器地址
38
- "host": "localhost",
39
- // 端口号
40
- "port": 6379,
41
- // 密码
42
- "password": "asd123",
43
- // 数据库
44
- "database": 0,
45
- // 前缀
46
- "prefix": ""
47
- };
48
- // 唯一标识符
49
- this.identifier = this.config.host + "/" + this.config.database;
50
- // 数据库连接器
51
- this.conn;
52
-
53
- // 订阅回调函数
54
- this.func_list = {};
55
- }
56
- }
57
-
58
- /**
59
- * 消息订阅
60
- * @param {String} channel 订阅的频道
61
- * @param {Function} func 操作回调函数,可判断成功与否
62
- */
63
- Redis.prototype.subscribe = function(channel, func) {
64
- this.conn.subscribe(channel);
65
- this.func_list[channel] = func;
66
- };
67
-
68
- /**
69
- * 取消订阅
70
- * @param {String} channel 订阅的频道
71
- */
72
- Redis.prototype.unsubscribe = function(channel) {
73
- this.conn.unsubscribe(channel);
74
- delete this.func_list[channel];
75
- };
76
-
77
- /**
78
- * 发布消息
79
- * @param {String} channel 频道
80
- * @param {String} message 订阅的消息
81
- * @param {Function} func 消息发布成功回调函数
82
- */
83
- Redis.prototype.publish = function(channel, message, func) {
84
- this.conn.publish(channel, message, func);
85
- };
86
-
87
- /**
88
- * 订阅消息处理, 如果没有订阅函数,则其他的订阅消息会进入该函数
89
- * @param {String} channel
90
- * @param {String} message
91
- */
92
- Redis.prototype.main = async function(channel, message) {};
93
-
94
- /**
95
- * 消息通知
96
- * @param {String} channel 频道
97
- * @param {String} message 消息
98
- */
99
- Redis.prototype.message = function(channel, message) {
100
- var func = this.func_list[channel];
101
- if (func) {
102
- func(message);
103
- } else {
104
- this.main(channel, message);
105
- }
106
- };
107
-
108
- /**
109
- * @description 设置配置
110
- * @param {Object} cg 配置对象或配置路径
111
- */
112
- Redis.prototype.setConfig = function(cg) {
113
- var obj;
114
- if (typeof(cg) === "string") {
115
- obj = cg.loadJson(this.dir);
116
- } else {
117
- obj = cg;
118
- }
119
- $.push(this.config, obj, true);
120
- this.identifier = this.config.host + "/" + this.config.database;
121
- };
122
-
123
- /**
124
- * @description 连接Redis数据库
125
- */
126
- Redis.prototype.open = async function() {
127
- var cg = this.config;
128
- var password = cg.password;
129
- if (!this.conn) {
130
- var _this = this;
131
- this.conn = await createClient(cg).on("ready", function() {
132
- // 监听订阅成功事件
133
- this.on("subscribe", function(channel, count) {
134
- console.log("频道" + channel + "订阅成功, 当前总计订阅数:" + count);
135
- });
136
- // 监听取消订阅事件
137
- this.on("unsubscribe", function(channel, count) {
138
- console.log("频道" + channel + "取消订阅, 当前总计订阅数:" + count);
139
- });
140
- // 收到消息后执行回调,message是redis发布的消息
141
- this.on("message", (channel, message) => {
142
- this.message(channel, message);
143
- });
144
- }).connect();
145
- if (password) {
146
- this.conn.auth({
147
- password
148
- });
149
- }
150
- }
151
-
152
- };
153
-
154
- /**
155
- * @description 关闭连接
156
- */
157
- Redis.prototype.close = function() {
158
- var cg = this.config;
159
- if (this.conn) {
160
- this.conn.quit();
161
- this.conn = null;
162
- }
163
- };
164
-
165
- /**
166
- * @description 查询或是设置缓存过期时间
167
- * @param {String} key 键
168
- * @param {Number} seconds 秒, 为空则查询有效时长
169
- * @return {Promise|Number|Boolean} 查询时长或设置结果, 时长为-1表示永不过期
170
- */
171
- Redis.prototype.ttl = async function(key, seconds) {
172
- var ret;
173
- if (seconds) {
174
- if (seconds > 0) {
175
- ret = await this.conn.expire(key, seconds);
176
- } else {
177
- ret = await this.conn.persist(key);
178
- }
179
- } else {
180
- ret = await this.conn.ttl(key);
181
- }
182
- return ret;
183
- };
184
-
185
- /**z
186
- * @description 增加整数值(负数为减)
187
- * @param {String} key 键
188
- * @param {Number} num 数值
189
- * @return {Promise|Number} 计算后的结果
190
- */
191
- Redis.prototype.addInt = async function(key, num) {
192
- var value = await this.get(key);
193
- var n = 0;
194
- if (value) {
195
- n = Number(value);
196
- }
197
- n += num;
198
- this.set(key, n);
199
- return n;
200
- };
201
-
202
- /**
203
- * @description 增加浮点数值(负数为减)
204
- * @param {String} key 键
205
- * @param {Number} num 数值
206
- * @return {Promise|Number} 计算后的结果
207
- */
208
- Redis.prototype.addFloat = async function(key, num) {
209
- var value = await this.get(key);
210
- var n = 0;
211
- if (value) {
212
- n = Number(value);
213
- }
214
- n += num;
215
- this.set(key, n);
216
- return n;
217
- };
218
-
219
- /**
220
- * @description 增加字符串值到指定缓存
221
- * @param {String} key 键
222
- * @param {String} str 添加的字符串
223
- * @return {Promise|String} 添加后的字符串
224
- */
225
- Redis.prototype.addStr = async function(key, str) {
226
- var ret = 0;
227
- try {
228
- ret = await this.conn.append(key, str);
229
- } catch (err) {
230
- console.error(err);
231
- }
232
- return ret;
233
- };
234
-
235
- /**
236
- * @description 删除缓存
237
- * @param {String} key 键
238
- * @return {Promise|Boolean} 成功返回true,失败返回false
239
- */
240
- Redis.prototype.del = async function(key) {
241
- var n = await this.conn.del(key);
242
- return n === 1;
243
- };
244
-
245
- /**
246
- * @description 修改缓存
247
- * @param {String} key 键
248
- * @param {Object} value 值
249
- * @param {Number} seconds 秒
250
- * @return {Object} 值
251
- */
252
- Redis.prototype.set = async function(key, value, seconds) {
253
- if (typeof(value) == "object") {
254
- value = JSON.stringify(value);
255
- }
256
- var ret;
257
-
258
- try {
259
- if (seconds) {
260
- ret = await this.conn.set(key, value, {
261
- EX: seconds,
262
- NX: true
263
- });
264
- } else {
265
- ret = await this.conn.set(key, value);
266
- }
267
- } catch (err) {
268
- console.log(err);
269
- }
270
- return ret === "OK";
271
- };
272
-
273
- /**
274
- * @description 判断键是否存在
275
- * @param {String} key 键
276
- * @return {Promise|Boolean} 有返回true, 没有返回false
277
- */
278
- Redis.prototype.has = async function(key) {
279
- var has = await this.conn.exists(key);
280
- if (has) {
281
- return true
282
- }
283
- return false;
284
- };
285
-
286
- /**
287
- * @description 修改缓存
288
- * @param {String} key 键
289
- * @param {Object} value 值
290
- * @param {Number} seconds 秒
291
- * @return {Promise|Object} 值
292
- */
293
- Redis.prototype.add = async function(key, value, seconds) {
294
- var has = await this.has(key);
295
- if (has) {
296
- return false
297
- }
298
- return await this.set(key, value, seconds);
299
- };
300
-
301
- /**
302
- * @description 查询缓存
303
- * @param {String} key 键
304
- * @return {Promise|Object} 查询值
305
- */
306
- Redis.prototype.get = async function(key) {
307
- var value = await this.conn.get(key);
308
- if (typeof(value) == "string") {
309
- if (value.startsWith("{") && value.endsWith("}")) {
310
- value = JSON.parse(value);
311
- } else if (value.startsWith("[") && value.endsWith("]")) {
312
- value = JSON.parse(value);
313
- }
314
- }
315
- return value;
316
- };
317
-
318
- /**
319
- * @description 查询缓存的字符串中的一段字符串
320
- * @param {String} key 键
321
- * @param {Number} start 开始位置
322
- * @param {Number} end 结束位置
323
- * @return {Promise|String} 查询值
324
- */
325
- Redis.prototype.getrange = function(key, start, end) {
326
- var _this = this;
327
- return new Promise((resolve, reject) => {
328
- _this.conn.getrange(key, start, end, function(err, ret) {
329
- if (err) {
330
- reject(err);
331
- } else {
332
- resolve(ret);
333
- }
334
- });
335
- });
336
- };
337
-
338
- /**
339
- * 在指定位置插入文本
340
- * @param {String} str 原字符串
341
- * @param {Number} index 索引
342
- * @param {String} str_sub 插入的字符串
343
- */
344
- Redis.prototype.insertString = function(str, index, str_sub) {
345
- return str.substring(0, index) + str_sub + str.substring(index);
346
- }
347
-
348
- /**
349
- * @description 在值的指定位置开始增加一段字符串
350
- * @param {String} key 键
351
- * @param {Number} index 开始位置
352
- * @param {String} value 变更的值
353
- * @return {Promise|Number} 字符串长度
354
- */
355
- Redis.prototype.setrange = async function(key, index, value) {
356
- var str = await this.get(key);
357
- if (str && str.length > index) {
358
- str = this.insertString(str, index, value);
359
- await this.set(key, str);
360
- }
361
- return str
362
- };
363
-
364
- /**
365
- * @description 清空缓存
366
- * @param {String} key 键, 为空则清空所有
367
- * @return {Promise|Array} 执行结果
368
- */
369
- Redis.prototype.clear = async function(key) {
370
- var ret;
371
- if (key) {
372
- ret = await this.conn.del(key);
373
- } else {
374
- console.log(this.conn);
375
- ret = await this.conn();
376
- }
377
- return ret;
378
- };
379
-
380
- /**
381
- * @description 排序
382
- * @param {String} key 键
383
- * @param {String} way = [asc|desc]排序方式, 可以为空
384
- * @param {String} obj_key 排序成员的键
385
- * @return {Promise|Array} 排序后的数组
386
- */
387
- Redis.prototype.sort = function(key) {
388
- var _this = this;
389
- return new Promise((resolve, reject) => {
390
- _this.conn.sort(key, function(err, ret) {
391
- if (err) {
392
- reject(err);
393
- } else {
394
- resolve(ret);
395
- }
396
- });
397
- });
398
- };
399
-
400
- /**
401
- * @description 获取所有键名
402
- * @param {String} key 键 支持*号, 前面加*表示后面名称一致, 前后加*表示包含名称, 后面加*表示前面名称一致
403
- * @return {Promise|Array} 键数组
404
- */
405
- Redis.prototype.keys = function(key) {
406
- var _this = this;
407
- if (!key) {
408
- key = "*";
409
- }
410
- return new Promise((resolve, reject) => {
411
- _this.conn.keys(key, function(err, ret) {
412
- if (err) {
413
- reject(err);
414
- } else {
415
- resolve(ret);
416
- }
417
- });
418
- });
419
- };
420
-
421
- /**
422
- * @description 修改数组缓存
423
- * @param {String} key 键
424
- * @param {Object} value 值
425
- * @param {Number} seconds 秒
426
- * @return {Promise|Array} 执行结果
427
- */
428
- Redis.prototype.list_set = function(key, value, seconds) {
429
- var _this = this;
430
- return new Promise((resolve, reject) => {
431
- _this.conn.rpush(key, value, function(err, ret) {
432
- if (err) {
433
- reject(err);
434
- } else {
435
- if (seconds) {
436
- _this.conn.expire(key, seconds);
437
- }
438
- resolve(ret);
439
- }
440
- });
441
- });
442
- };
443
-
444
- /**
445
- * @description 数组缓存追加对象
446
- * @param {String} key 键
447
- * @param {Object} value 值
448
- * @return {Promise|String} 追加后的数组
449
- */
450
- Redis.prototype.list_push = function(key, value) {
451
- var _this = this;
452
- return new Promise((resolve, reject) => {
453
- _this.conn.rpush(key, value, function(err, ret) {
454
- if (err) {
455
- reject(err);
456
- } else {
457
- resolve(ret);
458
- }
459
- });
460
- });
461
- };
462
-
463
- /**
464
- * @description 判断成员是否存在
465
- * @param {String} key 键
466
- * @param {Object} value 值
467
- * @return {Promise|Boolean} 存在返回true, 否则返回false
468
- */
469
- Redis.prototype.list_has = function(key, value) {
470
- var _this = this;
471
- return new Promise(async (resolve, reject) => {
472
- if (!value) {
473
- reject('值不能为空');
474
- }
475
- var arr = await _this.list_get(key);
476
- var has = false;
477
- var len = arr.length;
478
- for (var i = 0; i < len; i++) {
479
- if (value == arr[i]) {
480
- has = true;
481
- break;
482
- }
483
- }
484
- resolve(has);
485
- });
486
- };
487
-
488
- /**
489
- * @description 查询数组缓存
490
- * @param {String} key 键
491
- * @param {Number} start 起始位置
492
- * @param {Number} end 结束位置
493
- * @return {Promise|Array} 查询到的数组
494
- */
495
- Redis.prototype.list_get = function(key, start, end) {
496
- var _this = this;
497
- if (start === undefined) {
498
- start = 0;
499
- }
500
- if (end === undefined) {
501
- end = -1;
502
- }
503
- return new Promise((resolve, reject) => {
504
- _this.conn.lrange(key, start, end, function(err, ret) {
505
- if (err) {
506
- reject(err);
507
- } else {
508
- resolve(ret);
509
- }
510
- });
511
- });
512
- };
513
-
514
- /**
515
- * @description 清空数组缓存
516
- * @param {String} key 键
517
- * @param {Array} value 新成员, 没有则删除数组
518
- * @return {Promise|Boolean} 成功返回true,是否返回false
519
- */
520
- Redis.prototype.list_clear = function(key, value) {
521
- var _this = this;
522
- return new Promise((resolve, reject) => {
523
- if (key) {
524
- if (value) {
525
- // 如果有值则删除其他成员,保留新值
526
- _this.list_push(key, value).then(function(len) {
527
- _this.conn.ltrim(key, len - value.length, len - 1, function(err, ret) {
528
- if (err) {
529
- reject(err);
530
- } else {
531
- resolve(ret === "OK");
532
- }
533
- });
534
- }, function(er) {
535
- reject(er);
536
- });
537
- } else {
538
- // 直接删除键
539
- _this.conn.del(key, function(err, ret) {
540
- if (err) {
541
- reject(err);
542
- } else {
543
- resolve(ret === "OK");
544
- }
545
- });
546
- }
547
- } else {
548
- reject('键不能为空');
549
- }
550
- });
551
- };
552
- /**
553
- * @description 导出Redis类
554
- */
555
- exports.Redis = Redis;
556
-
557
- /**
558
- * @description redis连接池
559
- */
560
- if (!$.pool.redis) {
561
- $.pool.redis = {};
562
- }
563
-
564
- /**
565
- * @description 缓存管理器,用于创建缓存
566
- * @param {String} scope 作用域
567
- * @param {String} dir 当前路径
568
- * @return {Object} 返回一个缓存类
569
- */
570
- function redis_admin(scope, dir) {
571
- if (!scope) {
572
- scope = $.val.scope
573
- }
574
- var obj = $.pool.redis[scope];
575
- if (!obj) {
576
- $.pool.redis[scope] = new Redis(scope, dir);
577
- obj = $.pool.redis[scope];
578
- }
579
- return obj;
580
- }
581
-
582
- /**
583
- * @description 导出redis管理函数
584
- */
585
- exports.redis_admin = redis_admin;
package/test2.js DELETED
@@ -1,22 +0,0 @@
1
- const Redis = require('./index.js').Redis;
2
-
3
- async function test() {
4
- var rs = new Redis();
5
- var config = './config.json'.loadJson(__dirname);
6
- rs.setConfig(config);
7
- rs.open();
8
- rs.unsubscribe('test');
9
- // 添加订阅
10
- rs.subscribe('test', function(message) {
11
- console.log('来自订阅的消息', message);
12
- });
13
-
14
- setTimeout(function(){
15
- // 发布订阅消息
16
- rs.publish('test', '你好吗?' + Math.random(), function(error, result) {
17
- console.log(error, result);
18
- });
19
- },3000)
20
- }
21
-
22
- test();