feat:add redis mset func (#3820)

master^2
Qiu shao 9 months ago committed by GitHub
parent 4e3f1776dc
commit 9763c8b143
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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)

@ -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) {

Loading…
Cancel
Save