void CRedisClient::hmget(const string &key, const CRedisClient::VecString &fields, CResult &result) { _socket.clearBuffer(); Command cmd( "HMGET" ); cmd << key; VecString::const_iterator it = fields.begin(); VecString::const_iterator end = fields.end(); for ( ; it != end; ++it ) { cmd << *it; } _sendCommand( cmd ); _getReply( result ); ReplyType type = result.getType(); if ( REDIS_REPLY_ERROR == type ) { throw ReplyErr( result.getErrorString() ); }else if ( REDIS_REPLY_ARRAY != type ) { throw ProtocolErr( "HMGET: data recved is not arry" ); } }
void TestLrange( void ) { try { CRedisClient redis; redis.connect("127.0.0.1", 6379); string mykey = "key"; CRedisClient::VecString value; int64_t start = 0; int64_t stop = 33; uint64_t count = redis.lrange(mykey, start, stop, value); std::cout << count << std::endl; CRedisClient::VecString::const_iterator it = value.begin(); CRedisClient::VecString::const_iterator end = value.end(); while ( it != end ) { cout << *it << endl; ++it; } } catch( RdException& e ) { std::cout << "Redis exception:" << e.what() << std::endl; } catch( Poco::Exception& e ) { std::cout << "Poco_exception:" << e.what() << std::endl; } }
void PrintVector( const string& key,CRedisClient::VecString& values ) { CRedisClient::VecString::const_iterator it = values.begin(); CRedisClient::VecString::const_iterator end = values.end(); for ( ; it != end; ++it ) { std::cout << key << ": " << *it << std::endl; } }
uint64_t CRedisClient::sinter(const CRedisClient::VecString &keys, VecString &values) { Command cmd( "SINTER" ); VecString::const_iterator it = keys.begin(); VecString::const_iterator end = keys.end(); for ( ; it != end; ++it ) { cmd << *it; } return ( _getArry( cmd, values ) ); }
uint64_t CRedisClient::sunion(const CRedisClient::VecString &keys , VecString &members) { Command cmd( "SUNION" ); VecString::const_iterator it = keys.begin(); VecString::const_iterator end = keys.end(); for ( ; it != end; ++it ) { cmd << *it; } return ( _getArry( cmd, members ) ); }
void CRedisClient::slowlog(const CRedisClient::VecString &subcommand, CResult &reply) { Command cmd( "SLOWLOG" ); _socket.clearBuffer(); VecString::const_iterator it = subcommand.begin(); VecString::const_iterator end=subcommand.end(); for ( ; it !=end; ++it ) { cmd << *it; } _sendCommand(cmd); _getReply(reply); }
uint64_t CRedisClient::sunionstroe(const string &dest, const CRedisClient::VecString &keys) { Command cmd( "SUNIONSTORE" ); cmd << dest; VecString::const_iterator it = keys.begin(); VecString::const_iterator end = keys.end(); for ( ; it != end; ++it ) { cmd << *it; } int64_t num; _getInt( cmd, num ); return num; }
void CRedisClient::hmget(const string &key, const CRedisClient::VecString &fields, CResult &result) { Command cmd( "HMGET" ); cmd << key; VecString::const_iterator it = fields.begin(); VecString::const_iterator end = fields.end(); for ( ; it != end; ++it ) { cmd << *it; } _getArry( cmd , result ); }
uint64_t CRedisClient::sinterstore( const string& destKey ,const CRedisClient::VecString &keys ) { Command cmd( "SINTERSTORE" ); cmd << destKey; VecString::const_iterator it = keys.begin(); VecString::const_iterator end = keys.end(); for ( ; it != end; ++it ) { cmd << *it; } int64_t num = 0; _getInt( cmd, num ); return num; }
uint64_t CRedisClient::sadd(const string &key, const CRedisClient::VecString &members) { Command cmd( "SADD" ); cmd << key; VecString::const_iterator it = members.begin(); VecString::const_iterator end = members.end(); for ( ; it != end ; ++it ) { cmd << *it; } int64_t num; _getInt( cmd , num ); return num; }
uint64_t CRedisClient::hdel( const string &key, const CRedisClient::VecString &fields ) { Command cmd( "HDEL" ); cmd << key; VecString::const_iterator it = fields.begin(); VecString::const_iterator end = fields.end(); for ( ; it != end; ++it ) { cmd << *it; } int64_t num = 0; _getInt( cmd , num ); return num; }
///////////////////////////////////////////// test set ////////////////////////////////////////// void TestSet( void ) { try { CRedisClient redis; redis.connect( "127.0.0.1", 6379 ); //--------------------------test sadd------------------------------- CRedisClient::VecString members; members.push_back( "yuhaiyang" ); members.push_back( "zhouli" ); members.push_back( "严兴俊" ); uint64_t saddNum = redis.sadd( "testSet", members ); std::cout << "saddNum: " << saddNum << std::endl; //-------------------------test scard-------------------------------- uint64_t scardNum = redis.scard( "testSet" ); std::cout << "scardNum:" << scardNum << std::endl; //-------------------------test sdiff---------------------------------- std::cout << "=====================================sdiffData==========================" << std::endl; CRedisClient::VecString keys; keys.push_back("testSet"); keys.push_back("testSet2"); CRedisClient::VecString sdiffValues; uint64_t sdiffNum = redis.sdiff( keys, sdiffValues ); std::cout << "sdiffNum: " << sdiffNum << std::endl; //-------------------------test sdiffstore---------------------------- CRedisClient::VecString sdiffstorekeys; sdiffstorekeys.push_back("testSet"); sdiffstorekeys.push_back("testSet2"); uint64_t sdiffstoreNum = redis.sdiffstore( "diffSetKey",sdiffstorekeys ); std::cout << "===========================sdiffNum=================================" << std::endl; std::cout << "sdiffstoreNum: " << sdiffstoreNum << std::endl; //---------------------------test sinter-------------------------------- CRedisClient::VecString sinterValues; CRedisClient::VecString sinterKeys; sinterKeys.push_back( "testSet" ); sinterKeys.push_back( "testSet2" ); uint64_t sinterNum = redis.sinter( sinterKeys, sinterValues ); std::cout << "===========================sinterNum================================" << std::endl; std::cout << "sinterNum: " << sinterNum << std::endl; CRedisClient::VecString::const_iterator sinterIt = sinterValues.begin(); CRedisClient::VecString::const_iterator sinterEnd = sinterValues.end(); for ( ; sinterIt != sinterEnd; ++sinterIt ) { std::cout << "sinterIt: " << *sinterIt << std::endl; } //-----------------------------test sinterstore--------------------------- CRedisClient::VecString sinterstoreKeys; sinterstoreKeys.push_back( "testSet" ); sinterstoreKeys.push_back( "testSet2" ); uint64_t sinterstoreNum = redis.sinterstore( "interSetKey", sinterstoreKeys ); std::cout << "sinterstoreNum: " << sinterstoreNum << std::endl; //----------------------------test sismember---------------------------- bool ret = redis.sismember( "testSet", "yuhaiyang2" ); if ( ret ) { std::cout << "存在" << std::endl; }else { std::cout << "不存在" << std::endl; } CRedisClient::VecString membersMembers; uint64_t smembersNum = redis.smembers( "testSet", membersMembers ); std::cout << "smembersNum" << smembersNum << std::endl; PrintVector( "members", membersMembers ); //-----------------------------test smove------------------------------------- bool smoveRet = redis.smove( "testSet2", "testSet","duzong"); if ( smoveRet ) { std::cout << "smove ok" << std::endl; }else { std::cout << "smove failed" << std::endl; } //-----------------------------test spop-------------------------------------- string spopMember; bool spopRet = redis.spop( "testSet3", spopMember ); if ( spopRet ) { std::cout << "spop ok" << std::endl; std::cout << "spop data:" << spopMember << std::endl; }else { std::cout << "spop faliled" << std::endl; } //-----------------------------test srandmember----------------------------- string srandmember; bool srandRet = redis.srandmember( "testSet3", srandmember ); if ( srandRet ) { std::cout << "srandmember ok" <<std::endl; std::cout << "srandmember data:" << srandmember << std::endl; }else { std::cout << "srandmember failed" << std::endl; } CRedisClient::VecString srandMember; redis.srandmember( "testSet2", 20, srandMember ); PrintVector( "srandmember", srandMember ); //-----------------------------test srem--------------------------------------------- CRedisClient::VecString sremMembers; sremMembers.push_back( "nihao" ); sremMembers.push_back( "yuhaiyang" ); sremMembers.push_back( "zhouli" ); uint64_t sremNum = redis.srem( "testSet", sremMembers ); std::cout << "sremNum:" << sremNum << std::endl; //-----------------------------test sunion-------------------------------------------- CRedisClient::VecString sunionMembers; CRedisClient::VecString sunionKeys; sunionKeys.push_back("testSet"); sunionKeys.push_back("testSet2"); redis.sunion( sunionKeys, sunionMembers ); PrintVector( "sunionmembers", sunionMembers ); //--------------------------test sunionstore------------------------------------------ uint64_t sunionstoreNum = redis.sunionstroe( "sunionstoreSet", sunionKeys ); std::cout << "sunionstoreNum: " << sunionstoreNum << std::endl; //--------------------------test sscan-------------------------------------------------- std::cout << "====================sscan value===================" << std::endl; string value = "value_"; stringstream ss; CRedisClient::VecString sscanMembers1; for ( int i = 0; i < 100; ++i ) { value = "value_"; ss.clear(); ss.str(""); ss << i; value += ss.str(); sscanMembers1.push_back( value ); } redis.sadd( "testSet4", sscanMembers1 ); CRedisClient::VecString sscanMembers; redis.sscan( "testSet4", 0, sscanMembers,"value_?" ); while ( redis.sscan( "testSet4", -1, sscanMembers ,"value_?") ); PrintVector( "sscan", sscanMembers ); }catch( RdException& e ) { std::cout << "Redis exception:" << e.what() << std::endl; }catch( Poco::Exception& e ) { std::cout << "Poco_exception:" << e.what() << std::endl; } }
void TestHash( void ) { try { CRedisClient redis; redis.connect( "127.0.0.1", 6379 ); //-------------------------test hset hget--------------------------------- string value; int32_t hashRet = redis.hset( "testHash", "name5", "{ \n\"value\" : \"yuhaiyang\"\r\n}\n" ); std::cout << "hashRet: " << hashRet << std::endl; bool ret = redis.hget( "testHash", "name5" ,value ); std::cout << "ret: " << ret << std::endl; std::cout << "value:" << value << std::endl; //------------------------test hdel------------------------------------------ CRedisClient::VecString fields; fields.push_back( "name4" ); fields.push_back( "name5" ); uint64_t delNum = redis.hdel( "member", fields ); std::cout << "delNum: " << delNum << std::endl; //------------------------test hexists--------------------------------------- string field = "name4"; bool isExists = redis.hexists( "testHash", field ); if ( isExists ) std::cout << "testHash key exists field:" << field << std::endl; else std::cout << "testHash key not exists field:" << field << std::endl; //-----------------------test hgetall------------------------------------------- CRedisClient::MapString allValue; uint64_t allNum = redis.hgetall( "testHash", allValue ); std::cout << "allNum: " << allNum <<std::endl; CRedisClient::MapString::const_iterator it = allValue.begin(); for ( ; it != allValue.end(); it++ ) { std::cout << it->first << ":" << it->second << std::endl; } //------------------------test incrby------------------------------------------- uint64_t incrybyNum = redis.hincrby( "testHash", "num2", 20 ); std::cout << "incrybyNum: " << incrybyNum << std::endl; //------------------------test incrbyfloat------------------------------------- float floatNum = redis.hincrbyfloat( "testHash", "float", 10.1e2 ); std::cout << "floatNum: " << floatNum << std::endl; //------------------------test hkeys------------------------------------------- CRedisClient::VecString hkeysValue; uint64_t hkeysNum = redis.hkeys( "testHash", hkeysValue ); std::cout << "hkeysNum: " << hkeysNum << std::endl; CRedisClient::VecString::const_iterator hkeysit = hkeysValue.begin(); for ( ; hkeysit != hkeysValue.end(); hkeysit++ ) { std::cout << *hkeysit << std::endl; } //------------------------test hlen----------------------------------------------- // uint64_t fieldNum = redis.hlen( "testHash" ); // std::cout << "fieldNum: " << fieldNum << std::endl; // //------------------------test hmget--------------------------------------------- // CResult result; // CRedisClient::VecString hmgeFields; // hmgeFields.push_back("name4"); // hmgeFields.push_back("yuhaiyang"); // hmgeFields.push_back("num"); // redis.hmget( "testHash", hmgeFields,result ); // std::cout << "hmget:" << std::endl; // std::cout << result << std::endl; }catch( RdException& e ) { std::cout << "Redis exception:" << e.what() << std::endl; }catch( Poco::Exception& e ) { std::cout << "Poco_exception:" << e.what() << std::endl; } }