C++使用hiredis连接带密码的redis服务代码段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
redisContext *c = redisConnect("127.0.0.1", 9379);
// Error flags, 0 when there is no error
if (c->err)
{
printf("connect Redis error: %s\n", c->errstr);
exit(1);
}
else
{
printf("connect Redis sucess\n");
}


redisReply *reply = (redisReply *)redisCommand(c, "AUTH %s", "qKxxxRfApe2gxxxxx");
if (reply->type == REDIS_REPLY_ERROR)
{
printf("Redis Auth Error!!!\n");
}
else
{
printf("Redis Auth Sucess!!!\n");
}
freeReplyObject(reply);