redis:order set有序集合类型的操作(有序集合)

时间:2022-06-01 06:42:23

1. order set有序集合类型的操作(有序集合)

有序集合是在无序集合的基础上加了一个排序的依据,这个排序依据叫score,因此声明一个集合为有序集合的时候要加上score(作为排序的依据)

1.1. zadd key [NX|XX] [CH] [INCR] score member [score member ...]

语法:zadd key [NX|XX] [CH] [INCR] score member [score member ...]

作用:添加元素

例:

127.0.0.1:6379> zadd age 21 lily 22 tom 19 poly 15 wang

(integer) 4

1.2. zrange key start stop [WITHSCORES]

语法:zrange key start stop [WITHSCORES]

作用:把集合排序后(默认是升序排列),返回排序名次[start,stop]的元素(排序名次从0开始)

Withscores 是把score也打印出来

注:zrange key 0 -1   #返回所有排序的元素,默认升序

例:

127.0.0.1:6379> zadd age 21 lily 22 tom 19 poly 15 wang

(integer) 4

127.0.0.1:6379> zrange age 0 2

1) "wang"

2) "poly"

3) "lily"

127.0.0.1:6379> zrange age 0 -1   #查询所有的元素,默认升序

1) "wang"

2) "poly"

3) "lily"

4) "tom"

127.0.0.1:6379> zrange age 0 2 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

5) "lily"

6) "21"

127.0.0.1:6379> zrange age 0 -1 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

5) "lily"

6) "21"

7) "tom"

8) "22"

1.3. zrevrange key start stop [WITHSCORES]

语法:zrevrange key start stop [WITHSCORES]

作用:把集合降序排列,取排序名次[start,stop]之间的元素

例:

127.0.0.1:6379> zadd age 20 li 22 wu 25 wang 30 zhao

(integer) 4

127.0.0.1:6379> zrevrange age 0 -1 withscores

1) "zhao"

2) "30"

3) "wang"

4) "25"

5) "wu"

6) "22"

7) "li"

8) "20"

127.0.0.1:6379> zrevrange age 1 2 withscores

1) "wang"

2) "25"

3) "wu"

4) "22"

1.4. zrangebyscore key min max [WITHSCORES] [LIMIT offset count]

语法:zrangebyscore key min max [WITHSCORES] [LIMIT offset count]

作用:集合(升序)排序后,取score在[min,max]内的元素,并跳过offset个,取出count个

例:

127.0.0.1:6379> zrangebyscore age 14 20

1) "wang"

2) "poly"

127.0.0.1:6379> zrangebyscore age 14 20

1) "wang"

2) "poly"

127.0.0.1:6379> zrangebyscore age 14 20 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

127.0.0.1:6379> zrangebyscore age 14 20 withscores limit 1 2

1) "poly"

2) "19"

127.0.0.1:6379> zrangebyscore age 14 22 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

5) "lily"

6) "21"

7) "tom"

8) "22"

127.0.0.1:6379> zrangebyscore age 14 22 withscores limit 1 2

1) "poly"

2) "19"

3) "lily"

4) "21"

1.5. zrank key member

语法:zrank key member

作用:查询member的排名(升序,从 0名开始)

返回值:存在返回member的排名(升序,从0开始);不存在返回nil

例:

127.0.0.1:6379> zrange age 0 -1 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

5) "lily"

6) "21"

7) "tom"

8) "22"

127.0.0.1:6379> zrank age poly

(integer) 1

127.0.0.1:6379> zrank age wang

(integer) 0

127.0.0.1:6379> zrank age tom

(integer) 3

127.0.0.1:6379> zrank age haha

(nil)

1.6. zrevrank key member

语法:zrevrank key member

作用:查询member的排名(降序,从0名开始)

返回值:存在返回member的排名(降序,从0开始);不存在返回nil

例:

127.0.0.1:6379> zrevrank age wang

(integer) 3

127.0.0.1:6379> zrevrank age poly

(integer) 2

127.0.0.1:6379> zrevrank age tom

(integer) 0

1.7. zrem key member [member ...]

语法:zrem key member [member ...]

作用:删除集合中的元素member [member ...]

返回值:返回真正删除掉的元素的个数

例:

127.0.0.1:6379> zrange age 0 -1 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

5) "lily"

6) "21"

7) "tom"

8) "22"

127.0.0.1:6379> zrem age wang

(integer) 1

127.0.0.1:6379> zrange age 0 -1 withscores

1) "poly"

2) "19"

3) "lily"

4) "21"

5) "tom"

6) "22"

127.0.0.1:6379> zrem age poly lily haha

(integer) 2

127.0.0.1:6379> zrange age 0 -1 withscores

1) "tom"

2) "22"

1.8. zremrangebyscore key min max

语法:zremrangebyscore key min max

作用:按照score来删除元素,删除score在[min,max]之间的元素

返回值:返回真正删除元素的个数

例:

127.0.0.1:6379> zrange age 0 -1 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

5) "lily"

6) "21"

7) "tom"

8) "22"

127.0.0.1:6379> zremrangebyscore age 15 20

(integer) 2

127.0.0.1:6379> zrange age 0 -1 withscores

1) "lily"

2) "21"

3) "tom"

4) "22"

1.9. zremrangebyrank key start stop

语法:zremrangebyrank key start stop

作用:按排名删除元素,删除名次在[start,end]之间的元素(默认升序)

返回值:返回真正删除元素的个数

例:

127.0.0.1:6379> zrange age 0 -1 withscores

1) "wang"

2) "15"

3) "poly"

4) "19"

5) "lily"

6) "21"

7) "tom"

8) "22"

127.0.0.1:6379> zremrangebyrank age 0 1

(integer) 2

127.0.0.1:6379> zrange age 0 -1 withscores

1) "lily"

2) "21"

3) "tom"

4) "22"

1.10. zcard key

语法:zcard key

作用:返回集合中所有元素的个数

例:

127.0.0.1:6379> zadd age 20 li 22 wu 25 wang 30 zhao

(integer) 4

127.0.0.1:6379> zcard age

(integer) 4