如何在Go中使用ZAddArgs?

huangapple go评论72阅读模式
英文:

How to use ZAddArgs in Go

问题

我正在使用https://github.com/redis/go-redis/包。我尝试使用zaddGT选项。我在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。对于寻找解决方法的人:

  1. 使用ZSCORE member来获取当前分数
  2. 将现有分数与新分数进行比较
  3. 如果新分数较高,则进行更新。
英文:

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:

  1. Use ZSCORE member to retrieve the current score
  2. Compare the existing score to a new score
  3. If the new score is higher, update

huangapple
  • 本文由 发表于 2023年3月15日 23:06:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75746569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定