Redis 的 C++开发包 使用例子
刚接触C++ 不久 ,就要用到这个redis ,在各种好人的帮助下终于摸索出了个样本,贴上来给像我这样的新手使用。1.首先安装完毕redis
2.安装boost 库
3.开发包下载地址:
redis-cplusplus-client
4.上代码
#include "redisclient.h"
#include <iostream>
#include <boost/date_time.hpp>
using namespace boost;
using namespace std;
shared_ptr<redis::client> connect_client();
int main()
{
shared_ptr<redis::client> c;
c=connect_client();
c->rpush("setxxx","你好");
redis::client::string_vector vals;
long num=c->get_list("setxxx", vals);
for(int i = 0; i < vals.size(); i++)
cout << vals <<endl;
c->set("set003","abc333");
string s=c->get("set003");
cout<<s<<endl;
return 0;
}
shared_ptr<redis::client> connect_client()
{
const char* c_host = getenv("REDIS_HOST");
string host = "localhost";
if(c_host)
host = c_host;
return boost::shared_ptr<redis::client>( new redis::client(host) );
}
上面的代码演示了redis两种数据类型的调用和写入方法string 和list 列表 "setxxx"是列表部分。
我的环境是centos5.5 boost1.5
编译的时候用到的包列表:
anet.c
anet.h
anet.o
libredisclient.a
redisclient.h
上面的包都是自带的,编译的时候写进Makefile文件里就行了,最后祝你好运。
附:http://www.admin173.com/online/redis-latest/index.html 参考手册
说明:上面代码仅供学习交流使用
版权声明:本文为博主原创文章,未经博主允许不得转载。
页:
[1]