mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
feat(redis):add LpopCount,RpopCount (#2990)
This commit is contained in:
parent
03391b48ca
commit
cb7f3e8a17
@ -1170,6 +1170,26 @@ func (s *Redis) LpopCtx(ctx context.Context, key string) (val string, err error)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LpopCount is the implementation of redis lpopCount command.
|
||||||
|
func (s *Redis) LpopCount(key string, count int) ([]string, error) {
|
||||||
|
return s.LpopCountCtx(context.Background(), key, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LpopCountCtx is the implementation of redis lpopCount command.
|
||||||
|
func (s *Redis) LpopCountCtx(ctx context.Context, key string, count int) (val []string, err error) {
|
||||||
|
err = s.brk.DoWithAcceptable(func() error {
|
||||||
|
conn, err := getRedis(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
val, err = conn.LPopCount(ctx, key, count).Result()
|
||||||
|
return err
|
||||||
|
}, acceptable)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Lpush is the implementation of redis lpush command.
|
// Lpush is the implementation of redis lpush command.
|
||||||
func (s *Redis) Lpush(key string, values ...any) (int, error) {
|
func (s *Redis) Lpush(key string, values ...any) (int, error) {
|
||||||
return s.LpushCtx(context.Background(), key, values...)
|
return s.LpushCtx(context.Background(), key, values...)
|
||||||
@ -1432,6 +1452,26 @@ func (s *Redis) RpopCtx(ctx context.Context, key string) (val string, err error)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RpopCount is the implementation of redis rpopCount command.
|
||||||
|
func (s *Redis) RpopCount(key string, count int) ([]string, error) {
|
||||||
|
return s.RpopCountCtx(context.Background(), key, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RpopCountCtx is the implementation of redis rpopCount command.
|
||||||
|
func (s *Redis) RpopCountCtx(ctx context.Context, key string, count int) (val []string, err error) {
|
||||||
|
err = s.brk.DoWithAcceptable(func() error {
|
||||||
|
conn, err := getRedis(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
val, err = conn.RPopCount(ctx, key, count).Result()
|
||||||
|
return err
|
||||||
|
}, acceptable)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Rpush is the implementation of redis rpush command.
|
// Rpush is the implementation of redis rpush command.
|
||||||
func (s *Redis) Rpush(key string, values ...any) (int, error) {
|
func (s *Redis) Rpush(key string, values ...any) (int, error) {
|
||||||
return s.RpushCtx(context.Background(), key, values...)
|
return s.RpushCtx(context.Background(), key, values...)
|
||||||
|
@ -507,6 +507,14 @@ func TestRedis_List(t *testing.T) {
|
|||||||
vals, err = client.Lrange("key", 0, 10)
|
vals, err = client.Lrange("key", 0, 10)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.EqualValues(t, []string{"value2", "value3"}, vals)
|
assert.EqualValues(t, []string{"value2", "value3"}, vals)
|
||||||
|
vals, err = client.LpopCount("key", 2)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.EqualValues(t, []string{"value2", "value3"}, vals)
|
||||||
|
_, err = client.Lpush("key", "value1", "value2")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
vals, err = client.RpopCount("key", 4)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.EqualValues(t, []string{"value1", "value2"}, vals)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -523,6 +531,34 @@ func TestRedis_List(t *testing.T) {
|
|||||||
|
|
||||||
_, err = client.Rpush("key", "value3", "value4")
|
_, err = client.Rpush("key", "value3", "value4")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = client.LpopCount("key", 2)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = client.RpopCount("key", 2)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
t.Run("list redis type error", func(t *testing.T) {
|
||||||
|
runOnRedisWithError(t, func(client *Redis) {
|
||||||
|
client.Type = "nil"
|
||||||
|
_, err := client.Llen("key")
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = client.Lpush("key", "value1", "value2")
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = client.Lrem("key", 2, "value1")
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = client.Rpush("key", "value3", "value4")
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = client.LpopCount("key", 2)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, err = client.RpopCount("key", 2)
|
||||||
|
assert.Error(t, err)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user