设为首页 加入收藏

TOP

windows环境下通过c++使用redis(二)
2015-11-21 01:34:52 来源: 作者: 【 】 浏览:1
Tags:windows 环境 通过 使用 redis
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
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇使用PowerDesigner15对现有数据库.. 下一篇为什么你应该永远不要再使用Mongo..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: