mirror of
https://github.com/redis/go-redis.git
synced 2025-06-12 14:21:52 +03:00
Refactor Set, SetNX and SetXX with expiration.
This commit is contained in:
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{}
|
||||
|
Reference in New Issue
Block a user