ZINCRBY

ZINCRBY

有序集合中指定成员的分数加上增量increment,正负均可。

Command

127.0.0.1:6379> zadd animal 1 cat
(integer) 1
// 返回加上增量后的结果
127.0.0.1:6379> ZINCRBY animal 1 cat
"2"
// 当member不存在时,以增加为分数,增加member,返回增量
127.0.0.1:6379> ZINCRBY animal 2 dog
"2"
// 以浮点数作为增量
127.0.0.1:6379> ZINCRBY animal 1.1 cat
"3.1000000000000001"

但是为社么以浮点数作为增量时,member的分数变成了很长的浮点数?但是最后go代码里得出的结果却精确到了想要的结果?

有一个相关的issue解释了,https://github.com/antirez/redis/issues/1499,大致是这样子不影响排序,但是展示出来的就是这样长

Code

func zincrby(c redis.Conn) {
    defer c.Do("DEL", "animal")
    c.Do("ZADD", "animal", 1, "cat")
    newScore, _ := redis.Int(c.Do("ZINCRBY", "animal", 1, "cat"))
    fmt.Println("New score of member is:", newScore)
    newScore, _ = redis.Int(c.Do("ZINCRBY", "animal", 2, "dog"))
    fmt.Println("New score of new member is:", newScore)
    newScoreFloat, _ := redis.Float64(c.Do("ZINCRBY", "animal", 1.1, "cat"))
    fmt.Println("When increment is float, new score of member is:", newScoreFloat)
}

Output

$ go run main.go
New score of member is: 2
New score of new member is: 2
When increment is float, new score of member is: 3.1

你可能感兴趣的:(ZINCRBY)