Linux qtcreator编程使用redis客户端hiredis

简介: Linux qtcreator编程使用redis客户端hiredis

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;

}

相关文章
|
NoSQL IDE MongoDB
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
659 3
|
11月前
|
NoSQL IDE MongoDB
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
586 1
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
10月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
2005 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
NoSQL IDE MongoDB
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
1463 21
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
3458 1
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
1138 16
|
安全 算法 Ubuntu
Linux(openssl)环境:编程控制让证书自签的技巧。
总结:在Linux环境中,OpenSSL是一个非常实用的工具,可以帮助我们轻松地生成自签名证书。通过上述三个简单步骤,即可为内部网络、测试环境或开发环境创建自签名证书。但在公共访问场景下,建议购买经过权威认证机构签发的证书,以避免安全警告。
760 13
|
Java Linux 开发工具
Linux下版本控制器(SVN) -命令行客户端
Linux下版本控制器(SVN) -命令行客户端
462 4
|
NoSQL IDE MongoDB
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
598 2
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
SQL Linux 数据库
YashanDB Linux客户端安装
本文详细介绍了YashanDB客户端在Linux系统中的安装、使用与卸载步骤。安装方法包括适用于所有Linux平台的脚本安装和专用于CentOS的rpm安装。脚本安装需解压软件包并配置环境变量,而rpm安装则需以root用户执行相关命令。此外,文章还说明了如何通过yasql连接YashanDB并进行数据库操作,以及两种安装方式对应的卸载方法,帮助用户顺利完成客户端的管理与维护。