Skip to content

Commit 155426c

Browse files
DOC-4996 started hiredis client section
1 parent 909d2a3 commit 155426c

File tree

6 files changed

+192
-5
lines changed

6 files changed

+192
-5
lines changed

content/develop/clients/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ weight: 30
1919

2020
Use the Redis client libraries to connect to Redis servers from
2121
your own code. We document the following client libraries
22-
for six main languages:
22+
for seven main languages:
2323

2424
| Language | Client name | Docs | Supported |
2525
| :-- | :-- | :-- | :-- |
@@ -31,6 +31,7 @@ for six main languages:
3131
| [Java](https://www.java.com/en/) | [`Lettuce`](https://github.com/redis/lettuce) | [`Lettuce` guide]({{< relref "/develop/clients/lettuce" >}}) | Yes |
3232
| [Go](https://go.dev/) | [`go-redis`](https://github.com/redis/go-redis) | [`go-redis` guide]({{< relref "/develop/clients/go" >}}) | Yes |
3333
| [PHP](https://www.php.net/)| [`Predis`](https://github.com/predis/predis) | [`Predis` guide]({{< relref "/develop/clients/php" >}}) | No |
34+
| [C](https://en.wikipedia.org/wiki/C_(programming_language)) | [`hiredis`](https://github.com/redis/hiredis) | [`hiredis` guide]({{< relref "/develop/clients/hiredis" >}}) | Yes |
3435

3536
We also provide several higher-level
3637
[object mapping (OM)]({{< relref "/develop/clients/om-clients" >}})
@@ -46,7 +47,6 @@ Redis does not document directly:
4647

4748
| Language | Client name | Github | Docs |
4849
| :-- | :-- | :-- | :-- |
49-
| C | hiredis | https://github.com/redis/hiredis | https://github.com/redis/hiredis |
5050
| [C++](https://en.wikipedia.org/wiki/C%2B%2B) | Boost.Redis | https://github.com/boostorg/redis | https://www.boost.org/doc/libs/develop/libs/redis/doc/html/index.html |
5151
| [Dart](https://dart.dev/) | redis_dart_link | https://github.com/toolsetlink/redis_dart_link | https://github.com/toolsetlink/redis_dart_link |
5252
| [PHP](https://www.php.net/) | PhpRedis extension | https://github.com/phpredis/phpredis | https://github.com/phpredis/phpredis/blob/develop/README.md |

content/develop/clients/client-side-caching.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ categories:
1313
description: Server-assisted, client-side caching in Redis
1414
linkTitle: Client-side caching
1515
title: Client-side caching introduction
16-
weight: 20
16+
weight: 30
1717
---
1818

1919
*Client-side caching* reduces network traffic between
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Connect your C application to a Redis database.
13+
linkTitle: hiredis (C)
14+
title: hiredis guide (C)
15+
weight: 9
16+
---
17+
18+
[`hiredis`](https://github.com/redis/hiredis) is the client for the
19+
[C language](https://en.wikipedia.org/wiki/C_(programming_language)).
20+
The sections below explain how to install `hiredis` and connect your application
21+
to a Redis database.
22+
23+
`hiredis` requires a running Redis or [Redis Stack]({{< relref "/operate/oss_and_stack/install/install-stack/" >}}) server. See [Getting started]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis installation instructions.
24+
25+
## Build and install
26+
27+
Clone or download the `hiredis` source from the [Github repository](https://github.com/redis/hiredis).
28+
Then, in a terminal, go into the `hiredis` folder and run the `make` command to build
29+
the dynamically-loaded library for `hiredis` (this has the name `libhiredis.dylib` on
30+
MacOS and `libhiredis.so` on Linux). You can copy this library to your
31+
project folder or run `sudo make install` to install it to `/usr/local/lib`.
32+
You should also copy the header files `hiredis.h`, `alloc.h`, `read.h`, and
33+
`sds.h` to your project.
34+
35+
## Connect and test
36+
37+
The code in the example below connects to the server, stores and retrieves
38+
a string key using [`SET`]({{< relref "/commands/set" >}}) and
39+
[`GET`]({{< relref "/commands/get" >}}), and then finally closes the
40+
connection. An explanation of the code follows the example.
41+
42+
```c
43+
#include <stdio.h>
44+
45+
#include "hiredis.h"
46+
47+
int main() {
48+
// The `redisContext` type represents the connection
49+
// to the Redis server. Here, we connect to the
50+
// default host and port.
51+
redisContext *c = redisConnect("127.0.0.1", 6379);
52+
53+
// Check if the context is null or if a specific
54+
// error occurred.
55+
if (c == NULL || c->err) {
56+
if (c != NULL) {
57+
printf("Error: %s\n", c->errstr);
58+
// handle error
59+
} else {
60+
printf("Can't allocate redis context\n");
61+
}
62+
}
63+
64+
// Set a string key.
65+
redisReply *reply = redisCommand(c, "SET foo bar");
66+
67+
// Check and free the reply.
68+
if (reply != NULL) {
69+
printf("Reply: %s\n", reply->str);
70+
freeReplyObject(reply);
71+
}
72+
73+
// Get the key we have just stored.
74+
reply = redisCommand(c, "GET foo");
75+
76+
// Check and free the reply.
77+
if (reply != NULL) {
78+
printf("Reply: %s\n", reply->str);
79+
freeReplyObject(reply);
80+
}
81+
82+
// Close the connection.
83+
redisFree(c);
84+
}
85+
```
86+
87+
For a real project, you would build your code with a makefile, but for
88+
this simple test, you can just place it in a file called `main.c` and
89+
build it with the following command (assuming you used `make install` to
90+
install the `libhiredis` library):
91+
92+
```bash
93+
cc main.c -L/usr/local/lib -lhiredis
94+
```
95+
96+
The default executable filename is `a.out`. If you run this file from
97+
the terminal, you should see the following output:
98+
99+
```
100+
% ./a.out
101+
Reply: OK
102+
Reply: bar
103+
```
104+
105+
The code first uses `redisConnect()` to create the connection to
106+
use for all subsequent commands.
107+
108+
The `redisCommand()` function
109+
issues commands to the server, each of which returns a
110+
`redisReply` pointer. Here, the reply is a string, which you can
111+
access using the `str` field of the reply. The `redisCommand()`
112+
call allocates memory for the reply, so you should free this
113+
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.
116+
117+
Finally, you should close the connection to Redis with a
118+
call to `redisFree()`. This is not strictly necessary
119+
for this short test program, but real-world code will typically
120+
open and use many connections. You must free them after using them
121+
to prevent errors.
122+
123+
## More information
124+
125+
See the other pages in this section for more information and examples.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Handle commands and replies with `hiredis`.
13+
linkTitle: Issue commands
14+
title: Issue commands
15+
weight: 5
16+
---
17+
18+
Unlike the other [client libraries]({{< relref "/develop/clients" >}}),
19+
`hiredis` doesn't provide an extensive API to construct the many different
20+
Redis [commands]({{< relref "/commands" >}}). However, it does provide a lightweight and
21+
flexible API to help you construct commands and parse their replies from
22+
your own code.
23+
24+
The sections below describe the available functions in
25+
detail.
26+
27+
## Construct synchronous commands
28+
29+
Use the `redisCommand()` function to send commands to the server:
30+
31+
```c
32+
void *redisCommand(redisContext *c, const char *format, ...);
33+
```
34+
35+
This function receives a `redisContext` pointer and a pointer
36+
to a string containing the command. The command text is the
37+
same as the equivalent [`redis-cli`]({{< relref "/develop/tools/cli" >}})
38+
command. For example, to issue the command:
39+
40+
```
41+
SET foo bar
42+
```
43+
44+
you would use (with an existing `redisContext* c`):
45+
46+
```c
47+
redisReply *reply = redisCommand(c, "SET foo bar");
48+
```
49+
50+
The command string is interpreted in a similar way to the format
51+
string for `printf()`, so you can easily interpolate string values from
52+
your code into the command with the `%s` format specifier:
53+
54+
```c
55+
char *myKeyNumber = "1";
56+
char *myValue = "Hello";
57+
58+
// This issues the command 'SET key:1 Hello'.
59+
redisReply *reply = redisCommand(c, "SET key:%s %s", myKeyNumber, myValue);
60+
```
61+
62+

content/develop/clients/om-clients/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description: Object-Mapper libraries for Redis Stack
1414
linkTitle: Object mapping
1515
stack: true
1616
title: Object-Mapper libraries
17-
weight: 10
17+
weight: 20
1818
---
1919

2020
Redis OM (pronounced *REDiss OHM*) is a library that provides object mapping for Redis. With the help of Redis OM, you can map Redis data types, specifically Hashes and JSON documents, to objects of your preferred programming language or framework. Redis OM relies on Redis Stack's JSON, query, and search features, allowing you to query and search for objects.

content/develop/clients/pools-and-muxing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ categories:
1313
description: Manage Redis connections efficiently
1414
linkTitle: Pooling/multiplexing
1515
title: Connection pools and multiplexing
16-
weight: 30
16+
weight: 40
1717
---
1818

1919
Redis example code generally opens a connection, demonstrates

0 commit comments

Comments
 (0)