ZADD
- 如果有序集合不存在,将会创建一个新的有序集合,然后添加成员以及对应的分数
- NX选项只添加未存在于有序集合内的成员。如果成员已存在,返回0。反之则返回新成员的数量。
- XX选项只更新已存在的成员。如果成员不存在,则什么都不做。反之则更新成员的分数。两种情况都返回0。
- CH选项返回被更改的成员数。(包括新成员)
- INCR选项为成员分数增加指定增量。如果成员存在,更新成员的分数。反之则以增量作为分数来创建新成员。
Command
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
// If set doesn't exist, will create a new set. Then add members and corresponding scores. Returns the number of new members.
127.0.0.1:6379> exists animal
(integer) 0
127.0.0.1:6379> zadd animal 1 cat 2 dog
(integer) 2
// If use NX option, will only add members that doesn't exist. If member exists, returns 0.
// Else returns the number of new members.
127.0.0.1:6379> zadd animal NX 2 cat
(integer) 0
127.0.0.1:6379> zadd animal NX 3 fish
(integer) 1
// If use XX option, will only update existing members. If member doesn't exist, do nothing.
// Else update the score of existing member. Both returns 0.
127.0.0.1:6379> zadd animal XX 4 duck
(integer) 0
127.0.0.1:6379> zadd animal XX 4 fish
(integer) 0
// If use CH option, will return the number of members that score changed.(Include new members)
127.0.0.1:6379> zadd animal CH 3 cat 2 dog
(integer) 1
127.0.0.1:6379> zadd animal XX CH 5 fish
(integer) 1
// If use INCR option, if member is existing, will update member's score by increment.
// If member doesn't exist, will add member with increment as score. Both returns the result of calculation.
127.0.0.1:6379> zadd animal INCR 2 cat
"5"
127.0.0.1:6379> zadd animal INCR 2 goose
"2"
127.0.0.1:6379> zrange animal 0 -1 withscores
1) "dog"
2) "2"
3) "goose"
4) "2"
5) "cat"
6) "5"
7) "fish"
8) "5"
Code
func zadd(c redis.Conn) {
defer c.Do("DEL", "animal")
isExist, _ := c.Do("EXISTS", "animal")
if isExist == 1 {
c.Do("DEL", "animal")
}
// 1. If set doesn't exist, will create a new set. Then add members and corresponding scores.
// Returns the number of new members.
addedMembersCount, err := c.Do("ZADD", "animal", 1, "cat", 2, "dog")
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The number of added member is:", addedMembersCount)
// 2. If use NX option, will only add members that doesn't exist. If member exists, returns 0.
// Else returns the number of new members.
addedMembersCount, err = c.Do("ZADD", "animal", "NX", 2, "cat")
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The number of added NX member is:", addedMembersCount)
addedMembersCount, err = c.Do("ZADD", "animal", "NX", 3, "fish")
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The number of added NX member is:", addedMembersCount)
// 3. If use XX option, will only update existing members. If member doesn't exist, do nothing.
// Else update the score of existing member. Both returns 0.
addedMembersCount, err = c.Do("ZADD", "animal", "XX", 4, "duck")
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The number of added XX member is:", addedMembersCount)
addedMembersCount, err = c.Do("ZADD", "animal", "XX", 4, "fish")
if err != nil {
colorlog.Error(err.Error())
return
}
fmt.Println("The number of added XX member is:", addedMembersCount)
// 4. If use CH option, will return the number of members that score changed.(Include new members)
changedMembersCount, errCh := c.Do("ZADD", "animal", "CH", 3, "cat", 2, "dog")
if errCh != nil {
colorlog.Error(errCh.Error())
return
}
fmt.Println("The number of CH changed member is:", changedMembersCount)
changedMembersCount, errCh = c.Do("ZADD", "animal", "XX", "CH", 5, "fish")
if errCh != nil {
colorlog.Error(errCh.Error())
return
}
fmt.Println("The number of XX and CH changed member is:", changedMembersCount)
// 5. If use INCR option, if member is existing, will update member's score by increment.
// If member doesn't exist, will add member with increment as score. Both returns the result of calculation.
incrScore, incrErr := redis.Int(c.Do("ZADD", "animal", "INCR", 2, "cat"))
if incrErr != nil {
colorlog.Error(incrErr.Error())
return
}
fmt.Println("The increased score of member is:", incrScore)
incrScore, incrErr = redis.Int(c.Do("ZADD", "animal", "INCR", 2, "goose"))
if incrErr != nil {
colorlog.Error(incrErr.Error())
return
}
fmt.Println("The increased score of member is:", incrScore)
}
Output
$ go run main.go
The number of added member is: 2
The number of added NX member is: 0
The number of added NX member is: 1
The number of added XX member is: 0
The number of added XX member is: 0
The number of CH changed member is: 1
The number of XX and CH changed member is: 1
The increased score of member is: 5
The increased score of member is: 2