hiredis-vip库,集群,异步实例


hiredis-vip库,集群,异步实例


下载地址:https://github.com/vipshop/hiredis-vip


hiredis-vip支持redis集群,异步操作,pipeline


异步操作需要libevent库支持


代码:

#include 
#include 
#include 
#include 
#include 

#include 
#include 

const char *paddrs = "10.0.33.31:7000,10.0.33.31:7001,10.0.33.32:7000,10.0.33.32:7001,10.0.33.33:7000,10.0.33.33:7001";
redisClusterAsyncContext *predis_cluster_async_context = NULL;
struct event_base *base = NULL;

void redisAsyncCommandCallback(redisClusterAsyncContext *c, void *r, void *privdata) 
{
    redisReply *reply = (redisReply *)r;

    if(NULL != reply)
	{
		printf("redisAsyncCommandCallback: %lld, %s\n", reply->integer, reply->str);
	}
    else
    {
    	printf("redisAsyncCommandCallback: reply is NULL\n");
    }
    
    redisClusterAsyncDisconnect(predis_cluster_async_context);
}

void redisAsyncConnectCallback(const redisAsyncContext *c, int status) 
{
    if(status != REDIS_OK)
	{
		printf("redisAsyncConnectCallback failed\n");
	}
	else
	{
		printf("redisAsyncConnectCallback success\n");
	}
}

void redisAsyncDisconnectCallback(const redisAsyncContext *c, int status) 
{
    if(status != REDIS_OK)
	{
		printf("redisAsyncDisconnectCallback failed\n");
	}
	else
	{
		printf("redisAsyncDisconnectCallback success\n");
	}	
}

int redis_cluster_async_connect()
{
    int res = 0;

	printf("redis_cluster_async_connect, paddrs=%s\n", paddrs);

	base = event_base_new();

    // redis cluster connect with timeout 
	predis_cluster_async_context = redisClusterAsyncConnect(paddrs, HIRCLUSTER_FLAG_NULL);
	if(NULL == predis_cluster_async_context || predis_cluster_async_context->err)
	{
        //connect failed
		if(predis_cluster_async_context)
		{
			printf("redisClusterAsyncConnect failed: %s!\n", predis_cluster_async_context->errstr);

			redisClusterAsyncFree(predis_cluster_async_context);
			predis_cluster_async_context = NULL;
		}
		else
		{
			printf("redisClusterAsyncConnect failed: can't allocate redis contex!\n");
		}
		res = 1;
	}    
	else
	{
		printf("redisClusterAsyncConnect success\n");
		redisClusterLibeventAttach(predis_cluster_async_context, base);
        redisClusterAsyncSetConnectCallback(predis_cluster_async_context, redisAsyncConnectCallback);
        redisClusterAsyncSetDisconnectCallback(predis_cluster_async_context, redisAsyncDisconnectCallback);
	}
	
	return res;
}

int redis_cluster_async_command(char *command)
{
    if(NULL == command)
    {
    	printf("NULL == command \n");
        return 1;
    }

	printf("redis_cluster_async_command, command=%s\n", command);

    //execute redis command     
    if(REDIS_OK == redisClusterAsyncCommand(predis_cluster_async_context, redisAsyncCommandCallback, NULL, command))
    {
    	printf("redisClusterAsyncCommand success: command=%s\n", command);
    }
    else
    {
    	printf("redisClusterAsyncCommand fail: command=%s, errstr=%s\n", command, predis_cluster_async_context->errstr);
    }
    
    return 0;
}

int main(int argc, char **argv)
{	
	if(argc < 3)
	{
		printf("USAGE: ./redisasynctest [command] [key] [value]\n");
	}
	
	char command[1024];
	memset(command, 0, 1024);
	for(int i = 1; i < argc; i++)
	{
		strcat(command, argv[i]);
		strcat(command, "  ");
	}	
	
	redis_cluster_async_connect();

	redis_cluster_async_command(command);
	
	printf("event_base_dispatch start\n");
	event_base_dispatch(base);
	printf("event_base_dispatch end\n");
	
	return 0;
}



编译:

g++ -o redisasynctest redisasynctest.cpp -I/usr/local/include/hiredis-vip -levent -lhiredis_vip -L/usr/local/lib 


使用:

[root@centosx64 testcb]# ./redisasynctest get test0
redis_cluster_async_connect, paddrs=10.0.33.31:7000,10.0.33.31:7001,10.0.33.32:7000,10.0.33.32:7001,10.0.33.33:7000,10.0.33.33:7001
redisClusterAsyncConnect success
redis_cluster_async_command, command=get  test0  
redisClusterAsyncCommand success: command=get  test0  
event_base_dispatch start
redisAsyncConnectCallback success
redisAsyncCommandCallback: 0, test110
redisAsyncDisconnectCallback success
event_base_dispatch end




你可能感兴趣的:(hiredis-vip库,集群,异步实例)