本文记录一下hiredis同步读写的操作方式。
简单的实例
#include <iostream>
#include <stdio.h>
extern "C" {
#include <hiredis.h>
}
int test_1()
{
redisContext *c = redisConnect( "127.0.0.1", 6379 );
if ( c != NULL && c->err) {
std::cout << "Error: " << c->errstr << std::endl;
return -1;
}
std::cout << "hello world." << std::endl;
redisReply *reply = (redisReply *)redisCommand( c, "GET foo");
if ( reply == nullptr ) {
std::cout << "get redis failed" << std::endl;
return -2;
}
if ( reply->type == REDIS_REPLY_ERROR ) {
std::cout << "type == REDIS_REPLY_ERROR" << std::endl;
freeReplyObject( reply );
return -3;
}
std::cout << "set redis success. " << "SET foo bar" << std::endl;
freeReplyObject( reply );
return 0;
}
函数好像也只有非常简单的command。在使用的时候需要注意其中的内存用完是需要释放掉的。
本文通过一个简单的C++示例展示了如何使用hiredis进行同步读写操作。该示例连接本地Redis服务器,并执行GET命令获取键'foo'的值。
1852

被折叠的 条评论
为什么被折叠?



