Skip to content

Commit 6711c94

Browse files
author
Ruben Bridgewater
committed
Add duplicate function to duplicate the current client instance
Fixes #919
1 parent ac9ff9a commit 6711c94

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ the second word as first parameter:
572572
client.multi().script('load', 'return 1').exec(...);
573573
client.multi([['script', 'load', 'return 1']]).exec(...);
574574

575+
## client.duplicate([options])
576+
577+
Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option.
578+
575579
## client.send_command(command_name[, [args][, callback]])
576580

577581
Used internally to send commands to Redis. Nearly all Redis commands have been added to the `client` object.

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ RedisClient.prototype.create_stream = function () {
135135
RedisClient.prototype.cork = noop;
136136
RedisClient.prototype.uncork = noop;
137137

138+
RedisClient.prototype.duplicate = function (options) {
139+
var existing_options = clone(this.options);
140+
options = clone(options);
141+
for (var elem in options) { // jshint ignore: line
142+
existing_options[elem] = options[elem];
143+
}
144+
return new RedisClient(existing_options);
145+
};
146+
138147
RedisClient.prototype.initialize_retry_vars = function () {
139148
this.retry_timer = null;
140149
this.retry_totaltime = 0;

test/node_redis.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,45 @@ describe("The node_redis client", function () {
4242
});
4343
});
4444

45+
describe('duplicate', function () {
46+
it('check if all options got copied properly', function(done) {
47+
var client2 = client.duplicate();
48+
assert(client.connected);
49+
assert(!client2.connected);
50+
for (var elem in client.options) {
51+
if (client.options.hasOwnProperty(elem)) {
52+
assert.strictEqual(client2.options[elem], client.options[elem]);
53+
}
54+
}
55+
client2.on('ready', function () {
56+
client2.end(true);
57+
done();
58+
});
59+
});
60+
61+
it('check if all new options replaced the old ones', function(done) {
62+
var client2 = client.duplicate({
63+
no_ready_check: true
64+
});
65+
assert(client.connected);
66+
assert(!client2.connected);
67+
assert.strictEqual(client.options.no_ready_check, undefined);
68+
assert.strictEqual(client2.options.no_ready_check, true);
69+
assert.notDeepEqual(client.options, client2.options);
70+
for (var elem in client.options) {
71+
if (client.options.hasOwnProperty(elem)) {
72+
if (elem !== 'no_ready_check') {
73+
assert.strictEqual(client2.options[elem], client.options[elem]);
74+
}
75+
}
76+
}
77+
client2.on('ready', function () {
78+
client2.end(true);
79+
done();
80+
});
81+
});
82+
});
83+
4584
describe('big data', function () {
4685

4786
// Check if the fast mode for big strings is working correct

0 commit comments

Comments
 (0)