Redis 数据结构之zset有序集合

2019-08-19  本文已影响0人  Devops海洋的渔夫

zset类型

增加

zadd key score1 member1 score2 member2 ...

127.0.0.1:6379[1]> zadd a4 4 lisi 5 wangwu 6 zhaoliu 3 zhangsan
(integer) 4
127.0.0.1:6379[1]> 

获取

返回自定返回的元素

zrange key start stop

127.0.0.1:6379[1]> zadd a4 4 lisi 5 wangwu 6 zhaoliu 3 zhangsan
(integer) 4
127.0.0.1:6379[1]> zrange a4 0 -1
1) "zhangsan"
2) "lisi"
3) "wangwu"
4) "zhaoliu"
127.0.0.1:6379[1]> 

返回score值在min和max之间的成员

zrangebyscore key min max

127.0.0.1:6379[1]> zrangebyscore a4 5 6
1) "wangwu"
2) "zhaoliu"
127.0.0.1:6379[1]> 

返回成员member的score值

zscore key member

127.0.0.1:6379[1]> zscore a4 zhangsan
"3"
127.0.0.1:6379[1]> zscore a4 wangwu
"5"
127.0.0.1:6379[1]> zscore a4 zhaoliu
"6"
127.0.0.1:6379[1]> 

删除

删除指定元素

zrem key member1 member2 ...

127.0.0.1:6379[1]> zrem a4 zhangsan
(integer) 1
127.0.0.1:6379[1]> ZRANGE a4 0 -1
1) "lisi"
2) "wangwu"
3) "zhaoliu"
127.0.0.1:6379[1]> ZREM a4 lisi
(integer) 1
127.0.0.1:6379[1]> ZRANGE a4 0 -1
1) "wangwu"
2) "zhaoliu"
127.0.0.1:6379[1]> 

删除权重在指定范围的元素

zremrangebyscore key min max

# 查看权重在5、6之间的元素
127.0.0.1:6379[1]> ZRANGEBYSCORE a4 5 6
1) "wangwu"
2) "zhaoliu"

# 删除权重在5,6之间的元素
127.0.0.1:6379[1]> ZREMRANGEBYSCORE a4 5 6
(integer) 2

# 再查看权重在5,6之间的元素
127.0.0.1:6379[1]> ZRANGEBYSCORE a4 5 6
(empty list or set)

# 查看所有元素
127.0.0.1:6379[1]> ZRANGE a4 0 -1
(empty list or set)
127.0.0.1:6379[1]> 
上一篇下一篇

猜你喜欢

热点阅读