`
haoningabc
  • 浏览: 1465665 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

redis客户端hiredis

阅读更多
参考:http://blog.csdn.net/hj19870806/article/details/8724907
#include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <stdarg.h>
 #include <string.h>
 #include <assert.h>
 #include <hiredis/hiredis.h>
 
 void doTest()
 {
     int timeout = 10000;
     struct timeval tv;
     tv.tv_sec = timeout / 1000;
     tv.tv_usec = timeout * 1000;
     //以带有超时的方式链接Redis服务器,同时获取与Redis连接的上下文对象。
     //该对象将用于其后所有与Redis操作的函数。
     redisContext* c = redisConnect((char*)"127.0.0.1", 6379);
     if (c->err) {
         redisFree(c);
         return;
     }
     const char* command1 = "set stest1 value9";
     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;
     }
     int i;
      for (i = 0; i < r->elements; ++i) {
         redisReply* childReply = r->element[i];
         //之前已经介绍过,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;
     }
 */

    redisAppendCommand(c,command1);
    redisAppendCommand(c,command2);
    redisAppendCommand(c,command3);
    redisAppendCommand(c,command4);
    redisAppendCommand(c,command5);
     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;
 }
分享到:
评论

相关推荐

    redis使用c++ API 的hiredis连接详解

    此代码实现了redis使用hiredis c接口开发,具体实现了string和list类型的存取,具体信息请看 https://blog.csdn.net/bwangk/article/details/83060374

    C++操作redis的客户端hiredis库

    **C++操作Redis的客户端Hiredis库** Redis是一个高性能的键值存储系统,常用于数据缓存、消息队列等场景。为了在C++应用中与Redis进行交互,我们通常会使用客户端库,其中Hiredis是一个轻量级、高效的选项。本文将...

    Redis的C客户端hiRedis.zip

    hiRedis 是 Redis 官方指定的 C 语言客户端开发包,支持 Redis 完整的命令集、管线以及事件驱动编程。示例代码:redisContext *c = redisConnect("127.0.0.1", 6379); if (c-&gt;err) { printf("Error: %s\n", c-&gt;...

    C++ Redis 客户端简单使用

    本篇文章将深入探讨如何在C++中简单使用Redis客户端。 首先,为了在C++中与Redis进行交互,我们需要一个支持C++的Redis客户端库。常见的选择有`hiredis`,这是一个轻量级的C库,同时也提供了C++绑定。另一个是`cpp-...

    windows RedisClient redis客户端

    Windows上的Redis客户端是开发者在Windows操作系统中与Redis服务器交互的工具,它允许用户查看、操作和管理Redis缓存中的数据。 标题“windows RedisClient redis客户端”指的是在Windows环境下使用的Redis客户端...

    windows 操作系统redis客户端

    本篇将详细介绍如何在Windows环境下使用Redis客户端,以及相关的重要知识点。 1. Redis简介: Redis是一种基于内存的数据结构存储系统,它可以作为数据库、缓存和消息中间件。它支持多种数据结构,如字符串、哈希...

    redis 和 qt hiredis

    然后,可以创建一个简单的 Redis 客户端类,封装 hiredis 的函数,如 `redisConnect`、`redisCommand` 和 `redisFree` 等,以便在 Qt 代码中方便调用。 例如,一个基本的 Redis 客户端类可能如下所示: ```cpp #...

    利用hiredis库连接redis源码

    `hiredis`是一个高效、简洁的C语言`Redis`客户端库,它使得与`Redis`服务器进行交互变得更加简单。 首先,我们需要了解`Redis`。`Redis`是一款开源的、基于键值对的数据存储系统,它通常用作数据库、缓存和消息代理...

    Python-一个redis客户端实现设计于使用micropython

    在Python中,有多个Redis客户端库可供选择,如`redis-py`和`hiredis-py`。这些库提供了丰富的API,允许开发者方便地与Redis服务器交互,包括但不限于设置和获取键值、操作列表、集合、哈希表以及发布/订阅消息。例如...

    cpp-xRedis是一个C开发的redis客户端是对hiredis的C封装

    能与特点: 支持数据多节点分布存储,可自定义分片规则; 支持连接到官方集群,支持自动计算节点索引位置; 支持同时连接到每个分片的主从节点,支持主从读写分离;...支持对每个存储节点建立连接池;...

    tinyredis:基于hiredis的简单Java redis客户端

    小Redis 基于hiredis的简单Java redis客户端tinyredis 相对于其他 Java 客户端的优势在于其简单性提供的灵活性。 客户端与所使用的 redis 版本无关,并且不需要升级,因为在服务器上实现了新的命令和功能。 在掌握了...

    Redis32,64客户端和服务器以及客户端图形化软件

    提到的"02_window版redis客户端图形化安装程序"很可能是如RedisInsight、Redis Desktop Manager或Hiredis这样的工具。这类软件提供了友好的界面,使得用户可以通过鼠标点击和拖拽完成大部分操作,包括连接到Redis...

    redis-wstream:redis-wstream是一个node.js redis写入流,它使用现有的redis客户端(streams2)将二进制或utf8数据流式传输到redis密钥中。 用mranneynode_redis客户端测试

    redis-wstream redis-wstream是一个node.js redis写入流,它使用现有的redis客户端将二进制或utf8数据流化为redis密钥。 使用mranney / node_redis客户端进行了测试。 (streams2)安装npm install redis-wstream 您...

    aredis:用于Python asyncio的redis客户端(已支持redis服务器,哨兵和集群)

    阿雷迪斯 从 (这是Redis键值的Python接口)移植的高效且用户友好的异步redis客户端要获取更多信息,请阅读安装aredis需要运行中的Redis服务器。 要安装aredis,只需: $ pip3 install aredis[hiredis] 或从来源: $...

    HiRedis.jl:包装了hiredis C 库的Julia Redis 客户端

    包装了hiredis C 库的Julia Redis 客户端。 是 Redis 数据库的 C 客户端库。 是一个开源、BSD 许可的高级键值缓存和存储。 它通常被称为数据结构服务器,因为键可以包含字符串、散列、列表、集合、排序集合、位图...

    一个非常简单的消息队列中间件。在redis数据库和hiredis的基础上,设计的一个-Easy_redis_mq.zip

    Hiredis是Redis官方推荐的C语言客户端库,它专注于提供高效的Redis协议解析能力,简化了与Redis服务器交互的复杂性。Hiredis的轻量级特性使得它成为嵌入式系统和高并发环境下的首选客户端库。 **三、Easy_redis_mq...

    phpiredis:基于Hiredis的RedisPHP扩展

    Phpiredis是基于PHP 5.x至8.x的,它为Redis提供了一个简单高效的客户端,并为提供了一个快速的增量解析器/序列化器。 安装 要构建和使用此扩展,需要在系统上安装hiredis (&gt; = 0.14,&gt; = 1.0)。 通常,在大多数...

    redis连接库hiredis安装包

    使用Hiredis,你可以轻松地在C程序中构建高效的Redis客户端。在其他编程语言中,如Python、Java、Node.js等,也有基于Hiredis实现的客户端库,它们通常更易用,同时得益于Hiredis的底层性能优化。 总之,Hiredis是...

    node-redis-promised:具有 Promise 支持的 Node Redis 客户端

    redis-promised - 具有 promise 支持的 node.js redis 客户端这是一个完整的基于node.js Redis 客户端。 它支持所有 Redis 命令。 这个库增加了承诺支持,同时还保持了原始的回调支持。 安装: npm install redis-...

    PHP Swoole异步Redis客户端实现方法示例

    接下来,为了使用Swoole的异步Redis客户端,还需要安装`hiredis`库,这是一个高性能的Redis协议解析库。在CentOS系统中,可以通过`yum install libhiredis-devel`命令进行安装,在Ubuntu系统中,使用`apt install ...

Global site tag (gtag.js) - Google Analytics