ZREM && ZREMRANGEBYSCORE && ZREMRANGEBYLEX

ZREM && ZREMRANGEBYSCORE && ZREMRANGEBYLEX

1. ZREM

用于移除有序集中的一个或多个成员,不存在的成员将被忽略。

Command

$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ZADD vehicle 1 bmw 2 benz 3 bentley
(integer) 3
127.0.0.1:6379> ZREM vehicle bmw benz
(integer) 2
127.0.0.1:6379> ZREM vehicle fakeMember
(integer) 0

Code

func zrem(c redis.Conn) {
    defer c.Do("DEL", "vehicle")
    c.Do("ZADD", "vehicle", 1, "bmw", 2, "benz", 3, "bentley")
    //  1. Remove members normally, return number of removed members.
    remMemberCount, _ := c.Do("ZREM", "vehicle", "bmw", "benz")
    fmt.Println("Number of removed member is:", remMemberCount)
    //  2. Remove member that doesn't exist, return 0.
    remMemberCount, _ = c.Do("ZREM", "vehicle", "fakeMember")
    fmt.Println("Number of removed fake member is:", remMemberCount)
}

Output

$ go run main.go
Number of removed member is: 2
Number of removed fake member is: 0

2. ZREMRANGEBYSCORE

用于移除有序集中,指定分数(score)区间内的所有成员。

Command

$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ZADD vehicle 1 bmw 2 benz 3 bentley
(integer) 3
127.0.0.1:6379> ZREMRANGEBYSCORE vehicle 1 2
(integer) 2

Code

func zremrangebyscore(c redis.Conn) {
    defer c.Do("DEL", "vehicle")
    c.Do("ZADD", "vehicle", 1, "bmw", 2, "benz", 3, "bentley")
    //  Return number of removed members.
    remMemberCount, _ := c.Do("ZREMRANGEBYSCORE", "vehicle", 1, 2)
    fmt.Println("Number of removed member is:", remMemberCount)
}

Output

$ go run main.go
Number of removed member is: 2

3. ZREMRANGEBYLEX

用于移除有序集合中给定的字典区间的所有成员。其实也就是把对应条件下的ZRANGEBYLEX得到的成员删除。因此,此命令也是针对分数相同的成员(目前看来适用范围仅限于一个有序集合里所有成员的分数全部相同)。因为如果针对分数不同的成员,ZRANGEBYLEX得到的结果不是想要的排序出来的结果。

Command

$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ZADD vehicle 0 benz 0 audi 0 chrysler
(integer) 3
127.0.0.1:6379> ZREMRANGEBYLEX vehicle [a [b
(integer) 1

Code

func zremrangebylex(c redis.Conn) {
    defer c.Do("DEL", "vehicle")
    c.Do("ZADD", "vehicle", 0, "bmw", 0, "audi", 0, "chrysler")
    remMemberCount, _ := c.Do("ZREMRANGEBYLEX", "vehicle", "[a", "[b")
    fmt.Println("Number of removed member is:", remMemberCount)
}

Output

$ go run main.go
Number of removed member is: 1

你可能感兴趣的:(ZREM && ZREMRANGEBYSCORE && ZREMRANGEBYLEX)