You've already forked node-redis
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:
67
index.js
67
index.js
@@ -658,8 +658,9 @@ exports.commands = [
|
||||
"INFO", "MONITOR", "SLAVEOF", "CONFIG",
|
||||
// Publish/Subscribe
|
||||
"PUBLISH", "SUBSCRIBE", "PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE",
|
||||
"MULTI", "EXEC",
|
||||
// Undocumented commands
|
||||
"PING"
|
||||
"PING",
|
||||
];
|
||||
|
||||
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) {
|
||||
var self = this;
|
||||
exports.commands.forEach(function (command) {
|
||||
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) {
|
||||
if (err) {
|
||||
console.warn("Error starting MULTI request: " + err.stack);
|
||||
}
|
||||
});
|
||||
commands.forEach(function (args, command_num) {
|
||||
self.send_command(args[0], args[1], 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) {
|
||||
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'");
|
||||
}
|
||||
done = true;
|
||||
fn(new Error(err));
|
||||
} else if ('EXEC' == command) {
|
||||
done = true;
|
||||
fn(null, reply);
|
||||
}
|
||||
});
|
||||
});
|
||||
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);
|
||||
}, this);
|
||||
};
|
||||
|
||||
RedisClient.prototype.__defineGetter__('multi', function(){
|
||||
return new Multi(this);
|
||||
});
|
||||
|
||||
exports.createClient = function (port_arg, host_arg, options) {
|
||||
var port = port_arg || default_port,
|
||||
host = host_arg || default_host,
|
||||
|
49
test.js
49
test.js
@@ -339,30 +339,33 @@ tests.MSETNX = function () {
|
||||
|
||||
tests.MULTI = function () {
|
||||
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([
|
||||
["incr", ["multibar"], require_number(22, name)],
|
||||
["incr", ["multifoo"], require_number(12, name)]
|
||||
]);
|
||||
|
||||
client.multi([
|
||||
["mget", ["multifoo", "multibar"], function (err, res) {
|
||||
assert.strictEqual(2, res.length, name);
|
||||
assert.strictEqual("12", res[0].toString(), name);
|
||||
assert.strictEqual("22", res[1].toString(), name);
|
||||
}],
|
||||
["set", ["foo2"], require_error(name)],
|
||||
["incr", ["multifoo"], require_number(13, name)],
|
||||
["incr", ["multibar"], require_number(23, name)]
|
||||
], function (reply) {
|
||||
next(name);
|
||||
});
|
||||
client
|
||||
.multi
|
||||
.mset('some', '10', 'keys', '20')
|
||||
.incr('some')
|
||||
.incr('keys')
|
||||
.exec(function(err, replies){
|
||||
assert.strictEqual(null, err);
|
||||
assert.equal('OK', replies[0]);
|
||||
assert.equal(11, replies[1]);
|
||||
assert.equal(21, replies[2]);
|
||||
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 () {
|
||||
|
Reference in New Issue
Block a user