Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/t_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ void setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire,
if (getGenericCommand(c) == C_ERR) return;
}

/* KEEPTTL carries the existing expire over to the new value. If the key is
* already logically expired but not reclaimed yet, drop it first, otherwise the
* dead expire is moved onto the new value and the write is lost on the next read. */
if (flags & OBJ_KEEPTTL) expireIfNeeded(c->db, key);

genericSetKey(c,c->db,key, val,flags & OBJ_KEEPTTL,1);
g_pserver->dirty++;
notifyKeyspaceEvent(NOTIFY_STRING,"set",key,c->db->id);
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/expire.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,17 @@ start_server {tags {"expire"}} {
assert {$ttl <= 100 && $ttl > 80}
}

test {SET - KEEPTTL on a logically expired key keeps the new value} {
r flushall
r debug set-active-expire 0
r set foo old PXAT 1
assert_equal OK [r set foo new KEEPTTL]
assert_equal new [r get foo]
assert_equal 1 [r exists foo]
assert_equal -1 [r ttl foo]
r debug set-active-expire 1
}

test {Roundtrip for subkey expires works} {
r flushall
r sadd testkey foo bar baz
Expand Down