記一個使用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

切忌想當然啊。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM