void TestLlen( void )
{
	try
	{

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

		std::string value;
		int64_t count = redis.llen(mykey);
		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;
	}
}
Beispiel #2
0
void TestLpushx( void )
{
	try
	{

		CRedisClient redis;
		redis.connect("127.0.0.1", 6379);
        string mykey = "testList";
		string value = "x";

		int64_t count = redis.lpushx(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;
	}
}
void TestRpop( void )
{
	try
	{

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

		std::string value;
		bool flag = redis.rpop(mykey, value);
		std::cout << flag << std::endl;
		std::cout << value << 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;
	}
}
void TestBrpoplpush( void )
{
	try
	{
		CRedisClient redis;
		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);

		string value;
		uint64_t timeout = 1;
		bool flag=redis.brpoplpush("key", "key2", timeout, value);
		cout<<flag<<endl;
		cout<<value<<endl;
//		CRedisClient::VecString::const_iterator it=value.begin();
//		CRedisClient::VecString::const_iterator end=value.end();
//		while(it!=end)
//		{
//			cout<<*it<<endl;
//		}
	} catch( RdException& e )
	{
		std::cout << "Redis exception:" << e.what() << std::endl;
	} catch( Poco::Exception& e )
	{
		std::cout << "Poco_exception:" << e.what() << std::endl;
	}
}
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;
	}
}
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;
	}
}
void TestSbgsave(){
    CRedisClient redis;
    redis.connect( "127.0.0.1", 6379 );
    cout << "------test bgsave------" << endl;
    TestServerPrint("redis.bgsave()",redis.bgsave());
}
void TestSslaveof(){
    CRedisClient redis;
    redis.connect( "127.0.0.1", 6379 );
    cout << "------test slaveof------" << endl;
    redis.slaveof("127.0.0.1",6367);
}
void TestSflushdb(){
    CRedisClient redis;
    redis.connect( "127.0.0.1", 6379 );
    cout << "------test flushdb------" << endl;
    redis.flushdb();
}
void TestSdebugSegfault(){
    CRedisClient redis;
    redis.connect( "127.0.0.1", 6379 );
    cout << "------test debugSegfault------" << endl;
    redis.debugSegfault();
}
void TestSdebugObject(){
    CRedisClient redis;
    redis.connect( "127.0.0.1", 6379 );
    cout << "------test debugObject------" << endl;
    TestServerPrint("redis.debugObject(name",redis.debugObject("name"));
}
void TestSconfigResetstat(){
    CRedisClient redis;
    redis.connect( "127.0.0.1", 6379 );
    cout << "------test configResetstat-----" << endl;
    redis.configResetstat();
}
Beispiel #13
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;
    }
}
Beispiel #14
0
void TestHash2()
{
    try
    {
        CRedisClient redis;
        redis.connect( "127.0.0.1", 6379 );
        CResult result;
        //-------------------------------test hmset---------------------------
        //       CRedisClient::MapString pairs;
        //       string pair, value;
        //       std::stringstream ss;
        //       for ( int i = 0; i < 1000; i++ )
        //       {
        //           pair = "pair_";
        //           value = "value_";
        //           ss.str("");

        //           ss << i;
        //           pair += ss.str();
        //           value += ss.str();
        //           pairs.insert( CRedisClient::MapString::value_type(pair,value) );
        //       }
        //       redis.hmset( "testHash", pairs );
        //-------------------------------test hsetnx---------------------------
        //       bool hsetNxRet = redis.hsetnx( "testHash", "num4", "123" );
        //       if ( hsetNxRet )
        //          std::cout << "ok" << std::endl;
        //       else
        //           std::cout << "false" << std::endl;
        //------------------------------test hvals--------------------------------
        //        CRedisClient::VecString hvals;
        //        uint64_t num = redis.hvals( "testHash", hvals );
        //        std::cout << "num: " << num << std::endl;
        //        CRedisClient::VecString::const_iterator it = hvals.begin();
        //        CRedisClient::VecString::const_iterator end = hvals.end();
        //        for ( ; it != end; ++it )
        //        {
        //            std::cout << *it << std::endl;
        //        }
        //
        //-------------------------------test hscan-----------------------------

        std::cout << "====================hscan value===================" << std::endl;
        CRedisClient::MapString hscanPairs;

        redis.hscan( "testHash", 0, hscanPairs,"pair_1??" );
        while ( redis.hscan( "testHash", -1, hscanPairs ,"pair_1??") );

        CRedisClient::MapString::const_iterator hscanIt = hscanPairs.begin();
        CRedisClient::MapString::const_iterator hscanEnd = hscanPairs.end();

        for ( ; hscanIt != hscanEnd ; ++hscanIt )
        {
            std::cout << hscanIt->first <<": " << hscanIt->second << std::endl;
        }
        std::cout << "totalNum: " << hscanPairs.size() << 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;
    }
}
Beispiel #15
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;
    }
}
Beispiel #16
0
void TestRpoplpush( )
{
	try
	{
		CRedisClient redis;
		redis.connect("127.0.0.1", 6379);

		string source = "source";
		string dest = "dest";
//		CRedisClient::VecString srcValue;
//		srcValue.push_back("a");
//		srcValue.push_back("b");
//		srcValue.push_back("c");
//		CRedisClient::VecString destValue;
//		destValue.push_back("1");
//		destValue.push_back("2");
//		destValue.push_back("3");
//
//		redis.rpush(source, srcValue);
//		redis.rpush(dest, destValue);
//		srcValue.clear();
//		destValue.clear();
//
//		cout << "before test rpoplpush......" << endl;
//		cout << "old source.............." << endl;
//		int64_t start = 0;
//		int64_t stop = 2;
//		redis.lrange(source, start, stop, srcValue);
//		for ( CRedisClient::VecString::const_iterator it = srcValue.begin() ; it != srcValue.end() ;
//				it++ )
//		{
//			cout << *it << endl;
//		}
//		redis.lrange(dest, start, stop, destValue);
//		cout << "old dest.........." << endl;
//		for ( CRedisClient::VecString::const_iterator it = destValue.begin() ;
//				it != destValue.end() ; it++ )
//		{
//			cout << *it << endl;
//		}
//		srcValue.clear();
//		destValue.clear();
//
//		cout << "after test rpoplpush........" << endl;
		string value;
		bool flag = redis.rpoplpush(source, dest, value);
		cout << flag << endl;
		cout << "value====================" << value << endl;

//		int64_t start_new = 0;
//		int64_t stop_new = 20;
//		redis.lrange(source, start_new, stop_new, srcValue);
//		redis.lrange(dest, start_new, stop_new, destValue);
//		cout << "new source................" << endl;
//		for ( CRedisClient::VecString::const_iterator it = srcValue.begin() ; it != srcValue.end() ;
//				it++ )
//		{
//			cout << *it << endl;
//		}
//		cout << "new dest..................." << endl;
//		for ( CRedisClient::VecString::const_iterator it = destValue.begin() ;
//				it != destValue.end() ; it++ )
//		{
//			cout << *it << endl;
//		}

	} catch( RdException& e )
	{
		std::cout << "Redis exception:" << e.what() << std::endl;
	} catch( Poco::Exception& e )
	{
		std::cout << "Poco_exception:" << e.what() << std::endl;
	}
}