-
Notifications
You must be signed in to change notification settings - Fork 248
/
redis.js
226 lines (191 loc) · 5.26 KB
/
redis.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var sys = require("sys"),
tcp = require("tcp");
var CRLF = "\r\n";
function dbg(s) {
if (GLOBAL.DEBUG) {
sys.debug(s);
}
}
/**
* Constructs a new Redis client, calling "callback" if successfully connected.
*/
function Redis(callback, port, host) {
this.callbacks = [];
this.conn = tcp.createConnection(port || 6379, host || '127.0.0.1');
this.conn.setEncoding("binary");
this.conn.setTimeout(0);
if (callback) {
var redis = this;
this.conn.addListener("connect", function() {
callback(redis);
});
}
var redis = this, buffer = "", count = 0, n = 0, result = null;
this.conn.addListener("receive", function(data) {
function reply(obj) {
if (n > 0) {
dbg("storing " + obj);
result.push(obj);
obj = result;
n -= 1;
}
if (n == 0) {
dbg("returning " + obj);
redis.callbacks.shift().emitSuccess(obj);
}
}
dbg("receive: " + data);
buffer += data;
while (true) {
if (count > 0) {
dbg("waiting for " + count + " bytes...")
if (buffer.length < count) {
dbg("not enough, only " + buffer.length);
return;
}
var chunk = buffer.substring(0, count - 2);
buffer = buffer.substring(count);
count = 0;
dbg("got chunk " + chunk);
reply(chunk);
} else {
dbg("waiting for line")
var end = buffer.indexOf(CRLF);
if (end == -1) {
dbg("no CRLF");
return;
}
var command = buffer[0];
var line = buffer.substring(1, end);
buffer = buffer.substring(end + 2);
switch (command) {
case '+':
dbg("got line: " + line);
reply(line);
break;
case ':':
dbg("got line: " + line);
reply(parseInt(line, 10));
break;
case '$':
var c = parseInt(line, 10);
dbg("bulk reply detected " + c)
if (c == -1) {
dbg("got null");
reply(null);
} else {
count = c + 2;
continue;
}
break;
case '*':
n = parseInt(line, 10);
if (n == -1) {
n = 0;
dbg("got null");
reply(null);
} else {
dbg("multi bulk reply " + n);
result = [];
continue;
}
break;
case '-':
dbg("error " + line);
redis.callbacks.shift().emitError(line);
break;
default:
dbg("unexpected " + command + line);
}
}
}
});
}
Redis.prototype._send = function(command) {
var promise = new process.Promise;
this.callbacks.push(promise);
this.conn.send(command + CRLF, "binary");
return promise;
};
Redis.prototype.close = function() {
this._send("QUIT").addCallback(function() {
this.conn.close();
});
};
Redis.prototype.ping = function(callback) {
return this._send("PING");
};
// commands operating on string values
Redis.prototype.set = function(key, value) {
return this._send("SET " + key + " " + value.length + CRLF + value);
};
Redis.prototype.get = function(key) {
return this._send("GET " + key);
};
Redis.prototype.getset = function(key, value) {
return this._send("GETSET " + key + " " + value.length + CRLF + value);
};
Redis.prototype.mget = function(/*keys*/) {
var keys = [];
for (var i = 0; i < arguments.length; i++) {
keys.push(arguments[i]);
}
return this._send("MGET " + keys.join(" "));
};
Redis.prototype.setnx = function(key, value) {
return this._send("SETNX " + key + " " + value.length + CRLF + value);
};
Redis.prototype.incr = function(key, by) {
if (by) {
return this._send("INCRBY " + key + " " + by);
}
return this._send("INCR " + key);
};
Redis.prototype.decr = function(key, by) {
if (by) {
return this._send("DECRBY " + key + " " + by);
}
return this._send("DECR " + key);
};
Redis.prototype.del = function(key) {
return this._send("DEL " + key);
};
Redis.prototype.type = function(key) {
return this._send("TYPE " + key);
};
// commands operating on the key space
Redis.prototype.randomkey = function() {
return this._send("RANDOMKEY");
};
Redis.prototype.dbsize = function() {
return this._send("DBSIZE");
};
// commands operating on lists
Redis.prototype.rpush = function(key, value) {
return this._send("RPUSH " + key + " " + value.length + CRLF + value);
};
Redis.prototype.lpush = function(key, value) {
return this._send("LPUSH " + key + " " + value.length + CRLF + value);
};
Redis.prototype.llen = function(key) {
return this._send("LLEN " + key);
};
Redis.prototype.lrange = function(key, from, to) {
return this._send("LRANGE " + key + " " + from + " " + to);
};
// commands operating on sets
// commands operating on sorted sets
// multiple databases handling commmands
Redis.prototype.select = function(index) {
return this._send("SELECT " + index);
};
Redis.prototype.move = function(key, index) {
return this._send("MOVE " + key + " " + index);
};
Redis.prototype.flushdb = function() {
return this._send("FLUSHDB");
};
Redis.prototype.flushall = function() {
return this._send("FLUSHALL");
};
exports.Redis = Redis;