|
t(r);
redisFree(c);
return;
}
printf("The valueof 'stest1' is %s.\n",r->str);
freeReplyObject(r);
printf("Succeed toexecute 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 toexecute 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 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 toexecute command[%s].\n",command5);
printf("Begin totest 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] withPipeline.\n",command1);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed toexecute command[%s] with Pipeline.\n",command1);
if (REDIS_OK !=redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] withPipeline.\n",command2);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed toexecute command[%s] with Pipeline.\n",command2);
if (REDIS_OK !=redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] withPipeline.\n",command3);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed toexecute command[%s] with Pipeline.\n",command3);
if (REDIS_OK !=redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] withPipeline.\n",command4);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed toe |