zscan的返回值能夠看作是一個二維數組,第一維包含兩個元素:string類型的遊標cursor和集合元素數組;第二維即集合元素數組,這個數組交替存放着集合元素和score,元素和score也都是string類型的。固然全部的值都是經過指針來引用的,因此使用時務必當心。redis
當cursor爲0時表示,掃描結束;非0的cursor值用來進行後續掃描。api
集合元素數組reply->elements指示當前數組中包含多少個元素(指針),據此來遍歷整個數組。數組
示例代碼以下:post
//cLocal points to the local redisspa
llCursor=0;指針
done=false;element
while(!done)get
{cmd
cmd="zscan "+setName+" %lld count 10";
reply = (redisReply *)redisCommand(cLocal, cmd.c_str(), llCursor);
if(reply == NULL)
{
cout << "scan " << setName << "failed, error is: " << cLocal->errstr;
redisFree(cLocal);
cLocal = NULL;
break;
}博客if(reply->type == REDIS_REPLY_ARRAY)
{
if(reply->elements == 0)
{
done=true;
cout << "get 0 msg from " << setName;
}
else
{
llCursor=boost::lexical_cast<long long>(reply->element[0]->str);redisReply ** siteCounters=reply->element[1]->element;
for(size_t i=0; i<reply->element[1]->elements; i++)
{
string elem = siteCounters[i++]->str;
string score = siteCounters[i]->str;}
if(llCursor == 0)
{
done=true;
}
}
}
else
{
done=true;
}
freeReplyObject(reply);}
hscan和zscan api的用法是同樣的。
本文轉自個人我的博客「零一積流」,原連接在這裏:http://www.it-refer.com/2015/11/06/hiredis-zscan-hscan-reply/