zscan的返回值可以看做是一個二維數組,第一維包含兩個元素:string類型的游標cursor和集合元素數組;第二維即集合元素數組,這個數組交替存放着集合元素和score,元素和score也都是string類型的。當然所有的值都是通過指針來引用的,所以使用時務必小心。
當cursor為0時表示,掃描結束;非0的cursor值用來進行后續掃描。
集合元素數組reply->elements指示當前數組中包含多少個元素(指針),據此來遍歷整個數組。
示例代碼如下:
//cLocal points to the local redis
llCursor=0;
done=false;
while(!done)
{
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/