我正在使用hiredis C library连接到我的redis实例。我正在考虑更改我的redis.conf以启用requirepass选项。我知道对于redisConnect(),我只需不经过身份验证就连接到主机/端口,然后使用redisCommand()和上下文发送AUTH mypassword命令。然而,如何为redisAsyncConnect()做到这一点呢?查看源代码,当要求发送AUTH mypassword命令时,redisAsyncCommand()函数将失败。
发布于 2019-11-26 23:04:09
我对redisAsyncConnect()函数操作的分析是错误的。以下代码可以工作,唯一的警告是您不知道AUTH命令是否成功:
redisAsyncContext *async_context = redisAsyncConnect(redis_info.host, redis_info.port);
if (async_context->err)
{
throw std::runtime_error("Error creating async_context: " + async_context->errstr);
}
if (0 != strlen(redis_info.passwd))
{
if (REDIS_OK != redisAsyncCommand(async_context, NULL, NULL, "AUTH %s", redis_info.passwd))
{
throw std::runtime_error("Error sending AUTH in async_context");
}
}https://stackoverflow.com/questions/59040520
复制相似问题