redis3m使用hmget命令
redis3m使用hmget命令
刘燕阳 发表于3年前
redis3m使用hmget命令
  • 发表于 3年前
  • 阅读 43
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

摘要: 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);

}




  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 83
码字总数 8944
×
刘燕阳
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: