redisContext * pConn = redisConnect(redisIp.c_str(), redisPort);
if (m_cLocal == NULL)
{
return 1;
}redisif(pConn->err)
{
cout << "Connection to redis[" << redisIp << "] error: " << pConn->errstr;
redisFree(pConn);
return 2;
}數據庫
string cmd="set foo bar;
redisReply * reply=(redisReply *)redisCommand(pConn, cmd.c_str());
if(reply == NULL)
{
cout << "redis[" << redisIp << "] excute " << cmd << " failed, error is: " << pConn->errstr;
redisFree(pConn);
pConn=NULL;
return 3;
}
freeReplyObject(reply);get
Once an error is returned the context cannot be reused and you should set up a new connection.cmd
上面示例中執行命令時,若是返回了錯誤,則鏈接不可用,須要從新創建鏈接。string
更多文章,歡迎訪問個人站點:零一積流。it