Skip to content

Commit d8d0e2d

Browse files
authored
Merge pull request #1031 from BrianRossmajer/patch-1
Noting in documentation when client.duplicate() might be used
2 parents 782f255 + 983cc69 commit d8d0e2d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,33 @@ the second word as first parameter:
658658
Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option.
659659
If you pass a callback, duplicate is going to wait until the client is ready and returns it in the callback. If an error occurs in the meanwhile, that is going to return an error instead in the callback.
660660

661+
One example of when to use duplicate() would be to accomodate the connection-
662+
blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands
663+
are used on the same redisClient instance as non-blocking commands, the
664+
non-blocking ones may be queued up until after the blocking ones finish.
665+
666+
var Redis=require('redis');
667+
var client = Redis.createClient();
668+
var clientBlocking = client.duplicate();
669+
670+
var get = function() {
671+
console.log("get called");
672+
client.get("any_key",function() { console.log("get returned"); });
673+
setTimeout( get, 1000 );
674+
};
675+
var brpop = function() {
676+
console.log("brpop called");
677+
clientBlocking.brpop("nonexistent", 5, function() {
678+
console.log("brpop return");
679+
setTimeout( brpop, 1000 );
680+
});
681+
};
682+
get();
683+
brpop();
684+
685+
Another reason to use duplicate() is when multiple DBs on the same server are
686+
accessed via the redis SELECT command. Each DB could use its own connection.
687+
661688
## client.send_command(command_name[, [args][, callback]])
662689

663690
All Redis commands have been added to the `client` object. However, if new commands are introduced before this library is updated,

0 commit comments

Comments
 (0)