feat: add support for SINTERCARD command

This commit is contained in:
boatrainlsz 2022-11-21 21:59:49 +08:00
parent 0884e48a21
commit bc51c61a45
2 changed files with 47 additions and 0 deletions

View File

@ -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"

View File

@ -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())