From bb65dcdf0903459ed341c87de34ad689632dceff Mon Sep 17 00:00:00 2001 From: monkey92t Date: Sat, 7 Jan 2023 23:05:04 +0800 Subject: [PATCH] feat: add zintercard cmd Signed-off-by: monkey92t --- commands.go | 17 +++++++++++++++++ commands_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/commands.go b/commands.go index 87d9f4b7..2e37d735 100644 --- a/commands.go +++ b/commands.go @@ -268,6 +268,7 @@ type Cmdable interface { ZIncrBy(ctx context.Context, key string, increment float64, member string) *FloatCmd ZInter(ctx context.Context, store *ZStore) *StringSliceCmd ZInterWithScores(ctx context.Context, store *ZStore) *ZSliceCmd + ZInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd ZInterStore(ctx context.Context, destination string, store *ZStore) *IntCmd ZMScore(ctx context.Context, key string, members ...string) *FloatSliceCmd ZPopMax(ctx context.Context, key string, count ...int64) *ZSliceCmd @@ -2346,6 +2347,22 @@ func (c cmdable) ZInterWithScores(ctx context.Context, store *ZStore) *ZSliceCmd return cmd } +func (c cmdable) ZInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd { + args := make([]interface{}, 4+len(keys)) + args[0] = "zintercard" + 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) ZMScore(ctx context.Context, key string, members ...string) *FloatSliceCmd { args := make([]interface{}, 2+len(members)) args[0] = "zmscore" diff --git a/commands_test.go b/commands_test.go index d0c43c7b..01bc4e0c 100644 --- a/commands_test.go +++ b/commands_test.go @@ -4353,6 +4353,32 @@ var _ = Describe("Commands", func() { Expect(v).To(Equal([]string{"one", "two"})) }) + It("should ZInterCard", func() { + err := client.ZAdd(ctx, "zset1", redis.Z{Score: 1, Member: "one"}).Err() + Expect(err).NotTo(HaveOccurred()) + err = client.ZAdd(ctx, "zset1", redis.Z{Score: 2, Member: "two"}).Err() + Expect(err).NotTo(HaveOccurred()) + err = client.ZAdd(ctx, "zset2", redis.Z{Score: 1, Member: "one"}).Err() + Expect(err).NotTo(HaveOccurred()) + err = client.ZAdd(ctx, "zset2", redis.Z{Score: 2, Member: "two"}).Err() + Expect(err).NotTo(HaveOccurred()) + err = client.ZAdd(ctx, "zset2", redis.Z{Score: 3, Member: "three"}).Err() + Expect(err).NotTo(HaveOccurred()) + + // limit 0 means no limit + sInterCard := client.ZInterCard(ctx, 0, "zset1", "zset2") + Expect(sInterCard.Err()).NotTo(HaveOccurred()) + Expect(sInterCard.Val()).To(Equal(int64(2))) + + sInterCard = client.ZInterCard(ctx, 1, "zset1", "zset2") + Expect(sInterCard.Err()).NotTo(HaveOccurred()) + Expect(sInterCard.Val()).To(Equal(int64(1))) + + sInterCard = client.ZInterCard(ctx, 3, "zset1", "zset2") + Expect(sInterCard.Err()).NotTo(HaveOccurred()) + Expect(sInterCard.Val()).To(Equal(int64(2))) + }) + It("should ZInterWithScores", func() { err := client.ZAdd(ctx, "zset1", redis.Z{Score: 1, Member: "one"}).Err() Expect(err).NotTo(HaveOccurred())