summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-09-14 12:28:22 +0200
committerantirez <antirez@gmail.com>2015-09-14 12:32:23 +0200
commit5b6c764711798d077e9c32f7371c7fe53cc50674 (patch)
tree85a8539acc6138cfaf5191785c711b6707a8d5ae
parenta74ef35f071393d1daf3984edabaf0704a9e5bc4 (diff)
downloadredis-5b6c764711798d077e9c32f7371c7fe53cc50674.tar.gz
MOVE now can move TTL metadata as well.
MOVE was not able to move the TTL: when a key was moved into a different database number, it became persistent like if PERSIST was used. In some incredible way (I guess almost nobody uses Redis MOVE) this bug remained unnoticed inside Redis internals for many years. Finally Andy Grunwald discovered it and opened an issue. This commit fixes the bug and adds a regression test. Close #2765.
-rw-r--r--src/db.c4
-rw-r--r--tests/unit/basic.tcl13
2 files changed, 16 insertions, 1 deletions
diff --git a/src/db.c b/src/db.c
index b7cbbfd0a..5cb9463fb 100644
--- a/src/db.c
+++ b/src/db.c
@@ -714,7 +714,7 @@ void moveCommand(redisClient *c) {
robj *o;
redisDb *src, *dst;
int srcid;
- long long dbid;
+ long long dbid, expire;
if (server.cluster_enabled) {
addReplyError(c,"MOVE is not allowed in cluster mode");
@@ -748,6 +748,7 @@ void moveCommand(redisClient *c) {
addReply(c,shared.czero);
return;
}
+ expire = getExpire(c->db,c->argv[1]);
/* Return zero if the key already exists in the target DB */
if (lookupKeyWrite(dst,c->argv[1]) != NULL) {
@@ -755,6 +756,7 @@ void moveCommand(redisClient *c) {
return;
}
dbAdd(dst,c->argv[1],o);
+ if (expire) setExpire(dst,c->argv[1],expire);
incrRefCount(o);
/* OK! key moved, free the entry in the source DB */
diff --git a/tests/unit/basic.tcl b/tests/unit/basic.tcl
index b0b3b9bac..99bc1b85b 100644
--- a/tests/unit/basic.tcl
+++ b/tests/unit/basic.tcl
@@ -433,6 +433,19 @@ start_server {tags {"basic"}} {
set e
} {*ERR*index out of range}
+ test {MOVE can move key expire metadata as well} {
+ r select 10
+ r flushdb
+ r select 9
+ r set mykey foo ex 100
+ r move mykey 10
+ assert {[r ttl mykey] == -2}
+ r select 10
+ assert {[r ttl mykey] > 0 && [r ttl mykey] <= 100}
+ assert {[r get mykey] eq "foo"}
+ r select 9
+ }
+
test {SET/GET keys in different DBs} {
r set a hello
r set b world