Skip to content

Commit 41c8177

Browse files
committed
docs
1 parent 7ce0858 commit 41c8177

File tree

1 file changed

+70
-82
lines changed

1 file changed

+70
-82
lines changed

README.md

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ you'll need to use a callback.
4747
### Promises
4848

4949
Node Redis currently doesn't natively support promises (this is coming in v4), however you can wrap the methods you
50-
want to use with promises using the built-in Node.js `util.promisify` method on Node.js >= v8;
50+
want to use with promises using the built-in Node.js `util.promisify` method on Node.js >= v8;
5151

5252
```js
5353
const { promisify } = require("util");
5454
const getAsync = promisify(client.get).bind(client);
5555

56-
getAsync
57-
.then(console.log)
58-
.catch(console.error);
56+
getAsync.then(console.log).catch(console.error);
5957
```
6058

61-
### Sending Commands
59+
### Commands
60+
61+
This library is a 1 to 1 mapping of the [Redis commands](https://redis.io/commands).
6262

6363
Each Redis command is exposed as a function on the `client` object.
6464
All functions take either an `args` Array plus optional `callback` Function or
@@ -98,50 +98,35 @@ client.get("missingkey", function(err, reply) {
9898
});
9999
```
100100

101-
For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands)
102-
103101
Minimal parsing is done on the replies. Commands that return a integer return
104102
JavaScript Numbers, arrays return JavaScript Array. `HGETALL` returns an Object
105103
keyed by the hash keys. All strings will either be returned as string or as
106104
buffer depending on your setting. Please be aware that sending null, undefined
107105
and Boolean values will result in the value coerced to a string!
108106

109-
# Redis Commands
110-
111-
This library is a 1 to 1 mapping to [Redis commands](https://redis.io/commands).
112-
It is not a cache library so please refer to Redis commands page for full usage
113-
details.
114-
115-
Example setting key to auto expire using [SET command](https://redis.io/commands/set)
116-
117-
```js
118-
// this key will expire after 10 seconds
119-
client.set("key", "value!", "EX", 10);
120-
```
121-
122-
# API
107+
## API
123108

124-
## Connection and other Events
109+
### Connection and other Events
125110

126111
`client` will emit some events about the state of the connection to the Redis server.
127112

128-
### "ready"
113+
#### "ready"
129114

130115
`client` will emit `ready` once a connection is established. Commands issued
131116
before the `ready` event are queued, then replayed just before this event is
132117
emitted.
133118

134-
### "connect"
119+
#### "connect"
135120

136121
`client` will emit `connect` as soon as the stream is connected to the server.
137122

138-
### "reconnecting"
123+
#### "reconnecting"
139124

140125
`client` will emit `reconnecting` when trying to reconnect to the Redis server
141126
after losing the connection. Listeners are passed an object containing `delay`
142127
(in ms from the previous try) and `attempt` (the attempt #) attributes.
143128

144-
### "error"
129+
#### "error"
145130

146131
`client` will emit `error` when encountering an error connecting to the Redis
147132
server or when any other in node_redis occurs. If you use a command without
@@ -150,16 +135,16 @@ listener.
150135

151136
So please attach the error listener to node_redis.
152137

153-
### "end"
138+
#### "end"
154139

155140
`client` will emit `end` when an established Redis server connection has closed.
156141

157-
### "warning"
142+
#### "warning"
158143

159144
`client` will emit `warning` when password was set but none is needed and if a
160145
deprecated option / function / similar is used.
161146

162-
## redis.createClient()
147+
### redis.createClient()
163148

164149
If you have `redis-server` running on the same machine as node, then the
165150
defaults for port and host are probably fine and you don't need to supply any
@@ -201,9 +186,11 @@ using unix sockets if possible to increase throughput.
201186
| prefix | null | A string used to prefix all used keys (e.g. `namespace:test`). Please be aware that the `keys` command will not be prefixed. The `keys` command has a "pattern" as argument and no key and it would be impossible to determine the existing keys in Redis if this would be prefixed. |
202187
| retry_strategy | function | A function that receives an options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below. |
203188

189+
**`detect_buffers` example:**
190+
204191
```js
205-
var redis = require("redis");
206-
var client = redis.createClient({ detect_buffers: true });
192+
const redis = require("redis");
193+
const client = redis.createClient({ detect_buffers: true });
207194

208195
client.set("foo_rand000000000000", "OK");
209196

@@ -216,10 +203,9 @@ client.get("foo_rand000000000000", function(err, reply) {
216203
client.get(new Buffer("foo_rand000000000000"), function(err, reply) {
217204
console.log(reply.toString()); // Will print `<Buffer 4f 4b>`
218205
});
219-
client.quit();
220206
```
221207

222-
#### `retry_strategy` example
208+
**`retry_strategy` example:**
223209

224210
```js
225211
var client = redis.createClient({
@@ -244,7 +230,7 @@ var client = redis.createClient({
244230
});
245231
```
246232

247-
## client.auth(password[, callback])
233+
### client.auth(password[, callback])
248234

249235
When connecting to a Redis server that requires authentication, the `AUTH`
250236
command must be sent as the first command after connecting. This can be tricky
@@ -256,27 +242,15 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If
256242
you are doing this wrong, `client` will emit an error that looks
257243
something like this `Error: Ready check failed: ERR operation not permitted`.
258244

259-
## backpressure
260-
261-
### stream
262-
263-
The client exposed the used [stream](https://nodejs.org/api/stream.html) in
264-
`client.stream` and if the stream or client had to
265-
[buffer](https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback)
266-
the command in `client.should_buffer`. In combination this can be used to
267-
implement backpressure by checking the buffer state before sending a command and
268-
listening to the stream
269-
[drain](https://nodejs.org/api/stream.html#stream_event_drain) event.
270-
271-
## client.quit(callback)
245+
### client.quit(callback)
272246

273247
This sends the quit command to the redis server and ends cleanly right after all
274248
running commands were properly handled. If this is called while reconnecting
275249
(and therefore no connection to the redis server exists) it is going to end the
276250
connection right away instead of resulting in further reconnections! All offline
277251
commands are going to be flushed with an error in that case.
278252

279-
## client.end(flush)
253+
### client.end(flush)
280254

281255
Forcibly close the connection to the Redis server. Note that this does not wait
282256
until all replies have been parsed. If you want to exit cleanly, call
@@ -290,25 +264,28 @@ This example closes the connection to the Redis server before the replies have
290264
been read. You probably don't want to do this:
291265

292266
```js
293-
var redis = require("redis"),
294-
client = redis.createClient();
267+
const redis = require("redis");
268+
const client = redis.createClient();
295269

296-
client.set("foo_rand000000000000", "some fantastic value", function(err, reply) {
270+
client.set("hello", "world", function(err) {
297271
// This will either result in an error (flush parameter is set to true)
298272
// or will silently fail and this callback will not be called at all (flush set to false)
299-
console.log(err);
273+
console.error(err);
300274
});
301-
client.end(true); // No further commands will be processed
302-
client.get("foo_rand000000000000", function(err, reply) {
303-
console.log(err); // => 'The connection has already been closed.'
275+
276+
// No further commands will be processed
277+
client.end(true);
278+
279+
client.get("hello", function(err) {
280+
console.error(err); // => 'The connection has already been closed.'
304281
});
305282
```
306283

307284
`client.end()` without the flush parameter set to true should NOT be used in production!
308285

309-
## Error handling (>= v2.6)
286+
### Error Handling
310287

311-
Currently the following error subclasses exist:
288+
Currently the following `Error` subclasses exist:
312289

313290
- `RedisError`: _All errors_ returned by the client
314291
- `ReplyError` subclass of `RedisError`: All errors returned by **Redis** itself
@@ -322,30 +299,36 @@ Currently the following error subclasses exist:
322299

323300
All error classes are exported by the module.
324301

325-
Example:
302+
#### Example
326303

327304
```js
328-
var redis = require("./");
329-
var assert = require("assert");
330-
var client = redis.createClient();
305+
const assert = require("assert");
306+
307+
const redis = require("redis");
308+
const { AbortError, AggregateError, ReplyError } = require("redis");
309+
const client = redis.createClient();
331310

332311
client.on("error", function(err) {
333312
assert(err instanceof Error);
334-
assert(err instanceof redis.AbortError);
335-
assert(err instanceof redis.AggregateError);
336-
// The set and get get aggregated in here
313+
assert(err instanceof AbortError);
314+
assert(err instanceof AggregateError);
315+
316+
// The set and get are aggregated in here
337317
assert.strictEqual(err.errors.length, 2);
338318
assert.strictEqual(err.code, "NR_CLOSED");
339319
});
340-
client.set("foo", 123, "bar", function(err, res) {
320+
321+
client.set("foo", "bar", "baz", function(err, res) {
341322
// Too many arguments
342-
assert(err instanceof redis.ReplyError); // => true
323+
assert(err instanceof ReplyError); // => true
343324
assert.strictEqual(err.command, "SET");
344325
assert.deepStrictEqual(err.args, ["foo", 123, "bar"]);
345326

346327
redis.debug_mode = true;
328+
347329
client.set("foo", "bar");
348330
client.get("foo");
331+
349332
process.nextTick(function() {
350333
// Force closing the connection while the command did not yet return
351334
client.end(true);
@@ -366,7 +349,7 @@ If a command unresolved command got rejected a `UNCERTAIN_STATE` code is
366349
returned. A `CONNECTION_BROKEN` error code is used in case node_redis gives up
367350
to reconnect.
368351

369-
## client.unref()
352+
### client.unref()
370353

371354
Call `unref()` on the underlying socket connection to the Redis server, allowing
372355
the program to exit once no more commands are pending.
@@ -376,34 +359,35 @@ protocol. Any commands where client state is saved on the Redis server, e.g.
376359
`*SUBSCRIBE` or the blocking `BL*` commands will _NOT_ work with `.unref()`.
377360

378361
```js
379-
var redis = require("redis");
380-
var client = redis.createClient();
362+
const redis = require("redis");
363+
const client = redis.createClient();
381364

382365
/*
383-
Calling unref() will allow this program to exit immediately after the get
384-
command finishes. Otherwise the client would hang as long as the
385-
client-server connection is alive.
386-
*/
366+
* Calling unref() will allow this program to exit immediately after the get
367+
* command finishes. Otherwise the client would hang as long as the
368+
* client-server connection is alive.
369+
*/
387370
client.unref();
371+
388372
client.get("foo", function(err, value) {
389373
if (err) throw err;
390374
console.log(value);
391375
});
392376
```
393377

394-
## Friendlier hash commands
378+
### Hash Commands
395379

396380
Most Redis commands take a single String or an Array of Strings as arguments,
397381
and replies are sent back as a single String or an Array of Strings. When
398382
dealing with hash values, there are a couple of useful exceptions to this.
399383

400-
### client.hgetall(hash, callback)
384+
#### client.hgetall(hash, callback)
401385

402-
The reply from an HGETALL command will be converted into a JavaScript Object by
386+
The reply from an `HGETALL` command will be converted into a JavaScript Object by
403387
`node_redis`. That way you can interact with the responses using JavaScript
404388
syntax.
405389

406-
Example:
390+
**Example:**
407391

408392
```js
409393
client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
@@ -412,9 +396,11 @@ client.hgetall("hosts", function(err, obj) {
412396
});
413397
```
414398

415-
### client.hmset(hash, obj[, callback])
399+
#### client.hmset(hash, obj[, callback])
416400

417-
Multiple values in a hash can be set by supplying an object:
401+
Multiple values in a hash can be set by supplying an object.
402+
403+
**Example:**
418404

419405
```js
420406
client.HMSET(key2, {
@@ -426,15 +412,17 @@ client.HMSET(key2, {
426412
The properties and values of this Object will be set as keys and values in the
427413
Redis hash.
428414

429-
### client.hmset(hash, key1, val1, ... keyn, valn, [callback])
415+
#### client.hmset(hash, key1, val1, ...keyN, valN, [callback])
416+
417+
Multiple values may also be set by supplying more arguments.
430418

431-
Multiple values may also be set by supplying a list:
419+
**Example:**
432420

433421
```js
434422
client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value");
435423
```
436424

437-
## Publish / Subscribe
425+
### Publish / Subscribe
438426

439427
Example of the publish / subscribe API. This program opens two
440428
client connections, subscribes to a channel on one of them, and publishes to that

0 commit comments

Comments
 (0)