UNPKG

40.6 kBMarkdownView Raw
1redis - a node.js redis client
2===========================
3
4[![Build Status](https://travis-ci.org/NodeRedis/node_redis.svg?branch=master)](https://travis-ci.org/NodeRedis/node_redis)
5[![Coverage Status](https://coveralls.io/repos/NodeRedis/node_redis/badge.svg?branch=)](https://coveralls.io/r/NodeRedis/node_redis?branch=)
6[![Windows Tests](https://img.shields.io/appveyor/ci/BridgeAR/node-redis/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/BridgeAR/node-redis/branch/master)
7[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/NodeRedis/node_redis?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
8
9This is a complete and feature rich Redis client for node.js. __It supports all
10Redis commands__ and focuses on high performance.
11
12Install with:
13
14 npm install redis
15
16## Usage Example
17
18```js
19var redis = require("redis"),
20 client = redis.createClient();
21
22// if you'd like to select database 3, instead of 0 (default), call
23// client.select(3, function() { /* ... */ });
24
25client.on("error", function (err) {
26 console.log("Error " + err);
27});
28
29client.set("string key", "string val", redis.print);
30client.hset("hash key", "hashtest 1", "some value", redis.print);
31client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
32client.hkeys("hash key", function (err, replies) {
33 console.log(replies.length + " replies:");
34 replies.forEach(function (reply, i) {
35 console.log(" " + i + ": " + reply);
36 });
37 client.quit();
38});
39```
40
41This will display:
42
43 mjr:~/work/node_redis (master)$ node example.js
44 Reply: OK
45 Reply: 0
46 Reply: 0
47 2 replies:
48 0: hashtest 1
49 1: hashtest 2
50 mjr:~/work/node_redis (master)$
51
52Note that the API is entirely asynchronous. To get data back from the server,
53you'll need to use a callback. From v.2.6 on the API supports camelCase and
54snake_case and all options / variables / events etc. can be used either way. It
55is recommended to use camelCase as this is the default for the Node.js
56landscape.
57
58### Promises
59
60You can also use node_redis with promises by promisifying node_redis with
61[bluebird](https://github.com/petkaantonov/bluebird) as in:
62
63```js
64var redis = require('redis');
65bluebird.promisifyAll(redis.RedisClient.prototype);
66bluebird.promisifyAll(redis.Multi.prototype);
67```
68
69It'll add a *Async* to all node_redis functions (e.g. return client.getAsync().then())
70
71```js
72// We expect a value 'foo': 'bar' to be present
73// So instead of writing client.get('foo', cb); you have to write:
74return client.getAsync('foo').then(function(res) {
75 console.log(res); // => 'bar'
76});
77
78// Using multi with promises looks like:
79
80return client.multi().get('foo').execAsync().then(function(res) {
81 console.log(res); // => 'bar'
82});
83```
84
85### Sending Commands
86
87Each Redis command is exposed as a function on the `client` object.
88All functions take either an `args` Array plus optional `callback` Function or
89a variable number of individual arguments followed by an optional callback.
90Examples:
91
92```js
93client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
94// Works the same as
95client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
96// Or
97client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
98```
99
100Note that in either form the `callback` is optional:
101
102```js
103client.set("some key", "some val");
104client.set(["some other key", "some val"]);
105```
106
107If the key is missing, reply will be null. Only if the [Redis Command
108Reference](http://redis.io/commands) states something else it will not be null.
109
110```js
111client.get("missingkey", function(err, reply) {
112 // reply is null when the key is missing
113 console.log(reply);
114});
115```
116
117For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands)
118
119Minimal parsing is done on the replies. Commands that return a integer return
120JavaScript Numbers, arrays return JavaScript Array. `HGETALL` returns an Object
121keyed by the hash keys. All strings will either be returned as string or as
122buffer depending on your setting. Please be aware that sending null, undefined
123and Boolean values will result in the value coerced to a string!
124
125# Redis Commands
126
127This library is a 1 to 1 mapping to [Redis commands](https://redis.io/commands).
128It is not a cache library so please refer to Redis commands page for full usage
129details.
130
131Example setting key to auto expire using [SET command](https://redis.io/commands/set)
132
133```js
134// this key will expire after 10 seconds
135client.set('key', 'value!', 'EX', 10);
136```
137
138# API
139
140## Connection and other Events
141
142`client` will emit some events about the state of the connection to the Redis server.
143
144### "ready"
145
146`client` will emit `ready` once a connection is established. Commands issued
147before the `ready` event are queued, then replayed just before this event is
148emitted.
149
150### "connect"
151
152`client` will emit `connect` as soon as the stream is connected to the server.
153
154### "reconnecting"
155
156`client` will emit `reconnecting` when trying to reconnect to the Redis server
157after losing the connection. Listeners are passed an object containing `delay`
158(in ms) and `attempt` (the attempt #) attributes.
159
160### "error"
161
162`client` will emit `error` when encountering an error connecting to the Redis
163server or when any other in node_redis occurs. If you use a command without
164callback and encounter a ReplyError it is going to be emitted to the error
165listener.
166
167So please attach the error listener to node_redis.
168
169### "end"
170
171`client` will emit `end` when an established Redis server connection has closed.
172
173### "drain" (deprecated)
174
175`client` will emit `drain` when the TCP connection to the Redis server has been
176buffering, but is now writable. This event can be used to stream commands in to
177Redis and adapt to backpressure.
178
179If the stream is buffering `client.should_buffer` is set to true. Otherwise the
180variable is always set to false. That way you can decide when to reduce your
181send rate and resume sending commands when you get `drain`.
182
183You can also check the return value of each command as it will also return the
184backpressure indicator (deprecated). If false is returned the stream had to
185buffer.
186
187### "warning"
188
189`client` will emit `warning` when password was set but none is needed and if a
190deprecated option / function / similar is used.
191
192### "idle" (deprecated)
193
194`client` will emit `idle` when there are no outstanding commands that are
195awaiting a response.
196
197## redis.createClient()
198If you have `redis-server` running on the same machine as node, then the
199defaults for port and host are probably fine and you don't need to supply any
200arguments. `createClient()` returns a `RedisClient` object. Otherwise,
201`createClient()` accepts these arguments:
202
203* `redis.createClient([options])`
204* `redis.createClient(unix_socket[, options])`
205* `redis.createClient(redis_url[, options])`
206* `redis.createClient(port[, host][, options])`
207
208__Tip:__ If the Redis server runs on the same machine as the client consider
209using unix sockets if possible to increase throughput.
210
211#### `options` object properties
212| Property | Default | Description |
213|-----------|-----------|-------------|
214| host | 127.0.0.1 | IP address of the Redis server |
215| port | 6379 | Port of the Redis server |
216| path | null | The UNIX socket string of the Redis server |
217| url | null | The URL of the Redis server. Format: `[redis:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` (More info avaliable at [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis)). |
218| parser | javascript | __Deprecated__ Use either the built-in JS parser [`javascript`]() or the native [`hiredis`]() parser. __Note__ `node_redis` < 2.6 uses hiredis as default if installed. This changed in v.2.6.0. |
219| string_numbers | null | Set to `true`, `node_redis` will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above `Number.MAX_SAFE_INTEGER === 2^53`). Hiredis is incapable of this behavior, so setting this option to `true` will result in the built-in javascript parser being used no matter the value of the `parser` option. |
220| return_buffers | false | If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings. |
221| detect_buffers | false | If set to `true`, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. __Note__: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers. |
222| socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. |
223| no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will not respond to any commands. To work around this, `node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command indicates whether the server is ready for more commands. When ready, `node_redis` emits a `ready` event. Setting `no_ready_check` to `true` will inhibit this check. |
224| enable_offline_queue | true | By default, if there is no active connection to the Redis server, commands are added to a queue and are executed once the connection has been established. Setting `enable_offline_queue` to `false` will disable this feature and the callback will be executed immediately with an error, or an error will be emitted if no callback is specified. |
225| retry_max_delay | null | __Deprecated__ _Please use `retry_strategy` instead._ By default, every time the client tries to connect and fails, the reconnection delay almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits it to the maximum value provided in milliseconds. |
226| connect_timeout | 3600000 | __Deprecated__ _Please use `retry_strategy` instead._ Setting `connect_timeout` limits the total time for the client to connect and reconnect. The value is provided in milliseconds and is counted from the moment a new client is created or from the time the connection is lost. The last retry is going to happen exactly at the timeout time. Default is to try connecting until the default system socket timeout has been exceeded and to try reconnecting until 1h has elapsed. |
227| max_attempts | 0 | __Deprecated__ _Please use `retry_strategy` instead._ By default, a client will try reconnecting until connected. Setting `max_attempts` limits total amount of connection attempts. Setting this to 1 will prevent any reconnect attempt. |
228| retry_unfulfilled_commands | false | If set to `true`, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. `incr`). This is especially useful if you use blocking commands. |
229| password | null | If set, client will run Redis auth command on connect. Alias `auth_pass` __Note__ `node_redis` < 2.5 must use `auth_pass` |
230| db | null | If set, client will run Redis `select` command on connect. |
231| 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 on how to use the family type. |
232| disable_resubscribing | false | If set to `true`, a client won't resubscribe after disconnecting. |
233| rename_commands | null | Passing an object with renamed commands to use instead of the original functions. For example, if you renamed the command KEYS to "DO-NOT-USE" then the rename_commands object would be: `{ KEYS : "DO-NOT-USE" }` . See the [Redis security topics](http://redis.io/topics/security) for more info. |
234| tls | null | An object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback) to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel). |
235| prefix | null | A string used to prefix all used keys (e.g. `namespace:test`). Please be aware that the `keys` command will not be prefixed. The `keys` command has a "pattern" as argument and no key and it would be impossible to determine the existing keys in Redis if this would be prefixed. |
236| retry_strategy | function | A function that receives an options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below. |
237
238```js
239var redis = require("redis");
240var client = redis.createClient({detect_buffers: true});
241
242client.set("foo_rand000000000000", "OK");
243
244// This will return a JavaScript String
245client.get("foo_rand000000000000", function (err, reply) {
246 console.log(reply.toString()); // Will print `OK`
247});
248
249// This will return a Buffer since original key is specified as a Buffer
250client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
251 console.log(reply.toString()); // Will print `<Buffer 4f 4b>`
252});
253client.quit();
254```
255
256retry_strategy example
257
258```js
259var client = redis.createClient({
260 retry_strategy: function (options) {
261 if (options.error && options.error.code === 'ECONNREFUSED') {
262 // End reconnecting on a specific error and flush all commands with
263 // a individual error
264 return new Error('The server refused the connection');
265 }
266 if (options.total_retry_time > 1000 * 60 * 60) {
267 // End reconnecting after a specific timeout and flush all commands
268 // with a individual error
269 return new Error('Retry time exhausted');
270 }
271 if (options.attempt > 10) {
272 // End reconnecting with built in error
273 return undefined;
274 }
275 // reconnect after
276 return Math.min(options.attempt * 100, 3000);
277 }
278});
279```
280
281## client.auth(password[, callback])
282
283When connecting to a Redis server that requires authentication, the `AUTH`
284command must be sent as the first command after connecting. This can be tricky
285to coordinate with reconnections, the ready check, etc. To make this easier,
286`client.auth()` stashes `password` and will send it after each connection,
287including reconnections. `callback` is invoked only once, after the response to
288the very first `AUTH` command sent.
289NOTE: Your call to `client.auth()` should not be inside the ready handler. If
290you are doing this wrong, `client` will emit an error that looks
291something like this `Error: Ready check failed: ERR operation not permitted`.
292
293## backpressure
294
295### stream
296
297The client exposed the used [stream](https://nodejs.org/api/stream.html) in
298`client.stream` and if the stream or client had to
299[buffer](https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback)
300the command in `client.should_buffer`. In combination this can be used to
301implement backpressure by checking the buffer state before sending a command and
302listening to the stream
303[drain](https://nodejs.org/api/stream.html#stream_event_drain) event.
304
305## client.quit()
306
307This sends the quit command to the redis server and ends cleanly right after all
308running commands were properly handled. If this is called while reconnecting
309(and therefore no connection to the redis server exists) it is going to end the
310connection right away instead of resulting in further reconnections! All offline
311commands are going to be flushed with an error in that case.
312
313## client.end(flush)
314
315Forcibly close the connection to the Redis server. Note that this does not wait
316until all replies have been parsed. If you want to exit cleanly, call
317`client.quit()` as mentioned above.
318
319You should set flush to true, if you are not absolutely sure you do not care
320about any other commands. If you set flush to false all still running commands
321will silently fail.
322
323This example closes the connection to the Redis server before the replies have
324been read. You probably don't want to do this:
325
326```js
327var redis = require("redis"),
328 client = redis.createClient();
329
330client.set("foo_rand000000000000", "some fantastic value", function (err, reply) {
331 // This will either result in an error (flush parameter is set to true)
332 // or will silently fail and this callback will not be called at all (flush set to false)
333 console.log(err);
334});
335client.end(true); // No further commands will be processed
336client.get("foo_rand000000000000", function (err, reply) {
337 console.log(err); // => 'The connection has already been closed.'
338});
339```
340
341`client.end()` without the flush parameter set to true should NOT be used in production!
342
343## Error handling (>= v.2.6)
344
345Currently the following error subclasses exist:
346
347* `RedisError`: _All errors_ returned by the client
348* `ReplyError` subclass of `RedisError`: All errors returned by __Redis__ itself
349* `AbortError` subclass of `RedisError`: All commands that could not finish due
350 to what ever reason
351* `ParserError` subclass of `RedisError`: Returned in case of a parser error
352 (this should not happen)
353* `AggregateError` subclass of `AbortError`: Emitted in case multiple unresolved
354 commands without callback got rejected in debug_mode instead of lots of
355 `AbortError`s.
356
357All error classes are exported by the module.
358
359Example:
360```js
361var redis = require('./');
362var assert = require('assert');
363var client = redis.createClient();
364
365client.on('error', function (err) {
366 assert(err instanceof Error);
367 assert(err instanceof redis.AbortError);
368 assert(err instanceof redis.AggregateError);
369 // The set and get get aggregated in here
370 assert.strictEqual(err.errors.length, 2);
371 assert.strictEqual(err.code, 'NR_CLOSED');
372});
373client.set('foo', 123, 'bar', function (err, res) { // Too many arguments
374 assert(err instanceof redis.ReplyError); // => true
375 assert.strictEqual(err.command, 'SET');
376 assert.deepStrictEqual(err.args, ['foo', 123, 'bar']);
377
378 redis.debug_mode = true;
379 client.set('foo', 'bar');
380 client.get('foo');
381 process.nextTick(function () {
382 // Force closing the connection while the command did not yet return
383 client.end(true);
384 redis.debug_mode = false;
385 });
386});
387
388```
389
390Every `ReplyError` contains the `command` name in all-caps and the arguments (`args`).
391
392If node_redis emits a library error because of another error, the triggering
393error is added to the returned error as `origin` attribute.
394
395___Error codes___
396
397node_redis returns a `NR_CLOSED` error code if the clients connection dropped.
398If a command unresolved command got rejected a `UNCERTAIN_STATE` code is
399returned. A `CONNECTION_BROKEN` error code is used in case node_redis gives up
400to reconnect.
401
402## client.unref()
403
404Call `unref()` on the underlying socket connection to the Redis server, allowing
405the program to exit once no more commands are pending.
406
407This is an **experimental** feature, and only supports a subset of the Redis
408protocol. Any commands where client state is saved on the Redis server, e.g.
409`*SUBSCRIBE` or the blocking `BL*` commands will *NOT* work with `.unref()`.
410
411```js
412var redis = require("redis")
413var client = redis.createClient()
414
415/*
416 Calling unref() will allow this program to exit immediately after the get
417 command finishes. Otherwise the client would hang as long as the
418 client-server connection is alive.
419*/
420client.unref()
421client.get("foo", function (err, value){
422 if (err) throw(err)
423 console.log(value)
424})
425```
426
427## Friendlier hash commands
428
429Most Redis commands take a single String or an Array of Strings as arguments,
430and replies are sent back as a single String or an Array of Strings. When
431dealing with hash values, there are a couple of useful exceptions to this.
432
433### client.hgetall(hash, callback)
434
435The reply from an HGETALL command will be converted into a JavaScript Object by
436`node_redis`. That way you can interact with the responses using JavaScript
437syntax.
438
439Example:
440
441```js
442client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
443client.hgetall("hosts", function (err, obj) {
444 console.dir(obj);
445});
446```
447
448Output:
449
450```js
451{ mjr: '1', another: '23', home: '1234' }
452```
453
454### client.hmset(hash, obj[, callback])
455
456Multiple values in a hash can be set by supplying an object:
457
458```js
459client.HMSET(key2, {
460 "0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings
461 "some manner of key": "a type of value"
462});
463```
464
465The properties and values of this Object will be set as keys and values in the
466Redis hash.
467
468### client.hmset(hash, key1, val1, ... keyn, valn, [callback])
469
470Multiple values may also be set by supplying a list:
471
472```js
473client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value");
474```
475
476## Publish / Subscribe
477
478Example of the publish / subscribe API. This program opens two
479client connections, subscribes to a channel on one of them, and publishes to that
480channel on the other:
481
482```js
483var redis = require("redis");
484var sub = redis.createClient(), pub = redis.createClient();
485var msg_count = 0;
486
487sub.on("subscribe", function (channel, count) {
488 pub.publish("a nice channel", "I am sending a message.");
489 pub.publish("a nice channel", "I am sending a second message.");
490 pub.publish("a nice channel", "I am sending my last message.");
491});
492
493sub.on("message", function (channel, message) {
494 console.log("sub channel " + channel + ": " + message);
495 msg_count += 1;
496 if (msg_count === 3) {
497 sub.unsubscribe();
498 sub.quit();
499 pub.quit();
500 }
501});
502
503sub.subscribe("a nice channel");
504```
505
506When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into
507a "subscriber" mode. At that point, only commands that modify the subscription
508set are valid and quit (and depending on the redis version ping as well). When
509the subscription set is empty, the connection is put back into regular mode.
510
511If you need to send regular commands to Redis while in subscriber mode, just
512open another connection with a new client (hint: use `client.duplicate()`).
513
514## Subscriber Events
515
516If a client has subscriptions active, it may emit these events:
517
518### "message" (channel, message)
519
520Client will emit `message` for every message received that matches an active subscription.
521Listeners are passed the channel name as `channel` and the message as `message`.
522
523### "pmessage" (pattern, channel, message)
524
525Client will emit `pmessage` for every message received that matches an active
526subscription pattern. Listeners are passed the original pattern used with
527`PSUBSCRIBE` as `pattern`, the sending channel name as `channel`, and the
528message as `message`.
529
530### "message_buffer" (channel, message)
531
532This is the same as the `message` event with the exception, that it is always
533going to emit a buffer. If you listen to the `message` event at the same time as
534the `message_buffer`, it is always going to emit a string.
535
536### "pmessage_buffer" (pattern, channel, message)
537
538This is the same as the `pmessage` event with the exception, that it is always
539going to emit a buffer. If you listen to the `pmessage` event at the same time
540as the `pmessage_buffer`, it is always going to emit a string.
541
542### "subscribe" (channel, count)
543
544Client will emit `subscribe` in response to a `SUBSCRIBE` command. Listeners are
545passed the channel name as `channel` and the new count of subscriptions for this
546client as `count`.
547
548### "psubscribe" (pattern, count)
549
550Client will emit `psubscribe` in response to a `PSUBSCRIBE` command. Listeners
551are passed the original pattern as `pattern`, and the new count of subscriptions
552for this client as `count`.
553
554### "unsubscribe" (channel, count)
555
556Client will emit `unsubscribe` in response to a `UNSUBSCRIBE` command. Listeners
557are passed the channel name as `channel` and the new count of subscriptions for
558this client as `count`. When `count` is 0, this client has left subscriber mode
559and no more subscriber events will be emitted.
560
561### "punsubscribe" (pattern, count)
562
563Client will emit `punsubscribe` in response to a `PUNSUBSCRIBE` command.
564Listeners are passed the channel name as `channel` and the new count of
565subscriptions for this client as `count`. When `count` is 0, this client has
566left subscriber mode and no more subscriber events will be emitted.
567
568## client.multi([commands])
569
570`MULTI` commands are queued up until an `EXEC` is issued, and then all commands
571are run atomically by Redis. The interface in `node_redis` is to return an
572individual `Multi` object by calling `client.multi()`. If any command fails to
573queue, all commands are rolled back and none is going to be executed (For
574further information look at
575[transactions](http://redis.io/topics/transactions)).
576
577```js
578var redis = require("./index"),
579 client = redis.createClient(), set_size = 20;
580
581client.sadd("bigset", "a member");
582client.sadd("bigset", "another member");
583
584while (set_size > 0) {
585 client.sadd("bigset", "member " + set_size);
586 set_size -= 1;
587}
588
589// multi chain with an individual callback
590client.multi()
591 .scard("bigset")
592 .smembers("bigset")
593 .keys("*", function (err, replies) {
594 // NOTE: code in this callback is NOT atomic
595 // this only happens after the the .exec call finishes.
596 client.mget(replies, redis.print);
597 })
598 .dbsize()
599 .exec(function (err, replies) {
600 console.log("MULTI got " + replies.length + " replies");
601 replies.forEach(function (reply, index) {
602 console.log("Reply " + index + ": " + reply.toString());
603 });
604 });
605```
606
607### Multi.exec([callback])
608
609`client.multi()` is a constructor that returns a `Multi` object. `Multi` objects
610share all of the same command methods as `client` objects do. Commands are
611queued up inside the `Multi` object until `Multi.exec()` is invoked.
612
613If your code contains an syntax error an EXECABORT error is going to be thrown
614and all commands are going to be aborted. That error contains a `.errors`
615property that contains the concrete errors.
616If all commands were queued successfully and an error is thrown by redis while
617processing the commands that error is going to be returned in the result array!
618No other command is going to be aborted though than the onces failing.
619
620You can either chain together `MULTI` commands as in the above example, or you
621can queue individual commands while still sending regular client command as in
622this example:
623
624```js
625var redis = require("redis"),
626 client = redis.createClient(), multi;
627
628// start a separate multi command queue
629multi = client.multi();
630multi.incr("incr thing", redis.print);
631multi.incr("incr other thing", redis.print);
632
633// runs immediately
634client.mset("incr thing", 100, "incr other thing", 1, redis.print);
635
636// drains multi queue and runs atomically
637multi.exec(function (err, replies) {
638 console.log(replies); // 101, 2
639});
640```
641
642In addition to adding commands to the `MULTI` queue individually, you can also
643pass an array of commands and arguments to the constructor:
644
645```js
646var redis = require("redis"),
647 client = redis.createClient(), multi;
648
649client.multi([
650 ["mget", "multifoo", "multibar", redis.print],
651 ["incr", "multifoo"],
652 ["incr", "multibar"]
653]).exec(function (err, replies) {
654 console.log(replies);
655});
656```
657
658### Multi.exec_atomic([callback])
659
660Identical to Multi.exec but with the difference that executing a single command
661will not use transactions.
662
663## client.batch([commands])
664
665Identical to .multi without transactions. This is recommended if you want to
666execute many commands at once but don't have to rely on transactions.
667
668`BATCH` commands are queued up until an `EXEC` is issued, and then all commands
669are run atomically by Redis. The interface in `node_redis` is to return an
670individual `Batch` object by calling `client.batch()`. The only difference
671between .batch and .multi is that no transaction is going to be used.
672Be aware that the errors are - just like in multi statements - in the result.
673Otherwise both, errors and results could be returned at the same time.
674
675If you fire many commands at once this is going to boost the execution speed
676significantly compared to firing the same commands in a loop without waiting for
677the result! See the benchmarks for further comparison. Please remember that all
678commands are kept in memory until they are fired.
679
680## Monitor mode
681
682Redis supports the `MONITOR` command, which lets you see all commands received
683by the Redis server across all client connections, including from other client
684libraries and other computers.
685
686A `monitor` event is going to be emitted for every command fired from any client
687connected to the server including the monitoring client itself. The callback for
688the `monitor` event takes a timestamp from the Redis server, an array of command
689arguments and the raw monitoring string.
690
691Example:
692
693```js
694var client = require("redis").createClient();
695client.monitor(function (err, res) {
696 console.log("Entering monitoring mode.");
697});
698client.set('foo', 'bar');
699
700client.on("monitor", function (time, args, raw_reply) {
701 console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar']
702});
703```
704
705# Extras
706
707Some other things you might like to know about.
708
709## client.server_info
710
711After the ready probe completes, the results from the INFO command are saved in
712the `client.server_info` object.
713
714The `versions` key contains an array of the elements of the version string for
715easy comparison.
716
717 > client.server_info.redis_version
718 '2.3.0'
719 > client.server_info.versions
720 [ 2, 3, 0 ]
721
722## redis.print()
723
724A handy callback function for displaying return values when testing. Example:
725
726```js
727var redis = require("redis"),
728 client = redis.createClient();
729
730client.on("connect", function () {
731 client.set("foo_rand000000000000", "some fantastic value", redis.print);
732 client.get("foo_rand000000000000", redis.print);
733});
734```
735
736This will print:
737
738 Reply: OK
739 Reply: some fantastic value
740
741Note that this program will not exit cleanly because the client is still connected.
742
743## Multi-word commands
744
745To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass
746the second word as first parameter:
747
748 client.script('load', 'return 1');
749 client.multi().script('load', 'return 1').exec(...);
750 client.multi([['script', 'load', 'return 1']]).exec(...);
751
752## client.duplicate([options][, callback])
753
754Duplicate all current options and return a new redisClient instance. All options
755passed to the duplicate function are going to replace the original option. If
756you pass a callback, duplicate is going to wait until the client is ready and
757returns it in the callback. If an error occurs in the meanwhile, that is going
758to return an error instead in the callback.
759
760One example of when to use duplicate() would be to accommodate the connection-
761blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands
762are used on the same redisClient instance as non-blocking commands, the
763non-blocking ones may be queued up until after the blocking ones finish.
764
765 var Redis=require('redis');
766 var client = Redis.createClient();
767 var clientBlocking = client.duplicate();
768
769 var get = function() {
770 console.log("get called");
771 client.get("any_key",function() { console.log("get returned"); });
772 setTimeout( get, 1000 );
773 };
774 var brpop = function() {
775 console.log("brpop called");
776 clientBlocking.brpop("nonexistent", 5, function() {
777 console.log("brpop return");
778 setTimeout( brpop, 1000 );
779 });
780 };
781 get();
782 brpop();
783
784Another reason to use duplicate() is when multiple DBs on the same server are
785accessed via the redis SELECT command. Each DB could use its own connection.
786
787## client.send_command(command_name[, [args][, callback]])
788
789All Redis commands have been added to the `client` object. However, if new
790commands are introduced before this library is updated or if you want to add
791individual commands you can use `send_command()` to send arbitrary commands to
792Redis.
793
794All commands are sent as multi-bulk commands. `args` can either be an Array of
795arguments, or omitted / set to undefined.
796
797## client.add_command(command_name)
798
799Calling add_command will add a new command to the prototype. The exact command
800name will be used when calling using this new command. Using arbitrary arguments
801is possible as with any other command.
802
803## client.connected
804
805Boolean tracking the state of the connection to the Redis server.
806
807## client.command_queue_length
808
809The number of commands that have been sent to the Redis server but not yet
810replied to. You can use this to enforce some kind of maximum queue depth for
811commands while connected.
812
813## client.offline_queue_length
814
815The number of commands that have been queued up for a future connection. You can
816use this to enforce some kind of maximum queue depth for pre-connection
817commands.
818
819### Commands with Optional and Keyword arguments
820
821This applies to anything that uses an optional `[WITHSCORES]` or `[LIMIT offset
822count]` in the [redis.io/commands](http://redis.io/commands) documentation.
823
824Example:
825
826```js
827var args = [ 'myzset', 1, 'one', 2, 'two', 3, 'three', 99, 'ninety-nine' ];
828client.zadd(args, function (err, response) {
829 if (err) throw err;
830 console.log('added '+response+' items.');
831
832 // -Infinity and +Infinity also work
833 var args1 = [ 'myzset', '+inf', '-inf' ];
834 client.zrevrangebyscore(args1, function (err, response) {
835 if (err) throw err;
836 console.log('example1', response);
837 // write your code here
838 });
839
840 var max = 3, min = 1, offset = 1, count = 2;
841 var args2 = [ 'myzset', max, min, 'WITHSCORES', 'LIMIT', offset, count ];
842 client.zrevrangebyscore(args2, function (err, response) {
843 if (err) throw err;
844 console.log('example2', response);
845 // write your code here
846 });
847});
848```
849
850## Performance
851
852Much effort has been spent to make `node_redis` as fast as possible for common
853operations.
854
855```
856Lenovo T450s, i7-5600U and 12gb memory
857clients: 1, NodeJS: 6.2.0, Redis: 3.2.0, parser: javascript, connected by: tcp
858 PING, 1/1 avg/max: 0.02/ 5.26 2501ms total, 46916 ops/sec
859 PING, batch 50/1 avg/max: 0.06/ 4.35 2501ms total, 755178 ops/sec
860 SET 4B str, 1/1 avg/max: 0.02/ 4.75 2501ms total, 40856 ops/sec
861 SET 4B str, batch 50/1 avg/max: 0.11/ 1.51 2501ms total, 432727 ops/sec
862 SET 4B buf, 1/1 avg/max: 0.05/ 2.76 2501ms total, 20659 ops/sec
863 SET 4B buf, batch 50/1 avg/max: 0.25/ 1.76 2501ms total, 194962 ops/sec
864 GET 4B str, 1/1 avg/max: 0.02/ 1.55 2501ms total, 45156 ops/sec
865 GET 4B str, batch 50/1 avg/max: 0.09/ 3.15 2501ms total, 524110 ops/sec
866 GET 4B buf, 1/1 avg/max: 0.02/ 3.07 2501ms total, 44563 ops/sec
867 GET 4B buf, batch 50/1 avg/max: 0.10/ 3.18 2501ms total, 473171 ops/sec
868 SET 4KiB str, 1/1 avg/max: 0.03/ 1.54 2501ms total, 32627 ops/sec
869 SET 4KiB str, batch 50/1 avg/max: 0.34/ 1.89 2501ms total, 146861 ops/sec
870 SET 4KiB buf, 1/1 avg/max: 0.05/ 2.85 2501ms total, 20688 ops/sec
871 SET 4KiB buf, batch 50/1 avg/max: 0.36/ 1.83 2501ms total, 138165 ops/sec
872 GET 4KiB str, 1/1 avg/max: 0.02/ 1.37 2501ms total, 39389 ops/sec
873 GET 4KiB str, batch 50/1 avg/max: 0.24/ 1.81 2501ms total, 208157 ops/sec
874 GET 4KiB buf, 1/1 avg/max: 0.02/ 2.63 2501ms total, 39918 ops/sec
875 GET 4KiB buf, batch 50/1 avg/max: 0.31/ 8.56 2501ms total, 161575 ops/sec
876 INCR, 1/1 avg/max: 0.02/ 4.69 2501ms total, 45685 ops/sec
877 INCR, batch 50/1 avg/max: 0.09/ 3.06 2501ms total, 539964 ops/sec
878 LPUSH, 1/1 avg/max: 0.02/ 3.04 2501ms total, 41253 ops/sec
879 LPUSH, batch 50/1 avg/max: 0.12/ 1.94 2501ms total, 425090 ops/sec
880 LRANGE 10, 1/1 avg/max: 0.02/ 2.28 2501ms total, 39850 ops/sec
881 LRANGE 10, batch 50/1 avg/max: 0.25/ 1.85 2501ms total, 194302 ops/sec
882 LRANGE 100, 1/1 avg/max: 0.05/ 2.93 2501ms total, 21026 ops/sec
883 LRANGE 100, batch 50/1 avg/max: 1.52/ 2.89 2501ms total, 32767 ops/sec
884 SET 4MiB str, 1/1 avg/max: 5.16/ 15.55 2502ms total, 193 ops/sec
885 SET 4MiB str, batch 20/1 avg/max: 89.73/ 99.96 2513ms total, 223 ops/sec
886 SET 4MiB buf, 1/1 avg/max: 2.23/ 8.35 2501ms total, 446 ops/sec
887 SET 4MiB buf, batch 20/1 avg/max: 41.47/ 50.91 2530ms total, 482 ops/sec
888 GET 4MiB str, 1/1 avg/max: 2.79/ 10.91 2502ms total, 358 ops/sec
889 GET 4MiB str, batch 20/1 avg/max: 101.61/118.11 2541ms total, 197 ops/sec
890 GET 4MiB buf, 1/1 avg/max: 2.32/ 14.93 2502ms total, 430 ops/sec
891 GET 4MiB buf, batch 20/1 avg/max: 65.01/ 84.72 2536ms total, 308 ops/sec
892 ```
893
894## Debugging
895
896To get debug output run your `node_redis` application with `NODE_DEBUG=redis`.
897
898This is also going to result in good stack traces opposed to useless ones
899otherwise for any async operation.
900If you only want to have good stack traces but not the debug output run your
901application in development mode instead (`NODE_ENV=development`).
902
903Good stack traces are only activated in development and debug mode as this
904results in a significant performance penalty.
905
906___Comparison___:
907Useless stack trace:
908```
909ReplyError: ERR wrong number of arguments for 'set' command
910 at parseError (/home/ruben/repos/redis/node_modules/redis-parser/lib/parser.js:158:12)
911 at parseType (/home/ruben/repos/redis/node_modules/redis-parser/lib/parser.js:219:14)
912```
913Good stack trace:
914```
915ReplyError: ERR wrong number of arguments for 'set' command
916 at new Command (/home/ruben/repos/redis/lib/command.js:9:902)
917 at RedisClient.set (/home/ruben/repos/redis/lib/commands.js:9:3238)
918 at Context.<anonymous> (/home/ruben/repos/redis/test/good_stacks.spec.js:20:20)
919 at callFnAsync (/home/ruben/repos/redis/node_modules/mocha/lib/runnable.js:349:8)
920 at Test.Runnable.run (/home/ruben/repos/redis/node_modules/mocha/lib/runnable.js:301:7)
921 at Runner.runTest (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:422:10)
922 at /home/ruben/repos/redis/node_modules/mocha/lib/runner.js:528:12
923 at next (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:342:14)
924 at /home/ruben/repos/redis/node_modules/mocha/lib/runner.js:352:7
925 at next (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:284:14)
926 at Immediate._onImmediate (/home/ruben/repos/redis/node_modules/mocha/lib/runner.js:320:5)
927 at processImmediate [as _immediateCallback] (timers.js:383:17)
928```
929
930## How to Contribute
931- Open a pull request or an issue about what you want to implement / change. We're glad for any help!
932 - Please be aware that we'll only accept fully tested code.
933
934## Contributors
935
936The original author of node_redis is [Matthew Ranney](https://github.com/mranney)
937
938The current lead maintainer is [Ruben Bridgewater](https://github.com/BridgeAR)
939
940Many [others](https://github.com/NodeRedis/node_redis/graphs/contributors)
941contributed to `node_redis` too. Thanks to all of them!
942
943## License
944
945[MIT](LICENSE)
946
947### Consolidation: It's time for celebration
948
949Right now there are two great redis clients around and both have some advantages
950above each other. We speak about ioredis and node_redis. So after talking to
951each other about how we could improve in working together we (that is @luin and
952@BridgeAR) decided to work towards a single library on the long run. But step by
953step.
954
955First of all, we want to split small parts of our libraries into others so that
956we're both able to use the same code. Those libraries are going to be maintained
957under the NodeRedis organization. This is going to reduce the maintenance
958overhead, allows others to use the very same code, if they need it and it's way
959easyer for others to contribute to both libraries.
960
961We're very happy about this step towards working together as we both want to
962give you the best redis experience possible.
963
964If you want to join our cause by help maintaining something, please don't
965hesitate to contact either one of us.
966
\No newline at end of file