redis - a node.js redis client
===========================
This is a complete Redis client for node.js. It supports all Redis commands, including many recently added commands like EVAL from
experimental Redis server branches.
Install with:
npm install redis
Pieter Noordhuis has provided a binding to the official `hiredis` C library, which is non-blocking and fast. To use `hiredis`, do:
npm install hiredis redis
If `hiredis` is installed, `node_redis` will use it by default. Otherwise, a pure JavaScript parser will be used.
If you use `hiredis`, be sure to rebuild it whenever you upgrade your version of node. There are mysterious failures that can
happen between node and native code modules after a node upgrade.
## Usage
Simple example, included as `examples/simple.js`:
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
client.set("string key", "string val", redis.print);
client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
client.hkeys("hash key", function (err, replies) {
console.log(replies.length + " replies:");
replies.forEach(function (reply, i) {
console.log(" " + i + ": " + reply);
});
client.quit();
});
This will display:
mjr:~/work/node_redis (master)$ node example.js
Reply: OK
Reply: 0
Reply: 0
2 replies:
0: hashtest 1
1: hashtest 2
mjr:~/work/node_redis (master)$
## Performance
Here are typical results of `multi_bench.js` which is similar to `redis-benchmark` from the Redis distribution.
It uses 50 concurrent connections with no pipelining.
JavaScript parser:
PING: 20000 ops 42283.30 ops/sec 0/5/1.182
SET: 20000 ops 32948.93 ops/sec 1/7/1.515
GET: 20000 ops 28694.40 ops/sec 0/9/1.740
INCR: 20000 ops 39370.08 ops/sec 0/8/1.269
LPUSH: 20000 ops 36429.87 ops/sec 0/8/1.370
LRANGE (10 elements): 20000 ops 9891.20 ops/sec 1/9/5.048
LRANGE (100 elements): 20000 ops 1384.56 ops/sec 10/91/36.072
hiredis parser:
PING: 20000 ops 46189.38 ops/sec 1/4/1.082
SET: 20000 ops 41237.11 ops/sec 0/6/1.210
GET: 20000 ops 39682.54 ops/sec 1/7/1.257
INCR: 20000 ops 40080.16 ops/sec 0/8/1.242
LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212
LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363
LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287
The performance of `node_redis` improves dramatically with pipelining, which happens automatically in most normal programs.
### Sending Commands
Each Redis command is exposed as a function on the `client` object.
All functions take either take either an `args` Array plus optional `callback` Function or
a variable number of individual arguments followed by an optional 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) {});
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) {});
Note that in either form the `callback` is optional:
client.set("some key", "some val");
client.set(["some other key", "some val"]);
For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands)
The commands can be specified in uppercase or lowercase for convenience. `client.get()` is the same as `client.GET()`.
Minimal parsing is done on the replies. Commands that return a single line reply return JavaScript Strings,
integer replies return JavaScript Numbers, "bulk" replies return node Buffers, and "multi bulk" replies return a
JavaScript Array of node Buffers. `HGETALL` returns an Object with Buffers keyed by the hash keys.
# API
## Connection Events
`client` will emit some events about the state of the connection to the Redis server.
### "ready"
`client` will emit `ready` a connection is established to the Redis server and the server reports
that it is ready to receive commands. Commands issued before the `ready` event are queued,
then replayed just before this event is emitted.
### "connect"
`client` will emit `connect` at the same time as it emits `ready` unless `client.options.no_ready_check`
is set. If this options is set, `connect` will be emitted when the stream is connected, and then
you are free to try to send commands.
### "error"
`client` will emit `error` when encountering an error connecting to the Redis server.
Note that "error" is a special event type in node. If there are no listeners for an
"error" event, node will exit. This is usually what you want, but it can lead to some
cryptic error messages like this:
mjr:~/work/node_redis (master)$ node example.js
node.js:50
throw e;
^
Error: ECONNREFUSED, Connection refused
at IOWatcher.callback (net:870:22)
at node.js:607:9
Not very useful in diagnosing the problem, but if your program isn't ready to handle this,
it is probably the right thing to just exit.
`client` will also emit `error` if an exception is thrown inside of `node_redis` for whatever reason.
It would be nice to distinguish these two cases.
### "end"
`client` will emit `end` when an established Redis server connection has closed.
### "drain"
`client` will emit `drain` when the TCP connection to the Redis server has been buffering, but is now
writable. This event can be used to stream commands in to Redis and adapt to backpressure. Right now,
you need to check `client.command_queue.length` to decide when to reduce your send rate. Then you can
resume sending when you get `drain`.
### "idle"
`client` will emit `idle` when there are no outstanding commands that are awaiting a response.
## redis.createClient(port, host, options)
Create a new client connection. `port` defaults to `6379` and `host` defaults
to `127.0.0.1`. If you have `redis-server` running on the same computer as node, then the defaults for
port and host are probably fine. `options` in an object with the following possible properties:
* `parser`: which Redis protocol reply parser to use. Defaults to `hiredis` if that module is installed.
This may also be set to `javascript`.
* `return_buffers`: defaults to false. If set to `true`, then bulk data replies will be returned as node Buffer
objects instead of JavaScript Strings.
`createClient()` returns a `RedisClient` object that is named `client` in all of the examples here.
## client.auth(password, callback)
When connecting to Redis servers that require authentication, the `AUTH` command must be sent as the
first command after connecting. This can be tricky to coordinate with reconnections, the ready check,
etc. To make this easier, `client.auth()` stashes `password` and will send it after each connection,
including reconnections. `callback` is invoked only once, after the response to the very first
`AUTH` command sent.
## client.end()
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies.
This example closes the connection to the Redis server before the replies have been read. You probably don't
want to do this:
var redis = require("redis"),
client = redis.createClient();
client.set("foo_rand000000000000", "some fantastic value");
client.get("foo_rand000000000000", function (err, reply) {
console.log(reply.toString());
});
client.end();
`client.end()` is useful for timeout cases where something is stuck or taking too long and you want
to start over.
## Friendlier hash commands
Most Redis commands take a single String or an Array of Strings as argument
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于HTML5和NodeJs的网络版坦克大战.zip (718个子文件)
00 388B
MozillaRootCertificates.as 216KB
AESKey.as 114KB
AESKeyTest.as 52KB
BigInteger.as 31KB
TLSEngine.as 30KB
BlowFishKey.as 19KB
WebSocket.as 15KB
MD5.as 14KB
DESKey.as 14KB
SHA1Test.as 13KB
TLSSocket.as 11KB
SSLSecurityParameters.as 11KB
RSAKey.as 9KB
HMACTest.as 9KB
Base64.as 7KB
RFC2817Socket.as 7KB
Crypto.as 7KB
TLSSecurityParameters.as 7KB
MD5.as 7KB
X509Certificate.as 6KB
CBCModeTest.as 6KB
DER.as 6KB
ECBModeTest.as 6KB
TLSTest.as 6KB
CipherSuites.as 5KB
WebSocketMain.as 5KB
SSLConnectionState.as 4KB
TLSConnectionState.as 4KB
CTRModeTest.as 4KB
MD2.as 4KB
OFBModeTest.as 3KB
BulkCiphers.as 3KB
CFBModeTest.as 3KB
BlowFishKeyTest.as 3KB
RSAKeyTest.as 3KB
MAC.as 3KB
PEM.as 3KB
SHA256.as 3KB
TLSPRF.as 3KB
DESKeyTest.as 3KB
CFB8ModeTest.as 3KB
Random.as 3KB
IVMode.as 2KB
SHA1.as 2KB
ObjectIdentifier.as 2KB
XTeaKey.as 2KB
TLSConfig.as 2KB
TripleDESKey.as 2KB
Type.as 2KB
ECBMode.as 2KB
HMAC.as 2KB
SHA256Test.as 2KB
Sequence.as 2KB
MontgomeryReduction.as 2KB
XTeaKeyTest.as 2KB
ARC4.as 2KB
TLSError.as 2KB
SHA224Test.as 2KB
BarrettReduction.as 2KB
TripleDESKeyTest.as 2KB
CFBMode.as 1KB
TLSPRFTest.as 1KB
CFB8Mode.as 1KB
MD5Test.as 1KB
Hex.as 1KB
MD2Test.as 1KB
X509CertificateCollection.as 1KB
SHABase.as 1KB
ARC4Test.as 1KB
SimpleIVMode.as 1KB
OID.as 1KB
CBCMode.as 1KB
UTCTime.as 1KB
SSLPad.as 1KB
CTRMode.as 1KB
TLSPad.as 1KB
OFBMode.as 1KB
ISecurityParameters.as 1KB
PKCS5.as 1013B
ISymmetricKey.as 956B
BigIntegerTest.as 941B
WebSocketEvent.as 907B
PrintableString.as 893B
MACs.as 884B
Integer.as 850B
ByteString.as 849B
TestCase.as 824B
ClassicReduction.as 761B
TLSEvent.as 700B
TLSSocketEvent.as 686B
SSLEvent.as 654B
KeyExchanges.as 641B
IPad.as 623B
Memory.as 589B
SHA224.as 577B
Set.as 576B
IHMAC.as 573B
NullPad.as 548B
NullReduction.as 537B
共 718 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
博士僧小星
- 粉丝: 1945
- 资源: 5903
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功