ZINCRBY

ZINCRBY

有序集合中指定成員的分?jǐn)?shù)加上增量increment,正負(fù)均可。

Command

127.0.0.1:6379> zadd animal 1 cat
(integer) 1
// 返回加上增量后的結(jié)果
127.0.0.1:6379> ZINCRBY animal 1 cat
"2"
// 當(dāng)member不存在時(shí),以增加為分?jǐn)?shù),增加member,返回增量
127.0.0.1:6379> ZINCRBY animal 2 dog
"2"
// 以浮點(diǎn)數(shù)作為增量
127.0.0.1:6379> ZINCRBY animal 1.1 cat
"3.1000000000000001"

但是為社么以浮點(diǎn)數(shù)作為增量時(shí),member的分?jǐn)?shù)變成了很長的浮點(diǎn)數(shù)?但是最后go代碼里得出的結(jié)果卻精確到了想要的結(jié)果?

有一個(gè)相關(guān)的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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容