ordered set 是根據 score值有序排列的數據集合. 首先還是清空數據, 並清屏, 此步驟省略~~~~ 新建一條 ordered set 數據 myset1, 並存入4個字元串, score 的排列順序為1-4: 查看這個數據: 給 myset1 的值里新加一個字元e, score=10 ...
ordered set 是根據 score值有序排列的數據集合.
首先還是清空數據, 並清屏, 此步驟省略~~~~
新建一條 ordered set 數據 myset1, 並存入4個字元串, score 的排列順序為1-4:
127.0.0.1:6379> zadd myset1 1 a 2 b 3 c 4 d
(integer) 4
查看這個數據:
127.0.0.1:6379> zrange myset1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
給 myset1 的值里新加一個字元e, score=100:
127.0.0.1:6379> zadd myset1 100 e
(integer) 1
查看最新的 myset1 的值:
127.0.0.1:6379> zrange myset1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
查看 myset1 的值里一共有幾條數據:
127.0.0.1:6379> zcard myset1
(integer) 5
根據指定的 score 的範圍查看 myset1 的值里一共有幾條數據:
127.0.0.1:6379> zcount myset1 1 3
(integer) 3
127.0.0.1:6379> zcount myset1 1 5
(integer) 4
127.0.0.1:6379> zcount myset1 1 55
(integer) 4
127.0.0.1:6379> zcount myset1 1 101
(integer) 5
127.0.0.1:6379> zcount myset1 1 100
(integer) 5
127.0.0.1:6379> zcount myset1 1 99
(integer) 4
刪除 myset1 值里的某個數據:
127.0.0.1:6379> zrem myset1 b
(integer) 1
127.0.0.1:6379> zrange myset1 0 -1
1) "a"
2) "c"
3) "d"
4) "e"
127.0.0.1:6379> zrem myset1 d
(integer) 1
127.0.0.1:6379> zrange myset1 0 -1
1) "a"
2) "c"
3) "e"
查看某個數據的索引值:
127.0.0.1:6379> zrank myset1 e
(integer) 2
127.0.0.1:6379> zrank myset1 a
(integer) 0
反排序後查詢索引:
127.0.0.1:6379> zrevrank myset1 a
(integer) 2
127.0.0.1:6379> zrevrank myset1 e
(integer) 0
查詢 score值, 沒有的返回 nil:
127.0.0.1:6379> zscore myset1 e
"100"
127.0.0.1:6379> zscore myset1 a
"1"
127.0.0.1:6379> zscore myset1 jsjs
(nil)
支持對同一個 score值設置多個不同的數據:
127.0.0.1:6379> zrange myset1 0 -1
1) "a"
2) "c"
3) "e"
127.0.0.1:6379> zadd myset1 1 aaaa
(integer) 1
127.0.0.1:6379> zrange myset1 0 -1
1) "a"
2) "aaaa"
3) "c"
4) "e"
127.0.0.1:6379> zadd myset1 1 bbbb
(integer) 1
127.0.0.1:6379> zrange myset1 0 -1
1) "a"
2) "aaaa"
3) "bbbb"
4) "c"
5) "e"
查詢 score值, 再次證明 ordered set 支持對同一個 score值設置多個不同的數據:
127.0.0.1:6379> zscore myset1 a
"1"
127.0.0.1:6379> zscore myset1 aaaa
"1"
127.0.0.1:6379> zscore myset1 bbbb
"1"
查詢索引:
127.0.0.1:6379> zrank myset1 a
(integer) 0
127.0.0.1:6379> zrank myset1 aaaa
(integer) 1
127.0.0.1:6379> zrank myset1 bbbb
(integer) 2
根據指定的 score值的範圍查出數據:
127.0.0.1:6379> zrangebyscore myset1 1 2
1) "a"
2) "aaaa"
3) "bbbb"
127.0.0.1:6379> zrangebyscore myset1 1 4
1) "a"
2) "aaaa"
3) "bbbb"
4) "c"
以上, 就是關於 ordered set 的相關命令, enjoy~~~~