Ubuntu下boost库与mrpi-redis-cplusplus-client安装

工作中需要使用C++做一个服务该服务要访问REDIS缓存系统,之前是用C#做的有现成的类库可用。转到C++就没这么方便了所以决定上网搜搜资料。

想要安装该客户端系统必须要装boost库,此库安装步骤如下:

1. 上官方网站下载到最新版本1.49版,解压后进入目录,简单安装:

    ./bootstrap.sh

    sudo ./b2 install

 

2. 安装的时候可能会出现下面错误,处理办法如下: 

    过程中会提示: patchlevel.h:没有那个文件或目录 可以这样安装:

    sudo apt-get install python-dev

    过程中又提示:   bzlib.h:没有那个文件或目录 可以这样安装:

    sudo apt-get install libbz2-dev

    安装比较耗时我用了大概半个钟头以上时间。

 

总算把boots库安装完了也没出现啥错误下面开始安装redis客户端了:

1. 首先找到C++连接REDIS的客户端叫mrpi-redis-cplusplus-client-14e3829 注意后面一串数字,因为早些时候的客户端只能连接到REDIS的1.0大版本2.0版本是无法连接的。

    我找到的这个版本可以连接到2.0版本。在mrpi-redis-cplusplus-client下make 发现有错误。提示说 找不到lboost_thread-mt 这个玩意。

    找了好长时间资料最后有一老外说要修改Makefile文件,按照他得说法用VIM打开文件吧“lboost_thread-mt” 改成 “lboost_thread”保存。

 

2. 修改相关文件中服务器连接地址和端口(貌似有两三个地方),重新make 然后./test_client运行正常。

 

3. 虽然./test_client 运行没问题,但是对我们来说需要改造一下,去掉不用的并面向对象化。 由于我们项目只需操作队列,也就是是list 所以根据提供的test我写了一个类

#include "redisclient.h"
#include <iostream>
#include <boost/date_time.hpp>


class RedisHelper
{
    public:
        typedef redis::base_client<redis::default_hasher>::string_type ResStr;

        static void Push(string key,string val)
        {
            redis::client *c = RedisHelper::GetClient();

            try
            {
                c->rpush(key, val);
                delete c;
            }
            catch (redis::redis_error & e)
            {
                delete c;
            }
        };

        static ResStr Pop(string key)
        {
            redis::client *c = RedisHelper::GetClient();
            ResStr res;

            try
            {
                res = c->lpop(key);
                delete c;
            }
            catch (redis::redis_error & e)
            {
                delete c;
            }

            return res == "**nonexistent-key**" ? "" : res;
        };

    private:
        static redis::client* GetClient()
        {
            const char* c_host = getenv("REDIS_HOST");
            string host = "192.168.45.144";
            if(c_host)
              host = c_host;
            return new redis::client(host);
            //
redis::client::string_vector keys;
            
//redis::client::int_type count 

        };
        
};

    头文件代码也总结成了一个redisclient.h,由于内容较大故不贴出代码了,有需要的可以找我要。

 

    最后写个测试代码测试一把:

#include"redis/RedisHelper.h"

int main()
{
        RedisHelper rs;
        //rs.Push("key1","val1");

        cout << rs.Pop("noke") << endl;
        cout << (rs.Pop("key1") == "***nonexistent-key**") << endl;
    return 1;
};

    编译的时候别忘了链接库

    g++ king.cc -o king -lpthread -lboost_thread

 

    运行通过!

posted on 2012-04-23 16:57  老金  阅读(2458)  评论(0编辑  收藏  举报