redis里zset命令的用法舉例(有序集合)


積分排名 

zadd(key, score, member):向名稱為key的zset中添加元素member,score用於排序。如果該元素已經存在,則根據score更新該元素的順序。score分數  member用戶名

127.0.0.1:6379> zadd users 5 tom
(integer) 1
127.0.0.1:6379> 
127.0.0.1:6379> zadd users 10 xiaoqing
(integer) 1
127.0.0.1:6379> zadd users 5 wangcai
(integer) 1
127.0.0.1:6379> zadd users 15 xiaocai
(integer) 1
127.0.0.1:6379> zadd users 3 wangwang 
(integer) 1
127.0.0.1:6379> zadd users 0 wangcaixiao
(integer) 1
127.0.0.1:6379> zadd users 12 wangxiaocai
(integer) 1
127.0.0.1:6379> zadd users 20 xiaowangcai
(integer) 1
127.0.0.1:6379> zadd users 33 xiaoxiaoqiang
(integer) 1

zrange(key, start, end):返回名稱為key的zset(元素已按score從小到大排序)中的index從start到end的所有元素 顯示出的的不帶分數

127.0.0.1:6379> zrange users 0 -1
1) "wangcaixiao"
2) "wangwang"
3) "tom"
4) "wangcai"
5) "xiaoqing"
6) "wangxiaocai"
7) "xiaocai"
8) "xiaowangcai"
9) "xiaoxiaoqiang"

zrange(key, start, end,withscores):返回名稱為key的zset(元素已按score從小到大排序)中的index從start到end的所有元素 顯示出的的帶分數

127.0.0.1:6379> zrange users 0 -1 withscores
 1) "wangcaixiao"
 2) "0"
 3) "wangwang"
 4) "3"
 5) "tom"
 6) "5"
 7) "wangcai"
 8) "5"
 9) "xiaoqing"
10) "10"
11) "wangxiaocai"
12) "12"
13) "xiaocai"
14) "15"
15) "xiaowangcai"
16) "20"
17) "xiaoxiaoqiang"
18) "33"

zrevrange(key, start, end):返回名稱為key的zset(元素已按score從大到小排序)中的index從start到end的所有元素,不帶分數

127.0.0.1:6379> zrevrange users 0 -1 
1) "xiaoxiaoqiang"
2) "xiaowangcai"
3) "xiaocai"
4) "wangxiaocai"
5) "xiaoqing"
6) "wangcai"
7) "tom"
8) "wangwang"
9) "wangcaixiao"

zrevrange(key, start, end,withscores):返回名稱為key的zset(元素已按score從大到小排序)中的index從start到end的所有元素,帶分數

127.0.0.1:6379> zrevrange users 0 -1 withscores 
 1) "xiaoxiaoqiang"
 2) "33"
 3) "xiaowangcai"
 4) "20"
 5) "xiaocai"
 6) "15"
 7) "wangxiaocai"
 8) "12"
 9) "xiaoqing"
10) "10"
11) "wangcai"
12) "5"
13) "tom"
14) "5"
15) "wangwang"
16) "3"
17) "wangcaixiao"
18) "0"

zincrby(key, increment, member) :如果在名稱為key的zset中已經存在元素member,則該元素的score增加increment;否則向集合中添加該元素,其score的值為increment

把tom的分數加1
127.0.0.1:6379> zincrby users 1 tom
"6"

zcard 統計key中有幾個值,如統計users中有幾人

127.0.0.1:6379> zcard users
(integer) 9

zrevrank(key, member) :返回名稱為key的zset(元素已按score從大到小排序)中member元素的rank(即index,從0開始),若沒有member元素,返回“nil”

如,users中的xiaocai積分排名在第幾位

127.0.0.1:6379> zrevrank users xiaocai
(integer) 2
得到的是2,所以xiaocai積分排名在第三位,從0開始

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM