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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容