sorted_set
基本操作
(1)添加數據 zadd key score1 member1 score2 member2…
(2)獲取全部數據 zrange key start stop withscores
zrevrange key start stop withscores
(3)刪除數據
zrem key member1 member2…
(4) 按條件獲取數據 zrangebyscore key min max withscores limit
zrevrangebyscore key max min withscores limit
(5)按條件刪除數據 zremrangebyscore key min max
zremrangebyrank key start stop
(6)獲取集合數據總量 zcard key
zcard key min max
(7) 集合的交,並操作 zinterstore destination numkeys key1 key2…
zunionstore destination numkeys key1 key2…
(8)求兩個集合的交,並,差集並存儲到指定集合中
sinterstore destionation key1 key2
sunionstore destionation key1 key2
(9)獲取數據對應的索引 zrank key member
zrevrank key member
(2)score值獲取以及修改 zscore key member
zincrby key increment member
(3)刪除數據
zrem key member1 member2…
(4) 按條件獲取數據 zrangebyscore key min max withscores limit
zrevrangebyscore key max min withscores limit
(5)按條件刪除數據 zremrangebyscore key min max
zremrangebyrank key start stop
應用場景實例:
1.網站上的排名
不僅要對資源進行排序,同時還需要獲取到排序後的資源的索引,以便形成排行
大致結構如下:
zadd movies 143 aa 99 bb 100 cc
zrank movies bb
2.帶有權重的任務/消息隊列
通過score來記錄任務的權重
大體結構如下:
zadd task 3 order:id001
zadd task 1 order:id014
zadd task 9 order:id025
zrevrange task 0 -1 withscores
zrevrange task 0 0 withscores
zrem task order:id025