下载 redis2.4.* 版本源码
A,注册一个命令
struct redisCommand { char *name; //命令名称 redisCommandProc *proc; //命令调用函数 int arity; //命令参数个数 int flags; //如果标记为REDIS_CMD_DENYOOM,NULL,内存满后此命令不可执行,主要针对写数据库的命令 /* Use a function to determine which keys need to be loaded * in the background prior to executing this command. Takes precedence * over vm_firstkey and others, ignored when NULL */ redisVmPreloadProc *vm_preload_proc; /* What keys should be loaded in background when calling this command? */ int vm_firstkey; /* The first argument that's a key (0 = no keys) */ int vm_lastkey; /* THe last argument that's a key */ int vm_keystep; /* The step between first and last key */ };
主要需要设置前面四个参数即可,后面的几个属性是关于开启vm的时候redis预读选项的配置,redis2.6以后由于废弃了vm特性,我也没看到这些参数有什么用。
redisCommandProc函数有一个参数redisClient *c
可以通过c->argv获取命令的参数列表
然后经过后台处理后,调用addReply函数给客户端响应消息
B,配置keepalive特性
tcp开启keepalive特性,redis封装了anetTcpKeepAlive函数,不过没有启用,不知道为什么
在network.c的createClient函数里面添加如下代码,设置keepalive的几个参数值
anetNonBlock(NULL,fd); anetTcpNoDelay(NULL,fd); if(server.keepalive){ anetTcpKeepAlive(NULL,fd); setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (void*)&server.keepidle , sizeof(server.keepidle )); setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (void *)&server.keepinterval , sizeof(server.keepinterval )); setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (void *)&server.keepcount , sizeof(server.keepcount )); }
Redis 2.6.*里面是默认开启tcp keepalive的。