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开始
世界上最美的风景,是自己努力的模样