redis 值 hiredis (c/c++)

来源:互联网 发布:封印者n卡优化 编辑:程序博客网 时间:2024/05/21 10:50

0 前言: python 版的redis 直接pyredis接口(pip install 即可);redis server的安装(参照http://www.cnblogs.com/lpshou/p/3167396.html 不错的); 本次是c 引入hiredis第三方模块 ~ GitHub最新的https://github.com/redis/hiredis/tree/v0.13.3 

直接解压 --》 make ---> make install 即可; 


1,hiredis  helloword跑通:

坑一: g++ example.c -lhiredis 报错 需要更改example 的include路径#include<hiredis/hiredis.h>,

  5 #include <hiredis/hiredis.h>
  6 //#include <hiredis.h> 

 reply = (redisReply*)redisCommand(c, "AUTH %s", mypass);  需要显示的转化等


坑二: ./a.out 运行错误:error while loading shared libraries: libhiredis.so.0.10: cannot open shared object file: No such file or directory

ldd ./a.out 需要的库文件不存在,需要加一个软链 默认lib去/lib 或/usr/lib/找,其它路径需要vi /etc/ldconfig.d 加入 ldconfig /usr/lib/

  1. git clone https://github.com/redis/hiredis
  2. cd hiredis
  3. make
  4. sudo make install(复制生成的库到/usr/local/lib目录下)
  5. sudo ldconfig/usr/local/lib

   参照:http://blog.csdn.net/explorer1989/article/details/18980139  或 http://hahaya.github.io/operator-redis-under-linux/


终于跑通


2,redisCommand 返回值介绍(参看自https://github.com/redis/hiredis/tree/v0.13.3)edisCommand函数返回一个东西叫redisReply,我们需要通过判断它的type字段来知道返回了具体什么样的内容:

REDIS_REPLY_STATUS      表示状态,内容通过str字段查看,字符串长度是len字段REDIS_REPLY_ERROR       表示出错,查看出错信息,如上的str,len字段REDIS_REPLY_INTEGER    返回整数,从integer字段获取值REDIS_REPLY_NIL            没有数据返回REDIS_REPLY_STRING      返回字符串,查看str,len字段REDIS_REPLY_ARRAY       返回一个数组,查看elements的值(数组个数),通过element[index]的方式访问数组元素,每个数组元素是 一个redisReply对象的指针

4)另外有一个类似的函数,批量执行命令:

void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);

5)redisReply使用完毕后,需要使用函数freeReplyObject进行释放销毁

void redisFree(redisContext *c)的作用是断开连接,并释放redisContext的内容

6)redisCommand的函数执行流程说明:
  a.格式化redis command
  b.格式化后的命令内容放入redisContext的输出缓冲区
  c.调用redisGetReply函数执行命令,得到结果

7)管道的使用方式:
a.填入需要执行的命令

void redisAppendCommand(redisContext *c, const char *format, ...);void redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);

b.获取命令的输出结果

int redisGetReply(redisContext *c, void **reply);

c.释放输出结果

void freeReplyObject(void *reply);

http://blog.csdn.net/imxiangzi/article/details/52411678  (不错的 从hiredis安装API)

或者参考http://redis.cn/topics/data-types-intro.html#sets、

http://blog.csdn.net/xiejingfa/article/details/50594005

http://www.cnblogs.com/stephen-liu74/archive/2012/03/16/2351859.html



1 0
原创粉丝点击