Redis半知半解(三)----通信协议与命令对象

一、Redis通信协议
    · Redis基于RESP(Redis Serialization Protocol)协议来完成客户端和服务端的通信;
      RESP本质是一种文本协议,实现简单、易于解析。
    
    · 客户端和服务端通过tcp/流式域套接字来进行通信,为了防止粘包因此命令或数据结构以\r\n(CRLF)结尾
        (tcp会解分字节流,所以会混在一起)
                
    · 请求 get username:1234  --> *2\r\n$3\r\nget\r\n$13\r\nusername:1234\r\n
        (* <参数数量> CR LF $ <参数n字节数> CR LF <参数n的数据> CR LF)
                ## deps\hiredis\hiredis.c
                /* Build the command at protocol level */
                    cmd = malloc(totlen+1);
                    if (cmd == NULL)
                        return -1;
                    pos = sprintf(cmd,"*%d\r\n",argc);
                    for (j = 0; j < argc; j++) {
                        len = argvlen ? argvlen[j] : strlen(argv[j]);
                        pos += sprintf(cmd+pos,"$%zu\r\n",len);
                        memcpy(cmd+pos,argv[j],len);
                        pos += len;
                        cmd[pos++] = '\r';
                        cmd[pos++] = '\n';
                    }
                    assert(pos == totlen);
                    cmd[pos] = '\0';

                    *target = cmd;
                    return totlen;
    
    · redis-cli给输出优化转义;
        static sds cliFormatReplyTTY(redisReply *r, char *prefix)

    · redis中常见的命令返回被初始化在了内存中,以避免被频繁的创建销毁

二、Redis命令对象
    · Redis的命令使用的是redisCommand数据结构来管理
        struct redisCommand {
            char *name;             // 命令名
            redisCommandProc *proc; // 指向命令实现的函数
            int arity;      // 限制参数的个数,可以使用-N表示>=N
            char *sflags;   // 设置命令属性
            int flags;      // 用于追加额外的设置

            redisGetKeysProc *getkeys_proc;     /* 使用函数来确定命令行中的键参数。  
                                                   用于Redis集群重定向。  */
            int firstkey;   // first argument that is a key
            int lastkey;    // last argument that is a key
            int keystep;    // 命令获取一段参数的步长 , MSET KEY1 VAL1 KEY2 VAL2 -->2,因为mset参数步长为2(key、val)
            long long microseconds, calls;      // 表示命令的总耗时和被调用的次数
        };

三、附录(命令定义、命令对象的slags)

    · 命令:src\server.c

    · sflags:
           /* This is the meaning of the flags:
            *
            * w: write command (may modify the key space).
            * r: read command  (will never modify the key space).
            * m: may increase memory usage once called. Don't allow if out of memory.
            * a: admin command, like SAVE or SHUTDOWN.
            * p: Pub/Sub related command.
            * f: force replication of this command, regardless of 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.
            * S: Sort command output array if called from script, so that the output
            *    is deterministic.
            * 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.
            * k: Perform an implicit ASKING for this command, so the command will be
            *    accepted in cluster mode if the slot is marked as 'importing'.
            * F: Fast command: O(1) or O(log(N)) command that should never delay
            *    its execution as long as the kernel scheduler is giving us time.
            *    Note that commands that may trigger a DEL as a side effect (like SET)
            *    are not fast commands.
           */

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值