You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
Update documentation for awesome new MULTI syntax.
This commit is contained in:
86
README.md
86
README.md
@@ -212,45 +212,79 @@ Client will emit `punsubscribe` in response to a `PUNSUBSCRIBE` command. Listen
|
|||||||
channel name as `channel` and the new count of subscriptions for this client as `count`. When
|
channel name as `channel` and the new count of subscriptions for this client as `count`. When
|
||||||
`count` is 0, this client has left pub/sub mode and no more pub/sub events will be emitted.
|
`count` is 0, this client has left pub/sub mode and no more pub/sub events will be emitted.
|
||||||
|
|
||||||
## client.multi(commands, callback)
|
## client.multi([commands])
|
||||||
|
|
||||||
`MULTI` is supported. The syntax is a little awkward, but it works:
|
`MULTI` commands are queued up until an `EXEC` is issued, and then all commands are run atomically by
|
||||||
|
Redis. The interface in `node_redis` is to return an individual `Multi` object by calling `client.multi()`.
|
||||||
|
|
||||||
var redis = require("./index"),
|
var redis = require("./index"),
|
||||||
client = redis.createClient(), set_size = 20;
|
client = redis.createClient(), set_size = 20;
|
||||||
|
|
||||||
|
client.sadd("bigset", "a member");
|
||||||
|
client.sadd("bigset", "another member");
|
||||||
|
|
||||||
while (set_size > 0) {
|
while (set_size > 0) {
|
||||||
client.sadd("bigset", "member " + set_size);
|
client.sadd("bigset", "member " + set_size);
|
||||||
set_size -= 1;
|
set_size -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.MULTI([
|
// multi chain with an individual callback
|
||||||
["scard", ["bigset"], function (err, res) {
|
client.multi()
|
||||||
console.log("An individual callback, value: " + res.toString());
|
.scard("bigset")
|
||||||
}],
|
.smembers("bigset")
|
||||||
["smembers", ["bigset"]],
|
.keys("*", function (err, replies) {
|
||||||
["smembers", ["bigset"]],
|
client.mget(replies, redis.print);
|
||||||
["smembers", ["bigset"]],
|
})
|
||||||
["smembers", ["bigset"]],
|
.dbsize()
|
||||||
["keys", ["*"]],
|
.exec(function (err, replies) {
|
||||||
["dbsize", []]
|
console.log("MULTI got " + replies.length + " replies");
|
||||||
], function (replies) {
|
replies.forEach(function (reply, index) {
|
||||||
console.log("MULTI got " + replies.length + " replies");
|
console.log("Reply " + index + ": " + reply.toString());
|
||||||
replies.forEach(function (reply, index) {
|
});
|
||||||
console.log("Reply " + index + ": " + reply.toString());
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
`client.multi()` is a constructor that returns a `Multi` object. `Multi` objects share all of the
|
||||||
|
same command methods as `client` objects do. Commands are queued up inside the `Multi` object
|
||||||
|
until `Multi.exec()` is invoked.
|
||||||
|
|
||||||
|
You can either chain together `MULTI` commands as in the above example, or you can queue individual
|
||||||
|
commands while still sending regular client command as in this example:
|
||||||
|
|
||||||
|
var redis = require("redis"),
|
||||||
|
client = redis.createClient(), multi;
|
||||||
|
|
||||||
|
// start a separate multi command queue
|
||||||
|
multi = client.multi();
|
||||||
|
multi.incr("incr thing", redis.print);
|
||||||
|
multi.incr("incr other thing", redis.print);
|
||||||
|
|
||||||
|
// runs immediately
|
||||||
|
client.mset("incr thing", 100, "incr other thing", 1, redis.print);
|
||||||
|
|
||||||
|
// drains multi queue and runs atomically
|
||||||
|
multi.exec(function (err, replies) {
|
||||||
|
console.log(replies); // 101, 2
|
||||||
|
});
|
||||||
|
|
||||||
|
// you can re-run the same transaction if you like
|
||||||
|
multi.exec(function (err, replies) {
|
||||||
|
console.log(replies); // 102, 3
|
||||||
client.quit();
|
client.quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
`client.multi` takes an Array of 3-element Arrays. The elements are: `command`, `args`, and optionally `callback`.
|
In addition to adding commands to the `MULTI` queue individually, you can also pass an array
|
||||||
When the commands are all submitted, `EXEC` is called and the callbacks are invoked in order.
|
of commands and arguments to the constructor:
|
||||||
If a command is submitted that doesn't pass the syntax check, it will be removed from the
|
|
||||||
transaction.
|
|
||||||
|
|
||||||
The second argument to `client.multi` is an optional callback with a simple array of results.
|
var redis = require("redis"),
|
||||||
|
client = redis.createClient(), multi;
|
||||||
|
|
||||||
`MULTI` needs some love. This way works, but it's too ugly and not progressive. Patches and
|
client.multi([
|
||||||
suggestions are welcome.
|
["mget", "multifoo", "multibar", redis.print],
|
||||||
|
["incr", "multifoo"],
|
||||||
|
["incr", "multibar"]
|
||||||
|
]).exec(function (err, replies) {
|
||||||
|
console.log(replies);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
# Extras
|
# Extras
|
||||||
@@ -341,11 +375,9 @@ Defaults to 1.7. The default initial connection retry is 250, so the second ret
|
|||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
Need to implement WATCH/UNWATCH and progressive MULTI commands.
|
Need to add WATCH/UNWATCH.
|
||||||
|
|
||||||
Support variable argument style for MULTI commands.
|
Stream large set/get into and out of Redis.
|
||||||
|
|
||||||
Stream binary data into and out of Redis.
|
|
||||||
|
|
||||||
|
|
||||||
## Also
|
## Also
|
||||||
|
Reference in New Issue
Block a user