在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:hiredis-vip开源软件地址:https://gitee.com/vipshop/hiredis-vip开源软件介绍:HIREDIS-VIPHiredis-vip is a C client library for the Redis database. Hiredis-vip supported redis cluster. Hiredis-vip fully contained and based on Hiredis . CLUSTER SUPPORTFEATURES:
CLUSTER API:redisClusterContext *redisClusterContextInit(void);void redisClusterFree(redisClusterContext *cc);int redisClusterSetOptionAddNode(redisClusterContext *cc, const char *addr);int redisClusterSetOptionAddNodes(redisClusterContext *cc, const char *addrs);int redisClusterSetOptionConnectBlock(redisClusterContext *cc);int redisClusterSetOptionConnectNonBlock(redisClusterContext *cc);int redisClusterSetOptionParseSlaves(redisClusterContext *cc);int redisClusterSetOptionParseOpenSlots(redisClusterContext *cc);int redisClusterSetOptionRouteUseSlots(redisClusterContext *cc);int redisClusterSetOptionConnectTimeout(redisClusterContext *cc, const struct timeval tv);int redisClusterSetOptionTimeout(redisClusterContext *cc, const struct timeval tv);int redisClusterSetOptionMaxRedirect(redisClusterContext *cc, int max_redirect_count);int redisClusterConnect2(redisClusterContext *cc);void *redisClusterFormattedCommand(redisClusterContext *cc, char *cmd, int len);void *redisClustervCommand(redisClusterContext *cc, const char *format, va_list ap);void *redisClusterCommand(redisClusterContext *cc, const char *format, ...);void *redisClusterCommandArgv(redisClusterContext *cc, int argc, const char **argv, const size_t *argvlen);int redisClusterAppendFormattedCommand(redisClusterContext *cc, char *cmd, int len);int redisClustervAppendCommand(redisClusterContext *cc, const char *format, va_list ap);int redisClusterAppendCommand(redisClusterContext *cc, const char *format, ...);int redisClusterAppendCommandArgv(redisClusterContext *cc, int argc, const char **argv, const size_t *argvlen);int redisClusterGetReply(redisClusterContext *cc, void **reply);void redisClusterReset(redisClusterContext *cc);redisContext *ctx_get_by_node(redisClusterContext *cc, struct cluster_node *node);redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, int flags);int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc, redisConnectCallback *fn);int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn);int redisClusterAsyncFormattedCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, char *cmd, int len);int redisClustervAsyncCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, const char *format, va_list ap);int redisClusterAsyncCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, const char *format, ...);int redisClusterAsyncCommandArgv(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen);void redisClusterAsyncDisconnect(redisClusterAsyncContext *acc);void redisClusterAsyncFree(redisClusterAsyncContext *acc);redisAsyncContext *actx_get_by_node(redisClusterAsyncContext *acc, cluster_node *node); CLUSTER API (old api, version <= 0.3.0):redisClusterContext *redisClusterConnect(const char *addrs, int flags);redisClusterContext *redisClusterConnectWithTimeout(const char *addrs, const struct timeval tv, int flags);redisClusterContext *redisClusterConnectNonBlock(const char *addrs, int flags);void redisClusterFree(redisClusterContext *cc);void redisClusterSetMaxRedirect(redisClusterContext *cc, int max_redirect_count);void *redisClusterFormattedCommand(redisClusterContext *cc, char *cmd, int len);void *redisClustervCommand(redisClusterContext *cc, const char *format, va_list ap);void *redisClusterCommand(redisClusterContext *cc, const char *format, ...);void *redisClusterCommandArgv(redisClusterContext *cc, int argc, const char **argv, const size_t *argvlen);redisContext *ctx_get_by_node(struct cluster_node *node, const struct timeval *timeout, int flags);int redisClusterAppendFormattedCommand(redisClusterContext *cc, char *cmd, int len);int redisClustervAppendCommand(redisClusterContext *cc, const char *format, va_list ap);int redisClusterAppendCommand(redisClusterContext *cc, const char *format, ...);int redisClusterAppendCommandArgv(redisClusterContext *cc, int argc, const char **argv, const size_t *argvlen);int redisClusterGetReply(redisClusterContext *cc, void **reply);void redisClusterReset(redisClusterContext *cc);redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, int flags);int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc, redisConnectCallback *fn);int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn);int redisClusterAsyncFormattedCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, char *cmd, int len);int redisClustervAsyncCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, const char *format, va_list ap);int redisClusterAsyncCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, const char *format, ...);int redisClusterAsyncCommandArgv(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen);void redisClusterAsyncDisconnect(redisClusterAsyncContext *acc);void redisClusterAsyncFree(redisClusterAsyncContext *acc); Quick usageIf you want used but not read the follow, please reference the examples:https://github.com/vipshop/hiredis-vip/wiki Cluster synchronous APITo consume the synchronous API, there are only a few function calls that need to be introduced: redisClusterContext *redisClusterContextInit(void);int redisClusterSetOptionAddNodes(redisClusterContext *cc, const char *addrs);int redisClusterSetOptionMaxRedirect(redisClusterContext *cc, int max_redirect_count);int redisClusterSetOptionConnectTimeout(redisClusterContext *cc, const struct timeval tv);int redisClusterSetOptionTimeout(redisClusterContext *cc, const struct timeval tv);int redisClusterConnect2(redisClusterContext *cc);void *redisClusterCommand(redisClusterContext *cc, const char *format, ...);void redisClusterFree(redisClusterContext *cc); Cluster connectingThe function redisClusterContext *cc = redisClusterContextInit();redisClusterSetOptionAddNodes(cc, "127.0.0.1:6379,127.0.0.1:6380");redisClusterConnect2(cc);if (cc != NULL && cc->err) { printf("Error: %s\n", cc->errstr); // handle error} Cluster sending commandsThe next that will be introduced is reply = redisClusterCommand(clustercontext, "SET foo bar"); The specifier reply = redisClusterCommand(clustercontext, "SET foo %s", value); Internally, Hiredis-vip splits the command in different arguments and willconvert it to the protocol used to communicate with Redis.One or more spaces separates arguments, so you can use the specifiersanywhere in an argument: reply = redisClusterCommand(clustercontext, "SET key:%s %s", myid, value); Cluster multi-key commandsHiredis-vip supports mget/mset/del multi-key commands.Those multi-key commands is highly effective.Millions of keys in one mget command just used several seconds. Example: reply = redisClusterCommand(clustercontext, "mget %s %s %s %s", key1, key2, key3, key4); Cluster cleaning upTo disconnect and free the context the following function can be used: void redisClusterFree(redisClusterContext *cc); This function immediately closes the socket and then frees the allocations done increating the context. Cluster pipeliningThe function int redisClusterAppendCommand(redisClusterContext *cc, const char *format, ...);int redisClusterAppendCommandArgv(redisClusterContext *cc, int argc, const char **argv); After calling either function one or more times, void redisClusterReset(redisClusterContext *cc); Warning: You must call The following examples shows a simple cluster pipeline: redisReply *reply;redisClusterAppendCommand(clusterContext,"SET foo bar");redisClusterAppendCommand(clusterContext,"GET foo");redisClusterGetReply(clusterContext,&reply); // reply for SETfreeReplyObject(reply);redisClusterGetReply(clusterContext,&reply); // reply for GETfreeReplyObject(reply);redisClusterReset(clusterContext); Cluster asynchronous APIHiredis-vip comes with an cluster asynchronous API that works easily with any event library.Now we just support and test for libevent and redis ae, if you need for other event libraries,please contact with us, and we will support it quickly. ConnectingThe function redisClusterAsyncContext *acc = redisClusterAsyncConnect("127.0.0.1:6379", HIRCLUSTER_FLAG_NULL);if (acc->err) { printf("Error: %s\n", acc->errstr); // handle error} The cluster asynchronous context can hold a disconnect callback function that is called when theconnection is disconnected (either because of an error or per user request). This function shouldhave the following prototype: void(const redisAsyncContext *c, int status); On a disconnect, the You not need to reconnect in the disconnect callback, hiredis-vip will reconnect this connection itselfwhen commands come to this redis node. Setting the disconnect callback can only be done once per context. For subsequent calls it willreturn int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn); Sending commands and their callbacksIn an cluster asynchronous context, commands are automatically pipelined due to the nature of an event loop.Therefore, unlike the cluster synchronous API, there is only a single way to send commands.Because commands are sent to Redis cluster asynchronously, issuing a command requires a callback functionthat is called when the reply is received. Reply callbacks should have the following prototype: void(redisClusterAsyncContext *acc, void *reply, void *privdata); The The functions that can be used to issue commands in an asynchronous context are: int redisClusterAsyncCommand( redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, const char *format, ...); This function work like their blocking counterparts. The return value is If the reply for a command with a All pending callbacks are called with a DisconnectingAn cluster asynchronous connection can be terminated using: void redisClusterAsyncDisconnect(redisClusterAsyncContext *acc); When this function is called, the connection is not immediately terminated. Instead, newcommands are no longer accepted and the connection is only terminated when all pending commandshave been written to the socket, their respective replies have been read and their respectivecallbacks have been executed. After this, the disconnection callback is executed with the Hooking it up to event library XThere are a few hooks that need to be set on the cluster context object after it is created.See the AUTHORSHiredis-vip was maintained and used at vipshop(https://github.com/vipshop). The redis client library part in hiredis-vip is same as hiredis(https://github.com/redis/hiredis). The redis cluster client library part in hiredis-vip is written by deep(https://github.com/deep011). Hiredis-vip is released under the BSD license. |
请发表评论