From 13db7a193159ac3b05626b7e109c47da90f1b795 Mon Sep 17 00:00:00 2001 From: shenbaise9527 Date: Sun, 16 Jan 2022 10:27:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81redis=E7=9A=84LTrim?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20(#1443)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/stores/redis/redis.go | 12 ++++++++++++ core/stores/redis/redis_test.go | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go index d6b197fe..2479fc49 100644 --- a/core/stores/redis/redis.go +++ b/core/stores/redis/redis.go @@ -880,6 +880,18 @@ func (s *Redis) Lrem(key string, count int, value string) (val int, err error) { return } +// Ltrim is the implementation of redis ltrim command. +func (s *Redis) Ltrim(key string, start, stop int64) error { + return s.brk.DoWithAcceptable(func() error { + conn, err := getRedis(s) + if err != nil { + return err + } + + return conn.LTrim(key, start, stop).Err() + }, acceptable) +} + // Mget is the implementation of redis mget command. func (s *Redis) Mget(keys ...string) (val []string, err error) { err = s.brk.DoWithAcceptable(func() error { diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index eb85f419..92612654 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -361,6 +361,11 @@ func TestRedis_List(t *testing.T) { vals, err = client.Lrange("key", 0, 10) assert.Nil(t, err) assert.EqualValues(t, []string{"value2", "value3", "value4"}, vals) + err = client.Ltrim("key", 0, 1) + assert.Nil(t, err) + vals, err = client.Lrange("key", 0, 10) + assert.Nil(t, err) + assert.EqualValues(t, []string{"value2", "value3"}, vals) }) }