redis - a node.js redis client
===========================
[](https://travis-ci.org/NodeRedis/node_redis)
[](https://coveralls.io/r/NodeRedis/node_redis?branch=)
[](https://ci.appveyor.com/project/BridgeAR/node-redis/branch/master)
[](https://gitter.im/NodeRedis/node_redis?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
This is a complete and feature rich Redis client for node.js. __It supports all
Redis commands__ and focuses on high performance.
Install with:
npm install redis
## Usage Example
```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)$
Note that the API is entirely asynchronous. To get data back from the server,
you'll need to use a callback. From v.2.6 on the API supports camelCase and
snake_case and all options / variables / events etc. can be used either way. It
is recommended to use camelCase as this is the default for the Node.js
landscape.
### Promises
You can also use node_redis with promises by promisifying node_redis with
[bluebird](https://github.com/petkaantonov/bluebird) as in:
```js
var redis = require('redis');
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
```
It'll add a *Async* to all node_redis functions (e.g. return client.getAsync().then())
```js
// We expect a value 'foo': 'bar' to be present
// So instead of writing client.get('foo', cb); you have to write:
return client.getAsync('foo').then(function(res) {
console.log(res); // => 'bar'
});
// Using multi with promises looks like:
return client.multi().get('foo').execAsync().then(function(res) {
console.log(res); // => 'bar'
});
```
### 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.
Examples:
```js
client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
// Works the same as
client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {});
// Or
client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {});
```
Note that in either form the `callback` is optional:
```js
client.set("some key", "some val");
client.set(["some other key", "some val"]);
```
If the key is missing, reply will be null. Only if the [Redis Command
Reference](http://redis.io/commands) states something else it will not be null.
```js
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)
Minimal parsing is done on the replies. Commands that return a integer return
JavaScript Numbers, arrays return JavaScript Array. `HGETALL` returns an Object
keyed by the hash keys. All strings will either be returned as string or as
buffer depending on your setting. Please be aware that sending null, undefined
and Boolean values will result in the value coerced to a string!
# Redis Commands
This library is a 1 to 1 mapping to [Redis commands](https://redis.io/commands).
It is not a cache library so please refer to Redis commands page for full usage
details.
Example setting key to auto expire using [SET command](https://redis.io/commands/set)
```js
// this key will expire after 10 seconds
client.set('key', 'value!', 'EX', 10);
```
# API
## Connection and other Events
`client` will emit some events about the state of the connection to the Redis server.
### "ready"
`client` will emit `ready` once a connection is established. Commands issued
before the `ready` event are queued, then replayed just before this event is
emitted.
### "connect"
`client` will emit `connect` as soon as the stream is connected to the server.
### "reconnecting"
`client` will emit `reconnecting` when trying to reconnect to the Redis server
after losing the connection. Listeners are passed an object containing `delay`
(in ms) and `attempt` (the attempt #) attributes.
### "error"
`client` will emit `error` when encountering an error connecting to the Redis
server or when any other in node_redis occurs. If you use a command without
callback and encounter a ReplyError it is going to be emitted to the error
listener.
So please attach the error listener to node_redis.
### "end"
`client` will emit `end` when an established Redis server connection has closed.
### "drain" (deprecated)
`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.
If the stream is buffering `client.should_buffer` is set to true. Otherwise the
variable is always set to false. That way you can decide when to reduce your
send rate and resume sending commands when you get `drain`.
You can also check the return value of each command as it will also return the
backpressure indicator (deprecated). If false is returned the stream had to
buffer.
### "warning"
`client` will emit `warning` when password was set but none is needed and if a
deprecated option / function / similar is used.
### "idle" (deprecated)
`client` will emit `idle` when there are no outstanding commands that are
awaiting a response.
## redis.createClient()
If you have `redis-server` running on the same machine as node, then the
defaults for port and host are probably fine and you don't need to supply any
arguments. `createClient()` returns a `RedisClient` object. Otherwise,
`createClient()` accepts these arguments:
* `redis.createClient([options])`
* `redis.createClient(unix_socket[, options])`
* `redis.createClient(redis_url[, options])`
* `redis.createClient(port[, host][, options])`
__Tip:__ If the Redis server runs on the same machine as the client consider
using unix sockets if possible to increase throughput.
#### `options` object properties
| Property | Default | Description |
|-----------|-----------|-------------|
| host | 127.0.0.1 | IP address of the Redis server |
| port | 6379 | Port of the Redis server |
| path | null | The UNIX socket string of the Redis server |
| url | null | The URL of the Redis server. Format: `[redis:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` (More info avaliable at [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis)). |
| parser | javascript | __Deprecated__ Use either the built-in JS parser [`javascript`]() or the native [`hiredis`]() parser. __Note__ `node_redis` < 2.6 uses hiredis as default if installed. This changed in v.2.6.0. |
| string_numbers | null | Set to `true`, `node_redis` will return Redis number values as S

希希分享
- 粉丝: 7500
- 资源: 3944
最新资源
- 基于BP神经网络的OCR+API翻译的安卓app.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于Java SSH模式的小车驾照文科在线模拟考试网站.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于SSH的网上商城.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于ncnn的scrfd人脸检测安卓demo,增加106人脸关键点检测模型推理部署,支持实时多人检测.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于ssm框架的在线学习平台html版本.zip(毕设&课设&实训&大作业&竞赛&项目)
- 静态网页项目,简单的介绍了重庆.zip(课设&实训&大作业&项目)
- 组里做的环保平台,ssh项目.zip(课设&实训&大作业&项目)
- 基于Astro和Django的Web小组大作业.zip(课设&实训&大作业&项目)
- 俄罗斯方块闯关版,基于Python实现.zip(课设&实训&大作业&项目)
- 基于tensorflow2.x框架搭建的pix2pix深度网络来完成图像翻译任务,并且配合Django来实现可视化操作(毕设&课设&实训&大作业&竞赛&项目)
- 中小型网络维护指南视频教程.zip
- 基于golang实现一个web视频聊天后端服务.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于SSH的exam系统(毕设&课设&实训&大作业&竞赛&项目)
- 基于vue + thinkphp的前后端完全分离的个人博客系统.zip(毕设&课设&实训&大作业&竞赛&项目)
- 基于机器学习的OpenRank指标拟合与优化.zip(毕设&课设&实训&大作业&竞赛&项目)
- 【毕业设计-python】python基于语音识别的智能垃圾分类系统(完整前后端+mysql+说明文档+LW).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


