redis3m使用hmget命令


v1


#include <redis3m/redis3m.hpp>
#include <iostream>
#include <sys/time.h>

using namespace redis3m;

int main(int argc, char **argv)
{
    std::string fields;
    fields="1 2 3";

    simple_pool::ptr_t pool = simple_pool::create("127.0.0.1");

    connection::ptr_t c = pool->get();
    c->run(command("HSET")<<"chat_customer_sched"<<1<<100);
    c->run(command("HSET")<<"chat_customer_sched"<<2<<200);
    reply r = c->run(command("HMGET")<<"chat_customer_sched"<<fields);

    pool->put(c);
    printf("\n");
    while(1){
        sleep(1);
        printf("hello world!\n");
    }
}


v2


//g++ -lredis3m -lboost_regex -lboost_system -lboost_filesystem -lboost_log -lboost_thread -lboost_log_setup  -I/usr/local/include/boost -I/usr/local/include/redis3m main.cpp


#include <boost/lexical_cast.hpp>

#include <redis3m/redis3m.hpp>

#include <iostream>

#include <sys/time.h>


#define CUSTOMER_SCHED_PREFIX "test_customer_sched_"


int sel(redis3m::simple_pool::ptr_t& pool, std::vector<int>& vecCustomerId){

    int customerId = -1; 

    if(0 == vecCustomerId.size()){

        return customerId;

    }   

    redis3m::command cmd("HMGET");

    cmd<<CUSTOMER_SCHED_PREFIX;

    std::vector<int>::iterator iterVecCustomerId;

    for(iterVecCustomerId=vecCustomerId.begin(); iterVecCustomerId!=vecCustomerId.end(); iterVecCustomerId++){

        cmd<<*iterVecCustomerId;

    }   

    redis3m::connection::ptr_t c = pool->get();

    redis3m::reply r = c->run(cmd);

    std::map<int,int> mapCountRef;

    //if (redis3m::reply::type_t::ARRAY == r.type()){

    if (2 == r.type()){

        std::vector<redis3m::reply> vecReply = r.elements();

        if(0 == vecReply.size()){

            return customerId;

        }   

        std::vector<redis3m::reply>::iterator iterVecReply;

        for(iterVecReply=vecReply.begin(), iterVecCustomerId=vecCustomerId.begin();\

                iterVecReply!=vecReply.end() && iterVecCustomerId!=vecCustomerId.end();\

                iterVecReply++, iterVecCustomerId++){

            //if (iterVecReply->type() == redis3m::reply::type_t::STRING){

            if (iterVecReply->type() == 1){ 

                int value = boost::lexical_cast<int>(iterVecReply->str());

                std::cout<<"customer "<<*iterVecCustomerId<<" count ref = "<<value<<std::endl;

                mapCountRef[*iterVecCustomerId]=value;

            //}else if(iterVecReply->type() == redis3m::reply::type_t::NIL){

            }else if(iterVecReply->type() == 4){ 

                mapCountRef[*iterVecCustomerId]=0;

                std::cout<<"customer "<<*iterVecCustomerId<<" count ref = "<<0<<std::endl;

            }else{

                mapCountRef[*iterVecCustomerId]=0;

                std::cout<<"customer "<<*iterVecCustomerId<<" count ref = "<<0<<std::endl;

                std::cout<<"customer "<<*iterVecCustomerId<<" something wrong.";

            }   

        }   

    }   

    pool->put(c);

    return customerId;

}


int main(int argc, char **argv){

    redis3m::simple_pool::ptr_t pool = redis3m::simple_pool::create("127.0.0.1");

    std::vector<int> vecCustomerId;

    vecCustomerId.push_back(0);

    vecCustomerId.push_back(1);

    vecCustomerId.push_back(2);

    int customerId = sel(pool, vecCustomerId);

}




转载于:https://my.oschina.net/u/1176097/blog/403395

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值