1
0
mirror of https://github.com/redis/go-redis.git synced 2025-11-10 00:00:57 +03:00

feat(cmd): Add support for MSetEX command (#3580)

Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
This commit is contained in:
ofekshenawa
2025-11-05 13:35:43 +02:00
committed by GitHub
parent e2f6700d79
commit 284d93a4b3
6 changed files with 204 additions and 4 deletions

View File

@@ -1935,6 +1935,137 @@ var _ = Describe("Commands", func() {
Expect(mSetNX.Val()).To(Equal(true))
})
It("should MSetEX", func() {
SkipBeforeRedisVersion(8.3, "MSetEX is available since redis 8.4")
args := redis.MSetEXArgs{
Expiration: &redis.ExpirationOption{
Mode: redis.EX,
Value: 1,
},
}
mSetEX := client.MSetEX(ctx, args, "key1", "hello1", "key2", "hello2")
Expect(mSetEX.Err()).NotTo(HaveOccurred())
Expect(mSetEX.Val()).To(Equal(int64(1)))
// Verify keys were set
val1 := client.Get(ctx, "key1")
Expect(val1.Err()).NotTo(HaveOccurred())
Expect(val1.Val()).To(Equal("hello1"))
val2 := client.Get(ctx, "key2")
Expect(val2.Err()).NotTo(HaveOccurred())
Expect(val2.Val()).To(Equal("hello2"))
// Verify TTL was set
ttl1 := client.TTL(ctx, "key1")
Expect(ttl1.Err()).NotTo(HaveOccurred())
Expect(ttl1.Val()).To(BeNumerically(">", 0))
Expect(ttl1.Val()).To(BeNumerically("<=", 1*time.Second))
ttl2 := client.TTL(ctx, "key2")
Expect(ttl2.Err()).NotTo(HaveOccurred())
Expect(ttl2.Val()).To(BeNumerically(">", 0))
Expect(ttl2.Val()).To(BeNumerically("<=", 1*time.Second))
})
It("should MSetEX with NX mode", func() {
SkipBeforeRedisVersion(8.3, "MSetEX is available since redis 8.4")
client.Set(ctx, "key1", "existing", 0)
// Try to set with NX mode - should fail because key1 exists
args := redis.MSetEXArgs{
Condition: redis.NX,
Expiration: &redis.ExpirationOption{
Mode: redis.EX,
Value: 1,
},
}
mSetEX := client.MSetEX(ctx, args, "key1", "new1", "key2", "new2")
Expect(mSetEX.Err()).NotTo(HaveOccurred())
Expect(mSetEX.Val()).To(Equal(int64(0)))
val1 := client.Get(ctx, "key1")
Expect(val1.Err()).NotTo(HaveOccurred())
Expect(val1.Val()).To(Equal("existing"))
val2 := client.Get(ctx, "key2")
Expect(val2.Err()).To(Equal(redis.Nil))
client.Del(ctx, "key1")
// Now try with NX mode when keys don't exist - should succeed
mSetEX = client.MSetEX(ctx, args, "key1", "new1", "key2", "new2")
Expect(mSetEX.Err()).NotTo(HaveOccurred())
Expect(mSetEX.Val()).To(Equal(int64(1)))
val1 = client.Get(ctx, "key1")
Expect(val1.Err()).NotTo(HaveOccurred())
Expect(val1.Val()).To(Equal("new1"))
val2 = client.Get(ctx, "key2")
Expect(val2.Err()).NotTo(HaveOccurred())
Expect(val2.Val()).To(Equal("new2"))
})
It("should MSetEX with XX mode", func() {
SkipBeforeRedisVersion(8.3, "MSetEX is available since redis 8.4")
args := redis.MSetEXArgs{
Condition: redis.XX,
Expiration: &redis.ExpirationOption{
Mode: redis.EX,
Value: 1,
},
}
mSetEX := client.MSetEX(ctx, args, "key1", "new1", "key2", "new2")
Expect(mSetEX.Err()).NotTo(HaveOccurred())
Expect(mSetEX.Val()).To(Equal(int64(0)))
client.Set(ctx, "key1", "existing1", 0)
client.Set(ctx, "key2", "existing2", 0)
mSetEX = client.MSetEX(ctx, args, "key1", "new1", "key2", "new2")
Expect(mSetEX.Err()).NotTo(HaveOccurred())
Expect(mSetEX.Val()).To(Equal(int64(1)))
val1 := client.Get(ctx, "key1")
Expect(val1.Err()).NotTo(HaveOccurred())
Expect(val1.Val()).To(Equal("new1"))
val2 := client.Get(ctx, "key2")
Expect(val2.Err()).NotTo(HaveOccurred())
Expect(val2.Val()).To(Equal("new2"))
ttl1 := client.TTL(ctx, "key1")
Expect(ttl1.Err()).NotTo(HaveOccurred())
Expect(ttl1.Val()).To(BeNumerically(">", 0))
})
It("should MSetEX with map", func() {
SkipBeforeRedisVersion(8.3, "MSetEX is available since redis 8.4")
args := redis.MSetEXArgs{
Expiration: &redis.ExpirationOption{
Mode: redis.EX,
Value: 1,
},
}
mSetEX := client.MSetEX(ctx, args, map[string]interface{}{
"key1": "value1",
"key2": "value2",
})
Expect(mSetEX.Err()).NotTo(HaveOccurred())
Expect(mSetEX.Val()).To(Equal(int64(1)))
val1 := client.Get(ctx, "key1")
Expect(val1.Err()).NotTo(HaveOccurred())
Expect(val1.Val()).To(Equal("value1"))
val2 := client.Get(ctx, "key2")
Expect(val2.Err()).NotTo(HaveOccurred())
Expect(val2.Val()).To(Equal("value2"))
})
It("should SetWithArgs with TTL", func() {
args := redis.SetArgs{
TTL: 500 * time.Millisecond,