//redis,add,begin bool QTSServerInterface::ConRedis()//连接redis服务器 { if(fIfConSucess) return true; struct timeval timeout = { 0, 500000 }; // 0.5 seconds fRedisCon = redisConnectWithTimeout(fRedisIP.c_str(), fRedisPort, timeout);//test,redis的ip和端口应该在xml中指定 if (fRedisCon == NULL || fRedisCon->err) { if (fRedisCon) { qtss_printf("INFO:Connect redis failed,%s\n", fRedisCon->errstr); redisFree(fRedisCon); } else { qtss_printf("INFO:Connect redis failed,can't allocate redis context\n"); } fIfConSucess=false; } else { fIfConSucess=true; struct timeval timeoutEx = { 1, 0 }; // 1seconds,设置socket接收和发送超时 redisSetTimeout(fRedisCon,timeoutEx); RedisInit();//可能在这个函数的执行过程中,redis连接又失败了,所以真正的连接失败或者成功要看fIfConSucess qtss_printf("INFO:Connect redis sucess\n"); } return fIfConSucess; }
QTSS_Error RedisConnect() { if (sIfConSucess) return QTSS_NoErr; std::size_t timeout = 1;//timeout second if (sRedisClient->ConnectWithTimeOut(sRedis_IP, sRedisPort, timeout) == EASY_REDIS_OK)//return 0 if connect sucess { qtss_printf("Connect redis sucess\n"); sIfConSucess = true; std::size_t timeoutSocket = 1;//timeout socket second sRedisClient->SetTimeout(timeoutSocket); RedisInit(); } else { qtss_printf("Connect redis failed\n"); sIfConSucess = false; } return (QTSS_Error)(!sIfConSucess); }