开发者社区> 问答> 正文

如何安装C/C++ 客户端

操作步骤如下所示:


  1. 下载并编译安装C客户端,编译安装代码如下所示:git clone https://github.com/redis/hiredis.git
  2.      cd hiredis
  3.      make
  4.      sudo make install


在 C/C++编辑器中编写如下代码: #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #include <hiredis.h>
     int main(int argc, char **argv) {
     unsigned int j;
     redisContext *c;
     redisReply *reply;
     if (argc < 4) {
     printf("Usage: example xxx.kvstore.aliyuncs.com 6379 instance_id password\n");
     exit(0);
     }
     const char *hostname = argv[1];
     const int port = atoi(argv[2]);
     const char *instance_id = argv[3];
     const char *password = argv[4];
     struct timeval timeout = { 1, 500000 }; // 1.5 seconds
     c = redisConnectWithTimeout(hostname, port, timeout);
     if (c == NULL || c->err) {
     if (c) {
     printf("Connection error: %s\n", c->errstr);
     redisFree(c);
     } else {
     printf("Connection error: can't allocate redis context\n");
     }
     exit(1);
     }
     /* AUTH */
     reply = redisCommand(c, "AUTH %s", password);
     printf("AUTH: %s\n", reply->str);
     freeReplyObject(reply);
     /* PING server */
     reply = redisCommand(c,"PING");
     printf("PING: %s\n", reply->str);
     freeReplyObject(reply);
     /* Set a key */
     reply = redisCommand(c,"SET %s %s", "foo", "hello world");
     printf("SET: %s\n", reply->str);
     freeReplyObject(reply);
     /* Set a key using binary safe API */
     reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
     printf("SET (binary API): %s\n", reply->str);
     freeReplyObject(reply);
     /* Try a GET and two INCR */
     reply = redisCommand(c,"GET foo");
     printf("GET foo: %s\n", reply->str);
     freeReplyObject(reply);
     reply = redisCommand(c,"INCR counter");
     printf("INCR counter: %lld\n", reply->integer);
     freeReplyObject(reply);
     /* again ... */
     reply = redisCommand(c,"INCR counter");
     printf("INCR counter: %lld\n", reply->integer);
     freeReplyObject(reply);
     /* Create a list of numbers, from 0 to 9 */
     reply = redisCommand(c,"DEL mylist");
     freeReplyObject(reply);
     for (j = 0; j < 10; j++) {
     char buf[64];
     snprintf(buf,64,"%d",j);
     reply = redisCommand(c,"LPUSH mylist element-%s", buf);
     freeReplyObject(reply);
         }
     /* Let's check what we have inside the list */
     reply = redisCommand(c,"LRANGE mylist 0 -1");
     if (reply->type == REDIS_REPLY_ARRAY) {
     for (j = 0; j < reply->elements; j++) {
     printf("%u) %s\n", j, reply->element[j]->str);
     }
     }
     freeReplyObject(reply);
     /* Disconnects and frees the context */
     redisFree(c);
     return 0;
     }


编译上述代码。 gcc -o example -g example.c -I /usr/local/include/hiredis -lhiredis

测试运行。 example xxx.kvstore.aliyuncs.com 6379 instance_id password

至此完成通过 C/C++ 客户端连接云数据库 Redis。

展开
收起
云栖大讲堂 2017-10-30 10:30:00 1752 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载