feat: add zintercard cmd

Signed-off-by: monkey92t <golang@88.com>
This commit is contained in:
monkey92t 2023-01-07 23:05:04 +08:00
parent 09fcdbe6e4
commit bb65dcdf09
2 changed files with 43 additions and 0 deletions

View File

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

View File

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