C++Redis客户端(hiredis) include include include include int main(int argc, char argv) { unsigned int j; redisContext conn; redisReply reply; if (argc err) { if (conn) { printf("Connection error: %sn", conn>errstr); redisFree(conn); } else { printf("Connection error: can't allocate redis contextn"); } exit(1); } / AUTH / reply redisCommand(conn, "AUTH %s", password); printf("AUTH: %sn", reply>str); freeReplyObject(reply); / Set / reply redisCommand(conn,"SET %s %s", "welcome", "Hello, DCS for Redis!"); printf("SET: %sn", reply>str); freeReplyObject(reply); / Get / reply redisCommand(conn,"GET welcome"); printf("GET welcome: %sn", reply>str); freeReplyObject(reply); / Disconnects and frees the context / redisFree(conn); return 0; } 2.执行以下命令进行编译。 gcc connRedis.c o connRedis I /usr/local/include/hiredis lhiredis 如果有报错,可查找hiredis.h文件路径,并修改编译命令。 编译完后得到一个可执行文件connRedis。 3.执行以下命令,连接Redis实例。 ./connRedis {redisipaddress} 6379 {password} 其中, {redisinstanceaddress} 为Redis实例的IP地址,“6379”为Redis实例的端口。IP地址和端口获取见步骤1,请按实际情况修改后执行。 {password} 为创建Redis实例时自定义的密码,请按实际情况修改后执行。 返回以下回显信息,表示成功连接Redis实例。 AUTH: OK SET: OK GET welcome: Hello, DCS for Redis! 说明 如果运行报错找不到hiredis库文件,可参考如下命令,将相关文件复制到系统目录,并增加动态链接。 mkdir /usr/lib/hiredis cp /usr/local/lib/libhiredis.so.0.13 /usr/lib/hiredis/ mkdir /usr/include/hiredis cp /usr/local/include/hiredis/hiredis.h /usr/include/hiredis/ echo '/usr/local/lib' >>;>>;/etc/ld.so.conf ldconfig 以上so文件与.h文件的位置,需要替换成实际文件位置。