英文:
How to use ZAddArgs in Go
问题
我正在使用https://github.com/redis/go-redis/
包。我尝试使用zadd
的GT
选项。我在commands_test.go
中找到了一个使用ZAddArgs
方法来使用GT
选项的单元测试:
// 仅测试GT+LT选项。
added, err := client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
GT: true,
Members: []redis.Z{{Score: 1, Member: "one"}},
}).Result()
当我在我的代码中应用相同的策略时,Redis返回一个错误:syntax error
,没有其他解释。我做错了什么吗?这是我的代码:
score := 50
member := "Tester"
members := []redis.Z{{Score: score, Member: member}}
_, err := client.ZAddArgs(ctx, config.Key, redis.ZAddArgs{
GT: true,
Members: members,
}).Result()
if err != nil {
panic(err.Error()) // 结果是"ERR syntax error"
}
英文:
I am using https://github.com/redis/go-redis/
package. I am trying to use GT
option of zadd
. I found a unit test in commands_test.go
that uses ZAddArgs
method to use the GT
option:
// Test only the GT+LT options.
added, err := client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
GT: true,
Members: []redis.Z{{Score: 1, Member: "one"}},
}).Result()
When I apply the same strategy in my code, I get an error from Redis: syntax error
without other explanations. Am I doing something wrong? This is my code:
score := 50
member := "Tester"
members := []redis.Z{{Score: score, Member: member}}
_, err := client.ZAddArgs(ctx, config.Key, redis.ZAddArgs{
GT: true,
Members: members,
}).Result()
if err != nil {
panic(err.Error()) // results in "ERR syntax error"
}
答案1
得分: 1
看起来这是一个与Redis版本有关的问题。最低要求版本是6.2.0。对于寻找解决方法的人:
- 使用
ZSCORE member
来获取当前分数 - 将现有分数与新分数进行比较
- 如果新分数较高,则进行更新。
英文:
Looks like it's an issue with Redis version. It has to be a minimum of 6.2.0. For anyone looking for a workaround:
- Use
ZSCORE member
to retrieve the current score - Compare the existing score to a new score
- If the new score is higher, update
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论