在 C 中使用 hiredis 访问 redis 哈希

access redis hash with hiredis in C

我在 redis 数据库中有两个哈希,名称为 "hash1" 和 "hash2"。我在另一个 python 文件中创建了这些哈希值。 现在我想在 .c 文件中使用 hiredis 获取这些散列中的所有键和值。 那可能吗?我只看到一些例子,你现在应该输入键的名称以便获取它们的值,但我想根据哈希 name.Basically 获取所有键和值我想要这个命令 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 return 它们将键值作为列表在每个键之后可以找到每个值:

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);