Search code examples
c++redisredisclient

C++ RedisClient : how does RedisValue.toInt() work?


This is about RedisClient, which is a C++ client.

I know that you can't store integers in Redis (that are internally converted).

RedisSyncClient::command itself does not support integers as RedisBuffer can't be initialized with int, so:

redis->command("SET", "mykey", 1);

won't compile (you need to add numbers as string).

However RedisValue can be initialized with int (not sure when it is used) and it contains a public "toInt()" method:

redis->command("SET", "mykey", "1"); result = redis->command("GET", "mykey"); cout << "INT: " << result.toInt() << " , STR: " << result.toString() << endl;

INT: 0 , STR : 1

At first I thought that internally strings were being converted to int, but it always return "0". Testing "RedisValue.isInt()" it seems it fails to recognize numbers as it returns "false".

  • What is the intention of "toInt()" if numbers are only seen (by this library) as strings?
  • Am I using it incorrectly?
  • Is this a work-in-progress feature?

Solution

  • Now things make sense to me... It seems that it may be used for redis responses like:

    result = redis->command("EXISTS", "mykey"); cout << "? " << result.isInt() << endl;

    ? 1

    In this case toInt() works as expected.

    I just need to look into redis documentation and see which commands return int. It is not designed to be used with "normal" values.

    (sorry to answer my own question, I would be happy to accept other answers if they cover things I missed out).