且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux qtcreator编程使用redis客户端hiredis

更新时间:2022-08-13 18:33:34

1. 安装hiredis,下载链接https://github.com/redis/hiredis

这时redis自带的官方的C语言API。Linux安装很简单:

[plain]  view plain  copy


# cd {redis-src}  

# cd deps/hiredis/  

# make  

# make install  

现在hiredis已经被安装于/usr/local/include/hiredis/和/usr/local/lib/下。




2.qtcreator的.pro文件如下:


TEMPLATE = app

CONFIG += console

CONFIG -= app_bundle

CONFIG -= qt


#not good

#LIBS += -L /usr/local/lib -lhiredis

LIBS += "/usr/local/lib/libhiredis.a"


SOURCES += main.c

说明一下,如果使用QMAKE_LFLAGS += -lhiredis,等价于LIBS += /usr/local/lib/libhiredis.so


编译通过,但是运行时会报错:error while loading shared libraries:libhiredis.so.1: cannot open shared object file: No such file or directory

此时需要在/etc/ld.so.conf中加入libhiredis.so所在的目录:/usr/local/lib/

然后在终端执行命令,使之生效:

[root@localhost etc]# ldconfig




3.demo

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <hiredis/hiredis.h>

int main(int argc, char *argv[])

{

   unsigned int j;

   redisContext *c;

   redisReply *reply;

   const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";

   int port = (argc > 2) ? atoi(argv[2]) : 6379;

   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);

   }

   /* 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,"%u",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;

}