You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-10 11:43:01 +03:00
Removed unnecessary indentations and added some js syntax highlighting
This commit is contained in:
307
README.md
307
README.md
@@ -24,26 +24,26 @@ happen between node and native code modules after a node upgrade.
|
|||||||
Simple example, included as `examples/simple.js`:
|
Simple example, included as `examples/simple.js`:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient();
|
client = redis.createClient();
|
||||||
|
|
||||||
// if you'd like to select database 3, instead of 0 (default), call
|
// if you'd like to select database 3, instead of 0 (default), call
|
||||||
// client.select(3, function() { /* ... */ });
|
// client.select(3, function() { /* ... */ });
|
||||||
|
|
||||||
client.on("error", function (err) {
|
client.on("error", function (err) {
|
||||||
console.log("Error " + err);
|
console.log("Error " + err);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.set("string key", "string val", redis.print);
|
client.set("string key", "string val", redis.print);
|
||||||
client.hset("hash key", "hashtest 1", "some value", redis.print);
|
client.hset("hash key", "hashtest 1", "some value", redis.print);
|
||||||
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
|
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
|
||||||
client.hkeys("hash key", function (err, replies) {
|
client.hkeys("hash key", function (err, replies) {
|
||||||
console.log(replies.length + " replies:");
|
console.log(replies.length + " replies:");
|
||||||
replies.forEach(function (reply, i) {
|
replies.forEach(function (reply, i) {
|
||||||
console.log(" " + i + ": " + reply);
|
console.log(" " + i + ": " + reply);
|
||||||
});
|
|
||||||
client.quit();
|
|
||||||
});
|
});
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
This will display:
|
This will display:
|
||||||
@@ -93,23 +93,31 @@ All functions take either an `args` Array plus optional `callback` Function or
|
|||||||
a variable number of individual arguments followed by an optional callback.
|
a variable number of individual arguments followed by an optional callback.
|
||||||
Here is an example of passing an array of arguments and a callback:
|
Here is an example of passing an array of arguments and a callback:
|
||||||
|
|
||||||
client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
|
```js
|
||||||
|
client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
|
||||||
|
```
|
||||||
|
|
||||||
Here is that same call in the second style:
|
Here is that same call in the second style:
|
||||||
|
|
||||||
client.mset("test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
|
```js
|
||||||
|
client.mset("test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
|
||||||
|
```
|
||||||
|
|
||||||
Note that in either form the `callback` is optional:
|
Note that in either form the `callback` is optional:
|
||||||
|
|
||||||
client.set("some key", "some val");
|
```js
|
||||||
client.set(["some other key", "some val"]);
|
client.set("some key", "some val");
|
||||||
|
client.set(["some other key", "some val"]);
|
||||||
|
```
|
||||||
|
|
||||||
If the key is missing, reply will be null (probably):
|
If the key is missing, reply will be null (probably):
|
||||||
|
|
||||||
client.get("missingkey", function(err, reply) {
|
```js
|
||||||
// reply is null when the key is missing
|
client.get("missingkey", function(err, reply) {
|
||||||
console.log(reply);
|
// reply is null when the key is missing
|
||||||
});
|
console.log(reply);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands)
|
For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands)
|
||||||
|
|
||||||
@@ -197,7 +205,7 @@ every command on a client.
|
|||||||
* `socket_nodelay`: defaults to `true`. Whether to call setNoDelay() on the TCP stream, which disables the
|
* `socket_nodelay`: defaults to `true`. Whether to call setNoDelay() on the TCP stream, which disables the
|
||||||
Nagle algorithm on the underlying socket. Setting this option to `false` can result in additional throughput at the
|
Nagle algorithm on the underlying socket. Setting this option to `false` can result in additional throughput at the
|
||||||
cost of more latency. Most applications will want this set to `true`.
|
cost of more latency. Most applications will want this set to `true`.
|
||||||
* `socket_keepalive` defaults to `true`. Whether the keep-alive functionality is enabled on the underlying socket.
|
* `socket_keepalive` defaults to `true`. Whether the keep-alive functionality is enabled on the underlying socket.
|
||||||
* `no_ready_check`: defaults to `false`. When a connection is established to the Redis server, the server might still
|
* `no_ready_check`: defaults to `false`. When a connection is established to the Redis server, the server might still
|
||||||
be loading the database from disk. While loading, the server not respond to any commands. To work around this,
|
be loading the database from disk. While loading, the server not respond to any commands. To work around this,
|
||||||
`node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command
|
`node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command
|
||||||
@@ -216,25 +224,25 @@ limits total time for client to reconnect. Value is provided in milliseconds and
|
|||||||
* `max_attempts` defaults to `null`. By default client will try reconnecting until connected. Setting `max_attempts`
|
* `max_attempts` defaults to `null`. By default client will try reconnecting until connected. Setting `max_attempts`
|
||||||
limits total amount of reconnects.
|
limits total amount of reconnects.
|
||||||
* `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect.
|
* `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect.
|
||||||
* `family` defaults to `IPv4`. The client connects in IPv4 if not specified or if the DNS resolution returns an IPv4 address.
|
* `family` defaults to `IPv4`. The client connects in IPv4 if not specified or if the DNS resolution returns an IPv4 address.
|
||||||
You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns modules how to use the family type.
|
You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns modules how to use the family type.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient({detect_buffers: true});
|
client = redis.createClient({detect_buffers: true});
|
||||||
|
|
||||||
client.set("foo_rand000000000000", "OK");
|
client.set("foo_rand000000000000", "OK");
|
||||||
|
|
||||||
// This will return a JavaScript String
|
// This will return a JavaScript String
|
||||||
client.get("foo_rand000000000000", function (err, reply) {
|
client.get("foo_rand000000000000", function (err, reply) {
|
||||||
console.log(reply.toString()); // Will print `OK`
|
console.log(reply.toString()); // Will print `OK`
|
||||||
});
|
});
|
||||||
|
|
||||||
// This will return a Buffer since original key is specified as a Buffer
|
// This will return a Buffer since original key is specified as a Buffer
|
||||||
client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
|
client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
|
||||||
console.log(reply.toString()); // Will print `<Buffer 4f 4b>`
|
console.log(reply.toString()); // Will print `<Buffer 4f 4b>`
|
||||||
});
|
});
|
||||||
client.end();
|
client.end();
|
||||||
```
|
```
|
||||||
|
|
||||||
`createClient()` returns a `RedisClient` object that is named `client` in all of the examples here.
|
`createClient()` returns a `RedisClient` object that is named `client` in all of the examples here.
|
||||||
@@ -260,14 +268,14 @@ This example closes the connection to the Redis server before the replies have b
|
|||||||
want to do this:
|
want to do this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient();
|
client = redis.createClient();
|
||||||
|
|
||||||
client.set("foo_rand000000000000", "some fantastic value");
|
client.set("foo_rand000000000000", "some fantastic value");
|
||||||
client.get("foo_rand000000000000", function (err, reply) {
|
client.get("foo_rand000000000000", function (err, reply) {
|
||||||
console.log(reply.toString());
|
console.log(reply.toString());
|
||||||
});
|
});
|
||||||
client.end();
|
client.end();
|
||||||
```
|
```
|
||||||
|
|
||||||
`client.end()` is useful for timeout cases where something is stuck or taking too long and you want
|
`client.end()` is useful for timeout cases where something is stuck or taking too long and you want
|
||||||
@@ -305,23 +313,29 @@ with the responses using JavaScript syntax.
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
|
```js
|
||||||
client.hgetall("hosts", function (err, obj) {
|
client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
|
||||||
console.dir(obj);
|
client.hgetall("hosts", function (err, obj) {
|
||||||
});
|
console.dir(obj);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
|
|
||||||
{ mjr: '1', another: '23', home: '1234' }
|
```js
|
||||||
|
{ mjr: '1', another: '23', home: '1234' }
|
||||||
|
```
|
||||||
|
|
||||||
### client.hmset(hash, obj, [callback])
|
### client.hmset(hash, obj, [callback])
|
||||||
|
|
||||||
Multiple values in a hash can be set by supplying an object:
|
Multiple values in a hash can be set by supplying an object:
|
||||||
|
|
||||||
client.HMSET(key2, {
|
```js
|
||||||
"0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings
|
client.HMSET(key2, {
|
||||||
"some manner of key": "a type of value"
|
"0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings
|
||||||
});
|
"some manner of key": "a type of value"
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
The properties and values of this Object will be set as keys and values in the Redis hash.
|
The properties and values of this Object will be set as keys and values in the Redis hash.
|
||||||
|
|
||||||
@@ -329,8 +343,9 @@ The properties and values of this Object will be set as keys and values in the R
|
|||||||
|
|
||||||
Multiple values may also be set by supplying a list:
|
Multiple values may also be set by supplying a list:
|
||||||
|
|
||||||
client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value");
|
```js
|
||||||
|
client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value");
|
||||||
|
```
|
||||||
|
|
||||||
## Publish / Subscribe
|
## Publish / Subscribe
|
||||||
|
|
||||||
@@ -339,28 +354,28 @@ client connections, subscribes to a channel on one of them, and publishes to tha
|
|||||||
channel on the other:
|
channel on the other:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client1 = redis.createClient(), client2 = redis.createClient(),
|
client1 = redis.createClient(), client2 = redis.createClient(),
|
||||||
msg_count = 0;
|
msg_count = 0;
|
||||||
|
|
||||||
client1.on("subscribe", function (channel, count) {
|
client1.on("subscribe", function (channel, count) {
|
||||||
client2.publish("a nice channel", "I am sending a message.");
|
client2.publish("a nice channel", "I am sending a message.");
|
||||||
client2.publish("a nice channel", "I am sending a second message.");
|
client2.publish("a nice channel", "I am sending a second message.");
|
||||||
client2.publish("a nice channel", "I am sending my last message.");
|
client2.publish("a nice channel", "I am sending my last message.");
|
||||||
});
|
});
|
||||||
|
|
||||||
client1.on("message", function (channel, message) {
|
client1.on("message", function (channel, message) {
|
||||||
console.log("client1 channel " + channel + ": " + message);
|
console.log("client1 channel " + channel + ": " + message);
|
||||||
msg_count += 1;
|
msg_count += 1;
|
||||||
if (msg_count === 3) {
|
if (msg_count === 3) {
|
||||||
client1.unsubscribe();
|
client1.unsubscribe();
|
||||||
client1.end();
|
client1.end();
|
||||||
client2.end();
|
client2.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client1.incr("did a thing");
|
client1.incr("did a thing");
|
||||||
client1.subscribe("a nice channel");
|
client1.subscribe("a nice channel");
|
||||||
```
|
```
|
||||||
|
|
||||||
When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into a "subscriber" mode.
|
When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into a "subscriber" mode.
|
||||||
@@ -412,33 +427,33 @@ channel name as `channel` and the new count of subscriptions for this client as
|
|||||||
Redis. The interface in `node_redis` is to return an individual `Multi` object by calling `client.multi()`.
|
Redis. The interface in `node_redis` is to return an individual `Multi` object by calling `client.multi()`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
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", "a member");
|
||||||
client.sadd("bigset", "another 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// multi chain with an individual callback
|
// multi chain with an individual callback
|
||||||
client.multi()
|
client.multi()
|
||||||
.scard("bigset")
|
.scard("bigset")
|
||||||
.smembers("bigset")
|
.smembers("bigset")
|
||||||
.keys("*", function (err, replies) {
|
.keys("*", function (err, replies) {
|
||||||
// NOTE: code in this callback is NOT atomic
|
// NOTE: code in this callback is NOT atomic
|
||||||
// this only happens after the the .exec call finishes.
|
// this only happens after the the .exec call finishes.
|
||||||
client.mget(replies, redis.print);
|
client.mget(replies, redis.print);
|
||||||
})
|
})
|
||||||
.dbsize()
|
.dbsize()
|
||||||
.exec(function (err, replies) {
|
.exec(function (err, replies) {
|
||||||
console.log("MULTI got " + replies.length + " replies");
|
console.log("MULTI got " + replies.length + " replies");
|
||||||
replies.forEach(function (reply, index) {
|
replies.forEach(function (reply, index) {
|
||||||
console.log("Reply " + index + ": " + reply.toString());
|
console.log("Reply " + index + ": " + reply.toString());
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Multi.exec( callback )
|
### Multi.exec( callback )
|
||||||
@@ -456,43 +471,43 @@ You can either chain together `MULTI` commands as in the above example, or you c
|
|||||||
commands while still sending regular client command as in this example:
|
commands while still sending regular client command as in this example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient(), multi;
|
client = redis.createClient(), multi;
|
||||||
|
|
||||||
// start a separate multi command queue
|
// start a separate multi command queue
|
||||||
multi = client.multi();
|
multi = client.multi();
|
||||||
multi.incr("incr thing", redis.print);
|
multi.incr("incr thing", redis.print);
|
||||||
multi.incr("incr other thing", redis.print);
|
multi.incr("incr other thing", redis.print);
|
||||||
|
|
||||||
// runs immediately
|
// runs immediately
|
||||||
client.mset("incr thing", 100, "incr other thing", 1, redis.print);
|
client.mset("incr thing", 100, "incr other thing", 1, redis.print);
|
||||||
|
|
||||||
// drains multi queue and runs atomically
|
// drains multi queue and runs atomically
|
||||||
multi.exec(function (err, replies) {
|
multi.exec(function (err, replies) {
|
||||||
console.log(replies); // 101, 2
|
console.log(replies); // 101, 2
|
||||||
});
|
});
|
||||||
|
|
||||||
// you can re-run the same transaction if you like
|
// you can re-run the same transaction if you like
|
||||||
multi.exec(function (err, replies) {
|
multi.exec(function (err, replies) {
|
||||||
console.log(replies); // 102, 3
|
console.log(replies); // 102, 3
|
||||||
client.quit();
|
client.quit();
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
In addition to adding commands to the `MULTI` queue individually, you can also pass an array
|
In addition to adding commands to the `MULTI` queue individually, you can also pass an array
|
||||||
of commands and arguments to the constructor:
|
of commands and arguments to the constructor:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient(), multi;
|
client = redis.createClient(), multi;
|
||||||
|
|
||||||
client.multi([
|
client.multi([
|
||||||
["mget", "multifoo", "multibar", redis.print],
|
["mget", "multifoo", "multibar", redis.print],
|
||||||
["incr", "multifoo"],
|
["incr", "multifoo"],
|
||||||
["incr", "multibar"]
|
["incr", "multibar"]
|
||||||
]).exec(function (err, replies) {
|
]).exec(function (err, replies) {
|
||||||
console.log(replies);
|
console.log(replies);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -508,16 +523,16 @@ will emit a `monitor` event for every new monitor message that comes across. Th
|
|||||||
Here is a simple example:
|
Here is a simple example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var client = require("redis").createClient(),
|
var client = require("redis").createClient(),
|
||||||
util = require("util");
|
util = require("util");
|
||||||
|
|
||||||
client.monitor(function (err, res) {
|
client.monitor(function (err, res) {
|
||||||
console.log("Entering monitoring mode.");
|
console.log("Entering monitoring mode.");
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("monitor", function (time, args) {
|
client.on("monitor", function (time, args) {
|
||||||
console.log(time + ": " + util.inspect(args));
|
console.log(time + ": " + util.inspect(args));
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
# Extras
|
# Extras
|
||||||
@@ -541,13 +556,13 @@ The `versions` key contains an array of the elements of the version string for e
|
|||||||
A handy callback function for displaying return values when testing. Example:
|
A handy callback function for displaying return values when testing. Example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient();
|
client = redis.createClient();
|
||||||
|
|
||||||
client.on("connect", function () {
|
client.on("connect", function () {
|
||||||
client.set("foo_rand000000000000", "some fantastic value", redis.print);
|
client.set("foo_rand000000000000", "some fantastic value", redis.print);
|
||||||
client.get("foo_rand000000000000", redis.print);
|
client.get("foo_rand000000000000", redis.print);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
This will print:
|
This will print:
|
||||||
@@ -562,14 +577,14 @@ Note that this program will not exit cleanly because the client is still connect
|
|||||||
Boolean to enable debug mode and protocol tracing.
|
Boolean to enable debug mode and protocol tracing.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
client = redis.createClient();
|
client = redis.createClient();
|
||||||
|
|
||||||
redis.debug_mode = true;
|
redis.debug_mode = true;
|
||||||
|
|
||||||
client.on("connect", function () {
|
client.on("connect", function () {
|
||||||
client.set("foo_rand000000000000", "some fantastic value");
|
client.set("foo_rand000000000000", "some fantastic value");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
This will display:
|
This will display:
|
||||||
|
Reference in New Issue
Block a user