-
Notifications
You must be signed in to change notification settings - Fork 173
Home
deep edited this page Oct 27, 2015
·
19 revisions
-
SUPPORT REDIS CLUSTER
:- Connect to redis cluster and run commands. Handled ask and moved redirection.
-
SUPPORT MULTI-KEY COMMAND
:- Support
MSET
,MGET
andDEL
.
- Support
-
SUPPORT PIPELINE
:- Support redis pipeline and can contain multi-key command like above.
-
SUPPORT Asynchronous API
:- User can run commands with asynchronous mode.
###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;
}