为方便程序对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;
}

使用poco再次封装redis的更多相关文章

  1. openresty开发系列27--openresty中封装redis操作

    openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...

  2. JS弹出框插件zDialog再次封装

    zDialog插件网址:http://www.jq22.com/jquery-info2426 再次封装zDialog的代码: (function ($) { $.extend({ iDialog: ...

  3. easyui的window插件再次封装

    easyui的window插件再次封装 说明:该插件弹出的弹出框在最顶层的页面中,而不是在当前页面所在的iframe中,它的可拖动范围是整个浏览器窗口:所以不能用JS的parent对象获取弹出它的父页 ...

  4. ajax的再次封装!(改进版) —— new与不 new 有啥区别?

    生命不息重构不止! 上一篇写了一下我对ajax的再次封装的方法,收到了很多有价值的回复,比如有童鞋建议用$.extend,一开始还以为要做成插件呢,后来才知道,原来这个东东还可以实现合并.省着自己再去 ...

  5. ajax的再次封装!

    js的动态加载.缓存.更新以及复用 系列有点卡文,放心会继续的.先来点更基础的,为js的加载做点铺垫. jQuery的ajax本来就很方便了,为啥还要在进一步的封装呢?这个首先要看项目的具体需求了,如 ...

  6. javascript笔记——jqGrid再次封装

    xingrunzhao js插件再次封装 demo 'use strict'; /** * commerce grid框架 * 依赖jqgrid */ (function ($_self, jQuer ...

  7. 封装redis

    封装redis import redis # r = redis.Redis() class MyRedis(): def __init__(self,ip,password,port=6379,db ...

  8. Java代码封装redis工具类

    maven依赖关系: <dependency> <groupId>redis.clients</groupId> <artifactId>jedis&l ...

  9. 简单封装Redis做缓存

    基于Redis封装一个简单的Python缓存模块 0. Docker Redis安装 参考: Get Docker CE for CentOS Docker 安装 Redis 安装Docker时错误s ...

随机推荐

  1. Sublime text2 + cygwin编译C++

    1.安装cygwin2.安装sublime text23.将g++和gdb加入系统环境变量(windows系统)3.安装package control4.通过package control安装subl ...

  2. http://www.2cto.com/ 红黑联盟

    http://www.2cto.com/ 红黑联盟,一个不错的学习或者开阔眼界的网站,内部由中文书写.比较适合国人.

  3. JS 拷贝传值和引用传值

    1.拷贝传值:基本数据类型都是“拷贝传值”. 拷贝传值,就是将一个变量的值“拷贝”一份,传给了另一个变量. 拷贝传值中,两个变量之间没有任何联系,修改其中一个变量的值,另一个不会改变. 这两个变量之间 ...

  4. JS if 判断

    if条件判断 1.语法结构——只判断true,不判断false if(条件判断:结果只有两个true或false) { 条件为true,将执行该代码: } 说明:   if是系统关键字,必须全小写. ...

  5. Date、DateFormat、Calendar、System、Math类总结

    java.util.Date: 构造方法 public Date() 空参构造,返回当前时间 public Date(long 毫秒值) 指定毫秒值的时间 普通方法 long getTime() 获取 ...

  6. Mysql优化系列之查询性能优化前篇3(必须知道的几个事实)

    事实一:临时表没有任何索引 最常见的临时表莫过于在from子句中写子查询,遇到这种情况,Mysql会先将其查询结果放到一张临时表中, 然后将这个临时表当做普通表对待 事实二:执行计划优化 大多数的sq ...

  7. 用Navicat for mysql连接mysql报错1251-解决办法

    今天下了个 MySQL8.0,发现Navicat连接不上,总是报错1251: 原因是MySQL8.0版本的加密方式和MySQL5.0的不一样,连接会报错. 试了很多种方法,终于找到一种可以实现的: 更 ...

  8. loj6244 七选五

    题意:从n个数中选k个数,问有多少种排列与标准k项串恰好有x个位置相同. 标程: #include<cstdio> using namespace std; typedef long lo ...

  9. [JZOJ3187]【GDOI2013模拟8】的士

    题目 描述 题目大意 在一个数轴上,有些人要从某个点到达另一个点. 出租车从最左端出发,将所有人送到它们的目的地,最终到达最右边的点. 出租车只能做一个乘客,并且可以在图中将乘客丢下. 问最短时间. ...

  10. 爆表!猜猜这个大会的IQ总值有多高?

    “人人可及的未来,同样存在于「日拱一卒」的琐碎生活当中,那也是技术对生活最为直观的改变和演进.” “以通神明之德,以类万物之情”,这句来自<易经>的话,放到现今也合宜. 人类掌控事物发展的 ...