天天看點

Redis 源碼學習-Redis指令 get

get指令的實作是t_string.c中的getCommand函數。

void getCommand(redisClient *c) {
    getGenericCommand(c);
}

int getGenericCommand(redisClient *c) {
    robj *o;

    // 嘗試從資料庫中取出鍵 c->argv[1] 對應的值對象
    // 如果鍵不存在時,向用戶端發送回複資訊,并傳回 NULL
    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
        return REDIS_OK;

    // 值對象存在,檢查它的類型
    if (o->type != REDIS_STRING) {
        // 類型錯誤
        addReply(c,shared.wrongtypeerr);
        return REDIS_ERR;
    } else {
        // 類型正确,向用戶端傳回對象的值
        addReplyBulk(c,o);
        return REDIS_OK;
    }
}           

關于上面的檢查類型,redis在redis.h中定義了五種對象類型。

#define REDIS_STRING 0
#define REDIS_LIST 1
#define REDIS_SET 2
#define REDIS_ZSET 3
#define REDIS_HASH 4