You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Support new and imrpoved MULTI syntax.
This commit is contained in:
63
index.js
63
index.js
@@ -681,42 +681,69 @@ exports.commands.forEach(function (command) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
function Multi(client) {
|
function Multi(client, args) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.queue = [];
|
this.queue = [["MULTI"]];
|
||||||
this.queue.push(['MULTI']);
|
if (Array.isArray(args)) {
|
||||||
|
this.queue = this.queue.concat(args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.commands.forEach(function (command) {
|
exports.commands.forEach(function (command) {
|
||||||
Multi.prototype[command.toLowerCase()] = function () {
|
Multi.prototype[command.toLowerCase()] = function () {
|
||||||
var args = to_array(arguments);
|
var args = to_array(arguments);
|
||||||
args.unshift(command); // put command at the beginning
|
args.unshift(command);
|
||||||
this.queue.push(args);
|
this.queue.push(args);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
Multi.prototype.exec = function(fn){
|
Multi.prototype.exec = function(callback) {
|
||||||
var done;
|
var done = false, self = this;
|
||||||
this.queue.push(['EXEC']);
|
|
||||||
this.queue.forEach(function(args){
|
// drain queue, callback will catch "QUEUED" or error
|
||||||
|
this.queue.forEach(function(args, index) {
|
||||||
var command = args.shift();
|
var command = args.shift();
|
||||||
this.client[command](args, function(err, reply){
|
if (typeof args[args.length - 1] === "function") {
|
||||||
if (done) return;
|
args = args.slice(0, -1);
|
||||||
|
}
|
||||||
|
this.client[command](args, function (err, reply){
|
||||||
if (err) {
|
if (err) {
|
||||||
done = true;
|
var cur = self.queue[index];
|
||||||
fn(new Error(err));
|
if (typeof cur[cur.length -1] === "function") {
|
||||||
} else if ('EXEC' == command) {
|
cur[cur.length - 1](err);
|
||||||
done = true;
|
} else {
|
||||||
fn(null, reply);
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
self.queue.splice(index, 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
this.client.EXEC(function (err, reply) {
|
||||||
|
if (err) {
|
||||||
|
if (callback) {
|
||||||
|
callback(new Error(err));
|
||||||
|
} else {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.queue.slice(1).forEach(function (args, index) {
|
||||||
|
if (typeof args[args.length - 1] === "function") {
|
||||||
|
args[args.length - 1](null, reply[index]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
callback(null, reply);
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
RedisClient.prototype.__defineGetter__('multi', function(){
|
RedisClient.prototype.multi = function (args) {
|
||||||
return new Multi(this);
|
return new Multi(this, args);
|
||||||
});
|
};
|
||||||
|
|
||||||
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,
|
||||||
|
Reference in New Issue
Block a user