1. 设置命令
首先,redis定义了5个宏来确定当前设置的一些属性,具体如下
点击(此处)折叠或打开
- #define OBJ_SET_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_SET_EX (1<<2) /* Set if time in seconds is given */
- #define OBJ_SET_PX (1<<3) /* Set if time in ms in given */
点击(此处)折叠或打开
- /* SET key value [NX] [XX] [EX <seconds>] [PX <milliseconds>] */
- void setCommand(client *c) {
- int j;
- robj *expire = NULL;
- int unit = UNIT_SECONDS;
- int flags = OBJ_SET_NO_FLAGS;
- for (j = 3; j < c->argc; j++) {
- char *a = c->argv[j]->ptr;
- robj *next = (j == c->argc-1) ? NULL : c->argv[j+1];
- if ((a[0] == 'n' || a[0] == 'N') &&
- (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
- !(flags & OBJ_SET_XX))
- {
- flags |= OBJ_SET_NX;
- } else if ((a[0] == 'x' || a[0] == 'X') &&
- (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
- !(flags & OBJ_SET_NX))
- {
- flags |= OBJ_SET_XX;
- } else if ((a[0] == 'e' || a[0] == 'E') &&
- (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
- !(flags & OBJ_SET_PX) && next)
- {
- flags |= OBJ_SET_EX;
- unit = UNIT_SECONDS;
- expire = next;
- j++;
- } else if ((a[0] == 'p' || a[0] == 'P') &&
- (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
- !(flags & OBJ_SET_EX) && next)
- {
- flags |= OBJ_SET_PX;
- unit = UNIT_MILLISECONDS;
- expire = next;
- j++;
- } else {
- addReply(c,shared.syntaxerr);
- return;
- }
- }
- c->argv[2] = tryObjectEncoding(c->argv[2]);
- setGenericCommand(c,flags,c->argv[1],c->argv[2],expire,unit,NULL,NULL);
- }
这个就是整个set命令当中的重头戏,其余的所有set操作,在确定一些其他的参数后,都会调用这个函数,具体代码如下:
点击(此处)折叠或打开
- void setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire, int unit, robj *ok_reply, robj *abort_reply) {
- long long milliseconds = 0; /* initialized to avoid any harmness warning */
- if (expire) {
- if (getLongLongFromObjectOrReply(c, expire, &milliseconds, NULL) != C_OK)
- return;
- if (milliseconds <= 0) {
- addReplyErrorFormat(c,"invalid expire time in %s",c->cmd->name);
- return;
- }
- if (unit == UNIT_SECONDS) milliseconds *= 1000;
- }
- if ((flags & OBJ_SET_NX && lookupKeyWrite(c->db,key) != NULL) ||
- (flags & OBJ_SET_XX && lookupKeyWrite(c->db,key) == NULL))
- {
- addReply(c, abort_reply ? abort_reply : shared.nullbulk);
- return;
- }
- setKey(c->db,key,val);
- server.dirty++;
- if (expire) setExpire(c,c->db,key,mstime()+milliseconds);
- notifyKeyspaceEvent(NOTIFY_STRING,"set",key,c->db->id);
- if (expire) notifyKeyspaceEvent(NOTIFY_GENERIC,
- "expire",key,c->db->id);
- addReply(c, ok_reply ? ok_reply : shared.ok);
- }
这里需要注意的一点是:每次调用set命令时,redis都会先调用tryObjectEncoding这个函数来尽量将值的编码类型转换为占用存储空间较小的那一个,之后才会调用setGenericCommand函数进行实际操作~~
get操作就相对没那么复杂,只需要调用底层的查找就行了
点击(此处)折叠或打开
- int getGenericCommand(client *c) {
- robj *o;
- if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
- return C_OK;
- if (o->type != OBJ_STRING) {
- addReply(c,shared.wrongtypeerr);
- return C_ERR;
- } else {
- addReplyBulk(c,o);
- return C_OK;
- }
- }