标题:[实践OK]redis C接口hiredis 简单函数使用介绍 出处:向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除 时间:Wed, 06 Sep 2017 10:14:55 +0000 作者:jackxiang 地址:http://jackxiang.com/post/9445/ 内容: 背景:搞一些Nginx的扩展啥的都用那个hiredis进行静态编译后,编译进Nginx,这儿先研究一下这个Redis的C连接认证以及取数据先。 代码@/usr/local/src/hiredis: #include #include #include #include #include #include #include void doTest() { //redis默认监听端口为6387 可以再配置文件中修改 redisContext* c = redisConnect("127.0.0.1", 6379); if ( c->err) { redisFree(c); printf("Connect to redisServer faile\n"); return ; } printf("Connect to redisServer Success\n"); //验证 const char* password = "XjackMe42108"; redisReply* r = (redisReply *)redisCommand(c, "auth %s", password); if (r->type == REDIS_REPLY_ERROR) { printf("Authentication failure\n"); } else { printf("Authentication success\n"); } const char* command1 = "set stest1 value1"; r = (redisReply*)redisCommand(c, command1); if( NULL == r) { printf("Execut command1 failure\n"); redisFree(c); return; } 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); 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); redisFree(c); } int main() { doTest(); return 0; } 编译一下: gcc redisTest.c /home/test/rpmbuild/BUILD/ngx_http_monitor_module-2.2.0/hiredis/libhiredis.a -I/home/test/rpmbuild/BUILD/ngx_http_monitor_module-2.2.0/ 关于头文件之 linux下C include搜索的路径:http://blog.csdn.net/chosen0ne/article/details/7210946 编译静态的resis静态链接库make static,别make 会生成动态连接库,如果只指定 -L路径,会弄成了动态编译,有依赖SO的情况,静态文件大点无所谓了: cd hiredis make clean make static 静态编译出来就是这样的: ldd a.out linux-vdso.so.1 => (0x00007ffeec95e000) libc.so.6 => /lib64/libc.so.6 (0x0000003771400000) /lib64/ld-linux-x86-64.so.2 (0x0000003771000000) 执行: ./a.out Connect to redisServer Success Authentication success 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] 参考:http://blog.csdn.net/mniwc/article/details/12851837 Generated by Jackxiang's Bo-blog 2.1.1 Release