Hiredis是一个Redis的C客户端库函数,基本实现了Redis的协议的最小集。

花个两分钟跟我一起配置hiredis

当我们下载了最新版redis的时候,其实就已经自带了C++版本的操作库,只不过有些人没发现罢了。

进入到deps->hiredis目录下(在你的redis解压目录下有deps)

然后:make install

一步到位。

其实连测试函数他们都给你准备好了,在hedis文件夹中还有个文件夹,example,里面有个example.c文件。

这样编译,如果不会的话:首先需要把里面的头文件改一下:#include<hiredis/hiredis.h>
编译的时候记得带上依赖项:
gcc example.c -o example -L/usr/local/lib -lhiredis

当你运行的时候,(别给我说你不会运行:./example)如果不出意外,会跟你说依赖项找不着。
正常,教你一个治标的办法:

在/etc/ld.so.conf.d/目录下新建文件usr-libs.conf,内容是:/usr/local/lib

然后使用命令/sbin/ldconfig更新一下配置即可。

这东西配置完,你虚拟机重启之后就没了,永久配置好像在我的另一篇博客里有,动态库专栏下。

最后的运行效果:

hiredis从安装到项目实战操作

redis的C/C++ API

redisContext* redisConnect(const char *ip, int port);

参数释义:
该函数用来连接redis数据库, 两个参数分别是redis数据库的ip和端口,端口号一般为6379。

void *redisCommand(redisContext *c, const char *format...);

该函数用于执行redis数据库中的命令,第一个参数为连接数据库返回的redisContext,剩下的参数为变参.。

此函数的返回值为void*,但是一般会强制转换为redisReply类型,以便做进一步的处理。

void freeReplyObject(void *reply);

释放redisCommand执行后返回的的redisReply所占用的内存。

void redisFree(redisContext *c)

释放redisConnect()所产生的连接。

实操代码示例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<hiredis/hiredis.h>

int main(int argc, char **argv) {
 unsigned int j, isunix = 0;
 redisContext *c;		
 redisReply *reply;		:
 const char *hostname = (argc > 1) "127.0.0.1";

 if (argc > 2) {
  if (*argv[2] == 'u' || *argv[2] == 'U') {
   isunix = 1;
   /* in this case, host is the path to the unix socket */
   printf("Will connect to unix socket @%s\n", hostname);
  }
 }

 int port = (argc > 2) "Connection error: %s\n", c->errstr);	
   redisFree(c);	//释放redisConnect()所产生的连接。
  } else {
   printf("Connection error: can't allocate redis context\n");
  }
  exit(1);
 }

	 /* PING server */
 reply = redisCommand(c,"PING");	
 //该函数用于执行redis数据库中的命令,第一个参数为连接数据库返回的redisContext,剩下的参数为变参.。
	//此函数的返回值为void*,但是一般会强制转换为redisReply类型,以便做进一步的处理。
 
 printf("PING: %s\n", reply->str);
 freeReplyObject(reply);	//释放redisCommand执行后返回的的redisReply所占用的内存。

	 /* 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;
}
标签:
hiredis安装

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“hiredis从安装到项目实战操作”
暂无“hiredis从安装到项目实战操作”评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?