From 7a7b75e0816ba9fc5c9f945d82797c0575ec1a60 Mon Sep 17 00:00:00 2001 From: chenguijun <18811388234@163.com> Date: Sun, 23 May 2021 16:55:03 +0800 Subject: [PATCH] add command zDiff Signed-off-by: chenguijun <18811388234@163.com> --- commands.go | 31 +++++++++++++++++++++++++++++++ commands_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/commands.go b/commands.go index 2302db6..9c64390 100644 --- a/commands.go +++ b/commands.go @@ -277,6 +277,8 @@ type Cmdable interface { ZScore(ctx context.Context, key, member string) *FloatCmd ZUnionStore(ctx context.Context, dest string, store *ZStore) *IntCmd ZRandMember(ctx context.Context, key string, count int, withScores bool) *StringSliceCmd + ZDiff(ctx context.Context, keys ...string) *StringSliceCmd + ZDiffWithScores(ctx context.Context, keys ...string) *ZSliceCmd PFAdd(ctx context.Context, key string, els ...interface{}) *IntCmd PFCount(ctx context.Context, keys ...string) *IntCmd @@ -2368,6 +2370,35 @@ func (c cmdable) ZRandMember(ctx context.Context, key string, count int, withSco return cmd } +// redis-server version >= 6.2.0. +func (c cmdable) ZDiff(ctx context.Context, keys ...string) *StringSliceCmd { + args := make([]interface{}, 2+len(keys)) + args[0] = "zdiff" + args[1] = len(keys) + for i, key := range keys { + args[i+2] = key + } + + cmd := NewStringSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + +// redis-server version >= 6.2.0. +func (c cmdable) ZDiffWithScores(ctx context.Context, keys ...string) *ZSliceCmd { + args := make([]interface{}, 3+len(keys)) + args[0] = "zdiff" + args[1] = len(keys) + for i, key := range keys { + args[i+2] = key + } + args[len(keys)+2] = "withscores" + + cmd := NewZSliceCmd(ctx, args...) + _ = c(ctx, cmd) + return cmd +} + //------------------------------------------------------------------------------ func (c cmdable) PFAdd(ctx context.Context, key string, els ...interface{}) *IntCmd { diff --git a/commands_test.go b/commands_test.go index 3ae3007..1ce8872 100644 --- a/commands_test.go +++ b/commands_test.go @@ -3960,6 +3960,45 @@ var _ = Describe("Commands", func() { Expect(err).NotTo(HaveOccurred()) Expect(slice).To(Or(Equal([]string{"one", "1"}), Equal([]string{"two", "2"}))) }) + + It("should ZDiff", 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, "zset1", &redis.Z{Score: 3, Member: "three"}).Err() + Expect(err).NotTo(HaveOccurred()) + err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err() + Expect(err).NotTo(HaveOccurred()) + + v, err := client.ZDiff(ctx, "zset1", "zset2").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(v).To(Equal([]string{"two", "three"})) + }) + + It("should ZDiffWithScores", 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, "zset1", &redis.Z{Score: 3, Member: "three"}).Err() + Expect(err).NotTo(HaveOccurred()) + err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err() + Expect(err).NotTo(HaveOccurred()) + + v, err := client.ZDiffWithScores(ctx, "zset1", "zset2").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(v).To(Equal([]redis.Z{ + { + Member: "two", + Score: 2, + }, + { + Member: "three", + Score: 3, + }, + })) + }) }) Describe("streams", func() {