【问题标题】:hiredis async connect with passwordhiredis 异步连接密码
【发布时间】:2019-11-25 21:42:00
【问题描述】:
我正在使用 hiredis C library 连接到我的 redis 实例。我正在考虑更改我的 redis.conf 以启用 requirepass 选项。我知道对于redisConnect(),我只需连接到主机/端口而无需身份验证,然后使用redisCommand() 和上下文发送AUTH mypassword 命令。但是,如何为redisAsyncConnect() 做到这一点?查看源代码,当要求发送AUTH mypassword 命令时,redisAsyncCommand() 函数会失败。
【问题讨论】:
标签:
c
authentication
redis
hiredis
【解决方案1】:
我对@987654321@函数操作的分析有误。以下代码有效,唯一需要注意的是您不知道 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");
}
}