1
0
mirror of https://github.com/redis/go-redis.git synced 2025-06-15 12:41:41 +03:00

feature: add support for set keepttl (#1499)

* feature: add support for set keepttl
This commit is contained in:
x1nchen
2020-09-18 19:49:12 +08:00
committed by GitHub
parent bae2f9cc64
commit 10e779f856
3 changed files with 78 additions and 4 deletions

View File

@ -1145,6 +1145,23 @@ var _ = Describe("Commands", func() {
}, "1s", "100ms").Should(Equal(redis.Nil))
})
It("should Set with keepttl", func() {
// set with ttl
set := client.Set(ctx, "key", "hello", 5 * time.Second)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
// set with keepttl
set = client.Set(ctx, "key", "hello1", redis.KeepTTL)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
// set keepttl will Retain the ttl associated with the key
Expect(ttl.Val().Nanoseconds()).NotTo(Equal(-1))
})
It("should SetGet", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
@ -1183,6 +1200,16 @@ var _ = Describe("Commands", func() {
Expect(val).To(Equal("hello"))
})
It("should SetNX with keepttl", func() {
isSet, err := client.SetNX(ctx, "key", "hello1", redis.KeepTTL).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val().Nanoseconds()).To(Equal(int64(-1)))
})
It("should SetXX", func() {
isSet, err := client.SetXX(ctx, "key", "hello2", 0).Result()
Expect(err).NotTo(HaveOccurred())
@ -1217,6 +1244,32 @@ var _ = Describe("Commands", func() {
Expect(val).To(Equal("hello2"))
})
It("should SetXX with keepttl", func() {
isSet, err := client.SetXX(ctx, "key", "hello2", time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(false))
err = client.Set(ctx, "key", "hello", time.Second).Err()
Expect(err).NotTo(HaveOccurred())
isSet, err = client.SetXX(ctx, "key", "hello2", 5*time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
isSet, err = client.SetXX(ctx, "key", "hello3", redis.KeepTTL).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello3"))
// set keepttl will Retain the ttl associated with the key
ttl, err := client.TTL(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(ttl).NotTo(Equal(-1))
})
It("should SetRange", func() {
set := client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())