forked from thinkjs/think-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (119 loc) · 3.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* @Author: lushijie
* @Date: 2017-03-22 14:19:15
* @Last Modified by: lushijie
* @Last Modified time: 2018-01-09 13:04:24
*/
const helper = require('think-helper');
const assert = require('assert');
const IOredis = require('ioredis');
const Debounce = require('think-debounce');
const debounceInstance = new Debounce();
const _validExpire = Symbol('validExpire');
const _getConnection = Symbol('_getConnection');
const cacheConn = {};
// redis config see at https://github.com/luin/ioredis/blob/master/lib/redis.js
const defaultConfig = {
port: 6379,
host: '127.0.0.1',
password: ''
};
class thinkRedis {
constructor(config) {
config = helper.extend({}, defaultConfig, config);
this.redis = this[_getConnection](config);
}
/**
* getConnection by config
* @param {Object} config [description]
* @return {Object} [description]
*/
[_getConnection](config) {
delete config.cookie;
const md5 = helper.md5(JSON.stringify(config));
if (!cacheConn[md5] || !cacheConn[md5].connector.connecting) {
cacheConn[md5] = new IOredis(config);
}
return cacheConn[md5];
}
/**
* valid expire num
*/
[_validExpire](num) {
const msg = 'expire should be an integer great than zero';
assert(num && /^[+]?[0-9]+$/.test(num) && num > 0, msg);
}
/**
* add event listener
* @param {String} event [connect,ready,error,close,reconnecting,end is supported]
* @param {Function} callback [description]
* @return {void} [description]
*/
on(event, callback) {
this.redis.on(event, callback);
}
/**
* set key
* @param {Stirng} key [description]
* @param {String} value [description]
* @param {String} type [EX='seconds'|PX='milliseconds']
* @param {Int} expire [>0]
* @return {Promise} [description]
*/
set(key, value, type, expire) {
if (type) {
if (helper.isString(type)) {
assert(type === 'EX' || type === 'PX', 'type should eq "EX" or "PX"');
this[_validExpire](expire);
return this.redis.set(key, value, type, expire);
} else {
this[_validExpire](type);
return this.redis.set(key, value, 'PX', type);
}
}
// without type
return this.redis.set(key, value);
}
/**
* get key
* @param {String} key [description]
* @return {Promise} [description]
*/
get(key) {
return debounceInstance.debounce(key, () => this.redis.get(key));
}
/**
* delete key
* @param {String} key [description]
* @return {Promise} [description]
*/
delete(key) {
return this.redis.del(key);
}
/**
* increase key's value
* @param {String} key [description]
* @return {Promise} [description]
*/
increase(key) {
return this.redis.incr(key);
}
/**
* decrease key's value
* @param {String} key [description]
* @return {Promise} [description]
*/
decrease(key) {
return this.redis.decr(key);
}
/**
* close connection
* @return {void} [description]
*/
close() {
if (this.redis.connector.connecting) {
this.redis.disconnect();
}
}
}
module.exports = thinkRedis;