1.介紹
hiredis:
一個c/c++的訪問redis的api庫
地址:https://github.com/redis/hiredis
pipeline方式:
redis中的pipeline方式,指的是把多次請求交互封裝到一次完成,只交互一次,類似於多個請求“批處理”成一次交互
好處:
同樣是多次請求,用pipeline方式比多次請求的總的延時低,交互次數少,
即低延遲,高吞吐。
2.代碼
int pipeline_process(struct timeval access_timeout, std::vector<std::string> & pipeline_cmd, std::vector<std::string> &pipeline_resp, std::vector<bool> &pipeline_resp_status) { if (0 == redis_ctx) {return -1;} redisSetTimeout(redis_ctx, access_timeout); for (int i = 0; i < pipeline_cmd.size();i++) { redisAppendCommand(redis_ctx, pipeline_cmd[i].c_str()); } for (int i = 0; i < pipeline_cmd.size();i++) { bool status = false; std::string resp_str = ""; redisReply *reply = 0; if(redisGetReply(redis_ctx, (void **)&reply) == REDIS_OK && reply != NULL && reply->type == REDIS_REPLY_STRING) { status = true; resp_str = reply->str; } //free freeReplyObject(reply); pipeline_resp_status.push_back(status); pipeline_resp.push_back(resp_str); } return 0; }
3.解釋:
參數:
struct timeval access_timeout:訪問的超時時間
std::vector<std::string> & pipeline_cmd:pipeline處理的多個請求命令字符串
std::vector<std::string> &pipeline_resp:pipeline處理的多個請求返回的字符串
std::vector<bool> &pipeline_resp_status:pipeline處理的多個請求返回的狀態