redisCommand结构体详解


struct redisCommand {
    char *name;//命令名
    redisCommandProc *proc;//命令执行函数
    int arity; //参数个数,-N代表参数个数>=N,正数表示参数个数为N
    char *sflags; //命令的sflags属性字符串
    int flags;    //从sflags获取的整数mask值
    
    redisGetKeysProc *getkeys_proc;//获取key参数的可选函数,当下面3种情况都无法确定key参数的时候才需要使用该函数
    int firstkey; //第一个key的位置,0表示没有key
    int lastkey;  //最后一个key的位置;负数计算为正数第(argc+lastkey)个
    int keystep;  //参数为 key,val,key,val,...格式,第一个和最后一个key之间的key跨步
    long long microseconds;//命令从服务启动到现在的执行时间,单位:微秒
	long long calls;//命令从服务启动到现在的执行的次数
};

//返回key的个数和位置
int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) {
    int j, i = 0, last, *keys;
    UNUSED(argv);

    if (cmd->firstkey == 0) {
        *numkeys = 0;
        return NULL;
    }

    last = cmd->lastkey;
    if (last < 0) last = argc+last;
    keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1));
    for (j = cmd->firstkey; j <= last; j += cmd->keystep) {
        if (j >= argc) {
            /* Modules commands, and standard commands with a not fixed number
             * of arguments (negative arity parameter) do not have dispatch
             * time arity checks, so we need to handle the case where the user
             * passed an invalid number of arguments here. In this case we
             * return no keys and expect the command implementation to report
             * an arity or syntax error. */
            if (cmd->flags & CMD_MODULE || cmd->arity < 0) {
                zfree(keys);
                *numkeys = 0;
                return NULL;
            } else {
                serverPanic("Redis built-in command declared keys positions not matching the arity requirements.");
            }
        }
        keys[i++] = j;
    }
    *numkeys = i;
    return keys;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值