Redis server运行起来后(如何启动server,请参考上一篇 ”安装运行server“),可以使用自带的client - “redis-cli” 连接server。
为了后续的扩展,用c++写了个demo来连接server,使用的是hiredis.h里面的接口
CRedis::start - 与server建立连接
CRedis::set - 添加[key, value]
CRedis::get - 获取指定的数据,可以是整数或字符串
class CRedis
{
public:
CRedis(const std::string& ip, unsigned int port)
: redis_(NULL), serverIp_(ip), serverPort_(port)
{
std::cout << "server ip is: " << serverIp_ << std::endl;
std::cout << "server port is: " << serverPort_ << std::endl;
}
~CRedis()
{
if (redis_)
{
redisFree(redis_);
redis_ = NULL;
}
}
bool start()
{
redis_ = redisConnect(serverIp_.c_str(), serverPort_);
if (!redis_)
{
std::cout << "connect redis server is failed" << std::endl;
return false;
}
if (redis_->err)
{
std::cout << "connect redis server is failed with: " << redis_->errstr << std::endl;
redisFree(redis_);
redis_ = NULL;
return false;
}
std::cout << "connect redis server is successful" << std::endl;
return true;
}
void set(const std::string& command)
{
redisReply* reply = sendCommand(command);
if (!reply)
{
std::cout << "execute command[" << command << "] is failed" << std::endl;
return;
}
if (!(reply->type == REDIS_REPLY_STATUS and strcasecmp(reply->str, "OK") == 0))
{
std::cout << "execute command[" << command << "] is failed with " << reply->str << std::endl;
freeReplyObject(reply);
return;
}
freeReplyObject(reply);
std::cout << "execute command[" << command << "] is successful" << std::endl;
}
bool get(const std::string& command, long long& out)
{
redisReply* reply = sendCommand(command);
if (!reply)
{
std::cout << "execute command[" << command << "] is failed" << std::endl;
return false;
}
if (reply->type != REDIS_REPLY_INTEGER)
{
std::cout << "execute command[" << command << "] is failed due to invalid type[" << reply->type << "]" << std::endl;
freeReplyObject(reply);
return false;
}
out = reply->integer;
freeReplyObject(reply);
return true;
}
bool get(const std::string& command, std::string& out)
{
redisReply* reply = sendCommand(command);
if (!reply)
{
std::cout << "execute command[" << command << "] is failed" << std::endl;
return false;
}
if (reply->type != REDIS_REPLY_STRING)
{
std::cout << "execute command[" << command << "] is failed due to invalid type[" << reply->type << "]" << std::endl;
freeReplyObject(reply);
return false;
}
out = reply->str;
freeReplyObject(reply);
return true;
}
private:
inline redisReply* sendCommand(const std::string& command) const
{
return (redisReply*)redisCommand(redis_, command.c_str());
}
private:
redisContext* redis_;
std::string serverIp_;
unsigned int serverPort_;
};
外面创建实例,调用一些简单的命令
CRedis redis("127.0.0.1", 6379);
if (redis.start())
{
redis.set("set key1 abcdefg");
redis.set("set key2 xyz");
long long len1, len2;
redis.get("strlen key1", len1);
redis.get("strlen key2", len2);
std::cout << "key1 length is " << len1 << std::endl;
std::cout << "key2 length is " << len2 << std::endl;
std::string v1, v2;
redis.get("get key1", v1);
redis.get("get key2", v2);
std::cout << "key1 value is " << v1 << std::endl;
std::cout << "key2 value is " << v2 << std::endl;
}
输出
上一篇:Redis学习小计(1) - 安装运行server
下一篇:Redis学习小计(3) - redisObject