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);
}