coding redis-source-reading
26 February 2018

2018-02-26

run_with_period

每执行一次 serverCron 就会执行一次

serverCron () {
    ...
    server.cronloops++;
    return 1000 / server.hz;
}

cronloops 表示当前执行了多少次循环,返回 1000/server.hz

#define run_with_period(_ms_) if ((_ms_ <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz))))
#define run_with_period(_ms_) if ((_ms_ <= 100) || (server.cronloops % ((_ms_)/100)) == 0)

假设 ms == 1000
run_with_period(1000) => server.cronloops % (1000/100) = server.cronloops % 10
执行相当于 10 次 serverCron 参会执行 run_with_period(1000) 里面的功能
执行相当于 10 次 serverCron 耗时刚好是 1000ms

假设 ms == 2000
run_with_period(2000) => server.cronloops % (2000/100) = server.cronloops % 20
执行相当于 20 次 serverCron 参会执行 run_with_period(2000) 里面的功能
执行相当于 20 次 serverCron 耗时刚好是 2000ms

serverCron 每隔 100ms 执行一次,但是 run_with_period(1000) 里面的功能是每隔 1000ms 执行一次

lru key expired

#define LRU_CLOCK()                  ((1000/server.hz <= REDIS_LRU_CLOCK_RESOLUTION) ? server.lruclock : getLRUClock())
#define REDIS_LRU_BITS               24
#define REDIS_LRU_CLOCK_MAX          ((1<<REDIS_LRU_BITS)-1) /* Max value of obj->lru */
#define REDIS_LRU_CLOCK_RESOLUTION   1000                    /* LRU clock resolution in ms */

typedef struct redisObject {
    unsigned type:4;
    unsigned encoding:4;
    unsigned lru:REDIS_LRU_BITS;   /* lru time (relative to server.lruclock) */
    int refcount;
    void *ptr;
} robj;

unsigned int getLRUClock(void) {
    return (mstime() / REDIS_LRU_CLOCK_RESOLUTION) & REDIS_LRU_CLOCK_MAX;
}

2018-03-04

dict.c

2018-03-05

object.c
t_string.c

2018-03-08

redis 协议
networking.c (reply)