Skip to content

feat: add support for SINTERCARD command #2291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add support for SINTERCARD command
  • Loading branch information
boatrainlsz committed Nov 21, 2022
commit bc51c61a458d1bc4fb4424c7c3e912325ef980cc
17 changes: 17 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down