c++ [hiredis]多个hincrby只返回一个结果(REDIS_REPLY_INTEGER)

kninwzqo  于 2022-12-01  发布在  Redis
关注(0)|答案(2)|浏览(182)

使用hiredis时,使用redisAppendCommand放置多个hincrby命令,redisGetReply的reply-〉类型结果为REDIS_REPLY_INTEGER,只返回其中一个结果。
但是当我使用hmget时,reply-〉type的结果是REDIS_REPLY_ARRAY。

5t7ly7z5

5t7ly7z51#

因为你多次调用redisAppendCommand,所以你应该调用redisGetReply相同的次数来得到所有的回复。对于每个回复,它的类型都是REDIS_REPLY_INTEGER。因为hincrby的回复类型是整数类型,或者数组类型。
hmget的回复类型是数组回复,这就是为什么会得到REDIS_REPLY_ARRAY
既然你用c++标记了这个问题,你可以试试redis-plus-plus,这是一个用户友好的C++客户端,你不需要手动解析回复:

auto r = sw::redis::Redis("tcp://127.0.0.1:6379");
for (auto idx = 0; idx < 5; ++idx) {
    r.hincrby("key", "field", 1);
}
std::vector<std::string> fields = {"f1", "f2"};
std::vector<std::optinal<std::string>> vals;
r.hmget("key", fields.begin(), fields.end(), std::back_inserter(vals));
  • 免责声明:我是redis-plus-plus的作者。*
nx7onnlm

nx7onnlm2#

非常感谢你回答我的问题。
而HIREDIS的官方回复就在这里。
https://github.com/redis/hiredis/issues/1143

This is correct given the specifications of both commands.

The hmget command returns an array reply, while hincrby returns an. integer.

Here is a small example that appends several HINCRBY commands and then prints each of the replies.

相关问题