mm_redis 1.4.1 → 1.4.2
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 +13 -4
- package/index_new.js +585 -0
- package/package.json +5 -4
- package/test.js +68 -37
package/index.js
CHANGED
|
@@ -99,7 +99,6 @@ Redis.prototype.main = async function(channel, message) {};
|
|
|
99
99
|
* @param {String} message 消息
|
|
100
100
|
*/
|
|
101
101
|
Redis.prototype.message = function(channel, message) {
|
|
102
|
-
console.log("我接收到信息了" + message);
|
|
103
102
|
var func = this.func_list[channel];
|
|
104
103
|
if (func) {
|
|
105
104
|
func(message);
|
|
@@ -314,6 +313,9 @@ Redis.prototype.del = function(key) {
|
|
|
314
313
|
Redis.prototype.set = function(key, value, seconds) {
|
|
315
314
|
var _this = this;
|
|
316
315
|
return new Promise((resolve, reject) => {
|
|
316
|
+
if (typeof(value) == "object") {
|
|
317
|
+
value = JSON.stringify(value);
|
|
318
|
+
}
|
|
317
319
|
_this.conn.set(key, value, function(err, ret) {
|
|
318
320
|
if (err) {
|
|
319
321
|
reject(err);
|
|
@@ -369,11 +371,18 @@ Redis.prototype.add = function(key, value, seconds) {
|
|
|
369
371
|
Redis.prototype.get = function(key) {
|
|
370
372
|
var _this = this;
|
|
371
373
|
return new Promise((resolve, reject) => {
|
|
372
|
-
_this.conn.get(key, function(err,
|
|
374
|
+
_this.conn.get(key, function(err, value) {
|
|
373
375
|
if (err) {
|
|
374
376
|
reject(err);
|
|
375
377
|
} else {
|
|
376
|
-
|
|
378
|
+
if (typeof(value) == "string") {
|
|
379
|
+
if (value.startsWith("{") && value.endsWith("}")) {
|
|
380
|
+
value = JSON.parse(value);
|
|
381
|
+
} else if (value.startsWith("[") && value.endsWith("]")) {
|
|
382
|
+
value = JSON.parse(value);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
resolve(value);
|
|
377
386
|
}
|
|
378
387
|
});
|
|
379
388
|
});
|
|
@@ -670,4 +679,4 @@ function redis_admin(scope, dir) {
|
|
|
670
679
|
/**
|
|
671
680
|
* @description 导出redis管理函数
|
|
672
681
|
*/
|
|
673
|
-
exports.redis_admin = redis_admin;
|
|
682
|
+
exports.redis_admin = redis_admin;
|
package/index_new.js
ADDED
|
@@ -0,0 +1,585 @@
|
|
|
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/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_redis",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "这是超级美眉redis缓存帮助类",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node test.js"
|
|
7
|
+
"test": "node test.js",
|
|
8
|
+
"dev": "nodemon test.js"
|
|
8
9
|
},
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|
|
@@ -26,7 +27,7 @@
|
|
|
26
27
|
},
|
|
27
28
|
"homepage": "https://github.com/qiuwenwu/mm_redis#readme",
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"mm_expand": "^1.7.
|
|
30
|
-
|
|
30
|
+
"mm_expand": "^1.7.2",
|
|
31
|
+
"redis": "^2.8.0"
|
|
31
32
|
}
|
|
32
33
|
}
|
package/test.js
CHANGED
|
@@ -5,58 +5,89 @@ async function test() {
|
|
|
5
5
|
var rs = new Redis();
|
|
6
6
|
var config = './config.json'.loadJson(__dirname);
|
|
7
7
|
rs.setConfig(config);
|
|
8
|
-
rs.open();
|
|
8
|
+
await rs.open();
|
|
9
9
|
// await rs.set("a", "张三", 10);
|
|
10
10
|
// await rs.set("a_c", "张三", 10);
|
|
11
11
|
// await rs.set("b", "李四", 10);
|
|
12
12
|
//
|
|
13
13
|
// await rs.set("c", "王五", 10);
|
|
14
14
|
// await rs.set("d", "十五", 10);
|
|
15
|
-
bl = await rs.
|
|
16
|
-
console.log("
|
|
15
|
+
// bl = await rs.del("a");
|
|
16
|
+
// console.log("删除", bl);
|
|
17
|
+
// bl = await rs.del("age");
|
|
18
|
+
// console.log("删除", bl);
|
|
19
|
+
// bl = await rs.set("age", 10, 10);
|
|
20
|
+
// var ss = await rs.ttl("age");
|
|
21
|
+
// console.log("修改age", bl, ss);
|
|
22
|
+
// bl = await rs.ttl("age", 20);
|
|
23
|
+
// ss = await rs.ttl("age");
|
|
24
|
+
// console.log("修改age", bl, ss);
|
|
25
|
+
// bl = await rs.ttl("age", -3);
|
|
26
|
+
// ss = await rs.ttl("age");
|
|
27
|
+
// console.log("修改age", bl, ss);
|
|
17
28
|
|
|
18
|
-
//bl = await rs.del("d");
|
|
19
|
-
bl = await rs.add("d", "十五", 10);
|
|
20
|
-
console.log("添加" + bl);
|
|
29
|
+
// bl = await rs.del("d");
|
|
30
|
+
// bl = await rs.add("d", "十五", 10);
|
|
31
|
+
// console.log("添加" + bl);
|
|
32
|
+
// bl = await rs.add("d", "十五", 10);
|
|
33
|
+
// console.log("添加" + bl);
|
|
21
34
|
|
|
35
|
+
rs.set("uuid_123456", {
|
|
36
|
+
name: "李四",
|
|
37
|
+
age: 18
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
var user = await rs.get("uuid_123456");
|
|
41
|
+
console.log("测试输出", user);
|
|
42
|
+
// bl = await rs.set("age", 10, 10);
|
|
22
43
|
// var num = await rs.addInt("age", 11);
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
//
|
|
44
|
+
|
|
45
|
+
// await rs.set("a", "哈哈");
|
|
46
|
+
// var len = await rs.addStr("a", "丰B");
|
|
47
|
+
// var str = await rs.get("a");
|
|
48
|
+
// console.log("测试A", str, len);
|
|
49
|
+
// bl = await rs.del("a");
|
|
50
|
+
// console.log("测试A删除", bl);
|
|
51
|
+
|
|
52
|
+
// msg = await rs.setrange("a", 1, "asd");
|
|
26
53
|
// var len = await rs.addStr("a", "丰");
|
|
27
|
-
|
|
28
|
-
//
|
|
54
|
+
|
|
55
|
+
// var str = await rs.get("a");
|
|
56
|
+
// console.log("追加文本", str, len);
|
|
57
|
+
|
|
29
58
|
// var bl = await rs.has("a");
|
|
30
|
-
// console.log("是否存在"
|
|
59
|
+
// console.log("是否存在", bl);
|
|
60
|
+
// var bl = rs.clear();
|
|
61
|
+
// console.log("清空所有", bl);
|
|
62
|
+
|
|
63
|
+
// bl = await rs.list_clear("arr", ['第三', '第四', '第w']); // 清空数组,并重新增加成员
|
|
64
|
+
// console.log("清空数组" + bl);
|
|
31
65
|
|
|
32
|
-
|
|
33
|
-
console.log("
|
|
66
|
+
// num = await rs.list_set("arr", ['第一', '第二'], 10);
|
|
67
|
+
// console.log("设置数组" + num);
|
|
68
|
+
|
|
69
|
+
// time = await rs.ttl("arr");
|
|
70
|
+
// console.log(time);
|
|
71
|
+
// bl = await rs.ttl("arr", -11);
|
|
72
|
+
// console.log(bl);
|
|
73
|
+
// time = await rs.ttl("arr");
|
|
74
|
+
// console.log(time);
|
|
34
75
|
|
|
35
|
-
|
|
36
|
-
console.log(
|
|
76
|
+
// // var bl = await rs.list_has("arr", '第一');
|
|
77
|
+
// // console.log(bl);
|
|
78
|
+
// // await rs.sort("arr");
|
|
79
|
+
// var ar = await rs.list_get("arr");
|
|
80
|
+
// console.log(ar);
|
|
37
81
|
|
|
38
|
-
|
|
39
|
-
console.log(time);
|
|
40
|
-
bl = await rs.ttl("arr", -11);
|
|
41
|
-
console.log(bl);
|
|
42
|
-
time = await rs.ttl("arr");
|
|
43
|
-
console.log(time);
|
|
82
|
+
// //console.log("取值" + JSON.stringify(arr));
|
|
44
83
|
|
|
45
|
-
//
|
|
84
|
+
// //rs.del('a');
|
|
85
|
+
// // ret = await rs.clear(); // 清空缓存
|
|
86
|
+
// // console.log(ret);
|
|
87
|
+
// // var ret = await rs.getrange("a", 1, 3);
|
|
88
|
+
// // var arr = await rs.keys("a*");
|
|
89
|
+
// // console.log("取值" + arr);
|
|
90
|
+
// var bl = rs.close();
|
|
46
91
|
// console.log(bl);
|
|
47
|
-
// await rs.sort("arr");
|
|
48
|
-
var ar = await rs.list_get("arr");
|
|
49
|
-
console.log(ar);
|
|
50
|
-
|
|
51
|
-
//console.log("取值" + JSON.stringify(arr));
|
|
52
|
-
|
|
53
|
-
//rs.del('a');
|
|
54
|
-
// ret = await rs.clear(); // 清空缓存
|
|
55
|
-
// console.log(ret);
|
|
56
|
-
// var ret = await rs.getrange("a", 1, 3);
|
|
57
|
-
// var arr = await rs.keys("a*");
|
|
58
|
-
// console.log("取值" + arr);
|
|
59
|
-
var bl = rs.close();
|
|
60
|
-
console.log(bl);
|
|
61
92
|
}
|
|
62
93
|
test();
|