Skip to content

Commit 0294d8e

Browse files
DOC-4996 added trans/pipe page plus fixes
1 parent fb0840f commit 0294d8e

File tree

5 files changed

+185
-20
lines changed

5 files changed

+185
-20
lines changed

content/develop/clients/hiredis/_index.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ title: hiredis guide (C)
1515
weight: 9
1616
---
1717

18-
[`hiredis`](https://github.com/redis/hiredis) is the client for the
19-
[C language](https://en.wikipedia.org/wiki/C_(programming_language)).
18+
[`hiredis`](https://github.com/redis/hiredis) is the
19+
[C language](https://en.wikipedia.org/wiki/C_(programming_language))
20+
client for Redis.
2021
The sections below explain how to install `hiredis` and connect your application
2122
to a Redis database.
2223

@@ -102,17 +103,20 @@ Reply: OK
102103
Reply: bar
103104
```
104105

105-
The code first uses `redisConnect()` to create the connection to
106-
use for all subsequent commands.
106+
The code first uses `redisConnect()` to open the connection for
107+
all subsequent commands to use. See
108+
[Connect]({{< relref "/develop/clients/hiredis/connect" >}}) for
109+
more information about connecting to Redis.
107110

108111
The `redisCommand()` function
109112
issues commands to the server, each of which returns a
110113
`redisReply` pointer. Here, the reply is a string, which you can
111114
access using the `str` field of the reply. The `redisCommand()`
112115
call allocates memory for the reply, so you should free this
113116
with `freeReplyObject()` when you have finished using it.
114-
See [Issue commands]({{< relref "/develop/clients/hiredis/issue-commands" >}}) for more information about
115-
handling commands and their replies.
117+
See [Issue commands]({{< relref "/develop/clients/hiredis/issue-commands" >}})
118+
and [Handle replies]({{< relref "/develop/clients/hiredis/handle-replies" >}})
119+
for more information.
116120

117121
Finally, you should close the connection to Redis with a
118122
call to `redisFree()`. This is not strictly necessary
@@ -122,4 +126,8 @@ to prevent errors.
122126

123127
## More information
124128

129+
The [`hiredis`](https://github.com/redis/hiredis) Github repository contains
130+
examples and details that may be useful if you are using `hiredis` to
131+
implement a higher-level client for another programming language.
132+
125133
See the other pages in this section for more information and examples.

content/develop/clients/hiredis/connect.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ weight: 1
1717

1818
## Basic synchronous connection
1919

20+
The example below creates a simple synchronous connection to a local
21+
Redis server and tests the connection, before closing it with
22+
`redisFree()`. The `redisConnect()` function takes just a hostname
23+
and port as its arguments, and returns a context object.
24+
2025
```c
2126
#include <stdio.h>
2227

@@ -58,4 +63,6 @@ if (reply != NULL) {
5863

5964
// Close the connection.
6065
redisFree(c);
61-
```
66+
```
67+
68+

content/develop/clients/hiredis/handle-replies.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ categories:
99
- oss
1010
- kubernetes
1111
- clients
12-
description: Handle replies with `hiredis`.
13-
linkTitle: Handle replies
14-
title: Handle replies
12+
description: Handle command replies with `hiredis`.
13+
linkTitle: Handle command replies
14+
title: Handle command replies
1515
weight: 10
1616
---
1717

@@ -23,7 +23,7 @@ reply formats defined in the
2323
[RESP2 and RESP3]({{< relref "/develop/reference/protocol-spec#resp-protocol-description" >}})
2424
protocols, so its content varies greatly between calls.
2525

26-
A simple example is the status response returned by the `SET`
26+
A simple example is the status response returned by the [`SET`]({{< relref "/commands/set" >}})
2727
command. The code below shows how to get this from the `redisReply`
2828
object:
2929

@@ -40,7 +40,7 @@ if (reply != NULL) {
4040

4141
A null reply indicates an error, so you should always check for this.
4242
If an error does occur, then the `redisContext` object will have a
43-
non-zero error number in its integer `err` field and sometimes a textual
43+
non-zero error number in its integer `err` field and a textual
4444
description of the error in its `errstr` field.
4545

4646
For `SET`, a successful call will simply return an "OK" string that you
@@ -58,15 +58,15 @@ the stale pointer later.
5858

5959
The Redis
6060
[`RESP`]({{< relref "/develop/reference/protocol-spec#resp-protocol-description" >}})
61-
protocols support several different types of reply format for commands.
61+
protocols support several different reply formats for commands.
6262

6363
You can find the reply format for a command at the end of its
6464
reference page in the RESP2/RESP3 Reply section (for example, the
6565
[`INCRBY`]({{< relref "/commands/incrby" >}}) page shows that the
6666
command has an integer result). You can also determine the format
6767
using the `type` field of the reply object. This contains a
68-
different integer value for each type, and `hiredis.h` defines
69-
constants for each type (for example `REDIS_REPLY_STRING`).
68+
different integer value for each type. The `hiredis.h` header file
69+
defines constants for all of these integer values (for example `REDIS_REPLY_STRING`).
7070

7171
The `redisReply` struct has several fields to contain different
7272
types of replies, with different fields being set depending on
@@ -306,4 +306,24 @@ if (reply->type == REDIS_REPLY_ARRAY) {
306306
// >>> Key: address, value: 52 Festive Road
307307
// >>> Key: hobbies, value: Cosplay
308308
}
309-
```
309+
```
310+
311+
## Handling errors
312+
313+
When a command executes successfully, the `err` field of the context
314+
object will be set to zero. If a command fails, it will return either
315+
`NULL` or `REDIS_ERR`, depending on which function and command you used. When
316+
this happens, `context->err` will contain an error code
317+
318+
- `REDIS_ERR_IO`: There was an I/O error while creating the connection,
319+
or while trying to write or read data. Whenever `context->err` contains
320+
`REDIS_ERR_IO`, you can use the features of the standard library file
321+
[`errno.h`](https://en.wikipedia.org/wiki/Errno.h) to find out more
322+
information about the error.
323+
- `REDIS_ERR_EOF`: The server closed the connection which resulted in an empty read.
324+
- `REDIS_ERR_PROTOCOL`: There was an error while parsing the
325+
[RESP protocol]({{< relref "/develop/reference/protocol-spec" >}}).
326+
- `REDIS_ERR_OTHER`: Any other error. Currently, it is only used when the connection
327+
hostname can't be resolved.
328+
329+
The context object also has an `errstr` field that contains a descriptive error message.

content/develop/clients/hiredis/issue-commands.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ categories:
99
- oss
1010
- kubernetes
1111
- clients
12-
description: Handle commands and replies with `hiredis`.
12+
description: Construct commands and send them to the Redis server.
1313
linkTitle: Issue commands
1414
title: Issue commands
1515
weight: 5
@@ -33,7 +33,9 @@ void *redisCommand(redisContext *c, const char *format, ...);
3333
```
3434
3535
This function receives a `redisContext` pointer and a pointer
36-
to a string containing the command. The command text is the
36+
to a string containing the command (see
37+
[Connect]({{< relref "/develop/clients/hiredis/connect" >}})
38+
to learn how to obtain the context pointer). The command text is the
3739
same as the equivalent [`redis-cli`]({{< relref "/develop/tools/cli" >}})
3840
command. For example, to issue the command:
3941
@@ -47,6 +49,11 @@ you would use the following command with an existing `redisContext* c`:
4749
redisReply *reply = redisCommand(c, "SET foo bar");
4850
```
4951

52+
See the [Command reference]({{< relref "/commands" >}}) for examples
53+
of CLI commands that you can use with `hiredis`. Most code examples
54+
in other sections of the docs also have a CLI tab showing
55+
command sequences that are equivalent to the code.
56+
5057
The command string is interpreted in a similar way to the format
5158
string for `printf()`, so you can easily interpolate string values from
5259
your code into the command with the `%s` format specifier:
@@ -65,7 +72,9 @@ in fields of a [hash]({{< relref "/develop/data-types/hashes" >}})) object.
6572
To do this, use the `%b` format specifier and pass a pointer to the
6673
data buffer, followed by a `size_t` value indicating its length in bytes.
6774
As the example below shows, you can freely mix `%s` and `%b` specifiers
68-
in the same format string.
75+
in the same format string. Also, you can use the sequence `%%` to
76+
denote a literal percent sign, but the other `printf()` specifiers,
77+
such as `%d`, are not supported.
6978

7079
```c
7180
char *entryNumber = "1";
@@ -81,7 +90,7 @@ redisReply *reply = redisCommand(c,
8190
);
8291
```
8392

84-
The `redisCommand()` function has a variant `redisCommandArgv()`:
93+
The `redisCommand()` function has a variant called `redisCommandArgv()`:
8594

8695
```c
8796
void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
@@ -106,3 +115,11 @@ const size_t argvlen[] = {3, 8, 5};
106115
107116
redisReply *reply = redisCommandArgv(c, argc, argv, argvlen);
108117
```
118+
119+
## Command replies
120+
121+
The information in the `redisReply` object has several formats,
122+
and the format for a particular reply depends on the command that generated it.
123+
See
124+
[Handle replies]({{< relref "/develop/clients/hiredis/handle-replies" >}})
125+
to learn about the different reply formats and how to use them.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Learn how to use Redis pipelines and transactions
13+
linkTitle: Pipelines/transactions
14+
title: Pipelines and transactions
15+
weight: 20
16+
---
17+
18+
Redis lets you send a sequence of commands to the server together in a batch.
19+
There are two types of batch that you can use:
20+
21+
- **Pipelines** avoid network and processing overhead by sending several commands
22+
to the server together in a single communication. The server then sends back
23+
a single communication with all the responses. See the
24+
[Pipelining]({{< relref "/develop/use/pipelining" >}}) page for more
25+
information.
26+
- **Transactions** guarantee that all the included commands will execute
27+
to completion without being interrupted by commands from other clients.
28+
See the [Transactions]({{< relref "/develop/interact/transactions" >}})
29+
page for more information.
30+
31+
## Execute a pipeline
32+
33+
There is no command to explicitly start a pipeline with `hiredis`,
34+
but if you issue a command with the `redisAppendCommand()` function,
35+
it will be added to an output buffer without being sent
36+
immediately to the server.
37+
38+
There is also an input buffer that receives replies from
39+
commands. If you call `redisGetReply()` when the input buffer is empty,
40+
it will first send any commands that are queued in the output buffer and
41+
then wait for replies to arrive in the input buffer. It will then return
42+
the first reply only.
43+
44+
If you then make subsequent `redisGetReply()` calls, they will
45+
find the input buffer is not empty, but still has replies
46+
queued from previous commands. In this case, `redisGetReply()`
47+
will just remove and return replies from the input buffer
48+
until it is empty again.
49+
50+
The example below shows how to use `redisAppendCommand()`
51+
and `redisGetReply()` together:
52+
53+
```c
54+
redisAppendCommand(c, "SET fruit:0 Apple");
55+
redisAppendCommand(c, "SET fruit:1 Banana");
56+
redisAppendCommand(c, "SET fruit:2 Cherry");
57+
58+
redisAppendCommand(c, "GET fruit:0");
59+
redisAppendCommand(c, "GET fruit:1");
60+
redisAppendCommand(c, "GET fruit:2");
61+
62+
63+
redisReply *reply;
64+
65+
// Iterate once for each of the six commands in the
66+
// pipeline.
67+
for (int i = 0; i < 6; ++i) {
68+
redisGetReply(c, (void**) &reply);
69+
70+
// If an error occurs, the context object will
71+
// contain an error code and/or an error string.
72+
if (reply->type == REDIS_REPLY_ERROR) {
73+
printf("Error: %s", c->errstr);
74+
} else {
75+
printf("%s\n", reply->str);
76+
}
77+
78+
freeReplyObject(reply);
79+
}
80+
// >>> OK
81+
// >>> OK
82+
// >>> OK
83+
// >>> Apple
84+
// >>> Banana
85+
// >>> Cherry
86+
```
87+
88+
`redisAppendCommand()` has the same call signature as `redisCommand()` except that
89+
it doesn't return a `redisReply`. There is also a `redisAppendCommandArgv()`
90+
function that is analogous to `redisCommandArgv()` (see
91+
[Issue commands]({{< relref "/develop/clients/hiredis/issue-commands" >}})
92+
for more information).
93+
94+
`redisGetReply()` receives the usual
95+
context pointer and a pointer to a `redisReply` pointer (which you
96+
must cast to `void**`). After `redisGetReply()` returns,
97+
the reply pointer will point to the `redisReply` object returned by
98+
the queued command (see
99+
[Handle command replies]({{< relref "/develop/clients/hiredis/handle-replies" >}})
100+
for more information).
101+
102+
Call `redisGetReply()` once for each command that you added to the pipeline.
103+
You should check for errors after each call and free each reply object
104+
when you have finished processing it, as in the example above.
105+
106+
## Transactions
107+
108+
`hiredis` doesn't provide any special API to handle transactions, but
109+
you can implement them yourself using the [`MULTI`]({{< relref "/commands/multi" >}}),
110+
[`EXEC`]({{< relref "/commands/exec" >}}), and [`WATCH`]({{< relref "/commands/watch" >}})
111+
commands as you would from [`redis-cli`]({{< relref "/develop/tools/cli" >}}).
112+
See [Transactions]({{< relref "/develop/interact/transactions" >}})
113+
for more information.

0 commit comments

Comments
 (0)