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

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++ 1980人阅读 评论(2) 收藏 举报

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

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. }  
#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[i] <<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 参考手册

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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值