Redis 笔记(07)— sorted set 类型(添加、删除有序集合元素、获取分数范围内成员、按score排序、返回集合元素个数)
zset 可能是 Redis 提供的最為特色的數據結構,一方面它是一個 set,保證了內部 value 的唯一性,另一方面它可以給每個 value 賦予一個 score,代表這個 value 的排序權重。它的內部實現用的是一種叫做「跳躍列表」的數據結構。
zset 中最后一個 value 被移除后,數據結構自動刪除,內存被回收。
zset 可以用來存粉絲列表,value 值是粉絲的用戶 ID,score 是關注時間。我們可以對粉絲列表按關注時間進行排序。
zset 還可以用來存儲學生的成績,value 值是學生的 ID,score 是他的考試成績。我們可以對成績按分數進行排序就可以得到他的名次。
1. sorted set 類型相關命令
| 命令 | 說明 |
|---|---|
| zadd key score member | 添加元素到集合,元素在集集合中存在則更新應對的score |
| zrem key member | 刪除指定元素 |
| zcount key min max | 返回分數范圍內的成員變量 |
| zincrby key incr member | 按照incr幅度增加對應member的score值,返回score值 |
| zrank key member | 返回指定元素在集合中的排名(下標),集合中元素是按score從小到大排序的 |
| zrevrank key member | 集合中元素是按score從大到小排序的 |
| zrange key start end | 從集合中選擇指定區間的元素,返回的是有序集合 |
| zrevrange key start end | 同上,返回結果是按score逆序的 |
| zcard key | 返回集合中元素的個數 |
| zscore key member | 返回給定元素對應的score |
| zremrangebyrank key min max | 刪除集合中排名在給定區間的元素 |
| zinterstore distination numkeys | 相交多個結果集,導致排序的設置存儲在一個新的結果集 |
| zunionstore destination numberkeys | 添加多個排序集合導致排序的設置存儲在一個新的結果集,可以實現取得最大值(max),取得最小值(min) |
2. 使用示例
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> zadd jack 90 english
(integer) 1
127.0.0.1:6379> keys *
1) "jack"
127.0.0.1:6379> zadd tom 80 english
(integer) 1
127.0.0.1:6379> keys *
1) "jack"
2) "tom"
127.0.0.1:6379> zrem tom english # 刪除指定元素
(integer) 1
127.0.0.1:6379> keys *
1) "jack"
127.0.0.1:6379> zadd tom 80 english
(integer) 1
127.0.0.1:6379> zadd tom 95 math
(integer) 1
127.0.0.1:6379> zadd tom 91 chinese
(integer) 1
127.0.0.1:6379> keys *
1) "jack"
2) "tom"
127.0.0.1:6379> zcount tom 90 100 # 返回分數范圍內的成員變量
(integer) 2
127.0.0.1:6379> zincrby tom 3 chinese
"94"
127.0.0.1:6379> zrank tom chinese
(integer) 1
127.0.0.1:6379> zrank tom english
(integer) 0
127.0.0.1:6379> zrank tom math
(integer) 2
127.0.0.1:6379> zrevrank tom math
(integer) 0
127.0.0.1:6379> zrange tom 0 2
1) "english"
2) "chinese"
3) "math"
127.0.0.1:6379> zrevrange tom 0 2
1) "math"
2) "chinese"
3) "english"
127.0.0.1:6379> zcard tom
(integer) 3
127.0.0.1:6379> zscore tom math
"95"
127.0.0.1:6379>
從集合中選擇指定區間的元素,返回的是有序集合,如果要加上所攜帶的分數時,需要使用帶有 withscores 字段的語句。
127.0.0.1:6379> zrange tom 0 -1 withscores
1) "english"
2) "80"
3) "chinese"
4) "94"
5) "math"
6) "95"
3. 容器型數據結構的通用規則
list/set/hash/zset 這四種數據結構是容器型數據結構,它們共享下面兩條通用規則:
- create if not exists
如果容器不存在,那就創建一個,再進行操作。比如 rpush 操作剛開始是沒有列表的,Redis 就會自動創建一個,然后再 rpush 進去新元素。
- drop if no elements
如果容器里元素沒有了,那么立即刪除元素,釋放內存。這意味著 lpop 操作到最后一個元素,列表就消失了。
4. 過期時間
Redis 所有的數據結構都可以設置過期時間,時間到了,Redis 會自動刪除相應的對象。需要注意的是過期是以對象為單位,比如一個 hash 結構的過期是整個 hash 對象的過期,而不是其中的某個子 key。
還有一個需要特別注意的地方是如果一個字符串已經設置了過期時間,然后你調用了 set 方法修改了它,它的過期時間會消失。
127.0.0.1:6379> set str "hello world"
OK
127.0.0.1:6379> expire str 300
(integer) 1
127.0.0.1:6379> ttl str
(integer) 295
127.0.0.1:6379> ttl str
(integer) 294
127.0.0.1:6379> set str "hello"
OK
127.0.0.1:6379> ttl str
(integer) -1
127.0.0.1:6379>
總結
以上是生活随笔為你收集整理的Redis 笔记(07)— sorted set 类型(添加、删除有序集合元素、获取分数范围内成员、按score排序、返回集合元素个数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022-2028年中国领带行业投资分析
- 下一篇: 2022-2028年中国防臭袜行业投资分