Updated on 2024-05-20 GMT+08:00

Connecting to an Instance Using Hiredis

This section describes how to use hiredis to access a GeminiDB Redis instance.

Prerequisites

  • A GeminiDB Redis instance has been created and is running properly. For details about how to create a GeminiDB Redis instance, see Buying a Cluster Instance.
  • An ECS is available. For details, see Purchasing an ECS.
  • The GNU Compiler Collection (GCC) has been installed on the ECS.
  • The created ECS is in the same region, AZ, VPC, and security group as the GeminiDB Redis instance.

Procedure

  1. Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.

  2. Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
  3. Run the following command to download and decompress the hiredis package.

    wget https://github.com/redis/hiredis/archive/master.zip

  4. Go to the directory where the decompressed hiredis package is saved, and compile and install hiredis.

    make

    make install

  5. Write the test code connRedisTst.cc.

    For details about how to use hiredis, see the usage description on the Redis official website.

    The code is as follows:

    #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", "key", "hiredis test ok!");
         printf("SET: %s\n", reply->str);
         freeReplyObject(reply);
     
         /* Get */
         reply = redisCommand(conn,"GET key");
         printf("GET key: %s\n", reply->str);
         freeReplyObject(reply);
     
         /* Disconnects and frees the context */
         redisFree(conn);
         return 0;
    }

  6. Run the following command to perform compilation:

    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 stored and modify the compilation command.

    After the compilation, an executable connRedis file is obtained.

  7. Run the following command to connect to the instance.

    export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

    ./connRedis <redis_ip_address> 8635 <password>

    Replace the following information based on the site requirements:

    • <redis_ip_address> indicates the load balancer IP address obtained in 1.
    • 8635 is the port for accessing the GeminiDB Redis instance.
    • <password> indicates the password set when the instance is created.

  8. If the following information is displayed, the instance is successfully connected.

    AUTH: OK
    SET: OK
    GET key: Hello, hiredis test ok!