记一个使用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

切忌想当然啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值