之前的文章介绍了redis当中的模型,今天首先来拿字符串对象说说事~
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接口如下:
-
/* 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;
-
}
-
}
之后的一些api包括setrange, getrange, mget, mset, incrXXX那些函数基本思想都同上,先要检查参数的类型以及合法性,然后调用底层db给出的接口,然后返回,这边不再赘述,关于底层db的接口,会在讲完redis对象模型之后进行阅读以及分析~
阅读(12982) | 评论(0) | 转发(0) |