1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Implemented alternate MULTI syntax

Usage:

  client
    .multi
    .set(foo, bar)
    .set(counter, 1)
    .incr(counter)
    .exec(function(err, replies){});
This commit is contained in:
Tj Holowaychuk
2010-09-22 18:38:55 -07:00
parent 76ecd927cd
commit 91ebe64dae
2 changed files with 58 additions and 58 deletions

View File

@@ -658,8 +658,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) {
@@ -676,47 +677,43 @@ 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){
if (err) { var done;
console.warn("Error starting MULTI request: " + err.stack); this.queue.push(['EXEC']);
} this.queue.forEach(function(args){
}); var command = args.shift();
commands.forEach(function (args, command_num) { this.client[command](args, function(err, reply){
self.send_command(args[0], args[1], function (err, reply) { if (done) return;
if (err) { if (err) {
args[2](err); done = true;
commands.splice(command_num, 1); // what if this runs before all commands are sent? fn(new Error(err));
} else { } else if ('EXEC' == command) {
if (reply !== "QUEUED") { done = true;
console.warn("Unexpected MULTI reply: " + reply + " instead of 'QUEUED'"); fn(null, reply);
}
} }
}); });
}); }, this);
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);
}
});
};
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,
host = host_arg || default_host, host = host_arg || default_host,

49
test.js
View File

@@ -339,30 +339,33 @@ tests.MSETNX = function () {
tests.MULTI = function () { tests.MULTI = function () {
var name = "MULTI"; var name = "MULTI";
client.multi([
["mset", ["multifoo", "10", "multibar", "20"], require_string("OK", name)],
["set", ["foo2"], require_error(name)],
["incr", ["multifoo"], require_number(11, name)],
["incr", ["multibar"], require_number(21, name)]
]);
client.multi([ client
["incr", ["multibar"], require_number(22, name)], .multi
["incr", ["multifoo"], require_number(12, name)] .mset('some', '10', 'keys', '20')
]); .incr('some')
.incr('keys')
client.multi([ .exec(function(err, replies){
["mget", ["multifoo", "multibar"], function (err, res) { assert.strictEqual(null, err);
assert.strictEqual(2, res.length, name); assert.equal('OK', replies[0]);
assert.strictEqual("12", res[0].toString(), name); assert.equal(11, replies[1]);
assert.strictEqual("22", res[1].toString(), name); assert.equal(21, replies[2]);
}], next(name);
["set", ["foo2"], require_error(name)], });
["incr", ["multifoo"], require_number(13, name)], };
["incr", ["multibar"], require_number(23, name)]
], function (reply) { tests.MULTI_ERROR = function () {
next(name); 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 () {