Skip to content

Commit 5c1d2b1

Browse files
authored
Merge pull request #178 from johnpena/set-keepttl
Implement KEEPTTL option on SET
2 parents 1fa60f4 + 5b1c4d5 commit 5c1d2b1

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

cmd_string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (m *Miniredis) cmdSet(c *server.Peer, cmd string, args []string) {
5555
var (
5656
nx = false // set iff not exists
5757
xx = false // set iff exists
58+
keepttl = false // set keepttl
5859
ttl time.Duration
5960
)
6061

@@ -70,6 +71,10 @@ func (m *Miniredis) cmdSet(c *server.Peer, cmd string, args []string) {
7071
xx = true
7172
args = args[1:]
7273
continue
74+
case "KEEPTTL":
75+
keepttl = true
76+
args = args[1:]
77+
continue
7378
case "PX":
7479
timeUnit = time.Millisecond
7580
fallthrough
@@ -116,6 +121,11 @@ func (m *Miniredis) cmdSet(c *server.Peer, cmd string, args []string) {
116121
return
117122
}
118123
}
124+
if keepttl {
125+
if val, ok := db.ttl[key]; ok {
126+
ttl = val
127+
}
128+
}
119129

120130
db.del(key, true) // be sure to remove existing values of other type keys.
121131
// a vanilla SET clears the expire

cmd_string_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ func TestSet(t *testing.T) {
156156
)
157157
}
158158

159+
// KEEPTTL argument
160+
{
161+
s.Set("foo", "bar")
162+
s.SetTTL("foo", time.Second*1337)
163+
mustOK(t, c,
164+
"SET", "foo", "baz", "KEEPTTL",
165+
)
166+
s.CheckGet(t, "foo", "baz")
167+
equals(t, time.Second*1337, s.TTL("foo"))
168+
}
169+
159170
// Invalid argument
160171
mustDo(t, c,
161172
"SET", "one", "two", "FOO",

integration/string_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestString(t *testing.T) {
1616
c.Do("SET", "foo", "bar", "EX", "100")
1717
c.Do("SET", "foo", "bar", "EX", "noint")
1818
c.Do("SET", "utf8", "❆❅❄☃")
19+
c.Do("SET", "foo", "baz", "KEEPTTL")
1920

2021
// Failure cases
2122
c.Do("SET")

0 commit comments

Comments
 (0)