chore: refactor code (#1699)

This commit is contained in:
Kevin Wan
2022-03-23 18:24:44 +08:00
committed by GitHub
parent bbac994c8a
commit ec271db7a0
5 changed files with 33 additions and 32 deletions

View File

@@ -615,31 +615,6 @@ func (s *Redis) GetCtx(ctx context.Context, key string) (val string, err error)
return
}
// GetSet is the implementation of redis getset command.
func (s *Redis) GetSet(key, value string) (string, error) {
return s.GetSetCtx(context.Background(), key, value)
}
// GetSetCtx is the implementation of redis getset command.
func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}
if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil {
return nil
} else if err != nil {
return err
} else {
return nil
}
}, acceptable)
return
}
// GetBit is the implementation of redis getbit command.
func (s *Redis) GetBit(key string, offset int64) (int, error) {
return s.GetBitCtx(context.Background(), key, offset)
@@ -665,6 +640,29 @@ func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (val in
return
}
// GetSet is the implementation of redis getset command.
func (s *Redis) GetSet(key, value string) (string, error) {
return s.GetSetCtx(context.Background(), key, value)
}
// GetSetCtx is the implementation of redis getset command.
func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}
if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil {
return nil
}
return err
}, acceptable)
return
}
// Hdel is the implementation of redis hdel command.
func (s *Redis) Hdel(key string, fields ...string) (bool, error) {
return s.HdelCtx(context.Background(), key, fields...)

View File

@@ -703,9 +703,9 @@ func TestRedis_Set(t *testing.T) {
func TestRedis_GetSet(t *testing.T) {
runOnRedis(t, func(client *Redis) {
val, err := New(client.Addr, badType()).GetSet("hello", "world")
_, err := New(client.Addr, badType()).GetSet("hello", "world")
assert.NotNil(t, err)
val, err = client.GetSet("hello", "world")
val, err := client.GetSet("hello", "world")
assert.Nil(t, err)
assert.Equal(t, "", val)
val, err = client.Get("hello")