您的位置:首页 > 数据库 > Redis

redis客户端hiredis

2015-10-16 10:01 393 查看
Hiredis 在官网 http://redis.io/clients 中有说明This is the official C client. Support for the whole command set, pipelining, event driven programming.

在Linux平台下载hiredis开发包,解压

在命令行中 cd进入解压后的文件夹执行 Make 这样C客户端编译完毕。

接下来也是最关键的 配置。

可以手动配置 将对应.h与.so .a等文件拷贝到/usr/local/lib /user/local/include对应文件夹

或者直接执行自动配置 make install 建议直接使用此方法。

root@i wZ:~/download/hiredis# make install
mkdir -p /usr/local/include/hiredis /usr/local/lib
cp -a hiredis.h async.h read.h sds.h adapters /usr/local/include/hiredis
cp -a libhiredis.so /usr/local/lib/libhiredis.so.0.13
cd /usr/local/lib && ln -sf libhiredis.so.0.13 libhiredis.so
cp -a libhiredis.a /usr/local/lib
mkdir -p /usr/local/lib/pkgconfig
cp -a hiredis.pc /usr/local/lib/pkgconfig

至此hiredis开发包配置完毕

4. 编写程序

可以编写依赖hiredis 的程序了,

头文件需要包含 include <hiredis/hiredis.h>,然后编译的时候加上 -lhiredis

1、redisContext* redisConnect(const char *ip, int port)

说明:该函数用来连接redis数据库,参数为数据库的ip地址和端口,一般redis数据库的端口为6379。

类似的提供了一个函数redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv)

2、void *redisCommand(redisContext *c, const char *format, ...);

说明:该函数执行redis数据库中的操作命令,第一个参数为连接数据库时返回的redisContext,剩下的参数为变参,就如C标准函数printf函数一样的变参。

返回值为void*,一般强制转换成为redisReply类型,以便做进行进一步的处理。

3、void freeReplyObject(void *reply);

说明:释放redisCommand执行后返回的redisReply所占用的内存

4、void redisFree(redisContext *c);

说明:释放redisConnect()所产生的连接。

文档:
https://github.com/redis/hiredis
#include<hiredis/hiredis.h>

void doTest()
{
//redis默认监听端口是6379
redisContext* c=redisConnect("127.0.0.1",6379);
if(NULL==c || c->err)
{
redisFree(c);
printf("Connect tor redisServer failed.%s",c->errstr);
exit(1);
}
const char* command1="set stest1 value1";
redisReply* reply=(redisReply*)redisCommand(c,command1); // 执行命令,结果强转成redisReply*类型
if( NULL == reply)
{
printf("Execut command1 failure\n");
redisFree(c);     // 命令执行失败,释放内存
return;
}

if(! (reply->type== REDIS_REPLY_STATUS && strcasecmp(reply->str,"OK")==0 ))
{
printf("failed to execute command [%s]\n",command1);
freeReplyObject(reply);
redisFree(c);
return;
}
freeReplyObject(reply);
printf("Succeed to execute command[%s]\n", command1);
// 一切正常,则对返回值进行处理
}


g++ -lhiredis redisTest.c -o redisTest

#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;
}


参考:
http://blog.csdn.net/mniwc/article/details/12851837 http://blog.csdn.net/szchtx/article/details/21741843
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: