From dba444a3822e16fa14069afb7d8c4457786b01be Mon Sep 17 00:00:00 2001 From: Meng Ye <4025839+jk2K@users.noreply.github.com> Date: Wed, 19 Mar 2025 23:40:14 +0800 Subject: [PATCH] feat: support redis getdel command (#4709) --- core/stores/redis/redis.go | 22 ++++++++++++++++++++++ core/stores/redis/redis_test.go | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) 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) {