欢迎投稿

今日深度:

Redis 源码学习- Redis命令,

Redis 源码学习- Redis命令,


Redis命令

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;
};
Redis命令表定义
/* 
 * 命令表
 */
 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 属性中。

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。

FLAG含义解析

w: write command (may modify the key space).
写入命令,可能会修改 key space

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.
可能会占用大量内存的命令,调用时对内存占用进行检查

a: admin command, like SAVE or SHUTDOWN.
管理用途的命令,比如 SAVE 和 SHUTDOWN

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的槽可以接收这命令。

www.htsjk.Com true http://www.htsjk.com/redis/42718.html NewsArticle Redis 源码学习- Redis命令, Redis命令 redis.h中定了命令的数据结构,并且在redis.c中定义了命令表。 Redis命令数据结构 struct redisCommand { char *name; redisCommandProc *proc; int arity; char *sflags; /* Fl...
评论暂时关闭