为方便程序对redis操作,我对poco的redis进行了再次封装,主要是针对自己应用需要的部分。

开发工具:netbean

系统环境:centos7

poco版本: poco-1.9.0-all

其实只用了redis中的list

头文件:

#include <Poco/Redis/Exception.h>
#include <Poco/Redis/Client.h>
#include <Poco/Redis/Command.h>
#include <Poco/Redis/Array.h>
#include <iostream>
#include <list>
using std::string;
using std::wstring;

using namespace std;
using Poco::Redis::Client;
using Poco::Redis::Command;
using Poco::Redis::RedisException;
using Poco::Redis::BulkString;
using Poco::Redis::Array;

class PocoRedis {
public:
    PocoRedis();
    PocoRedis(string host,int port);
    PocoRedis(const PocoRedis& orig);
    bool connect();
    
    bool set(string key,string val);
    string get(string key);
    
    //列表操作
    int llen(string key);
    //移出并获取列表的第一个元素
    string lpop(string key);
    //将一个或多个值插入到列表头部
    bool lpush(string key, string value);
    //获取列表指定范围内的元素
    std::list<string> lrange(string key, int start,int end);
    //通过索引获取列表中的元素
    string lindex(string key, int index);
    
    string rpop(string key);
    bool rpush(string key, string value);
    bool lset(string key,int index, string value);
    virtual ~PocoRedis();
private:
    Client* redis;
    string _host;
    int _port;
    
    bool _isConnected;
};

cpp文件

#include <valarray>
#include <list>

#include "PocoRedis.h"

PocoRedis::PocoRedis() {
    this->_host = "127.0.0.1";
    this->_port = 6379;
}

PocoRedis::PocoRedis(string host,int port){
    this->_host = host;
    this->_port = port;
}

bool PocoRedis::connect(){
    this->redis = new Client;
    try
    {
        this->redis->connect(_host,_port);
        this->_isConnected = true;
        std::cout << "connect to [" << _host << ':' << _port << ']' << "success. " << std::endl;
        return true;
    }
    catch (Poco::Exception& e)
    {
        std::cout << "Couldn't connect to [" << _host << ':' << _port << ']' << e.message() << ". " << std::endl;
        this->_isConnected = false;
        return false;
    }    
}

bool PocoRedis::set(string key, string val){
    Command setCommand = Command::set(key, val);
    try
    {
            std::string result = this->redis->execute<std::string>(setCommand);
            return (result.compare("OK") == 0);
    }
    catch (Poco::Exception& e)
    {
        return false;
    }
}

string PocoRedis::get(string key){
    Command getCommand = Command::get(key);
    try
    {
        BulkString result = this->redis->execute<BulkString>(getCommand);
        return result.value();
    }
    catch (RedisException& e)
    {
        return "";
    }
}

bool PocoRedis::lpush(string key, string val){
    Command lpush = Command::lpush(key, val);
    try
    {
        Poco::Int64 result = this->redis->execute<Poco::Int64>(lpush);
        return (result > 1);
    }
    catch (Poco::Exception& e)
    {
        return false;
    }
}

int PocoRedis::llen(string key){
    Command llen = Command::llen("mylist");
    try
    {
            Poco::Int64 n = this->redis->execute<Poco::Int64>(llen);
            return n;
    }    
    catch (Poco::Exception& e)
    {
        return 0;
    }
}

string PocoRedis::lindex(string key, int index){
    Command lindex = Command::lindex(key, index);
    try
    {
            BulkString result = this->redis->execute<BulkString>(lindex);
            return result.value();
    }
    catch (Poco::Exception& e)
    {
        return 0;
    }    
}

std::list<string> PocoRedis::lrange(string key, int start, int end){
    Command lrange = Command::lrange(key,start,end);
    std::list<string> res;    
    try
    {
        Array result = this->redis->execute<Array>(lrange);

for(int i=0;i<result.size();i++){
            res.push_back(result.get<BulkString>(i).value());
        }
        return res;
    }
    catch (Poco::Exception& e)
    {
        return res;
    }    
}

string PocoRedis::lpop(string key){
    Command lpop = Command::lpop(key);
    try
    {
            BulkString result = this->redis->execute<BulkString>(lpop);
            return result.value();
    }
    catch (RedisException& e)
    {
        return "";
    }
}

string PocoRedis::rpop(string key){
    Command lpop = Command::rpop(key);
    try
    {
            BulkString result = this->redis->execute<BulkString>(lpop);
            return result.value();
    }
    catch (RedisException& e)
    {
        return "";
    }
}

bool PocoRedis::rpush(string key, string val){
    Command lpush = Command::rpush(key, val);
    try
    {
        Poco::Int64 result = this->redis->execute<Poco::Int64>(lpush);
        return (result > 1);
    }
    catch (Poco::Exception& e)
    {
        return false;
    }
}

bool PocoRedis::lset(string key,int index, string val){
    Command lset = Command::lset(key,index, val);
    try
    {
        std::string result = this->redis->execute<std::string>(lset);
        return (result.compare("Hello") == 0);
    }
    catch (Poco::Exception& e)
    {
        return false;
    }
}
PocoRedis::PocoRedis(const PocoRedis& orig) {
    
}

PocoRedis::~PocoRedis() {
    if(this->redis!=NULL){
        delete this->redis;
    }
}

调用:

int main_redis2(int argc,char * argv[]){
    PocoRedis pr("127.0.0.1",6379);
    bool connect = pr.connect();
    
    pr.set("age","40");
    pr.lpush("language","jp");
    pr.lpush("language","en");
    
    std::list<string> rss = pr.lrange("language",0,2);
    std::list<string>::iterator si;
    for (si = rss.begin(); si != rss.end(); ++si)   
        cout << *si << endl;
    
    return 0;
}

最新文章

  1. 实例:对2个Makefile的备注
  2. mysql的优化
  3. 【bzoj3211】花神游历各国
  4. java war包加载提示jar not loaded
  5. 如何Windows分页控件中增加统计功能
  6. bzoj 1503 splay
  7. TcpClient类与TcpListener类
  8. Java Web之Servlet
  9. FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM
  10. Php数据类型之整型详解
  11. nodejs 导出excel
  12. gvim编辑文件到github乱码
  13. Web Api 返回参数,实现统一标准化!
  14. 【记录】让人淡疼的BUG之参数传送错误
  15. python3实现邮件发送程序
  16. Python数据分析(二): Numpy技巧 (1/4)
  17. github常见操作和常见错误!错误提示:fatal: remote origin already exist
  18. 细说 JavaScript 七种数据类型
  19. PHP初级程序员出路
  20. 20155218 2006-2007-2 《Java程序设计》第5周学习总结

热门文章

  1. Web中常见的绕过和技巧
  2. Mysql优化系列之查询性能优化前篇3(必须知道的几个事实)
  3. Spring Boot Redis Cluster实战
  4. java_打印流
  5. Expression表达式 实现and、or搜索
  6. html-from提交表单
  7. half adder vs. full adder
  8. 来杭州云栖大会,全面了解企业如何实现云上IT治理
  9. javascript 数组的方法(一)
  10. 03. 将pdb调试文件包含到.vsix包中