Search code examples
gogo-redis

How to use ZAddArgs in Go


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"
}

Solution

  • 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