redis.h中定了命令的数据结构,而且在redis.c中定义了命令表。redis
struct redisCommand {
char *name;
redisCommandProc *proc;
int arity;
char *sflags; /* Flags as string representation, one char per flag. */
int flags; /* The actual flags, obtained from the 'sflags' field. */
/* Use a function to determine keys arguments in a command line.
* Used for Redis Cluster redirect. */
redisGetKeysProc *getkeys_proc;
int firstkey; /* The first argument that's a key (0 = no keys) */ int lastkey; 说/* The last argument that's a key */
int keystep; /* The step between first and last key */
long long microseconds, calls;
};
复制代码
/*
* 命令表
*/
struct redisCommand redisCommandTable[] = {
{"get",getCommand,2,"r",0,NULL,1,1,1,0,0},
{"set",setCommand,-3,"wm",0,NULL,1,1,1,0,0},
... // 后面的省略
};
复制代码
命令的基本结构由命令名称(name)和实现函数(proc)组成。数据库
arity表示为参数的个数,能够用 -N 表示 >= N。sflags是一个用来表示FLAG的字符串,每一个标志一个字符。命令中的FLAG首先是设置在sflags上,以后初始化服务器时调用redis.c中的populateCommandTable()函数从 sflags 属性中计算出真正的 FLAG 到 flags 属性中。bash
getkeys_proc是从命令中判断命令的键参数。在Redis 集群转向时使用。一个可选的函数,用于从命令中取出key参数,仅在如下三个参数都不足以表示key参数时使用。服务器
firstkey为第一个 key 参数的位置;lastkey为最后一个 key 参数的位置;keystep为key参数步长,从 first 参数和 last 参数之间,全部 key 的步数(step)。好比说, MSET 命令的格式为 MSET key value [key value ...],它的 step 就为 2。数据结构
microseconds记录执行这个命令耗费的总微秒数,初始化为 0。calls记录命令被执行的总次数,初始化为 0。less
w: write command (may modify the key space).
写入命令,可能会修改 key spacedom
r: read command (will never modify the key space).
读命令,不修改 key space函数
m: may increase memory usage once called. Don't allow if out of memory.
可能会占用大量内存的命令,调用时对内存占用进行检查ui
a: admin command, like SAVE or SHUTDOWN.
管理用途的命令,好比 SAVE 和 SHUTDOWNthis
p: Pub/Sub related command.
发布/订阅相关的命令
f: force replication of this command, regardless of server.dirty.
无视 server.dirty ,强制复制这个命令。
s: command not allowed in scripts.
不容许在脚本中使用的命令
R: random command. Command is not deterministic, that is, the same command
with the same arguments, with the same key space, may have different results. For instance SPOP and RANDOMKEY are two random commands. 随机命令。命令是非肯定性的:对于一样的命令,一样的参数,一样的键,结果可能不一样。好比 SPOP 和 RANDOMKEY 就是这样的例子。
S: Sort command output array if called from script, so that the output is deterministic. 若是命令在 Lua 脚本中执行,那么对输出进行排序,从而得出肯定性的输出。
l: Allow command while loading the database. 容许在载入数据库时使用的命令。
t: Allow command while a slave has stale data but is not allowed to server this data. Normally no command is accepted in this condition but just a few. 容许在附属节点带有过时数据时执行的命令。 这类命令不多有,只有几个。
M: Do not automatically propagate the command on MONITOR.
不要在 MONITOR 模式下自动广播的命令。
k: Perform an implicit ASKING for this command, so the command will be accepted in cluster mode if the slot is marked as 'importing'. 为这个命令执行一个显式的ASKING,使得在集群模式下,一个被标示为importing的槽能够接收这命令。