c - hiredis 异步连接密码

标签 c authentication redis hiredis

我正在使用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/

相关文章:

c - fflush 在命名管道中不起作用

c++ - 将 DKM 项目链接到内核镜像 (VIP) 项目作为 VxWorks Workbench4 中的子项目/额外模块

.net - Azure.Identity 库(例如 DefaultAzureCredential)是否支持 token 缓存?

php - php 表单中的数据未插入数据库

authentication - 向 Laravel 的授权用户添加更多信息

python - Redis 管道和 python 多处理

c - C 编程语言的 TC++ 中 int i=00100 错误

c - 在单个整数上同步的最佳方法

redis - 错误未知命令扫描,redis-cli

docker - 使用身份验证从 Dockerfile 运行 redis docker 容器