使用C中的hiredis访问redis散列



redis数据库中有两个散列,名称分别为"hash1"one_answers"hash2"。我在另一个python文件中创建了这些散列。现在,我想在一个.c文件中使用hiredis获得这些哈希中的所有键和值。这可能吗?我只看到过这样的例子,你现在应该使用键的名称来获取它们的值,但我想根据哈希的名称来获得所有的键和值。基本上我想要这个命令redis_cache.hgetall(HASH_NAME)但与hiredis。

感谢

redisReply是一个类型化对象(请参阅类型字段),多批量回复具有特定类型(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: %sn", reply->str );
} else if ( reply->type != REDIS_REPLY_ARRAY ) {
  printf( "Unexpected type: %dn", 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);

最新更新