阅读set、setnx、setex、psetex、mset

This commit is contained in:
LingZhaoHui 2020-11-08 19:42:17 +08:00
parent f22dbb2dda
commit 732cc1e6ff
3 changed files with 22 additions and 9 deletions

View File

@ -2951,7 +2951,9 @@ void *IOThreadMain(void *myid) {
}
}
/* Initialize the data structures needed for threaded I/O. */
/* Initialize the data structures needed for threaded I/O.
* 线IO
*/
void initThreadedIO(void) {
io_threads_active = 0; /* We start with threads not active. */

View File

@ -89,7 +89,7 @@ static inline char sdsReqType(size_t string_size) {
sds sdsnewlen(const void *init, size_t initlen) {
void *sh;
sds s;
char type = sdsReqType(initlen); // 根据字符串长度选择不同烦人类型。
char type = sdsReqType(initlen); // 根据字符串长度选择不同sds类型。
/* Empty strings are usually created in order to append. Use type 8
* since type 5 is not good at this. */
if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;

View File

@ -97,7 +97,8 @@ void setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire,
void setCommand(client *c) {
int j;
robj *expire = NULL; // 超时时间robj类型
// 字符串的超时时间单位有秒和毫秒两种程序中根据此值来确认超时的单位此值只有两个取值分别为UNIT_SECONDSUNIT_MILLISECONDS
// 字符串的超时时间单位有秒和毫秒两种,程序中根据此值来确认超时的单位,
//此值只有两个取值分别为UNIT_SECONDSUNIT_MILLISECONDS
int unit = UNIT_SECONDS;
// int类型它是一个二进制串程序中根据此值来确定key是否应该被设置到数据库。
int flags = OBJ_SET_NO_FLAGS;
@ -147,17 +148,23 @@ void setCommand(client *c) {
c->argv[2] = tryObjectEncoding(c->argv[2]);
setGenericCommand(c,flags,c->argv[1],c->argv[2],expire,unit,NULL,NULL);
}
/**
* key值不存在的时候在执行
**/
void setnxCommand(client *c) {
c->argv[2] = tryObjectEncoding(c->argv[2]);
setGenericCommand(c,OBJ_SET_NX,c->argv[1],c->argv[2],NULL,0,shared.cone,shared.czero);
}
/**
* key-value设置到数据库key的超时秒数
**/
void setexCommand(client *c) {
c->argv[3] = tryObjectEncoding(c->argv[3]);
setGenericCommand(c,OBJ_SET_NO_FLAGS,c->argv[1],c->argv[3],c->argv[2],UNIT_SECONDS,NULL,NULL);
}
/**
* key-value设置到数据库key的超时毫秒数
**/
void psetexCommand(client *c) {
c->argv[3] = tryObjectEncoding(c->argv[3]);
setGenericCommand(c,OBJ_SET_NO_FLAGS,c->argv[1],c->argv[3],c->argv[2],UNIT_MILLISECONDS,NULL,NULL);
@ -317,8 +324,7 @@ void msetGenericCommand(client *c, int nx) {
return;
}
/* Handle the NX flag. The MSETNX semantic is to return zero and don't
* set anything if at least one key alerady exists. */
/* 当nx参数为1时需要遍历每个key在数据库中是否存在当有任意一个key存在时表示参数不合法会报错退出 */
if (nx) {
for (j = 1; j < c->argc; j += 2) {
if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
@ -327,7 +333,12 @@ void msetGenericCommand(client *c, int nx) {
}
}
}
/**
* key-value设置入数据库时
* tryObjectEncoding函数将每个value编码
* key-value添加到数据库中mset和msetex不能设置超时时间
* expire
* */
for (j = 1; j < c->argc; j += 2) {
c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
setKey(c,c->db,c->argv[j],c->argv[j+1]);