diff --git a/core/stores/redis/redis.go b/core/stores/redis/redis.go index 5dfb86db..6adafd42 100644 --- a/core/stores/redis/redis.go +++ b/core/stores/redis/redis.go @@ -51,7 +51,8 @@ type ( Pipeliner = red.Pipeliner // Z represents sorted set member. - Z = red.Z + Z = red.Z + ZStore = red.ZStore IntCmd = red.IntCmd FloatCmd = red.FloatCmd @@ -1394,6 +1395,20 @@ func (s *Redis) Zrevrank(key string, field string) (val int64, err error) { return } +func (s *Redis) Zunionstore(dest string, store ZStore, keys ...string) (val int64, err error) { + err = s.brk.DoWithAcceptable(func() error { + conn, err := getRedis(s) + if err != nil { + return err + } + + val, err = conn.ZUnionStore(dest, store, keys...).Result() + return err + }, acceptable) + + return +} + func (s *Redis) String() string { return s.Addr } diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index 50bc4478..7bd10764 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -715,6 +715,20 @@ func TestRedis_SortedSet(t *testing.T) { assert.Equal(t, 0, len(pairs)) _, err = NewRedis(client.Addr, "").Zrevrank("key", "value") assert.NotNil(t, err) + client.Zadd("second", 2, "aa") + client.Zadd("third", 3, "bbb") + val, err = client.Zunionstore("union", ZStore{ + Weights: []float64{1, 2}, + Aggregate: "SUM", + }, "second", "third") + assert.Nil(t, err) + assert.Equal(t, int64(2), val) + vals, err = client.Zrange("union", 0, 10000) + assert.Nil(t, err) + assert.EqualValues(t, []string{"aa", "bbb"}, vals) + ival, err := client.Zcard("union") + assert.Nil(t, err) + assert.Equal(t, 2, ival) }) }