`
haoningabc
  • 浏览: 1489855 次
  • 性别: 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;
 }
分享到:
评论

相关推荐

    C++ Redis 客户端简单使用

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

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

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

    hiredis Redis 客户端库的绑定.zip

    hiredis Redis 客户端库的绑定lua-hiredis — hiredis Redis 客户端库的绑定请参阅名为 的文件中版权信息COPYRIGHT。项目状态lua-hiredis 模块非常稳定。作者和团队在高负载生产环境中大量使用它。在功能完整性方面...

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

    windows RedisClient redis客户端

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

    Go 的 Redis 客户端将完整的 redis 命令列表映射到等效的 Go 函数 .zip

    menteslibres.net/gosexy/redis此软件包redis以前是官方 redis C 客户端 hiredis的包装器。截至 2014 年 10 月,软件包redis被拆分为两个不同的软件包RESP 解码器( resp) 和redis 客户端 ( redis),本页面对此进行了...

    windows 操作系统redis客户端

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

    Eventmachine redis 客户端.zip

    Eventmachine redis 客户端埃姆希雷迪斯什么EventMachine 的 Redis 客户端设计得快速而简单。为什么我想要一个这样的客户使用 C hiredis 库来解析 redis 回复有一个方便的 pubsub API公开底层 redis 连接的状态,...

    vs2003-hiredis-vc++客户端连接池,线程安全

    本项目"vs2003-hiredis-vc++客户端连接池,线程安全"正是针对这种情况设计的,旨在提供一个线程安全的Redis客户端连接池解决方案。 首先,我们需要了解`hiredis`。`hiredis`是由Antirez编写的轻量级、高性能的C语言...

    redis 和 qt hiredis

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

    C++ Redis 客户端.zip

    redis3m 一个 C++ Redis客户端,旨在带来我在开源库上使用 Redis 和 C++ 的经验。主要目标提供一个简单而高效的hiredis包装器,带有内存管理等 C++ 功能连接池系统,使用 sentinel 支持高可用性一组随时可用且可与...

    Twisted Python 的 Redis 客户端库.zip

    Twisted Python 的 Redis 客户端库txRedisTwisted Python的异步Redis客户端。安装通过pip安装。使用示例可在本存储库的 examples/ 目录中找到。pip 安装 txredis包含两个协议实现,一个使用自定义的基于 Twisted 的...

    利用hiredis库连接redis源码

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

    Go 的异步 Redis 客户端.zip

    Go 的异步 Redis 客户端基数Radix 是 Go 的 Redis 客户端。安装go get github.com/fzzbt/radix/redis运行测试go get -u launchpad.net/gocheckcd $GOROOT/src/pkg/github.com/fzzbt/radix/redisgo test -v -bench="....

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

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

    现代、异步、超快的 C++11 Redis 客户端.zip

    现代、异步、超快的 C++11 Redis 客户端氧化还原现代、异步且速度超快的 C++11 Redis 客户端 [ ] ( https://travis-ci.org/hmartiro/redox )Redox 是Redis键值存储的 C++ 接口 ,可让您轻松编写既优雅又高性能的应用...

    Python asyncio 的 redis 客户端(支持 redis 服务器、sentinel 和 cluster).zip

    To install aredis, just $ pip3 install aredis[hiredis] or from source $ python setup.py install Getting Started More Examples Tips Starting with Python 3.8, you can use asyncio REPL $ python -m ...

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

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

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

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

Global site tag (gtag.js) - Google Analytics