有序集合类型是使用散列表和跳跃表(Skip list)实现的,所以即使读取位于中间部分的数据速度也很快(时间复杂度是O(log(N)))。
有序集合类型中元素唯一性,有序性。
有序是按照元素的分值排序,分值是添加元素时指定的,可以改变元素的分值从而改变元素的顺序。
以下命令中的key 为有序集合的key(键名)
1.添加元素
ZADD key score value[score value…]
向有序集合中添加一个或多个元素,元素值为value ,对应的分值为score ,如果集合中不存在value,则添加,否则更改该元素的分值==score
score 可以为整型或浮点型 ,特殊值:+inf 正无穷 -inf负元穷
localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zadd ranking 83 mi (integer) 0
2.获取元素的分值
zscore key value
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zscore ranking iphone "90"
3.按排名范围查询元素,返回值按由小到大的顺序排列
ZRANGE key startIndex , endIndex [WITHSCORES]
添加到有序集合中的元素会按分值由小到大排名。
排名写法:
1.分值最小的排名为0 ,分值最大的排名等于元素个数-1,
2.分值最大的排名为-1,分值最小的排名等于负的元素个数
如:有10个元素的有序集合:
1.元素值最小的排名为0 ,元素值最大的排名为9,
2 元素值最小的排名为-10 ,元素值最大的排名为-1,
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zrange ranking 0 3 1) "mi" 2) "sony" 3) "huawei" 4) "iphone" localhost:6379> zrange ranking 0 -1 1) "mi" 2) "sony" 3) "huawei" 4) "iphone" localhost:6379> zrange ranking -2 -1 1) "huawei" 2) "iphone" localhost:6379> zrange ranking -1 -2 (empty list or set)如果需要返回分值,则zrange 命令最后面增加:WITHSCORES
localhost:6379> zrange ranking 0 -1 withscores 1) "mi" 2) "78" 3) "sony" 4) "80" 5) "huawei" 6) "82" 7) "iphone" 8) "90"4.按排名范围查询元素,返回值按由大到小的顺序排列
ZREVRANGE key startIndex , endIndex [WITHSCORES]
zrevrange 与zrange 命令相同,只是返回值按由大到小顺序排列
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zrevrange ranking 0 -1 1) "iphone" 2) "huawei" 3) "sony" 4) "mi" localhost:6379> zrevrange ranking 0 3 1) "iphone" 2) "huawei" 3) "sony" 4) "mi" localhost:6379> zrevrange ranking -4 -1 1) "iphone" 2) "huawei" 3) "sony" 4) "mi"5.按分值范围查询元素,返回值按元素分值小到大排序
ZRANGEBYSCORE key minScore maxScore [WITHSCROES] [LIMIT offset count]
查询分值>=minScore && 分值<=maxScore 的元素。
(分值可以使用-inf负无穷大+inf正无穷大)
("("+分值 代表不包含该分值)
WITHSCROES :返回值中带有元素的分值
[LIMIT offset count]:在查询结果的元素中,以排序在offset 位置开始取count个元素,offset由0开始
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zrangebyscore ranking 80 90 1) "sony" 2) "huawei" 3) "iphone" localhost:6379> zrangebyscore ranking 80 90 withscores 1) "sony" 2) "80" 3) "huawei" 4) "82" 5) "iphone" 6) "90" localhost:6379> zrangebyscore ranking 80 90 withscores 1) "sony" 2) "80" 3) "huawei" 4) "82" 5) "iphone" 6) "90" localhost:6379> zrangebyscore ranking 80 90 withscores limit 0 2 1) "sony" 2) "80" 3) "huawei" 4) "82"
6.增加某个元素的分值
Z INCRBY key increment value
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zrange ranking 0 -1 withscores 1) "mi" 2) "78" 3) "sony" 4) "80" 5) "huawei" 6) "82" 7) "iphone" 8) "90" localhost:6379> zincrby ranking 8 mi "86" localhost:6379> zrange ranking 0 -1 withscores 1) "sony" 2) "80" 3) "huawei" 4) "82" 5) "mi" 6) "86" 7) "iphone" 8) "90"
7.获取有序集合中的元素个数
ZCARD key
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zcard ranking (integer) 48.获取指定分值范围的元素的个数
ZCOUNT key minScore maxScore
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zcount ranking 80 90 (integer) 29.删除一个或多个元素,返回删 除元素的个数
ZREM key value1 value2 value...
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zrem ranking sony mi (integer) 2 localhost:6379> zrange ranking 0 -1 1) "huawei" 2) "iphone"
10.按排名范围删除元素
ZREMRANGEBYRANK key startIndex endIndex
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zremrangebyrank ranking 0 2 (integer) 3 localhost:6379> zrange ranking 0 -1 1) "iphone"
11.按分值范围删除元素
ZREMRANGEBYSCORE key minScore maxScore
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zremrangebyscore ranking 70 80 (integer) 2 localhost:6379> zrange ranking 0 -1 1) "huawei"
12.获取元素的排名
ZRANK key value //返回元素值==value的元素的排名 (由小到到的排名顺序,最小值排名为0)
ZREVRANK key value //返回元素值==value的元素的排名 (由大到小的排名顺序,最大值排名为0)
localhost:6379> del ranking (integer) 1 localhost:6379> zadd ranking 90 iphone 80 sony 82 huawei 78 mi (integer) 4 localhost:6379> zrank ranking sony (integer) 1 localhost:6379> zrevrank ranking sony (integer) 2 localhost:6379> zrank ranking iphone (integer) 3 localhost:6379> zrevrank ranking iphone (integer) 0
13.有序集合的交运算
ZINTERSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]]
[AGREGATE SUM|MIN|MAX]
N个有序集合中相同元素分值计算,并将结果保存在目标有序集合中。
destination :交运算的结果保存到有序集合destination 中
numkeys :指定参与计算的有序集合的个数
key [key …]:参与计算的有序集合
[WEIGHTS weight [weight …]]:可选参数,指定参与计算的每一个集合的权重,计算时,集合中元素的分值乘以权重,再与其它集合计算
[AGREGATE SUM|MIN|MAX]:可选参数,默认为 集合中相同元素进行sum计算,可以指定为min 取小值或max 取最大值计算
localhost:6379> del testOne (integer) 1 localhost:6379> del testTwo (integer) 1 localhost:6379> zadd testOne 90 tom 80 carry 70 lisa (integer) 3 localhost:6379> zadd testTwo 85 tom 95 carry 83 lisa (integer) 3 localhost:6379> zinterstore testSumResult 2 testOne testTwo (integer) 3 localhost:6379> zrange testSumResult 0 -1 withscores 1) "lisa" 2) "153" 3) "carry" 4) "175" 5) "tom" 6) "175" localhost:6379> zinterstore testMinResult 2 testOne testTwo aggregate min (integer) 3 localhost:6379> zrange testMinResult 0 -1 withscores 1) "lisa" 2) "70" 3) "carry" 4) "80" 5) "tom" 6) "85" localhost:6379> zinterstore testMaxResult 2 testOne testTwo aggregate max (integer) 3 localhost:6379> zrange testMaxResult 0 -1 withscores 1) "lisa" 2) "83" 3) "tom" 4) "90" 5) "carry" 6) "95"