diff --git a/src/db/string.go b/src/db/string.go index abf985b..28a5727 100644 --- a/src/db/string.go +++ b/src/db/string.go @@ -121,21 +121,27 @@ func Set(db *DB, args [][]byte) redis.Reply { case updatePolicy: result = db.PutIfExists(key, entity) } - if result > 0 { - if ttl != unlimitedTTL { - expireTime := time.Now().Add(time.Duration(ttl) * time.Millisecond) - db.Expire(key, expireTime) - db.AddAof(reply.MakeMultiBulkReply([][]byte{ - []byte("SET"), - args[0], - args[1], - })) - db.AddAof(makeExpireCmd(key, expireTime)) - } else { - db.Persist(key) // override ttl - db.AddAof(makeAofCmd("set", args)) - } + /* + * 如果设置了ttl 则以最新的ttl为准 + * 如果没有设置ttl 是新增key的情况,不设置ttl。 + * 如果没有设置ttl 且已存在key的 不修改ttl 但需要增加aof + */ + if ttl != unlimitedTTL { + expireTime := time.Now().Add(time.Duration(ttl) * time.Millisecond) + db.Expire(key, expireTime) + db.AddAof(reply.MakeMultiBulkReply([][]byte{ + []byte("SET"), + args[0], + args[1], + })) + db.AddAof(makeExpireCmd(key, expireTime)) + } else if result > 0{ + db.Persist(key) // override ttl + db.AddAof(makeAofCmd("set", args)) + }else{ + db.AddAof(makeAofCmd("set", args)) } + if policy == upsertPolicy || result > 0 { return &reply.OkReply{} } else {