Redis sorted set类型
Redis sorted set类型
sorted set类型是按照score排序的唯一键的集合。例如我们可以做排行榜等实现。
sorted set类型操作。
zadd命令
zadd命令通过score排序然后添加所有指定的member到sort set中。如果member已经存在于sortset中,则修改该member对应的score值。不存在则创建该member。
[XX|NX] NX not exists,只有不存在时,会创建该member, XX只有存在修改该成员的score。
127.0.0.1:6379> help zadd ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...] summary: Add one or more members to a sorted set, or update its score if it already exists since: 1.2.0 group: sorted_set 127.0.0.1:6379> zadd sortset1 1 oracle 2 mysql 3 postgres 4 sqlserver 5 mongodb 6 redis (integer) 6
zrange命令
zrange命令用来查询sorted set类型。
127.0.0.1:6379> help zrange ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES] summary: Return a range of members in a sorted set since: 1.2.0 group: sorted_set 127.0.0.1:6379> zrange sortset1 0 10 withscores 1) "oracle" 2) "1" 3) "mysql" 4) "2" 5) "postgres" 6) "3" 7) "sqlserver" 8) "4" 9) "mongodb" 10) "5" 11) "redis" 12) "6"