linux下Redis的安装及C客户端API

来源:互联网 发布:厦门科华 知乎 编辑:程序博客网 时间:2024/06/06 14:05

目录(?)[+]

Redis安装步骤:

1.redis server安装

wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz #下载文件
tar xzf redis-2.4.6.tar.gz
cd redis-2.4.6
make
cp src/redis-server src/redis-cli /usr/bin/ #方便在终端在任何地方直接运行
cp redis.conf /etc/
ufw allow 6379 #ubuntu下开启端口


修改/etc/redis.conf,让server以守护进程在后台执行。
daemonize yes


2.启动redis服务
redis-server /etc/redis.conf


3.检测redis服务是否正常启动

ps -ef | grep redis


Hiredis客户端下载地址:https://github.com/antirez/hiredis/zipball/master


Hiredis安装步骤:
tar zxvf antirez-hiredis-v0.10.1-0-g3cc6a7f.zip
cd antirez-hiredis-3cc6a7f
make


mkdir /usr/lib/hiredis
cp libhiredis.so /usr/lib/hiredis #将动态连接库libhiredis.so至/usr/lib/hiredis
mkdir /usr/include/hiredis
cp hiredis.h /usr/include/hiredis




1.连接hiredis服务器
#include <stdio.h>
#include <hiredis/hiredis.h>


redisContext *conn = redisConnect("127.0.0.1", 6379); //redis server默认端口
if(conn->err){
printf("connection error: %s", conn->str);
}


2.发送命令至服务器
redisReply *reply = redisCommand(conn, "set key value");


3.关闭Reply对象
freeReplyObject(reply);


4.关闭连接
redisFree(conn);




具体一些其他的API可以看文件的README.md

 
#######################################################################
 

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


刚接触C++ 不久 ,就要用到这个redis ,在各种好人的帮助下终于摸索出了个样本,贴上来给像我这样的新手使用。

1.首先安装完毕redis

2.安装boost 库

3.开发包下载地址:

redis-cplusplus-client

4.上代码

[cpp] view plaincopy
  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. }  
[cpp] view plaincopy
  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文件里就行了,最后祝你好运。

0 0
原创粉丝点击