Skip to content
deep edited this page Sep 30, 2015 · 19 revisions

特点与局限 特点: 支持cluster 支持mget,mset,del多key操作 支持pipeline 支持异步api 局限: 部分命令不支持,例如flushall,info,config set等全局命令 smove, sinter, sunion, sdiff等取集合交集并集的多key命令暂不支持(如果有需要,可以支持) 迁移或者扩容过程中,异步api和多key操作会受到限制 为了支持cluster特性,对用户增加了如下api 连接redis cluster redisClusterContext *redisClusterConnect(const char *addrs); 连接redis cluster,并设置超时 redisClusterContext *redisClusterConnectWithTimeout(const char *addrs, const struct timeval tv); 非阻塞连接redis cluster redisClusterContext *redisClusterConnectNonBlock(const char *addrs); 释放资源 void redisClusterFree(redisClusterContext *cc); 设置最大重定向次数 void redisClusterSetMaxRedirect(redisClusterContext *cc, int max_redirect_count); 执行命令 void *redisClusterCommand(redisClusterContext *cc, const char *format, ...); pipeline支持 int redisClusterAppendCommand(redisClusterContext *cc, const char *format, ...); 获取pipeline的响应 int redisClusterGetReply(redisClusterContext *cc, void **reply); 异步连接redis cluster redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs); 设置异步建立连接的回调函数 int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc, redisConnectCallback *fn); 设置异步断开连接的回调函数 int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn); 执行命令-异步 int redisClusterAsyncCommand(redisClusterAsyncContext *acc, redisCallbackFn *fn, void *privdata, const char *format, ...); 断开异步连接并释放资源 void redisClusterAsyncDisconnect(redisClusterAsyncContext *acc); 释放异步资源 void redisClusterAsyncFree(redisClusterAsyncContext *acc);

示例1 #include<stdio.h> #include<hircluster.h> int main() { char *key=”key-a”; char *field=”field-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); redisClusterFree(cc); return 0; } 示例2--异步 #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; } 示例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);

redisClusterFree(cc); return 0; }

Clone this wiki locally