Skip to content

Commit 7857d00

Browse files
Merge pull request #1332 from redis/DOC-4996-hiredis-docs
DOC-4996 hiredis docs
2 parents dd1cef1 + 4ac92d9 commit 7857d00

File tree

9 files changed

+756
-5
lines changed

9 files changed

+756
-5
lines changed

content/develop/clients/_index.md

+2-2
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

+1-1
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
+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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
19+
[C language](https://en.wikipedia.org/wiki/C_(programming_language))
20+
client for Redis.
21+
The sections below explain how to install `hiredis` and connect your application
22+
to a Redis database.
23+
24+
`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.
25+
26+
## Build and install
27+
28+
Clone or download the `hiredis` source from the [Github repository](https://github.com/redis/hiredis).
29+
Then, in a terminal, go into the `hiredis` folder and run the `make` command to build
30+
the dynamically-loaded library for `hiredis` (this has the name `libhiredis.dylib` on
31+
MacOS and `libhiredis.so` on Linux). You can copy this library to your
32+
project folder or run `sudo make install` to install it to `/usr/local/lib`.
33+
You should also copy the header files `hiredis.h`, `alloc.h`, `read.h`, and
34+
`sds.h` to your project.
35+
36+
## Connect and test
37+
38+
The code in the example below connects to the server, stores and retrieves
39+
a string key using [`SET`]({{< relref "/commands/set" >}}) and
40+
[`GET`]({{< relref "/commands/get" >}}), and then finally closes the
41+
connection. An explanation of the code follows the example.
42+
43+
```c
44+
#include <stdio.h>
45+
46+
#include "hiredis.h"
47+
48+
int main() {
49+
// The `redisContext` type represents the connection
50+
// to the Redis server. Here, we connect to the
51+
// default host and port.
52+
redisContext *c = redisConnect("127.0.0.1", 6379);
53+
54+
// Check if the context is null or if a specific
55+
// error occurred.
56+
if (c == NULL || c->err) {
57+
if (c != NULL) {
58+
printf("Error: %s\n", c->errstr);
59+
// handle error
60+
} else {
61+
printf("Can't allocate redis context\n");
62+
}
63+
64+
exit(1);
65+
}
66+
67+
// Set a string key.
68+
redisReply *reply = redisCommand(c, "SET foo bar");
69+
printf("Reply: %s\n", reply->str); // >>> Reply: OK
70+
freeReplyObject(reply);
71+
72+
// Get the key we have just stored.
73+
reply = redisCommand(c, "GET foo");
74+
printf("Reply: %s\n", reply->str); // >>> Reply: bar
75+
freeReplyObject(reply);
76+
77+
// Close the connection.
78+
redisFree(c);
79+
}
80+
```
81+
82+
For a real project, you would build your code with a makefile, but for
83+
this simple test, you can just place it in a file called `main.c` and
84+
build it with the following command (assuming you used `make install` to
85+
install the `libhiredis` library):
86+
87+
```bash
88+
cc main.c -L/usr/local/lib -lhiredis
89+
```
90+
91+
The default executable filename is `a.out`. If you run this file from
92+
the terminal, you should see the following output:
93+
94+
```
95+
% ./a.out
96+
Reply: OK
97+
Reply: bar
98+
```
99+
100+
The code first uses `redisConnect()` to open the connection for
101+
all subsequent commands to use. See
102+
[Connect]({{< relref "/develop/clients/hiredis/connect" >}}) for
103+
more information about connecting to Redis.
104+
105+
The `redisCommand()` function
106+
issues commands to the server, each of which returns a
107+
`redisReply` pointer. Here, the reply is a string, which you can
108+
access using the `str` field of the reply. The `redisCommand()`
109+
call allocates memory for the reply, so you should free this
110+
with `freeReplyObject()` when you have finished using it.
111+
See [Issue commands]({{< relref "/develop/clients/hiredis/issue-commands" >}})
112+
and [Handle replies]({{< relref "/develop/clients/hiredis/handle-replies" >}})
113+
for more information.
114+
115+
Finally, you should close the connection to Redis with a
116+
call to `redisFree()`. This is not strictly necessary
117+
for this short test program, but real-world code will typically
118+
open and use many connections. You must free them after using them
119+
to prevent errors.
120+
121+
## More information
122+
123+
The [`hiredis`](https://github.com/redis/hiredis) Github repository contains
124+
examples and details that may be useful if you are using `hiredis` to
125+
implement a higher-level client for another programming language. There are
126+
also examples showing how to use `hiredis` from a
127+
[C++ application](https://github.com/redis/hiredis/blob/master/examples/example-qt.cpp)
128+
created with [Qt](https://www.qt.io/) and how to use the
129+
[asynchronous API](https://github.com/redis/hiredis?tab=readme-ov-file#asynchronous-api)
130+
with the [libev](https://software.schmorp.de/pkg/libev.html) and
131+
[libevent](https://libevent.org/) libraries.
132+
133+
See the other pages in this section for more information and examples.
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Connect to the server with `hiredis`.
13+
linkTitle: Connect
14+
title: Connect
15+
weight: 1
16+
---
17+
18+
## Basic synchronous connection
19+
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+
25+
```c
26+
#include <stdio.h>
27+
28+
#include "hiredis.h"
29+
.
30+
.
31+
.
32+
33+
// The `redisContext` type represents the connection
34+
// to the Redis server. Here, we connect to the
35+
// default host and port.
36+
redisContext *c = redisConnect("127.0.0.1", 6379);
37+
38+
// Check if the context is null or if a specific
39+
// error occurred.
40+
if (c == NULL || c->err) {
41+
if (c != NULL) {
42+
printf("Error: %s\n", c->errstr);
43+
// handle error
44+
} else {
45+
printf("Can't allocate redis context\n");
46+
}
47+
48+
exit(1);
49+
}
50+
51+
// Set a string key.
52+
redisReply *reply = redisCommand(c, "SET foo bar");
53+
printf("Reply: %s\n", reply->str); // >>> Reply: OK
54+
freeReplyObject(reply);
55+
56+
// Get the key we have just stored.
57+
reply = redisCommand(c, "GET foo");
58+
printf("Reply: %s\n", reply->str); // >>> Reply: bar
59+
freeReplyObject(reply);
60+
61+
// Close the connection.
62+
redisFree(c);
63+
```
64+
65+

0 commit comments

Comments
 (0)