Redis源码解析(三)SET命令实现

在处理过程中,会首先调用
parseExtendedStringArgumentsOrReply函数对SET命令的参数进行解析。

该函数对参数的解析方式有两种。一种是逐个字符进行比较,如NX参数的解析语句为(opt[0] == 'N') && (opt[0] == 'X')。第二种是使用strcasecmp忽略大小写进行比较。

在解析完成后将解析结果记录在int类型的flag变量里。多参数使用偏移量进行记录。

定义如下:

#define OBJ_NO_FLAGS 0 #define OBJ_SET_NX (1<<0) /* Set if key not exists. */ #define OBJ_SET_XX (1<<1) /* Set if key exists. */ #define OBJ_EX (1<<2) /* Set if time in seconds is given */ #define OBJ_PX (1<<3) /* Set if time in ms in given */ #define OBJ_KEEPTTL (1<<4) /* Set and keep the ttl */ #define OBJ_SET_GET (1<<5) /* Set if want to get key before set */ #define OBJ_EXAT (1<<6) /* Set if timestamp in second is given */ #define OBJ_PXAT (1<<7) /* Set if timestamp in ms is given */ #define OBJ_PERSIST (1<<8) /* Set if we need to remove the ttl */


处理

在解析完成后会调用setGenericCommand进行SET命令的通用处理。

第一步,上送超时参数时,设置超时参数。

第二步,上送NX参数或XX参数时,检查对应键值是否存在。

第三步,上送GET参数时,设置返回项为原String。

第四步,使用genericSetKey函数进行数据存放。服务器操作次数加一。

第五步,使用notifyKeyspaceEvent发布消息。

第六步,上送GET参数时,跳过后续参数中所有GET。

支付源码

上一篇:

下一篇: