Skip to content

Commit 715c09d

Browse files
author
Ruben Bridgewater
committed
v.2.0.0
1 parent 55e4a9b commit 715c09d

File tree

3 files changed

+131
-84
lines changed

3 files changed

+131
-84
lines changed

README.md

Lines changed: 51 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ redis - a node.js redis client
55
[![Coverage Status](https://coveralls.io/repos/NodeRedis/node_redis/badge.svg?branch=)](https://coveralls.io/r/NodeRedis/node_redis?branch=)
66
[![Windows Tests](https://img.shields.io/appveyor/ci/bcoe/node-redis/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/bcoe/node-redis)
77

8-
This is a complete Redis client for node.js. It supports all Redis commands,
9-
including many recently added commands.
8+
This is a complete Redis client for node.js. It supports all Redis commands and focuses on performance.
109

1110
Install with:
1211

@@ -54,21 +53,26 @@ Note that the API is entire asynchronous. To get data back from the server,
5453
you'll need to use a callback. The return value from most of the API is a
5554
backpressure indicator.
5655

56+
You can also use node_redis with promises by promisifying node_redis with [bluebird](https://github.com/petkaantonov/bluebird) as in:
57+
58+
```js
59+
var redis = require('redis');
60+
bluebird.promisifyAll(redis.RedisClient.prototype);
61+
```
62+
5763
### Sending Commands
5864

5965
Each Redis command is exposed as a function on the `client` object.
6066
All functions take either an `args` Array plus optional `callback` Function or
6167
a variable number of individual arguments followed by an optional callback.
62-
Here is an example of passing an array of arguments and a callback:
63-
64-
```js
65-
client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
66-
```
67-
68-
Here is that same call in the second style:
68+
Here are examples how to use the api:
6969

7070
```js
71-
client.mset("test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
71+
client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
72+
// Works the same as
73+
client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
74+
// Or
75+
client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
7276
```
7377

7478
Note that in either form the `callback` is optional:
@@ -78,7 +82,7 @@ client.set("some key", "some val");
7882
client.set(["some other key", "some val"]);
7983
```
8084

81-
If the key is missing, reply will be null (probably):
85+
If the key is missing, reply will be null. Only if the [Redis Command Reference](http://redis.io/commands) states something else it will not be null.
8286

8387
```js
8488
client.get("missingkey", function(err, reply) {
@@ -103,15 +107,13 @@ JavaScript Array of node Buffers. `HGETALL` returns an Object with Buffers keyed
103107

104108
### "ready"
105109

106-
`client` will emit `ready` once a connection is established to the Redis server and the server reports
107-
that it is ready to receive commands. Commands issued before the `ready` event are queued,
110+
`client` will emit `ready` once a connection is established. Commands issued before the `ready` event are queued,
108111
then replayed just before this event is emitted.
109112

110113
### "connect"
111114

112115
`client` will emit `connect` at the same time as it emits `ready` unless `client.options.no_ready_check`
113-
is set. If this options is set, `connect` will be emitted when the stream is connected, and then
114-
you are free to try to send commands.
116+
is set. If this options is set, `connect` will be emitted when the stream is connected.
115117

116118
### "reconnecting"
117119

@@ -120,26 +122,9 @@ are passed an object containing `delay` (in ms) and `attempt` (the attempt #) at
120122

121123
### "error"
122124

123-
`client` will emit `error` when encountering an error connecting to the Redis server.
124-
125-
Note that "error" is a special event type in node. If there are no listeners for an
126-
"error" event, node will exit. This is usually what you want, but it can lead to some
127-
cryptic error messages like this:
128-
129-
mjr:~/work/node_redis (master)$ node example.js
130-
131-
node.js:50
132-
throw e;
133-
^
134-
Error: ECONNREFUSED, Connection refused
135-
at IOWatcher.callback (net:870:22)
136-
at node.js:607:9
137-
138-
Not very useful in diagnosing the problem, but if your program isn't ready to handle this,
139-
it is probably the right thing to just exit.
125+
`client` will emit `error` when encountering an error connecting to the Redis server or when any other in node_redis occurs.
140126

141-
`client` will also emit `error` if an exception is thrown inside of `node_redis` for whatever reason.
142-
It would be nice to distinguish these two cases.
127+
So please attach the error listener to node_redis.
143128

144129
### "end"
145130

@@ -149,8 +134,7 @@ It would be nice to distinguish these two cases.
149134

150135
`client` will emit `drain` when the TCP connection to the Redis server has been buffering, but is now
151136
writable. This event can be used to stream commands in to Redis and adapt to backpressure. Right now,
152-
you need to check `client.command_queue.length` to decide when to reduce your send rate. Then you can
153-
resume sending when you get `drain`.
137+
you need to check `client.command_queue.length` to decide when to reduce your send rate and resume sending commands when you get `drain`.
154138

155139
### "idle"
156140

@@ -169,39 +153,35 @@ port and host are probably fine and you don't need to supply any arguments. `cre
169153

170154
`options` is an object with the following possible properties:
171155

172-
* `parser`: which Redis protocol reply parser to use. Defaults to `hiredis` if that module is installed.
173-
This may also be set to `javascript`.
174-
* `return_buffers`: defaults to `false`. If set to `true`, then all replies will be sent to callbacks as node Buffer
175-
objects instead of JavaScript Strings.
176-
* `detect_buffers`: default to `false`. If set to `true`, then replies will be sent to callbacks as node Buffer objects
177-
if any of the input arguments to the original command were Buffer objects.
156+
* `parser`: *hiredis*; Which Redis protocol reply parser to use. If `hiredis` is not installed it will fallback to `javascript`.
157+
* `return_buffers`: *false*; If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings.
158+
* `detect_buffers`: *false*; If set to `true`, then replies will be sent to callbacks as Buffers
159+
if any of the input arguments to the original command were Buffers.
178160
This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to
179161
every command on a client.
180-
* `socket_nodelay`: defaults to `true`. Whether to call setNoDelay() on the TCP stream, which disables the
181-
Nagle algorithm on the underlying socket. Setting this option to `false` can result in additional throughput at the
182-
cost of more latency. Most applications will want this set to `true`.
183-
* `socket_keepalive` defaults to `true`. Whether the keep-alive functionality is enabled on the underlying socket.
184-
* `no_ready_check`: defaults to `false`. When a connection is established to the Redis server, the server might still
185-
be loading the database from disk. While loading, the server not respond to any commands. To work around this,
162+
* `socket_nodelay`: *true*; Disables the [Nagle algorithm](https://en.wikipedia.org/wiki/Nagle%27s_algorithm).
163+
Setting this option to `false` can result in additional throughput at the cost of more latency.
164+
Most applications will want this set to `true`.
165+
* `socket_keepalive` *true*; Whether the keep-alive functionality is enabled on the underlying socket.
166+
* `no_ready_check`: *false*; When a connection is established to the Redis server, the server might still
167+
be loading the database from disk. While loading the server will not respond to any commands. To work around this,
186168
`node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command
187169
indicates whether the server is ready for more commands. When ready, `node_redis` emits a `ready` event.
188170
Setting `no_ready_check` to `true` will inhibit this check.
189-
* `enable_offline_queue`: defaults to `true`. By default, if there is no active
171+
* `enable_offline_queue`: *true*; By default, if there is no active
190172
connection to the redis server, commands are added to a queue and are executed
191173
once the connection has been established. Setting `enable_offline_queue` to
192-
`false` will disable this feature and the callback will be execute immediately
193-
with an error, or an error will be thrown if no callback is specified.
194-
* `retry_max_delay`: defaults to `null`. By default every time the client tries to connect and fails time before
195-
reconnection (delay) almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits delay
196-
to maximum value, provided in milliseconds.
197-
* `connect_timeout` defaults to `86400000`. Setting `connect_timeout` limits total time for client to reconnect.
198-
Value is provided in milliseconds and is counted once the disconnect occurred. The last retry is going to happen exactly at the timeout time.
174+
`false` will disable this feature and the callback will be executed immediately
175+
with an error, or an error will be emitted if no callback is specified.
176+
* `retry_max_delay`: *null*; By default every time the client tries to connect and fails the reconnection delay almost doubles.
177+
This delay normally grows infinitely, but setting `retry_max_delay` limits it to the maximum value, provided in milliseconds.
178+
* `connect_timeout` *86400000*; Setting `connect_timeout` limits total time for client to reconnect.
179+
The value is provided in milliseconds and is counted once the disconnect occurred. The last retry is going to happen exactly at the timeout time.
199180
That way the default is to try reconnecting until 24h passed.
200-
* `max_attempts` defaults to `0`. By default client will try reconnecting until connected. Setting `max_attempts`
181+
* `max_attempts` *0*; By default client will try reconnecting until connected. Setting `max_attempts`
201182
limits total amount of connection tries. Setting this to 1 will prevent any reconnect tries.
202-
* `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect.
203-
* `family` defaults to `IPv4`. The client connects in IPv4 if not specified or if the DNS resolution returns an IPv4 address.
204-
You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns modules how to use the family type.
183+
* `auth_pass` *null*; If set, client will run redis auth command on connect.
184+
* `family` *IPv4*; You can force using IPv6 if you set the family to 'IPv6'. See Node.js [net](https://nodejs.org/api/net.html) or [dns](https://nodejs.org/api/dns.html) modules how to use the family type.
205185

206186
```js
207187
var redis = require("redis"),
@@ -221,11 +201,9 @@ client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
221201
client.end();
222202
```
223203

224-
225-
226204
## client.auth(password, callback)
227205

228-
When connecting to Redis servers that require authentication, the `AUTH` command must be sent as the
206+
When connecting to a Redis server that requires authentication, the `AUTH` command must be sent as the
229207
first command after connecting. This can be tricky to coordinate with reconnections, the ready check,
230208
etc. To make this easier, `client.auth()` stashes `password` and will send it after each connection,
231209
including reconnections. `callback` is invoked only once, after the response to the very first
@@ -247,10 +225,11 @@ var redis = require("redis"),
247225
client = redis.createClient();
248226

249227
client.set("foo_rand000000000000", "some fantastic value");
228+
client.end(); // No further commands will be processed
250229
client.get("foo_rand000000000000", function (err, reply) {
251-
console.log(reply.toString());
230+
// This won't be called anymore
231+
console.log(err);
252232
});
253-
client.end();
254233
```
255234

256235
`client.end()` is useful for timeout cases where something is stuck or taking too long and you want
@@ -436,10 +415,8 @@ client.multi()
436415
same command methods as `client` objects do. Commands are queued up inside the `Multi` object
437416
until `Multi.exec()` is invoked.
438417

439-
The `callback` of `.exec()` will get invoked with two arguments:
440-
441-
* `err` **type:** `null | Array` err is either null or an array of Error Objects corresponding the the sequence the commands where chained. The last item of the array will always be an `EXECABORT` type of error originating from the `.exec()` itself.
442-
* `results` **type:** `null | Array` results is an array of responses corresponding the the sequence the commands where chained.
418+
If your code contains an syntax error an EXECABORT error is going to be thrown and all commands are going to be aborted. That error contains a `.errors` property that contains the concret errors.
419+
If all commands were queued successfully and an error is thrown by redis while processing the commands that error is going to be returned in the result array! No other command is going to be aborted though than the onces failing.
443420

444421
You can either chain together `MULTI` commands as in the above example, or you can queue individual
445422
commands while still sending regular client command as in this example:
@@ -561,7 +538,7 @@ Used internally to send commands to Redis. Nearly all Redis commands have been a
561538
However, if new commands are introduced before this library is updated, you can use `send_command()` to send arbitrary commands to Redis.
562539
The command has to be lower case.
563540

564-
All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted.
541+
All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted / set to undefined.
565542

566543
## client.connected
567544

@@ -720,22 +697,15 @@ hiredis parser:
720697

721698
To get debug output run your `node_redis` application with `NODE_DEBUG=redis`.
722699

723-
## TODO
724-
725-
1. 100% coverage
726-
2. More performance improvements
727-
3. Stream large set/get values into and out of Redis. Otherwise the entire value must be in node's memory.
728-
4. Performance can be better for very large values in the js parser.
729-
730700
## How to Contribute
731701
- open a pull request and then wait for feedback
732702

733703
## Contributors
734-
Many people have have added features and fixed bugs in `node_redis`. Thanks to all of them!
704+
Many [people](https://github.com/NodeRedis/node_redis/graphs/contributors) have have added features and fixed bugs in `node_redis`. Thanks to all of them!
735705

736706
## LICENSE - "MIT License"
737707

738-
Copyright (c) 2015 Matthew Ranney, http://ranney.com/
708+
Copyright (c) by NodeRedis
739709

740710
Permission is hereby granted, free of charge, to any person
741711
obtaining a copy of this software and associated documentation
@@ -757,3 +727,5 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
757727
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
758728
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
759729
OTHER DEALINGS IN THE SOFTWARE.
730+
731+
Originally developed by Matthew Ranney, http://ranney.com/

0 commit comments

Comments
 (0)