记一个使用hiredis时犯的错误

使用redis存一些key,value数据,有部分value数据是整形值,于是想当然的写了下边的代码:

 67 bool DBManager::getKeyInteger(const std::string& key, uint64_t& ret_val) {
 68   redisContext* c;
 69   redisReply* reply;
 70 
 71   struct timeval timeout = {1, 500000};
 72   c = redisConnectWithTimeout(m_host.c_str(), m_port, timeout);
 73   if (c->err) {
 74     LOG(ERROR) << "Connection error:" << c->errstr;
 75     return false;
 76   }
 77   reply = reinterpret_cast<redisReply*>(redisCommand(c, "GET %s", key.c_str()));
 78   VLOG(3) << "redisCommand: GET " << key;
 79   if (reply->type != REDIS_REPLY_INTEGER) {
 80     LOG(ERROR) << "redisReply type error:" << reply->type;
 81 
 82     freeReplyObject(reply);
 83     redisFree(c);
 84     return false;
 85   }
 86   ret_val = reply->integer;
 87 
 88   freeReplyObject(reply);
 89   redisFree(c);
 90   return true;
 91 }

这里边的问题在于:即使value值的类型是integer(至少看上去是,实际server也确实是这么存的),但使用GET返回的值的类型(reply->type)仍是REDIS_REPLY_STRING,需要自己程序里转成long long。

看一下redis Protocol specification里对不同类型返回值的说明就非常清楚了。其中Integer reply的说明如下:

Integer reply
This type of reply is just a CRLF terminated string representing an integer, prefixed by a ":" byte. For example ":0\r\n", or ":1000\r\n" are integer replies.
With commands like INCR or LASTSAVE using the integer reply to actually return a value there is no special meaning for the returned integer. It is just an incremental number for INCR, a UNIX time for LASTSAVE and so on.
Some commands like EXISTS will return 1 for true and 0 for false.
Other commands like SADD, SREM and SETNX will return 1 if the operation was actually done, 0 otherwise.
The following commands will reply with an integer reply: SETNX, DEL, EXISTS, INCR, INCRBY, DECR, DECRBY, DBSIZE, LASTSAVE, RENAMENX, MOVE, LLEN, SADD, SREM, SISMEMBER, SCARD

切忌想当然啊。

posted @ 2012-08-28 18:56  刘浩de技术博客  阅读(7617)  评论(0编辑  收藏  举报