support zunionstore in redis (#410)

master
Kevin Wan 4 years ago committed by GitHub
parent ebec5aafab
commit a784982030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

Loading…
Cancel
Save