Redis 的 C++开发包 使用例子

来源:互联网 发布:韩国tvn软件下载 编辑:程序博客网 时间:2024/06/05 03:57

http://blog.csdn.net/youngqj/article/details/8266177

1.首先安装完毕redis

2.安装boost 库

3.开发包下载地址:

redis-cplusplus-client

4.上代码

  1. #include "redisclient.h"  
  2. #include <iostream>  
  3. #include <boost/date_time.hpp>  
  4. using namespace boost;  
  5. using namespace std;  
  6. shared_ptr<redis::client> connect_client();  
  7. int main()  
  8. {  
  9.         shared_ptr<redis::client> c;  
  10.         c=connect_client();  
  11.         c->rpush("setxxx","你好");  
  12.         redis::client::string_vector vals;  
  13.         long num=c->get_list("setxxx", vals);  
  14.         
  15.         for(int i = 0; i < vals.size(); i++)  
  16.                 cout << vals[i] <<endl;  
  17.           
  18.         c->set("set003","abc333");  
  19.         string s=c->get("set003");  
  20.         cout<<s<<endl;  
  21.         return 0;  
  22. }  
  23. shared_ptr<redis::client> connect_client()  
  24. {  
  25.         const char* c_host = getenv("REDIS_HOST");  
  26.         string host = "localhost";  
  27.         if(c_host)  
  28.                 host = c_host;  
  29.         return boost::shared_ptr<redis::client>( new redis::client(host) );  
  30. }  

上面的代码演示了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 参考手册

说明:上面代码仅供学习交流使用


0 0
原创粉丝点击