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`:
```js
var redis = require("redis"),
client = redis.createClient();
// if you'd like to select database 3, instead of 0 (default), call
// client.select(3, function() { /* ... */ });
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 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"]);
If the key is missing, reply will be null (probably):
client.get("missingkey", function(err, 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)
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 all replies will be sent to callbacks as node Buffer
objects instead of JavaScript Strings.
* `detect_buffers`: default to `false`. If set to `true`, then replies will be sent to callbacks as node Buffer objects
if any of the input arguments to the original command were Buffer objects.
This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to
every command on a client.
* `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
cost of more latency. Most applications will want this set to `true`.
* `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,
`node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command
indicates whether the server is ready for more commands. When ready, `node_redis` emits a `ready` event.
Setting `no_ready_check` to `true` wil
没有合适的资源?快使用搜索试试~ 我知道了~
ShopNC B2B2C
共2000个文件
php:1657个
js:783个
jpg:592个
需积分: 10 29 下载量 78 浏览量
2018-07-10
15:04:20
上传
评论 3
收藏 40.23MB RAR 举报
温馨提示
个人整理的比较经典的3个程序包,这是其中之一。有做b2b2c商城的朋友,可以参考学习,一起探讨一下,上传顺序一
资源推荐
资源详情
资源评论
收起资源包目录
ShopNC B2B2C (2000个子文件)
00 388B
no.2js 23B
MozillaRootCertificates.as 216KB
AESKey.as 117KB
AESKeyTest.as 53KB
BigInteger.as 33KB
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 8KB
Crypto.as 7KB
RFC2817Socket.as 7KB
TLSSecurityParameters.as 7KB
MD5.as 7KB
X509Certificate.as 7KB
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 4KB
BlowFishKeyTest.as 4KB
CFBModeTest.as 4KB
BulkCiphers.as 3KB
MAC.as 3KB
RSAKeyTest.as 3KB
PEM.as 3KB
SHA256.as 3KB
TLSPRF.as 3KB
DESKeyTest.as 3KB
CFB8ModeTest.as 3KB
Random.as 3KB
IVMode.as 3KB
SHA1.as 3KB
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 2KB
CFB8Mode.as 2KB
TLSPRFTest.as 2KB
SHABase.as 2KB
Hex.as 2KB
MD5Test.as 1KB
X509CertificateCollection.as 1KB
MD2Test.as 1KB
ARC4Test.as 1KB
SimpleIVMode.as 1KB
OID.as 1KB
CBCMode.as 1KB
UTCTime.as 1KB
CTRMode.as 1KB
TLSPad.as 1KB
SSLPad.as 1KB
OFBMode.as 1KB
PKCS5.as 1KB
ISecurityParameters.as 1KB
ISymmetricKey.as 990B
BigIntegerTest.as 979B
PrintableString.as 941B
MACs.as 921B
WebSocketEvent.as 907B
Integer.as 893B
ByteString.as 891B
TestCase.as 865B
ClassicReduction.as 795B
TLSEvent.as 726B
TLSSocketEvent.as 710B
SSLEvent.as 679B
KeyExchanges.as 664B
IPad.as 654B
Memory.as 616B
SHA224.as 604B
Set.as 602B
IHMAC.as 599B
NullPad.as 581B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Cenlym
- 粉丝: 1
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功