Merge pull request #409 from bpiddubnyi/bugfix/setxx-zero-ex

Fix "invalid expire time in set" for SetXX with expiration = 0
This commit is contained in:
Vladimir Mihailenco 2016-10-24 12:52:32 +03:00 committed by GitHub
commit 80cf5d1652
2 changed files with 24 additions and 3 deletions

View File

@ -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

View File

@ -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))