|
| 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. |
0 commit comments