Using hiredis in C++ to Access an Instance
Access a DCS Redis instance through hiredis on an ECS in the same VPC. For more information on how to use other Redis clients, visit https://redis.io/clients.
The operations described in this section apply only to single-node, master/standby, and Proxy Cluster instances. To connect to a Redis Cluster instance in C++, see https://github.com/sewenew/redis-plus-plus?_ga=2.64990636.268662337.1603553558-977760105.1588733325#redis-cluster.
Prerequisites
- The DCS Redis instance you want to access is in the Running state.
- An ECS has been created to serve as your Redis client. For more information on how to create ECSs, see the Elastic Cloud Server User Guide.
- If the ECS runs the Linux OS, ensure that the GCC compilation environment has been installed on the ECS.
Procedure
- View the IP address/domain name and port number of the DCS Redis instance to be accessed.
For details, see Viewing Instance Details.
- Log in to the ECS.
- Install GCC, Make, and hiredis.
If the system does not provide a compiling environment, run the following yum command to install the environment:
yum install gcc make
- Run the following command to download and decompress the hiredis package:
wget https://github.com/redis/hiredis/archive/master.zip;
- Go to the directory where the decompressed hiredis package is saved, and compile and install hiredis.
make
make install
- Access the DCS instance by using hiredis.
The following describes connection and password authentication of hiredis. For more information on how to use hiredis, visit the Redis official website.
- Edit the sample code for connecting to a DCS instance, and then save the code and exit.
Example:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <hiredis.h> int main(int argc, char **argv) { unsigned int j; redisContext *conn; redisReply *reply; if (argc < 3) { printf("Usage: example {instance_ip_address} 6379 {password}\n"); exit(0); } const char *hostname = argv[1]; const int port = atoi(argv[2]); const char *password = argv[3]; struct timeval timeout = { 1, 500000 }; // 1.5 seconds conn = redisConnectWithTimeout(hostname, port, timeout); if (conn == NULL || conn->err) { if (conn) { printf("Connection error: %s\n", conn->errstr); redisFree(conn); } else { printf("Connection error: can't allocate redis context\n"); } exit(1); } /* AUTH */ reply = redisCommand(conn, "AUTH %s", password); printf("AUTH: %s\n", reply->str); freeReplyObject(reply); /* Set */ reply = redisCommand(conn,"SET %s %s", "welcome", "Hello, DCS for Redis!"); printf("SET: %s\n", reply->str); freeReplyObject(reply); /* Get */ reply = redisCommand(conn,"GET welcome"); printf("GET welcome: %s\n", reply->str); freeReplyObject(reply); /* Disconnects and frees the context */ redisFree(conn); return 0; } - Run the following command to compile the code:
gcc connRedis.c -o connRedis -I /usr/local/include/hiredis -lhiredis
If an error is reported, locate the directory where the hiredis.h file is saved and modify the compilation command.
After the compilation, an executable connRedis file is obtained.
- Run the following command to access the chosen DCS Redis instance:
./connRedis {redis_ip_address} 6379 {password}
{redis_instance_address} indicates the IP address/domain name of DCS instance and 6379 is an example port number of DCS instance. For details on how to obtain the IP address/domain name and port, see 1. Change the IP address/domain name and port as required. {password} indicates the password used for login to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation.
You have successfully accessed the instance if the following command output is displayed:
AUTH: OK SET: OK GET welcome: Hello, DCS for Redis!
If an error is reported, indicating that the hiredis library files cannot be found, run the following commands to copy related files to the system directories and add dynamic links:
mkdir /usr/lib/hiredis
cp /usr/local/lib/libhiredis.so.0.13 /usr/lib/hiredis/
mkdir /usr/include/hiredis
cp /usr/local/include/hiredis/hiredis.h /usr/include/hiredis/
echo '/usr/local/lib' >>/etc/ld.so.conf
ldconfig
Replace the locations of the so and .h files with actual ones before running the commands.
- Edit the sample code for connecting to a DCS instance, and then save the code and exit.
Video Guide
Watch the following video about accessing a DCS Redis instance.
Last Article: Using phpredis to Access an Instance
Next Article: Using redis-py to Access an Instance
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.