forked from mirror/redis
Merge pull request #69 from go-redis/feature/refactor-set-setnx-setxx
Refactor Set, SetNX and SetXX with expiration.
This commit is contained in:
commit
c6692db208
|
@ -167,7 +167,7 @@ var _ = Describe("Cluster", func() {
|
|||
Expect(err).To(Equal(redis.Nil))
|
||||
Expect(val).To(Equal(""))
|
||||
|
||||
val, err = client.Set("A", "VALUE").Result()
|
||||
val, err = client.Set("A", "VALUE", 0).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("OK"))
|
||||
|
||||
|
@ -181,7 +181,7 @@ var _ = Describe("Cluster", func() {
|
|||
})
|
||||
|
||||
It("should follow redirects", func() {
|
||||
Expect(client.Set("A", "VALUE").Err()).NotTo(HaveOccurred())
|
||||
Expect(client.Set("A", "VALUE", 0).Err()).NotTo(HaveOccurred())
|
||||
Expect(redis.HashSlot("A")).To(Equal(6373))
|
||||
|
||||
// Slot 6373 is stored on the second node
|
||||
|
|
15
command.go
15
command.go
|
@ -338,12 +338,25 @@ func (cmd *BoolCmd) String() string {
|
|||
|
||||
func (cmd *BoolCmd) parseReply(rd *bufio.Reader) error {
|
||||
v, err := parseReply(rd, nil)
|
||||
// `SET key value NX` returns nil when key already exists.
|
||||
if err == Nil {
|
||||
cmd.val = false
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
cmd.err = err
|
||||
return err
|
||||
}
|
||||
cmd.val = v.(int64) == 1
|
||||
switch vv := v.(type) {
|
||||
case int64:
|
||||
cmd.val = vv == 1
|
||||
return nil
|
||||
case string:
|
||||
cmd.val = vv == "OK"
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("got %T, wanted int64 or string")
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
|
|
@ -26,7 +26,7 @@ var _ = Describe("Command", func() {
|
|||
})
|
||||
|
||||
It("should have a plain string result", func() {
|
||||
set := client.Set("foo", "bar")
|
||||
set := client.Set("foo", "bar", 0)
|
||||
Expect(set.String()).To(Equal("SET foo bar: OK"))
|
||||
|
||||
get := client.Get("foo")
|
||||
|
@ -34,7 +34,7 @@ var _ = Describe("Command", func() {
|
|||
})
|
||||
|
||||
It("should have correct val/err states", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -47,7 +47,7 @@ var _ = Describe("Command", func() {
|
|||
})
|
||||
|
||||
It("should escape special chars", func() {
|
||||
set := client.Set("key", "hello1\r\nhello2\r\n")
|
||||
set := client.Set("key", "hello1\r\nhello2\r\n", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -58,7 +58,7 @@ var _ = Describe("Command", func() {
|
|||
|
||||
It("should handle big vals", func() {
|
||||
val := string(bytes.Repeat([]byte{'*'}, 1<<16))
|
||||
set := client.Set("key", val)
|
||||
set := client.Set("key", val, 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -70,7 +70,7 @@ var _ = Describe("Command", func() {
|
|||
It("should handle many keys #1", func() {
|
||||
const n = 100000
|
||||
for i := 0; i < n; i++ {
|
||||
client.Set("keys.key"+strconv.Itoa(i), "hello"+strconv.Itoa(i))
|
||||
client.Set("keys.key"+strconv.Itoa(i), "hello"+strconv.Itoa(i), 0)
|
||||
}
|
||||
keys := client.Keys("keys.*")
|
||||
Expect(keys.Err()).NotTo(HaveOccurred())
|
||||
|
@ -83,7 +83,7 @@ var _ = Describe("Command", func() {
|
|||
keys := []string{"non-existent-key"}
|
||||
for i := 0; i < n; i++ {
|
||||
key := "keys.key" + strconv.Itoa(i)
|
||||
client.Set(key, "hello"+strconv.Itoa(i))
|
||||
client.Set(key, "hello"+strconv.Itoa(i), 0)
|
||||
keys = append(keys, key)
|
||||
}
|
||||
keys = append(keys, "non-existent-key")
|
||||
|
@ -100,7 +100,7 @@ var _ = Describe("Command", func() {
|
|||
})
|
||||
|
||||
It("should convert strings via helpers", func() {
|
||||
set := client.Set("key", "10")
|
||||
set := client.Set("key", "10", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
|
||||
n, err := client.Get("key").Int64()
|
||||
|
|
70
commands.go
70
commands.go
|
@ -25,6 +25,18 @@ func (c *commandable) Process(cmd Cmder) {
|
|||
c.process(cmd)
|
||||
}
|
||||
|
||||
func usePrecise(dur time.Duration) bool {
|
||||
return dur < time.Second || dur%time.Second != 0
|
||||
}
|
||||
|
||||
func formatMs(dur time.Duration) string {
|
||||
return strconv.FormatInt(int64(dur/time.Millisecond), 10)
|
||||
}
|
||||
|
||||
func formatSec(dur time.Duration) string {
|
||||
return strconv.FormatInt(int64(dur/time.Second), 10)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func (c *commandable) Auth(password string) *StatusCmd {
|
||||
|
@ -77,8 +89,8 @@ func (c *commandable) Exists(key string) *BoolCmd {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) Expire(key string, dur time.Duration) *BoolCmd {
|
||||
cmd := NewBoolCmd("EXPIRE", key, strconv.FormatInt(int64(dur/time.Second), 10))
|
||||
func (c *commandable) Expire(key string, expiration time.Duration) *BoolCmd {
|
||||
cmd := NewBoolCmd("EXPIRE", key, formatSec(expiration))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
@ -146,8 +158,8 @@ func (c *commandable) Persist(key string) *BoolCmd {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) PExpire(key string, dur time.Duration) *BoolCmd {
|
||||
cmd := NewBoolCmd("PEXPIRE", key, strconv.FormatInt(int64(dur/time.Millisecond), 10))
|
||||
func (c *commandable) PExpire(key string, expiration time.Duration) *BoolCmd {
|
||||
cmd := NewBoolCmd("PEXPIRE", key, formatMs(expiration))
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
@ -425,19 +437,22 @@ func (c *commandable) MSetNX(pairs ...string) *BoolCmd {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) PSetEx(key string, dur time.Duration, value string) *StatusCmd {
|
||||
cmd := NewStatusCmd(
|
||||
"PSETEX",
|
||||
key,
|
||||
strconv.FormatInt(int64(dur/time.Millisecond), 10),
|
||||
value,
|
||||
)
|
||||
func (c *commandable) PSetEx(key string, expiration time.Duration, value string) *StatusCmd {
|
||||
cmd := NewStatusCmd("PSETEX", key, formatMs(expiration), value)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) Set(key, value string) *StatusCmd {
|
||||
cmd := NewStatusCmd("SET", key, value)
|
||||
func (c *commandable) Set(key, value string, expiration time.Duration) *StatusCmd {
|
||||
args := []string{"SET", key, value}
|
||||
if expiration > 0 {
|
||||
if usePrecise(expiration) {
|
||||
args = append(args, "PX", formatMs(expiration))
|
||||
} else {
|
||||
args = append(args, "EX", formatSec(expiration))
|
||||
}
|
||||
}
|
||||
cmd := NewStatusCmd(args...)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
@ -453,14 +468,35 @@ func (c *commandable) SetBit(key string, offset int64, value int) *IntCmd {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SetEx(key string, dur time.Duration, value string) *StatusCmd {
|
||||
cmd := NewStatusCmd("SETEX", key, strconv.FormatInt(int64(dur/time.Second), 10), value)
|
||||
func (c *commandable) SetEx(key string, expiration time.Duration, value string) *StatusCmd {
|
||||
cmd := NewStatusCmd("SETEX", key, formatSec(expiration), value)
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *commandable) SetNX(key, value string) *BoolCmd {
|
||||
cmd := NewBoolCmd("SETNX", key, value)
|
||||
func (c *commandable) SetNX(key, value string, expiration time.Duration) *BoolCmd {
|
||||
var cmd *BoolCmd
|
||||
if expiration == 0 {
|
||||
// Use old `SETNX` to support old Redis versions.
|
||||
cmd = NewBoolCmd("SETNX", key, value)
|
||||
} else {
|
||||
if usePrecise(expiration) {
|
||||
cmd = NewBoolCmd("SET", key, value, "PX", formatMs(expiration), "NX")
|
||||
} else {
|
||||
cmd = NewBoolCmd("SET", key, value, "EX", formatSec(expiration), "NX")
|
||||
}
|
||||
}
|
||||
c.Process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *Client) SetXX(key, value string, expiration time.Duration) *BoolCmd {
|
||||
var cmd *BoolCmd
|
||||
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
|
||||
}
|
||||
|
|
136
commands_test.go
136
commands_test.go
|
@ -149,7 +149,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(debug.Err()).To(HaveOccurred())
|
||||
Expect(debug.Err().Error()).To(Equal("ERR no such key"))
|
||||
|
||||
client.Set("foo", "bar")
|
||||
client.Set("foo", "bar", 0)
|
||||
debug = client.DebugObject("foo")
|
||||
Expect(debug.Err()).NotTo(HaveOccurred())
|
||||
Expect(debug.Val()).To(ContainSubstring(`serializedlength:4`))
|
||||
|
@ -162,10 +162,10 @@ var _ = Describe("Commands", func() {
|
|||
Describe("keys", func() {
|
||||
|
||||
It("should Del", func() {
|
||||
set := client.Set("key1", "Hello")
|
||||
set := client.Set("key1", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
set = client.Set("key2", "World")
|
||||
set = client.Set("key2", "World", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -175,7 +175,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Dump", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -185,7 +185,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Exists", func() {
|
||||
set := client.Set("key1", "Hello")
|
||||
set := client.Set("key1", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -199,7 +199,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Expire", func() {
|
||||
set := client.Set("key", "Hello")
|
||||
set := client.Set("key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -211,7 +211,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(ttl.Err()).NotTo(HaveOccurred())
|
||||
Expect(ttl.Val()).To(Equal(10 * time.Second))
|
||||
|
||||
set = client.Set("key", "Hello World")
|
||||
set = client.Set("key", "Hello World", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -221,7 +221,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should ExpireAt", func() {
|
||||
set := client.Set("key", "Hello")
|
||||
set := client.Set("key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -261,7 +261,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(migrate.Err()).NotTo(HaveOccurred())
|
||||
Expect(migrate.Val()).To(Equal("NOKEY"))
|
||||
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -275,7 +275,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(move.Err()).NotTo(HaveOccurred())
|
||||
Expect(move.Val()).To(Equal(false))
|
||||
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -299,7 +299,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Object", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -317,7 +317,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Persist", func() {
|
||||
set := client.Set("key", "Hello")
|
||||
set := client.Set("key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -339,7 +339,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should PExpire", func() {
|
||||
set := client.Set("key", "Hello")
|
||||
set := client.Set("key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -359,7 +359,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should PExpireAt", func() {
|
||||
set := client.Set("key", "Hello")
|
||||
set := client.Set("key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -379,7 +379,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should PTTL", func() {
|
||||
set := client.Set("key", "Hello")
|
||||
set := client.Set("key", "Hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -399,7 +399,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(randomKey.Err()).To(Equal(redis.Nil))
|
||||
Expect(randomKey.Val()).To(Equal(""))
|
||||
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -409,7 +409,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Rename", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -423,7 +423,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should RenameNX", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -437,7 +437,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Restore", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -481,7 +481,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(ttl.Err()).NotTo(HaveOccurred())
|
||||
Expect(ttl.Val() < 0).To(Equal(true))
|
||||
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -495,7 +495,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Type", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -512,7 +512,7 @@ var _ = Describe("Commands", func() {
|
|||
|
||||
It("should Scan", func() {
|
||||
for i := 0; i < 1000; i++ {
|
||||
set := client.Set(fmt.Sprintf("key%d", i), "hello")
|
||||
set := client.Set(fmt.Sprintf("key%d", i), "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
}
|
||||
|
||||
|
@ -583,7 +583,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should BitCount", func() {
|
||||
set := client.Set("key", "foobar")
|
||||
set := client.Set("key", "foobar", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -601,11 +601,11 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should BitOpAnd", func() {
|
||||
set := client.Set("key1", "1")
|
||||
set := client.Set("key1", "1", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
set = client.Set("key2", "0")
|
||||
set = client.Set("key2", "0", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -619,11 +619,11 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should BitOpOr", func() {
|
||||
set := client.Set("key1", "1")
|
||||
set := client.Set("key1", "1", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
set = client.Set("key2", "0")
|
||||
set = client.Set("key2", "0", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -637,11 +637,11 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should BitOpXor", func() {
|
||||
set := client.Set("key1", "\xff")
|
||||
set := client.Set("key1", "\xff", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
set = client.Set("key2", "\x0f")
|
||||
set = client.Set("key2", "\x0f", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -655,7 +655,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should BitOpNot", func() {
|
||||
set := client.Set("key1", "\x00")
|
||||
set := client.Set("key1", "\x00", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -669,7 +669,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Decr", func() {
|
||||
set := client.Set("key", "10")
|
||||
set := client.Set("key", "10", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -677,7 +677,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(decr.Err()).NotTo(HaveOccurred())
|
||||
Expect(decr.Val()).To(Equal(int64(9)))
|
||||
|
||||
set = client.Set("key", "234293482390480948029348230948")
|
||||
set = client.Set("key", "234293482390480948029348230948", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -687,7 +687,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should DecrBy", func() {
|
||||
set := client.Set("key", "10")
|
||||
set := client.Set("key", "10", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -701,7 +701,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(get.Err()).To(Equal(redis.Nil))
|
||||
Expect(get.Val()).To(Equal(""))
|
||||
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -729,7 +729,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should GetRange", func() {
|
||||
set := client.Set("key", "This is a string")
|
||||
set := client.Set("key", "This is a string", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -765,7 +765,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Incr", func() {
|
||||
set := client.Set("key", "10")
|
||||
set := client.Set("key", "10", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -779,7 +779,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should IncrBy", func() {
|
||||
set := client.Set("key", "10")
|
||||
set := client.Set("key", "10", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -789,7 +789,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should IncrByFloat", func() {
|
||||
set := client.Set("key", "10.50")
|
||||
set := client.Set("key", "10.50", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -797,7 +797,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
|
||||
Expect(incrByFloat.Val()).To(Equal(10.6))
|
||||
|
||||
set = client.Set("key", "5.0e3")
|
||||
set = client.Set("key", "5.0e3", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -848,8 +848,21 @@ var _ = Describe("Commands", func() {
|
|||
Expect(get.Val()).To(Equal("hello"))
|
||||
})
|
||||
|
||||
It("should Set with expiration", func() {
|
||||
err := client.Set("key", "hello", 100*time.Millisecond).Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
val, err := client.Get("key").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("hello"))
|
||||
|
||||
Eventually(func() error {
|
||||
return client.Get("foo").Err()
|
||||
}, "1s", "100ms").Should(Equal(redis.Nil))
|
||||
})
|
||||
|
||||
It("should SetGet", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -869,11 +882,11 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should SetNX", func() {
|
||||
setNX := client.SetNX("key", "hello")
|
||||
setNX := client.SetNX("key", "hello", 0)
|
||||
Expect(setNX.Err()).NotTo(HaveOccurred())
|
||||
Expect(setNX.Val()).To(Equal(true))
|
||||
|
||||
setNX = client.SetNX("key", "hello2")
|
||||
setNX = client.SetNX("key", "hello2", 0)
|
||||
Expect(setNX.Err()).NotTo(HaveOccurred())
|
||||
Expect(setNX.Val()).To(Equal(false))
|
||||
|
||||
|
@ -882,8 +895,39 @@ var _ = Describe("Commands", func() {
|
|||
Expect(get.Val()).To(Equal("hello"))
|
||||
})
|
||||
|
||||
It("should SetNX with expiration", func() {
|
||||
isSet, err := client.SetNX("key", "hello", time.Second).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(isSet).To(Equal(true))
|
||||
|
||||
isSet, err = client.SetNX("key", "hello2", time.Second).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(isSet).To(Equal(false))
|
||||
|
||||
val, err := client.Get("key").Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(val).To(Equal("hello"))
|
||||
})
|
||||
|
||||
It("should SetXX", func() {
|
||||
isSet, err := client.SetXX("key", "hello2", time.Second).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(isSet).To(Equal(false))
|
||||
|
||||
err = client.Set("key", "hello", time.Second).Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
isSet, err = client.SetXX("key", "hello2", time.Second).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 SetRange", func() {
|
||||
set := client.Set("key", "Hello World")
|
||||
set := client.Set("key", "Hello World", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -897,7 +941,7 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should StrLen", func() {
|
||||
set := client.Set("key", "hello")
|
||||
set := client.Set("key", "hello", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
|
@ -2176,7 +2220,7 @@ var _ = Describe("Commands", func() {
|
|||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
return multi.Exec(func() error {
|
||||
multi.Set("key", strconv.FormatInt(v+1, 10))
|
||||
multi.Set("key", strconv.FormatInt(v+1, 10), 0)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -2187,7 +2231,7 @@ var _ = Describe("Commands", func() {
|
|||
n = 1000
|
||||
}
|
||||
|
||||
err := client.Set("key", "0").Err()
|
||||
err := client.Set("key", "0", 0).Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
|
|
@ -36,7 +36,7 @@ func ExampleNewFailoverClient() {
|
|||
}
|
||||
|
||||
func ExampleClient() {
|
||||
if err := client.Set("foo", "bar").Err(); err != nil {
|
||||
if err := client.Set("foo", "bar", 0).Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ func ExampleClient_Incr() {
|
|||
|
||||
func ExampleClient_Pipelined() {
|
||||
cmds, err := client.Pipelined(func(c *redis.Pipeline) error {
|
||||
c.Set("key1", "hello1")
|
||||
c.Set("key1", "hello1", 0)
|
||||
c.Get("key1")
|
||||
return nil
|
||||
})
|
||||
|
@ -73,7 +73,7 @@ func ExampleClient_Pipelined() {
|
|||
|
||||
func ExamplePipeline() {
|
||||
pipeline := client.Pipeline()
|
||||
set := pipeline.Set("key1", "hello1")
|
||||
set := pipeline.Set("key1", "hello1", 0)
|
||||
get := pipeline.Get("key1")
|
||||
cmds, err := pipeline.Exec()
|
||||
fmt.Println(cmds, err)
|
||||
|
@ -93,7 +93,7 @@ func ExampleMulti() {
|
|||
n, _ := strconv.ParseInt(s, 10, 64)
|
||||
|
||||
return tx.Exec(func() error {
|
||||
tx.Set("key", strconv.FormatInt(n+1, 10))
|
||||
tx.Set("key", strconv.FormatInt(n+1, 10), 0)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ var _ = Describe("Multi", func() {
|
|||
get *redis.StringCmd
|
||||
)
|
||||
cmds, err := multi.Exec(func() error {
|
||||
set = multi.Set("key", "hello")
|
||||
set = multi.Set("key", "hello", 0)
|
||||
get = multi.Get("key")
|
||||
return nil
|
||||
})
|
||||
|
@ -53,9 +53,9 @@ var _ = Describe("Multi", func() {
|
|||
}()
|
||||
|
||||
cmds, err := multi.Exec(func() error {
|
||||
multi.Set("key1", "hello1")
|
||||
multi.Set("key1", "hello1", 0)
|
||||
multi.Discard()
|
||||
multi.Set("key2", "hello2")
|
||||
multi.Set("key2", "hello2", 0)
|
||||
return nil
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
|
|
@ -25,12 +25,12 @@ var _ = Describe("Pipelining", func() {
|
|||
})
|
||||
|
||||
It("should pipeline", func() {
|
||||
set := client.Set("key2", "hello2")
|
||||
set := client.Set("key2", "hello2", 0)
|
||||
Expect(set.Err()).NotTo(HaveOccurred())
|
||||
Expect(set.Val()).To(Equal("OK"))
|
||||
|
||||
pipeline := client.Pipeline()
|
||||
set = pipeline.Set("key1", "hello1")
|
||||
set = pipeline.Set("key1", "hello1", 0)
|
||||
get := pipeline.Get("key2")
|
||||
incr := pipeline.Incr("key3")
|
||||
getNil := pipeline.Get("key4")
|
||||
|
|
|
@ -109,7 +109,7 @@ var _ = Describe("Client", func() {
|
|||
defer db1.Close()
|
||||
|
||||
Expect(db1.Get("key").Err()).To(Equal(redis.Nil))
|
||||
Expect(db1.Set("key", "value").Err()).NotTo(HaveOccurred())
|
||||
Expect(db1.Set("key", "value", 0).Err()).NotTo(HaveOccurred())
|
||||
|
||||
Expect(client.Get("key").Err()).To(Equal(redis.Nil))
|
||||
Expect(db1.Get("key").Val()).To(Equal("value"))
|
||||
|
@ -253,7 +253,7 @@ func BenchmarkRedisSet(b *testing.B) {
|
|||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := client.Set("key", "hello").Err(); err != nil {
|
||||
if err := client.Set("key", "hello", 0).Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -281,7 +281,7 @@ func BenchmarkRedisGet(b *testing.B) {
|
|||
client := redis.NewTCPClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
})
|
||||
if err := client.Set("key", "hello").Err(); err != nil {
|
||||
if err := client.Set("key", "hello", 0).Err(); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.StartTimer()
|
||||
|
@ -318,7 +318,7 @@ func BenchmarkSetExpire(b *testing.B) {
|
|||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := client.Set("key", "hello").Err(); err != nil {
|
||||
if err := client.Set("key", "hello", 0).Err(); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
if err := client.Expire("key", time.Second).Err(); err != nil {
|
||||
|
@ -336,7 +336,7 @@ func BenchmarkPipeline(b *testing.B) {
|
|||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := client.Pipelined(func(pipe *redis.Pipeline) error {
|
||||
pipe.Set("key", "hello")
|
||||
pipe.Set("key", "hello", 0)
|
||||
pipe.Expire("key", time.Second)
|
||||
return nil
|
||||
})
|
||||
|
|
|
@ -36,7 +36,7 @@ var _ = Describe("Sentinel", func() {
|
|||
defer client.Close()
|
||||
|
||||
// Set value on master, verify
|
||||
err = client.Set("foo", "master").Err()
|
||||
err = client.Set("foo", "master", 0).Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
val, err := master.Get("foo").Result()
|
||||
|
|
Loading…
Reference in New Issue