Redis中的Zset是有序的、可重复的(元素不能重但score可重),其中每个元素都有一个分值score,Redis会根据其score对其进行从小到大的排序。
1.zadd key score member [score member]
添加元素
zadd subject 3 JAVA 2 C 1 mysql
2.zrange key start stop
获取指定返回的元素,stop为-1表示获取所有
zrange subject 0 -1
3.ZRANGEBYSCORE KEY -inf +inf
升序排列
zrangebyscore subject -inf +inf
ZRANGEBYSCORE KEY -inf +inf WiTHSCORES #升序排列带分值
ZRANGEBYSCORE subject -inf +inf WiTHSCORES
ZRANGEBYSCORE key -inf score +inf WITHSCORES 按条件显示
ZRANGEBYSCORE subject -inf 2 WITHSCORES #显示分值小于等于2且带分值升序显示
ZRANGEBYSCORE subject 2 +inf WITHSCORES #显示分值大于等于2且带分值升序显示
4.ZREVRANGE KEY 0 -1
降序排序
ZREVRANGE subject 0 -1
5.zrem key value
移除指定元素,若元素不存在返回0
zrem subject mysql
6.zcard key
获取zset长度
zcard subject
7.zcount key start stop
获取指定区间的集合长度
zcount subject 0 2
zcount subject 0 3
zcount subject 0 4