Skip to content

Commit dba444a

Browse files
authored
feat: support redis getdel command (#4709)
1 parent b24fb3e commit dba444a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

core/stores/redis/redis.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,28 @@ func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (int, e
609609
return int(v), nil
610610
}
611611

612+
// GetDel is the implementation of redis getdel command.
613+
// Available since: redis version 6.2.0
614+
func (s *Redis) GetDel(key string) (string, error) {
615+
return s.GetDelCtx(context.Background(), key)
616+
}
617+
618+
// GetDelCtx is the implementation of redis getdel command.
619+
// Available since: redis version 6.2.0
620+
func (s *Redis) GetDelCtx(ctx context.Context, key string) (string, error) {
621+
conn, err := getRedis(s)
622+
if err != nil {
623+
return "", err
624+
}
625+
626+
val, err := conn.GetDel(ctx, key).Result()
627+
if errors.Is(err, red.Nil) {
628+
return "", nil
629+
}
630+
631+
return val, err
632+
}
633+
612634
// GetSet is the implementation of redis getset command.
613635
func (s *Redis) GetSet(key, value string) (string, error) {
614636
return s.GetSetCtx(context.Background(), key, value)

core/stores/redis/redis_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,27 @@ func TestRedis_Set(t *testing.T) {
10711071
})
10721072
}
10731073

1074+
func TestRedis_GetDel(t *testing.T) {
1075+
t.Run("get_del", func(t *testing.T) {
1076+
runOnRedis(t, func(client *Redis) {
1077+
val, err := newRedis(client.Addr).GetDel("hello")
1078+
assert.Equal(t, "", val)
1079+
assert.Nil(t, err)
1080+
err = client.Set("hello", "world")
1081+
assert.Nil(t, err)
1082+
val, err = client.Get("hello")
1083+
assert.Nil(t, err)
1084+
assert.Equal(t, "world", val)
1085+
val, err = client.GetDel("hello")
1086+
assert.Nil(t, err)
1087+
assert.Equal(t, "world", val)
1088+
val, err = client.Get("hello")
1089+
assert.Nil(t, err)
1090+
assert.Equal(t, "", val)
1091+
})
1092+
})
1093+
}
1094+
10741095
func TestRedis_GetSet(t *testing.T) {
10751096
t.Run("set_get", func(t *testing.T) {
10761097
runOnRedis(t, func(client *Redis) {

0 commit comments

Comments
 (0)