コード例 #1
0
void TestPfmerge( void )
{
	try
	{
		CRedisClient redis;
		redis.connect("127.0.0.1", 6379);

		CRedisClient::VecString element;
		element.push_back("a");
		element.push_back("b");
		element.push_back("c");
		string key1("key1");
		redis.pfadd(key1, element);

		CRedisClient::VecString element2;
		element2.push_back("a");
		element2.push_back("d");
		string key2("key2");
		redis.pfadd(key2, element2);

		CRedisClient::VecString keys;
		keys.push_back(key1);
		keys.push_back(key2);
		bool flag = redis.pfmerge("test", keys);
		cout << flag << endl;
	} catch( RdException& e )
	{
		std::cout << "Redis exception:" << e.what() << std::endl;
	} catch( Poco::Exception& e )
	{
		std::cout << "Poco_exception:" << e.what() << std::endl;
	}
}
コード例 #2
0
void TestBrpop( void )
{
	try
	{
		CRedisClient redis;
		redis.setTimeout(0,0);
		redis.connect("127.0.0.1", 6379);

		string key = "key";
		string key2 = "key2";
		CRedisClient::VecString keyValue;
		keyValue.push_back("a");
		keyValue.push_back("b");
		keyValue.push_back("c");
		CRedisClient::VecString key2Value;
		key2Value.push_back("1");
		key2Value.push_back("2");
		key2Value.push_back("3");

        redis.rpush(key, keyValue);
        redis.rpush(key2, key2Value);

		CRedisClient::VecString keys;
		keys.push_back("key");
		keys.push_back("key2");

		CRedisClient::MapString value;
		uint64_t timeout = 3;
        if ( !redis.brpop(keys, timeout, value) )
        {
            std::cout << "brpop falied" << std::endl;
            return;
        }

		CRedisClient::MapString::const_iterator it = value.begin();
		CRedisClient::MapString::const_iterator end = value.end();
		while ( it != end )
		{
			cout << it->first << endl;
			cout << it->second << 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;
	}
}
コード例 #3
0
void TestPfadd( void )
{
	try
	{
		CRedisClient redis;
		redis.connect("127.0.0.1", 6379);

		CRedisClient::VecString element;
		element.push_back("a");
		element.push_back("b");
		element.push_back("c");
		string key("key1");
		uint64_t count = redis.pfadd(key, element);
		cout << count << endl;
	} catch( RdException& e )
	{
		std::cout << "Redis exception:" << e.what() << std::endl;
	} catch( Poco::Exception& e )
	{
		std::cout << "Poco_exception:" << e.what() << std::endl;
	}
}
コード例 #4
0
void TestRpush( void )
{
	try
	{

		CRedisClient redis;
		redis.connect("127.0.0.1", 6379);
		string mykey = "key";

		CRedisClient::VecString value;
		value.push_back("e");
		value.push_back("g");
		value.push_back("j");
		int count = redis.rpush(mykey, value);
		std::cout << count << 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;
	}
}
コード例 #5
0
uint64_t CRedisClient::stringToVecString(string& str, CRedisClient::VecString& vec)
{
    uint64_t i=0;
    uint64_t len=str.length();
    uint64_t posStart=0;
    while(i<len)
    {
        if (str[i]=='\n')
        {
            vec.push_back(str.substr(posStart,i-posStart));
            posStart=i+1;
        }
        ++i;
    }
    return vec.size();

}
コード例 #6
0
uint64_t CRedisClient::hvals(const string &key, CRedisClient::VecString &values)
{
    CResult result;
    hvals( key, result );

    ReplyType type = result.getType();
    if ( REDIS_REPLY_ERROR == type )
    {
        throw ReplyErr( result.getErrorString() );
    }else if ( REDIS_REPLY_ARRAY != type )
    {
        throw ProtocolErr( "HVALS: data recved is not arry");
    }
    CResult::ListCResult::const_iterator it = result.getArry().begin();
    CResult::ListCResult::const_iterator end = result.getArry().end();
    for ( ; it != end; ++it )
    {
        values.push_back( static_cast<string>(*it) );
    }
    return values.size();
}
コード例 #7
0
///////////////////////////////////////////// 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;
    }
}
コード例 #8
0
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;
    }
}