diff --git a/commands.go b/commands.go index 5ac7de7..4511284 100644 --- a/commands.go +++ b/commands.go @@ -788,10 +788,14 @@ func (c *cmdable) SetNX(key string, value interface{}, expiration time.Duration) // Zero expiration means the key has no expiration time. func (c *cmdable) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd { var cmd *BoolCmd - if usePrecise(expiration) { - cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx") + if expiration == 0 { + cmd = NewBoolCmd("set", key, value, "xx") } else { - cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx") + if usePrecise(expiration) { + cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx") + } else { + cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx") + } } c.process(cmd) return cmd diff --git a/commands_test.go b/commands_test.go index 2db90e2..7da5f2a 100644 --- a/commands_test.go +++ b/commands_test.go @@ -996,6 +996,23 @@ var _ = Describe("Commands", func() { }) It("should SetXX", func() { + isSet, err := client.SetXX("key", "hello2", 0).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(isSet).To(Equal(false)) + + err = client.Set("key", "hello", 0).Err() + Expect(err).NotTo(HaveOccurred()) + + isSet, err = client.SetXX("key", "hello2", 0).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(isSet).To(Equal(true)) + + val, err := client.Get("key").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(val).To(Equal("hello2")) + }) + + It("should SetXX with expiration", func() { isSet, err := client.SetXX("key", "hello2", time.Second).Result() Expect(err).NotTo(HaveOccurred()) Expect(isSet).To(Equal(false))