From 3caf52bceb25d68e5ad5a34fbe3347e42ba5ef30 Mon Sep 17 00:00:00 2001 From: monkey92t Date: Sun, 11 Jul 2021 18:27:48 +0800 Subject: [PATCH] Add the count option to the rpop command(fix #1813) (#1815) --- commands.go | 7 +++++++ commands_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/commands.go b/commands.go index d350b36..8fd0397 100644 --- a/commands.go +++ b/commands.go @@ -190,6 +190,7 @@ type Cmdable interface { LSet(ctx context.Context, key string, index int64, value interface{}) *StatusCmd LTrim(ctx context.Context, key string, start, stop int64) *StatusCmd RPop(ctx context.Context, key string) *StringCmd + RPopCount(ctx context.Context, key string, count int) *StringSliceCmd RPopLPush(ctx context.Context, source, destination string) *StringCmd RPush(ctx context.Context, key string, values ...interface{}) *IntCmd RPushX(ctx context.Context, key string, values ...interface{}) *IntCmd @@ -1451,6 +1452,12 @@ func (c cmdable) RPop(ctx context.Context, key string) *StringCmd { return cmd } +func (c cmdable) RPopCount(ctx context.Context, key string, count int) *StringSliceCmd { + cmd := NewStringSliceCmd(ctx, "rpop", key, count) + _ = c(ctx, cmd) + return cmd +} + func (c cmdable) RPopLPush(ctx context.Context, source, destination string) *StringCmd { cmd := NewStringCmd(ctx, "rpoplpush", source, destination) _ = c(ctx, cmd) diff --git a/commands_test.go b/commands_test.go index 1557312..12f75cc 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2268,6 +2268,20 @@ var _ = Describe("Commands", func() { Expect(lRange.Val()).To(Equal([]string{"one", "two"})) }) + It("should RPopCount", func() { + rPush := client.RPush(ctx, "list", "one", "two", "three", "four") + Expect(rPush.Err()).NotTo(HaveOccurred()) + Expect(rPush.Val()).To(Equal(int64(4))) + + rPopCount := client.RPopCount(ctx, "list", 2) + Expect(rPopCount.Err()).NotTo(HaveOccurred()) + Expect(rPopCount.Val()).To(Equal([]string{"four", "three"})) + + lRange := client.LRange(ctx, "list", 0, -1) + Expect(lRange.Err()).NotTo(HaveOccurred()) + Expect(lRange.Val()).To(Equal([]string{"one", "two"})) + }) + It("should RPopLPush", func() { rPush := client.RPush(ctx, "list", "one") Expect(rPush.Err()).NotTo(HaveOccurred())