From cd582ed5769b18f2aff68916ef3807e8a10669c4 Mon Sep 17 00:00:00 2001 From: Mattias Lundell Date: Thu, 9 Jun 2016 17:39:51 +0200 Subject: [PATCH] add support for SPOP with a count argument (available in REDIS 3.2) --- commands.go | 8 ++++++++ commands_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/commands.go b/commands.go index 5107b7b3..f6994f04 100644 --- a/commands.go +++ b/commands.go @@ -946,12 +946,20 @@ func (c cmdable) SMove(source, destination string, member interface{}) *BoolCmd return cmd } +// Redis `SPOP key` command. func (c cmdable) SPop(key string) *StringCmd { cmd := NewStringCmd("spop", key) c.process(cmd) return cmd } +// Redis `SPOP key count` command. +func (c cmdable) SPopN(key string, count int64) *StringSliceCmd { + cmd := NewStringSliceCmd("spop", key, count) + c.process(cmd) + return cmd +} + // Redis `SRANDMEMBER key` command. func (c cmdable) SRandMember(key string) *StringCmd { cmd := NewStringCmd("srandmember", key) diff --git a/commands_test.go b/commands_test.go index 466f991f..b4d0ff3b 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1790,6 +1790,34 @@ var _ = Describe("Commands", func() { sMembers := client.SMembers("set") Expect(sMembers.Err()).NotTo(HaveOccurred()) Expect(sMembers.Val()).To(HaveLen(2)) + + }) + + It("should SPopN", func() { + sAdd := client.SAdd("set", "one") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd("set", "two") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd("set", "three") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd("set", "four") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + + sPopN := client.SPopN("set", 1) + Expect(sPopN.Err()).NotTo(HaveOccurred()) + Expect(sPopN.Val()).NotTo(Equal([]string{""})) + + sMembers := client.SMembers("set") + Expect(sMembers.Err()).NotTo(HaveOccurred()) + Expect(sMembers.Val()).To(HaveLen(3)) + + sPopN = client.SPopN("set", 4) + Expect(sPopN.Err()).NotTo(HaveOccurred()) + Expect(sPopN.Val()).To(HaveLen(3)) + + sMembers = client.SMembers("set") + Expect(sMembers.Err()).NotTo(HaveOccurred()) + Expect(sMembers.Val()).To(HaveLen(0)) }) It("should SRandMember and SRandMemberN", func() {