一般使用的就是hiredis这个库文件,在hiredis上封装一层,封装成面向对象的方法。

redis.h
#ifndef _REDIS_H_
#define _REDIS_H_


#include<stdio.h>
#include<iostream>
#include<string>
#include<hiredis/hiredis.h>
using namespace std;
class Redis
{
public:
	Redis(){}
	~Redis()
{
	this->_connect = NULL;
	this->_reply = NULL;
}
bool Connect(string host,int port)
{
	this->_connect = redisConnect(host.c_str(),port);
	if(this->_connect != NULL && this->_connect->err)
	{
		cout<<"connect is error:"<<this->_connect-			>errstr<<endl;
		return false;
}
	return true;
}

void set(string key,string value)
{
	redisCommand(this->_connect,"SET %s, %s",key.c_str(),value.c_str());
}
string get(string key)
{
	this->_reply = (redisReply*)redisCommand(this->_connect,"GET %s",key.c_str());
	string str = this->_reply->str;
	freeReplyObject(this->_reply);
	return str;
}
private:
	redisContext* _connect;
	redisReply*_reply;
};
#endif


redis.cpp
#include "redis.h"
int main()
{
	string str = "";
	Redis *r = new Redis();
	if(!r->Connect("127.0.0.1",6379))
	{
		cout<<"connect error"<<endl;
	return 0;
}
	r->set("tcp","123");
	str = r->get("tcp");
	cout<<"get tcp is :"<<str.c_str()<<endl;
	delete r;
	return 0;
}