1

我在 redis 数据库中有两个哈希,名称为“hash1”和“hash2”。我在另一个 python 文件中创建了这些哈希。现在我想用hiredis在.c文件中获取那些散列中的所有键和值。那可能吗?我只看到了一些示例,您现在应该输入键的名称以获取它们的值,但我想根据哈希的名称获取所有键和值。基本上我想要这个命令 redis_cache.hgetall(HASH_NAME) ,但使用hiredis。

谢谢

4

1 回答 1

2

redisReply 是一个类型化的对象(参见 type 字段),而多批量回复具有特定的类型(REDIS_REPLY_ARRAY)。查看hiredis文档:

The number of elements in the multi bulk reply is stored in reply->elements.
Every element in the multi bulk reply is a redisReply object as well
and can be accessed via reply->element[..index..].
Redis may reply with nested arrays but this is fully supported.

HGETALL 将它们的键值作为列表返回,每个值都可以在每个键之后找到:

redisReply *reply = redisCommand(redis, "HGETALL %s", "foo");
if ( reply->type == REDIS_REPLY_ERROR ) {
  printf( "Error: %s\n", reply->str );
} else if ( reply->type != REDIS_REPLY_ARRAY ) {
  printf( "Unexpected type: %d\n", reply->type );
} else {
  int i;
  for (i = 0; i < reply->elements; i = i + 2 ) {
    printf( "Result: %s = %s \n", reply->element[i]->str, reply->element[i + 1]->str );
  }
}
freeReplyObject(reply);
于 2015-11-10T13:03:43.970 回答