Repository: zeromicro/go-zero
Pull request #3820 (2023-12-28 06:24:22)

feat:add redis mset func
The current go-zero Redis library lacks an implementation for the Redis MSET method, and it would be beneficial to add this. Implementing the Redis MSET method will facilitate batch insertion into Redis in the future, reducing network IO.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3820?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
Attention: `3 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`7ca13bc`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/7ca13bc25e5aeab186216cb10b463968c1aff34e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.67% compared to head [(`03e51cf`)](https://app.codecov.io/gh/zeromicro/go-zero/pull/3820?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.67%.

<details><summary>Additional details and impacted files</summary>



| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3820?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) | Coverage Δ | |
|---|---|---|
| [core/stores/redis/redis.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3820?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro#diff-Y29yZS9zdG9yZXMvcmVkaXMvcmVkaXMuZ28=) | `98.13% <78.57%> (-0.17%)` | :arrow_down: |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/3820/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)

</details>

Patch:
diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go
index 8cc3f4e7..bbad891c 100644
--- a/core/stores/redis/redis.go
+++ b/core/stores/redis/redis.go
@@ -1339,6 +1339,30 @@ func (s *Redis) MgetCtx(ctx context.Context, keys ...string) (val []string, err
 	return
 }
 
+// Mset is the implementation of redis mset command.
+func (s *Redis) Mset(fieldsAndValues ...any) (string, error) {
+	return s.MsetCtx(context.Background(), fieldsAndValues...)
+}
+
+// MsetCtx is the implementation of redis mset command.
+func (s *Redis) MsetCtx(ctx context.Context, fieldsAndValues ...any) (val string, err error) {
+	err = s.brk.DoWithAcceptable(func() error {
+		conn, err := getRedis(s)
+		if err != nil {
+			return err
+		}
+
+		val, err = conn.MSet(ctx, fieldsAndValues...).Result()
+		if err != nil {
+			return err
+		}
+
+		return nil
+	}, acceptable)
+
+	return
+}
+
 // Persist is the implementation of redis persist command.
 func (s *Redis) Persist(key string) (bool, error) {
 	return s.PersistCtx(context.Background(), key)


Test patch:
diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go
index a9dd5d66..29cc5417 100644
--- a/core/stores/redis/redis_test.go
+++ b/core/stores/redis/redis_test.go
@@ -660,6 +660,33 @@ func TestRedis_List(t *testing.T) {
 	})
 }
 
+func TestRedis_Mset(t *testing.T) {
+	t.Run("mset", func(t *testing.T) {
+		runOnRedis(t, func(client *Redis) {
+			// Attempt to Mget with a bad client type, expecting an error.
+			_, err := New(client.Addr, badType()).Mset("key1", "value1")
+			assert.NotNil(t, err)
+
+			// Set multiple key-value pairs using Mset and expect no error.
+			_, err = client.Mset("key1", "value1", "key2", "value2")
+			assert.Nil(t, err)
+
+			// Retrieve the values for the keys set above using Mget and expect no error.
+			vals, err := client.Mget("key1", "key2")
+			assert.Nil(t, err)
+			assert.EqualValues(t, []string{"value1", "value2"}, vals)
+		})
+	})
+
+	// Test case for Mset operation with an incorrect number of arguments, expecting an error.
+	t.Run("mset error", func(t *testing.T) {
+		runOnRedisWithError(t, func(client *Redis) {
+			_, err := client.Mset("key1", "value1", "key2")
+			assert.Error(t, err)
+		})
+	})
+}
+
 func TestRedis_Mget(t *testing.T) {
 	t.Run("mget", func(t *testing.T) {
 		runOnRedis(t, func(client *Redis) {