diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go index f7c18a250..66fcef4e0 100644 --- a/core/stores/redis/redis.go +++ b/core/stores/redis/redis.go @@ -609,6 +609,28 @@ func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (int, e return int(v), nil } +// GetDel is the implementation of redis getdel command. +// Available since: redis version 6.2.0 +func (s *Redis) GetDel(key string) (string, error) { + return s.GetDelCtx(context.Background(), key) +} + +// GetDelCtx is the implementation of redis getdel command. +// Available since: redis version 6.2.0 +func (s *Redis) GetDelCtx(ctx context.Context, key string) (string, error) { + conn, err := getRedis(s) + if err != nil { + return "", err + } + + val, err := conn.GetDel(ctx, key).Result() + if errors.Is(err, red.Nil) { + return "", nil + } + + return val, err +} + // GetSet is the implementation of redis getset command. func (s *Redis) GetSet(key, value string) (string, error) { return s.GetSetCtx(context.Background(), key, value) diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index 062e344cc..388b105a0 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -1071,6 +1071,27 @@ func TestRedis_Set(t *testing.T) { }) } +func TestRedis_GetDel(t *testing.T) { + t.Run("get_del", func(t *testing.T) { + runOnRedis(t, func(client *Redis) { + val, err := newRedis(client.Addr).GetDel("hello") + assert.Equal(t, "", val) + assert.Nil(t, err) + err = client.Set("hello", "world") + assert.Nil(t, err) + val, err = client.Get("hello") + assert.Nil(t, err) + assert.Equal(t, "world", val) + val, err = client.GetDel("hello") + assert.Nil(t, err) + assert.Equal(t, "world", val) + val, err = client.Get("hello") + assert.Nil(t, err) + assert.Equal(t, "", val) + }) + }) +} + func TestRedis_GetSet(t *testing.T) { t.Run("set_get", func(t *testing.T) { runOnRedis(t, func(client *Redis) {