我正在使用hiredis C library连接到我的 redis 实例。我正在考虑更改我的 redis.conf 以启用 requirepass
选项。我知道对于 redisConnect()
我只需连接到主机/端口而不进行身份验证,然后使用 redisCommand()
发送 AUTH mypassword
命令和背景。但是,如何为 redisAsyncConnect()
做到这一点呢?查看源代码,当要求发送 AUTH mypassword
命令时,redisAsyncCommand()
函数会失败。
最佳答案
我对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");
}
}
关于c - hiredis 异步连接密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59040520/