You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Merge remote branch 'tj/features/multi'
Conflicts: test.js
This commit is contained in:
67
index.js
67
index.js
@@ -662,8 +662,9 @@ exports.commands = [
|
|||||||
"INFO", "MONITOR", "SLAVEOF", "CONFIG",
|
"INFO", "MONITOR", "SLAVEOF", "CONFIG",
|
||||||
// Publish/Subscribe
|
// Publish/Subscribe
|
||||||
"PUBLISH", "SUBSCRIBE", "PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE",
|
"PUBLISH", "SUBSCRIBE", "PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE",
|
||||||
|
"MULTI", "EXEC",
|
||||||
// Undocumented commands
|
// Undocumented commands
|
||||||
"PING"
|
"PING",
|
||||||
];
|
];
|
||||||
|
|
||||||
exports.commands.forEach(function (command) {
|
exports.commands.forEach(function (command) {
|
||||||
@@ -680,46 +681,42 @@ exports.commands.forEach(function (command) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Transactions - "DISCARD", "WATCH", "UNWATCH",
|
function Multi(client) {
|
||||||
|
this.client = client;
|
||||||
|
this.queue = [];
|
||||||
|
this.queue.push(['MULTI']);
|
||||||
|
}
|
||||||
|
|
||||||
RedisClient.prototype.multi = function (commands, callback) {
|
exports.commands.forEach(function (command) {
|
||||||
var self = this;
|
Multi.prototype[command.toLowerCase()] = function () {
|
||||||
|
var args = to_array(arguments);
|
||||||
|
args.unshift(command); // put command at the beginning
|
||||||
|
this.queue.push(args);
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
this.send_command("MULTI", function (err, reply) {
|
Multi.prototype.exec = function(fn){
|
||||||
|
var done;
|
||||||
|
this.queue.push(['EXEC']);
|
||||||
|
this.queue.forEach(function(args){
|
||||||
|
var command = args.shift();
|
||||||
|
this.client[command](args, function(err, reply){
|
||||||
|
if (done) return;
|
||||||
if (err) {
|
if (err) {
|
||||||
console.warn("Error starting MULTI request: " + err.stack);
|
done = true;
|
||||||
}
|
fn(new Error(err));
|
||||||
});
|
} else if ('EXEC' == command) {
|
||||||
commands.forEach(function (args, command_num) {
|
done = true;
|
||||||
self.send_command(args[0], args[1], function (err, reply) {
|
fn(null, reply);
|
||||||
if (err) {
|
|
||||||
args[2](err);
|
|
||||||
commands.splice(command_num, 1); // what if this runs before all commands are sent?
|
|
||||||
} else {
|
|
||||||
if (reply !== "QUEUED") {
|
|
||||||
console.warn("Unexpected MULTI reply: " + reply + " instead of 'QUEUED'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
this.send_command("EXEC", function (err, replies) {
|
|
||||||
replies.forEach(function (reply, reply_num) {
|
|
||||||
if (typeof commands[reply_num][2] === "function") {
|
|
||||||
commands[reply_num][2](null, reply);
|
|
||||||
} else {
|
|
||||||
if (exports.debug_mode) {
|
|
||||||
console.log("no callback for multi response " + reply_num + ", skipping.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (typeof callback === "function") {
|
|
||||||
callback(replies);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}, this);
|
||||||
};
|
};
|
||||||
RedisClient.prototype.MULTI = function (commands) {
|
|
||||||
return this.multi(commands);
|
RedisClient.prototype.__defineGetter__('multi', function(){
|
||||||
};
|
return new Multi(this);
|
||||||
|
});
|
||||||
|
|
||||||
exports.createClient = function (port_arg, host_arg, options) {
|
exports.createClient = function (port_arg, host_arg, options) {
|
||||||
var port = port_arg || default_port,
|
var port = port_arg || default_port,
|
||||||
|
35
test.js
35
test.js
@@ -343,8 +343,6 @@ tests.SETEX = function () {
|
|||||||
client.ttl(["setex key"], last(name, require_number_pos(name)));
|
client.ttl(["setex key"], last(name, require_number_pos(name)));
|
||||||
};
|
};
|
||||||
|
|
||||||
// plenty of tests of MSET already
|
|
||||||
|
|
||||||
tests.MSETNX = function () {
|
tests.MSETNX = function () {
|
||||||
var name = "MSETNX";
|
var name = "MSETNX";
|
||||||
client.mset(["mset1", "val1", "mset2", "val2", "mset3", "val3"], require_string("OK", name));
|
client.mset(["mset1", "val1", "mset2", "val2", "mset3", "val3"], require_string("OK", name));
|
||||||
@@ -355,6 +353,39 @@ tests.MSETNX = function () {
|
|||||||
client.exists(["mset4"], last(name, require_number(1, name)));
|
client.exists(["mset4"], last(name, require_number(1, name)));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
tests.MULTI_4 = function () {
|
||||||
|
var name = "MULTI_4";
|
||||||
|
|
||||||
|
client.multi
|
||||||
|
.mset('some', '10', 'keys', '20')
|
||||||
|
.incr('some')
|
||||||
|
.incr('keys')
|
||||||
|
.mget('some', 'keys')
|
||||||
|
.exec(function(err, replies){
|
||||||
|
assert.strictEqual(null, err);
|
||||||
|
assert.equal('OK', replies[0]);
|
||||||
|
assert.equal(11, replies[1]);
|
||||||
|
assert.equal(21, replies[2]);
|
||||||
|
assert.equal(11, replies[3][0].toString());
|
||||||
|
assert.equal(21, replies[3][1].toString());
|
||||||
|
next(name);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
tests.MULTI_ERROR = function () {
|
||||||
|
var name = "MULTI_ERROR";
|
||||||
|
|
||||||
|
client
|
||||||
|
.multi
|
||||||
|
.set('something', 'amazing')
|
||||||
|
.set('invalid')
|
||||||
|
.exec(function(err, replies){
|
||||||
|
assert.equal("ERR wrong number of arguments for 'set' command", err.message);
|
||||||
|
assert.strictEqual(undefined, replies);
|
||||||
|
next(name);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
tests.HGETALL = function () {
|
tests.HGETALL = function () {
|
||||||
var name = "HGETALL";
|
var name = "HGETALL";
|
||||||
client.hmset(["hosts", "mjr", "1", "another", "23", "home", "1234"], require_string("OK", name));
|
client.hmset(["hosts", "mjr", "1", "another", "23", "home", "1234"], require_string("OK", name));
|
||||||
|
Reference in New Issue
Block a user