记一个使用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
切忌想当然啊。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2011-08-28 《暗时间》的笔记-我在南大的七年
2011-08-28 《暗时间》的笔记-学习习惯(四):知识结构