1

I have two hashes in the redis database with names "hash1" and "hash2". I created those hashes in another python file. Now i want to get all the keys and values that are in those hashes with hiredis in a .c file. Is that possible? I have only seen examples where you should now the name of the keys in order to take their values but i want to get all the keys and values according to the hash's name.Basically i want this command redis_cache.hgetall(HASH_NAME) but with hiredis.

Thank you

jmfel
  • 43
  • 2
  • 9

1 Answers1

3

A redisReply is a typed object (see the type field), and a multi-bulk reply has a specific type (REDIS_REPLY_ARRAY). Look at the hiredis documentation:

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.

The HGETALL return they key value as list there each value may be found after each key:

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);
Nick Bondarenko
  • 6,211
  • 4
  • 35
  • 56