Skip to content
deep edited this page Oct 27, 2015 · 19 revisions

CLUSTER SUPPORT

FEATURES:

  • SUPPORT REDIS CLUSTER:

    • Connect to redis cluster and run commands. Handled ask and moved redirection.
  • SUPPORT MULTI-KEY COMMAND:

    • Support MSET, MGET and DEL.
  • SUPPORT PIPELINE:

    • Support redis pipeline and can contain multi-key command like above.
  • SUPPORT Asynchronous API:

    • User can run commands with asynchronous mode.

CLUSTER API

connect to redis cluster

redisClusterContext *redisClusterConnect(const char *addrs);

Connect to redis cluster and set a timeout

redisClusterContext *redisClusterConnectWithTimeout(const char *addrs, const struct timeval tv);

Connect to redis cluster with nonblock mode

redisClusterContext *redisClusterConnectNonBlock(const char *addrs);

Release the redisClusterContext

void redisClusterFree(redisClusterContext *cc);

Set the MaxRedirect

void redisClusterSetMaxRedirect(redisClusterContext *cc, int max_redirect_count);

Execute the command and get the reply

void *redisClusterCommand(redisClusterContext *cc, const char *format, ...);

Append the command for pipeline

int redisClusterAppendCommand(redisClusterContext *cc, const char *format, ...);

Get the reply for pipeline

int redisClusterGetReply(redisClusterContext *cc, void **reply);

If you do not care about the reply for pipeline, then do not use redisClusterGetReply() , just calls redisCLusterReset one time. If the reply in the redisClusterGetReply() returns error, in the end calls redisCLusterReset one time.

void redisCLusterReset(redisClusterContext *cc);

Connect to redis cluster for asynch

redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs);

Set the connection callback for asynch

int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc, redisConnectCallback *fn);

Set the disconnection callback for asynch

int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn);

Execute the commands for asynch

int redisClusterAsyncCommand(redisClusterAsyncContext *acc, redisCallbackFn *fn, void *privdata, const char *format, ...);

Disconnect to redis cluster and release the resource

void redisClusterAsyncDisconnect(redisClusterAsyncContext *acc);

Release the resource

void redisClusterAsyncFree(redisClusterAsyncContext *acc);

EXAMPLES

###example-1

#include<stdio.h>
#include<hircluster.h>
int main()
{
	char *key=key-a”;
	char *field=field-1”;
	char *key1=key1”;
	char *value2=value-1”;
	char *key2=key1”;
	char *value2=value-1”;
	redisClusterContext *cc = redisClusterConnect(“127.0.0.1:34501,127.0.0.1:34502,127.0.0.1:34503”);
	if(cc == NULL || cc->err)
	{
		printf("connect error : %s\n", cc == NULL ? "NULL" : cc->errstr);
		return -1;
	}
	
	redisReply *reply = redisClusterCommand(cc, "hmget %s %s", key, field);
	if(reply == NULL)
	{
		printf("reply is null[%s]\n", cc->errstr);
		redisClusterFree(cc);
		return -1;
	}
	
	printf(“reply->type:%d”, reply->type);
	
	freeReplyObject(reply);

	reply = redisClusterCommand(cc, "mset %s %s %s %s", key1, value1, key2, value2);
	if(reply == NULL)
	{
		printf("reply is null[%s]\n", cc->errstr);
		redisClusterFree(cc);
		return -1;
	}

	printf(“reply->str:%s”, reply->str);
	
	freeReplyObject(reply);
	redisClusterFree(cc);
	return 0;
}

###example-2 --asynchronous

#include<stdio.h>
#include<hircluster.h>
#include<adapters/libevent.h>
 
int all_count=0;
void getCallback(redisAsyncContext *ac, void *r, void *privdata)
{
	redisReply *reply = r;
	calldata *data = (calldata *)privdata;
	redisClusterAsyncContext *acc = data->acc;
	int count = data->count;
	all_count ++;
	if(all_count >= count)
	{
		redisClusterAsyncDisconnect(acc);
	}
}

void connectCallback(const redisAsyncContext *c, int status)
{
	if (status != REDIS_OK) {
		printf("Error: %s\n", c->errstr);
		return;
	}
	printf("Connected...\n");
}

void disconnectCallback(const redisAsyncContext *c, int status)
{
	if (status != REDIS_OK) {
		printf("Error: %s\n", c->errstr);
		return;
	}
	
	printf("\nDisconnected...\n");
}

int main(int argc, char **argv)
{
	int status;
	struct event_base *base = event_base_new();
	redisClusterAsyncContext *acc = redisClusterAsyncConnect("127.0.0.1:34501,127.0.0.1:34501");
	if (acc->err)
	{
		printf("Error: %s\n", acc->errstr);
		return 1;
	}
	redisClusterLibeventAttach(acc,base);
	redisClusterAsyncSetConnectCallback(acc,connectCallback);
	redisClusterAsyncSetDisconnectCallback(acc,disconnectCallback);
	calldata data;
	data.acc = acc;
	data.count = 10000;
	for(i = 0; i < 10000; i ++)
	{
		status = redisClusterAsyncCommand(acc, getCallback, &data, "set %d %d", i, i);
		if(status != REDIS_OK)
		{
			printf("error: %d %s\n", acc->err, acc->errstr);
		}
	}
	
	event_base_dispatch(base);
	return 0;
}

###example-3 --pipeline

#include<stdio.h>
#include<hircluster.h>
int main()
{
	redisClusterContext *cc = redisClusterConnect(“127.0.0.1:34501,127.0.0.1:34502,127.0.0.1:34503”);
	redisReply *reply;
	if(cc == NULL || cc->err)
	{
		printf("connect error : %s\n", cc == NULL ? "NULL" : cc->errstr);
		return -1;
	}
 
	redisClusterAppendCommand(cc, "set key1 value1");
	redisClusterAppendCommand(cc, "get key1");
	redisClusterAppendCommand(cc, "mset key2 value2 key3 value3");
	redisClusterAppendCommand(cc, "mget key2 key3");
	
	redisClusterGetReply(cc, &reply);  //for "set key1 value1"
	freeReplyObject(reply);
	redisClusterGetReply(cc, &reply);  //for "get key1"
	freeReplyObject(reply);
	redisClusterGetReply(cc, &reply);  //for "mset key2 value2 key3 value3"
	freeReplyObject(reply);
	redisClusterGetReply(cc, &reply);  //for "mget key2 key3"
	freeReplyObject(reply);
	
	redisCLusterReset(cc);
		
	...
	...
	
	redisClusterFree(cc);
	return 0;
}
Clone this wiki locally