go - 使用带有选项标志的 ZADD

标签 go redis

如果记录首先不存在,我正在尝试将带有分数的记录添加到排序集中:

func (r *rDA) InsertIntoSortedSetIfNotExist(value int32, score int32) error {
    conn, err := r.pool.GetContext(r.ctx)
    if err != nil {
        return err
    }
    defer conn.Close()

    _, err = conn.Do("ZADD NX", "some_key", score, value)  // err = "ERR command 'ZADD NX' is not supported."
    return err
}
但是,这不适用于 ZADD NX 的错误。不支持。
我试过这个,它也不起作用:
_, err = conn.Do("ZADD", "some_key", score, value, "NX")  // err = "ERR syntax error"
如果有人知道如何使用 redigo 使用 ZADD NX 命令,将不胜感激。谢谢!

最佳答案

您面临的问题是由于 NX 放置错误

conn.Do("ZADD", "some_key", "NX",score, value) 
redis 的命令在 NX 上是这样使用的
ZADD NAME NX 2 'Jhon'
您也可以在 https://redis.io/commands/zadd#zadd-options-redis-302-or-greater 查看文档

ZADD supports a list of options, specified after the name of the key and before the first score argument.

关于go - 使用带有选项标志的 ZADD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64023420/

相关文章:

c# - BookSleeve - 设置哈希时性能不佳

session 突然不起作用

django - 将 2 个缓存条目合并为一个

go - Go 中的 C++ fmod 等价于什么?

go - GetById 和 CreateOrUpdate 不起作用

go - 在go中用UTF-8格式编码字符串

redis - 将 key 存储在 redis 集中

Spring Redis - 主条目过期后未删除索引

json - GoLang Json 福汇

带有 golang 属性的 xml 文档