From bc51c61a458d1bc4fb4424c7c3e912325ef980cc Mon Sep 17 00:00:00 2001 From: boatrainlsz <yinger521999@gmail.com> Date: Mon, 21 Nov 2022 21:59:49 +0800 Subject: [PATCH] feat: add support for SINTERCARD command --- commands.go | 17 +++++++++++++++++ commands_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/commands.go b/commands.go index 45d00419..87d9f4b7 100644 --- a/commands.go +++ b/commands.go @@ -207,6 +207,7 @@ type Cmdable interface { SDiff(ctx context.Context, keys ...string) *StringSliceCmd SDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd SInter(ctx context.Context, keys ...string) *StringSliceCmd + SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd SInterStore(ctx context.Context, destination string, keys ...string) *IntCmd SIsMember(ctx context.Context, key string, member interface{}) *BoolCmd SMIsMember(ctx context.Context, key string, members ...interface{}) *BoolSliceCmd @@ -1612,6 +1613,22 @@ func (c cmdable) SInter(ctx context.Context, keys ...string) *StringSliceCmd { return cmd } +func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd { + args := make([]interface{}, 4+len(keys)) + args[0] = "sintercard" + numkeys := int64(0) + for i, key := range keys { + args[2+i] = key + numkeys++ + } + args[1] = numkeys + args[2+numkeys] = "limit" + args[3+numkeys] = limit + cmd := NewIntCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + func (c cmdable) SInterStore(ctx context.Context, destination string, keys ...string) *IntCmd { args := make([]interface{}, 2+len(keys)) args[0] = "sinterstore" diff --git a/commands_test.go b/commands_test.go index 5dd6c0fb..476644d0 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2551,6 +2551,36 @@ var _ = Describe("Commands", func() { Expect(sInter.Val()).To(Equal([]string{"c"})) }) + It("should SInterCard", func() { + sAdd := client.SAdd(ctx, "set1", "a") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd(ctx, "set1", "b") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd(ctx, "set1", "c") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + + sAdd = client.SAdd(ctx, "set2", "b") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd(ctx, "set2", "c") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd(ctx, "set2", "d") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + sAdd = client.SAdd(ctx, "set2", "e") + Expect(sAdd.Err()).NotTo(HaveOccurred()) + //limit 0 means no limit,see https://redis.io/commands/sintercard/ for more details + sInterCard := client.SInterCard(ctx, 0, "set1", "set2") + Expect(sInterCard.Err()).NotTo(HaveOccurred()) + Expect(sInterCard.Val()).To(Equal(int64(2))) + + sInterCard = client.SInterCard(ctx, 1, "set1", "set2") + Expect(sInterCard.Err()).NotTo(HaveOccurred()) + Expect(sInterCard.Val()).To(Equal(int64(1))) + + sInterCard = client.SInterCard(ctx, 3, "set1", "set2") + Expect(sInterCard.Err()).NotTo(HaveOccurred()) + Expect(sInterCard.Val()).To(Equal(int64(2))) + }) + It("should SInterStore", func() { sAdd := client.SAdd(ctx, "set1", "a") Expect(sAdd.Err()).NotTo(HaveOccurred())