设为首页 收藏本站
查看: 1133|回复: 0

[经验分享] Redis教程(十五):C语言连接操作代码实例

[复制链接]

尚未签到

发表于 2018-11-5 12:17:13 | 显示全部楼层 |阅读模式
  转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/143.html
  在之前的博客中已经非常详细的介绍了Redis的各种操作命令、运行机制和服务器初始化参数配置。本篇博客是该系列博客中的最后一篇,在这里将给出基于Redis客户端组件访问并操作Redis服务器的代码示例。然而需要说明的是,由于Redis官方并未提供基于C接口的Windows平台客户端,因此下面的示例仅可运行于Linux/Unix平台。但是对于使用其它编程语言的开发者而言,如C#和Java,Redis则提供了针对这些语言的客户端组件,通过该方式,同样可以达到基于Windows平台与Redis服务器进行各种交互的目的。
  该篇博客中使用的客户端来自于Redis官方网站,是Redis推荐的基于C接口的客户端组件,见如下链接:
  https://github.com/antirez/hiredis
  在下面的代码示例中,将给出两种最为常用的Redis命令操作方式,既普通调用方式和基于管线的调用方式。
  注:在阅读代码时请留意注释。
#include #include #include #include #include #include #include void doTest(){  int timeout = 10000;  struct timeval tv;  tv.tv_sec = timeout / 1000;  tv.tv_usec = timeout * 1000;  //以带有超时的方式链接Redis服务器,同时获取与Redis连接的上下文对象。  //该对象将用于其后所有与Redis操作的函数。  redisContext* c = redisConnectWithTimeout("192.168.149.137",6379,tv);  if (c->err) {    redisFree(c);    return;  }  const char* command1 = "set stest1 value1";  redisReply* r = (redisReply*)redisCommand(c,command1);  //需要注意的是,如果返回的对象是NULL,则表示客户端和服务器之间出现严重错误,必须重新链接。  //这里只是举例说明,简便起见,后面的命令就不再做这样的判断了。  if (NULL == r) {    redisFree(c);    return;  }  //不同的Redis命令返回的数据类型不同,在获取之前需要先判断它的实际类型。  //至于各种命令的返回值信息,可以参考Redis的官方文档,或者查看该系列博客的前几篇  //有关Redis各种数据类型的博客。:)  //字符串类型的set命令的返回值的类型是REDIS_REPLY_STATUS,然后只有当返回信息是"OK"  //时,才表示该命令执行成功。后面的例子以此类推,就不再过多赘述了。  if (!(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK") == 0)) {    printf("Failed to execute command[%s].n",command1);    freeReplyObject(r);    redisFree(c);    return;  }  //由于后面重复使用该变量,所以需要提前释放,否则内存泄漏。  freeReplyObject(r);  printf("Succeed to execute command[%s].n",command1);  const char* command2 = "strlen stest1";  r = (redisReply*)redisCommand(c,command2);  if (r->type != REDIS_REPLY_INTEGER) {    printf("Failed to execute command[%s].n",command2);    freeReplyObject(r);    redisFree(c);    return;  }  int length = r->integer;  freeReplyObject(r);  printf("The length of 'stest1' is %d.n",length);  printf("Succeed to execute command[%s].n",command2);  const char* command3 = "get stest1";  r = (redisReply*)redisCommand(c,command3);  if (r->type != REDIS_REPLY_STRING) {    printf("Failed to execute command[%s].n",command3);    freeReplyObject(r);    redisFree(c);    return;  }  printf("The value of 'stest1' is %s.n",r->str);  freeReplyObject(r);  printf("Succeed to execute command[%s].n",command3);  const char* command4 = "get stest2";  r = (redisReply*)redisCommand(c,command4);  //这里需要先说明一下,由于stest2键并不存在,因此Redis会返回空结果,这里只是为了演示。  if (r->type != REDIS_REPLY_NIL) {    printf("Failed to execute command[%s].n",command4);    freeReplyObject(r);    redisFree(c);    return;  }  freeReplyObject(r);  printf("Succeed to execute command[%s].n",command4);  const char* command5 = "mget stest1 stest2";  r = (redisReply*)redisCommand(c,command5);  //不论stest2存在与否,Redis都会给出结果,只是第二个值为nil。  //由于有多个值返回,因为返回应答的类型是数组类型。  if (r->type != REDIS_REPLY_ARRAY) {    printf("Failed to execute command[%s].n",command5);    freeReplyObject(r);    redisFree(c);    //r->elements表示子元素的数量,不管请求的key是否存在,该值都等于请求是键的数量。    assert(2 == r->elements);    return;  }  for (int i = 0; i < r->elements; ++i) {    redisReply* childReply = r->element;    //之前已经介绍过,get命令返回的数据类型是string。    //对于不存在key的返回值,其类型为REDIS_REPLY_NIL。    if (childReply->type == REDIS_REPLY_STRING)      printf("The value is %s.n",childReply->str);  }  //对于每一个子应答,无需使用者单独释放,只需释放最外部的redisReply即可。  freeReplyObject(r);  printf("Succeed to execute command[%s].n",command5);  printf("Begin to test pipeline.n");  //该命令只是将待发送的命令写入到上下文对象的输出缓冲区中,直到调用后面的  //redisGetReply命令才会批量将缓冲区中的命令写出到Redis服务器。这样可以  //有效的减少客户端与服务器之间的同步等候时间,以及网络IO引起的延迟。  //至于管线的具体性能优势,可以考虑该系列博客中的管线主题。  if (REDIS_OK != redisAppendCommand(c,command1)    || REDIS_OK != redisAppendCommand(c,command2)    || REDIS_OK != redisAppendCommand(c,command3)    || REDIS_OK != redisAppendCommand(c,command4)    || REDIS_OK != redisAppendCommand(c,command5)) {    redisFree(c);    return;  }  redisReply* reply = NULL;  //对pipeline返回结果的处理方式,和前面代码的处理方式完全一直,这里就不再重复给出了。  if (REDIS_OK != redisGetReply(c,(void**)&reply)) {    printf("Failed to execute command[%s] with Pipeline.n",command1);    freeReplyObject(reply);    redisFree(c);  }  freeReplyObject(reply);  printf("Succeed to execute command[%s] with Pipeline.n",command1);  if (REDIS_OK != redisGetReply(c,(void**)&reply)) {    printf("Failed to execute command[%s] with Pipeline.n",command2);    freeReplyObject(reply);    redisFree(c);  }  freeReplyObject(reply);  printf("Succeed to execute command[%s] with Pipeline.n",command2);  if (REDIS_OK != redisGetReply(c,(void**)&reply)) {    printf("Failed to execute command[%s] with Pipeline.n",command3);    freeReplyObject(reply);    redisFree(c);  }  freeReplyObject(reply);  printf("Succeed to execute command[%s] with Pipeline.n",command3);  if (REDIS_OK != redisGetReply(c,(void**)&reply)) {    printf("Failed to execute command[%s] with Pipeline.n",command4);    freeReplyObject(reply);    redisFree(c);  }  freeReplyObject(reply);  printf("Succeed to execute command[%s] with Pipeline.n",command4);  if (REDIS_OK != redisGetReply(c,(void**)&reply)) {    printf("Failed to execute command[%s] with Pipeline.n",command5);    freeReplyObject(reply);    redisFree(c);  }  freeReplyObject(reply);  printf("Succeed to execute command[%s] with Pipeline.n",command5);  //由于所有通过pipeline提交的命令结果均已为返回,如果此时继续调用redisGetReply,  //将会导致该函数阻塞并挂起当前线程,直到有新的通过管线提交的命令结果返回。  //最后不要忘记在退出前释放当前连接的上下文对象。  redisFree(c);  return;}int main() {  doTest();  return 0;}//输出结果如下://Succeed to execute command[set stest1 value1].//The length of 'stest1' is 6.//Succeed to execute command[strlen stest1].//The value of 'stest1' is value1.//Succeed to execute command[get stest1].//Succeed to execute command[get stest2].//The value is value1.//Succeed to execute command[mget stest1 stest2].//Begin to test pipeline.//Succeed to execute command[set stest1 value1] with Pipeline.//Succeed to execute command[strlen stest1] with Pipeline.//Succeed to execute command[get stest1] with Pipeline.//Succeed to execute command[get stest2] with Pipeline.//Succeed to execute command[mget stest1 stest2] with Pipeline.#p#分页标题#e#


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631106-1-1.html 上篇帖子: REDIS GEO: REDIS新增位置查询功能 下篇帖子: Redis教程(十四):内存优化介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表