天天看點

有序集合

order set 有序集合

zadd key score1 value1 score2 value2 ..

添加元素

redis 127.0.0.1:6379> zadd stu 18 lily 19 hmm 20 lilei 21 lilei

(integer) 3 (由此看,也是唯一性的)

(排序的依據是score...)

zrem key value1 value2 ..

作用: 删除集合中的元素

有序集合
zremrangebyscore key min max

作用: 按照socre來删除元素,删除score在[min,max]之間的

redis 127.0.0.1:6379> zremrangebyscore stu 4 10

(integer) 2

redis 127.0.0.1:6379> zrange stu 0 -1

1) "f"

zremrangebyrank key start end

作用: 按排名删除元素,删除名次在[start,end]之間的

redis 127.0.0.1:6379> zremrangebyrank stu 0 1

1) "c"

2) "e"

3) "f"

4) "g"

zrank key member

查詢member的排名(升續 0名開始)

zrevrank key memeber

查詢 member的排名(降續 0名開始)

ZRANGE key start stop [WITHSCORES]

把集合排序後,傳回名次[start,stop]的元素(從0開始)

預設是升續排列

Withscores 是把score也列印出來

zrevrange key start stop

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

zrangebyscore key min max [withscores] limit offset N

作用: 集合(升續)排序後,取score在[min,max]内的元素,

并跳過 offset個, 取出N個

redis 127.0.0.1:6379> zadd stu 1 a 3 b 4 c 9 e 12 f 15 g

(integer) 6

redis 127.0.0.1:6379> zrangebyscore stu 3 12 limit 1 2 withscores

2) "4"

3) "e"

4) "9"

zcard key

傳回元素個數

zcount key min max

傳回[min,max] 區間内元素的數量

zinterstore destination numkeys key1 [key2 ...]

[WEIGHTS weight [weight ...]]

[AGGREGATE SUM|MIN|MAX]

求key1,key2的交集,key1,key2的權重分别是 weight1,weight2

聚合方法用: sum |min|max

聚合的結果,儲存在dest集合内

注意: weights ,aggregate如何了解?

答: 如果有交集, 交集元素又有socre,score怎麼處理?

Aggregate sum->score相加 , min 求最小score, max 最大score

另: 可以通過weigth設定不同key的權重, 交集時,socre * weights

詳見下例

redis 127.0.0.1:6379> zadd z1 2 a 3 b 4 c

(integer) 3

redis 127.0.0.1:6379> zadd z2 2.5 a 1 b 8 d

redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2

redis 127.0.0.1:6379> zrange tmp 0 -1

1) "b"

2) "a"

redis 127.0.0.1:6379> zrange tmp 0 -1 withscores

3) "a"

4) "4.5"

redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 aggregate sum

redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 aggregate min

2) "1"

4) "2"

redis 127.0.0.1:6379> zinterstore tmp 2 z1 z2 weights 1 2

2) "5"

4) "7"

繼續閱讀