Redis(2.8.3) 命令学习 - Sorted Sets

ZADD key score member [score member ...]

Add one or more members to a sorted set, or update its score if it already exists

127.0.0.1:6379> ZADD foo 1 one
(integer) 1
127.0.0.1:6379> ZADD foo 2 two
(integer) 1
127.0.0.1:6379> ZADD foo 3 three
(integer) 1
127.0.0.1:6379> ZRANGE foo 0 -1
1) "one"
2) "two"
3) "three"

More: http://redis.io/commands/zaddhttp://www.redis.cn/commands/zadd.html

ZCARD key

Get the number of members in a sorted set

127.0.0.1:6379> ZRANGE foo 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> ZCARD foo
(integer) 3
127.0.0.1:6379> ZRANGE none 0 -1
(empty list or set)
127.0.0.1:6379> ZCARD none
(integer) 0

More: http://redis.io/commands/zcardhttp://www.redis.cn/commands/zcount.html

ZCOUNT key min max

Count the members in a sorted set with scores within the given values

127.0.0.1:6379> ZADD foo 90 A 80 B 70 C 60 D
(integer) 4
127.0.0.1:6379> ZCOUNT foo 70 80
(integer) 2
127.0.0.1:6379> ZCOUNT foo (70 80
(integer) 1
127.0.0.1:6379> ZCOUNT foo 70 (80
(integer) 1

More: http://redis.io/commands/zcounthttp://www.redis.cn/commands/zcount.html

ZINCRBY key increment member

Increment the score of a member in a sorted set

127.0.0.1:6379> ZADD foo 1 a 2 b
(integer) 2
127.0.0.1:6379> ZINCRBY foo 2 a
"3"
127.0.0.1:6379> ZRANGE foo 0 -1 WITHSCORES
1) "b"
2) "2"
3) "a"
4) "3"

More: http://redis.io/commands/zincrbyhttp://www.redis.cn/commands/zincrby.html

ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

Intersect multiple sorted sets and store the resulting sorted set in a new key

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZADD bar 1 b 2 c 3 d
(integer) 3
127.0.0.1:6379> ZINTERSTORE result 2 foo bar
(integer) 2
127.0.0.1:6379> ZRANGE result 0 -1 WITHSCORES
1) "b"
2) "3"
3) "c"
4) "5"
127.0.0.1:6379> ZINTERSTORE result 2 foo bar AGGREGATE MAX
(integer) 2
127.0.0.1:6379> ZRANGE result 0 -1 WITHSCORES
1) "b"
2) "2"
3) "c"
4) "3"

More: http://redis.io/commands/zinterstorehttp://www.redis.cn/commands/zinterstore.html

ZLEXCOUNT key min max

Count the number of members in a sorted set between a given lexicographical range

More: http://redis.io/commands/zlexcount

ZRANGE key start stop [WITHSCORES]

Return a range of members in a sorted set, by index

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d
(integer) 4
127.0.0.1:6379> ZRANGE foo 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
127.0.0.1:6379> ZRANGE foo 1 2 WITHSCORES
1) "b"
2) "2"
3) "c"
4) "3"

More: http://redis.io/commands/zrangehttp://www.redis.cn/commands/zrange.html

ZRANGEBYLEX key min max [LIMIT offset count]

Return a range of members in a sorted set, by lexicographical range

More: http://redis.io/commands/zrangebylex

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Return a range of members in a sorted set, by score

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> ZRANGEBYSCORE foo -inf +inf
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> ZRANGEBYSCORE foo -inf +inf LIMIT 0 3
1) "a"
2) "b"
3) "c"
127.0.0.1:6379> ZRANGEBYSCORE foo 1 3 WITHSCORES
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
127.0.0.1:6379> ZRANGEBYSCORE foo (1 3
1) "b"
2) "c"
127.0.0.1:6379> ZRANGEBYSCORE foo 1 (3
1) "a"
2) "b"

More: http://redis.io/commands/zremrangebyscorehttp://www.redis.cn/commands/zrangebyscore.html

ZRANK key member
Determine the index of a member in a sorted set

127.0.0.1:6379> ZADD foo 1 a 2 b 2 c 3 d
(integer) 4
127.0.0.1:6379> ZRANK foo a
(integer) 0
127.0.0.1:6379> ZRANK foo b
(integer) 1
127.0.0.1:6379> ZRANK foo c
(integer) 2
127.0.0.1:6379> ZRANK foo e
(nil)

More: http://redis.io/commands/zrankhttp://www.redis.cn/commands/zrank.html

ZREM key member [member ...]

Remove one or more members from a sorted set

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d
(integer) 4
127.0.0.1:6379> ZREM foo b d
(integer) 2
127.0.0.1:6379> ZRANGE foo 0 -1
1) "a"
2) "c"
127.0.0.1:6379> ZREM foo e
(integer) 0

More: http://redis.io/commands/zremhttp://www.redis.cn/commands/zrem.html

ZREMRANGEBYLEX key min max

Remove all members in a sorted set between the given lexicographical range

More: http://redis.io/commands/zremrangebylex

ZREMRANGEBYRANK key start stop

Remove all members in a sorted set within the given indexes

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> ZREMRANGEBYRANK foo 0 2
(integer) 3
127.0.0.1:6379> ZRANGE foo 0 -1 WITHSCORES
1) "d"
2) "4"
3) "e"
4) "5"

More: http://redis.io/commands/zremrangebyrankhttp://www.redis.cn/commands/zremrangebyrank.html

ZREMRANGEBYSCORE key min max

Remove all members in a sorted set within the given scores

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> ZREMRANGEBYSCORE foo 3 5
(integer) 3
127.0.0.1:6379> ZRANGE foo 0 -1 WITHSCORES
1) "a"
2) "1"
3) "b"
4) "2"

More: http://redis.io/commands/zremrangebyscorehttp://www.redis.cn/commands/zremrangebyscore.html

ZREVRANGE key start stop [WITHSCORES]

Return a range of members in a sorted set, by index, with scores ordered from high to low

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d
(integer) 4
127.0.0.1:6379> ZREVRANGE foo 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> ZREVRANGE foo 0 1 WITHSCORES
1) "d"
2) "4"
3) "c"
4) "3"

More: http://redis.io/commands/zrevrangehttp://www.redis.cn/commands/zrevrange.html

ZREVRANGEBYLEX key max min [LIMIT offset count]

Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.

More: http://redis.io/commands/zrevrangebylex

ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

Return a range of members in a sorted set, by score, with scores ordered from high to low

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d
(integer) 4
127.0.0.1:6379> ZREVRANGEBYSCORE foo +inf -inf
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> ZREVRANGEBYSCORE foo (3 1 WITHSCORES
1) "b"
2) "2"
3) "a"
4) "1"

More: http://redis.io/commands/zrevrangebyscorehttp://www.redis.cn/commands/zrevrangebyscore.html

ZREVRANK key member

Determine the index of a member in a sorted set, with scores ordered from high to low

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 2 d
(integer) 4
127.0.0.1:6379> ZREVRANK foo a
(integer) 3
127.0.0.1:6379> ZREVRANK foo b
(integer) 2
127.0.0.1:6379> ZREVRANK foo c
(integer) 0
127.0.0.1:6379> ZREVRANK foo d
(integer) 1

More: http://redis.io/commands/zrevrankhttp://www.redis.cn/commands/zrevrank.html

ZSCORE key member

Get the score associated with the given member in a sorted set

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c 4 d
(integer) 4
127.0.0.1:6379> ZSCORE foo b
"2"
127.0.0.1:6379> ZSCORE foo e
(nil)

More: http://redis.io/commands/zscorehttp://www.redis.cn/commands/zscore.html

ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

Add multiple sorted sets and store the resulting sorted set in a new key

127.0.0.1:6379> ZADD foo 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZADD bar 1 b 2 c 3 d
(integer) 3
127.0.0.1:6379> ZUNIONSTORE result 2 foo bar
(integer) 4
127.0.0.1:6379> ZRANGE result 0 -1 WITHSCORES
1) "a"
2) "1"
3) "b"
4) "3"
5) "d"
6) "3"
7) "c"
8) "5"
127.0.0.1:6379> ZUNIONSTORE result 2 foo bar AGGREGATE MIN
(integer) 4
127.0.0.1:6379> ZRANGE result 0 -1 WITHSCORES
1) "a"
2) "1"
3) "b"
4) "1"
5) "c"
6) "2"
7) "d"
8) "3"

More: http://redis.io/commands/zunionstorehttp://www.redis.cn/commands/zunionstore.html

ZSCAN key cursor [MATCH pattern] [COUNT count]

Incrementally iterate sorted sets elements and associated scores

时间: 2024-12-11 18:19:11

Redis(2.8.3) 命令学习 - Sorted Sets的相关文章

Redis(2.8.3) 命令学习 - Hashs

HDEL key field [field ...] Delete one or more hash fields 127.0.0.1:6379> HSET book.1 title helloworld (integer) 0 127.0.0.1:6379> HEXISTS book.1 title (integer) 1 127.0.0.1:6379> HDEL book.1 title (integer) 1 127.0.0.1:6379> HEXISTS book.1 ti

Redis(2.8.3) 命令学习 - Lists

BLPOP key [key ...] timeout Remove and get the first element in a list, or block until one is available More: http://redis.io/commands/blpop, http://www.redis.cn/commands/blpop.html BRPOP key [key ...] timeout Remove and get the last element in a lis

Redis(2.8.3) 命令学习 - Connection

AUTH password Authenticate to the server More: http://redis.io/commands/auth, http://www.redis.cn/commands/auth.html ECHO message Echo the given string 127.0.0.1:6379> ECHO "Hello World!" "Hello World!" More: http://redis.io/command

Redis(2.8.3) 命令学习 - Server

BGREWRITEAOF Asynchronously rewrite the append-only file BGSAVE Asynchronously save the dataset to disk CLIENT KILL [ip:port] [ID client-id] [TYPE normal|slave|pubsub] [ADDR ip:port] [SKIPME yes/no] Kill the connection of a client CLIENT LIST Get the

Redis(2.8.3) 命令学习 - Transactions

DISCARD Discard all commands issued after MULTI More: http://redis.io/commands/discard, http://www.redis.cn/commands/discard.html EXEC Execute all commands issued after MULTI More: http://redis.io/commands/exec, http://www.redis.cn/commands/exec.html

Redis(2.8.3) 命令学习 - Pub/Sub

PSUBSCRIBE pattern [pattern ...] Listen for messages published to channels matching the given patterns PUBSUB subcommand [argument [argument ...]] Inspect the state of the Pub/Sub subsystem PUBLISH channel message Post a message to a channel PUNSUBSC

Redis 命令学习

每天不学习点新的东西,感觉就有点会被社会淘汰掉了.也许现在学习的知识会很快忘记,下次学习用到这个知识点的时候,再回来翻记录的笔记,我想这样会比从头再学,效率会高点吧. 闲话不多聊,回归正题.今天学习redis的命令,争取把常用的redis命令都敲一遍. redis命令就是在redi服务上执行的.要想启动redis服务.我是在windows本机上安装的redis.先切换到redis目录 然后启动redis服务. 启动redis redis-cli Redis 键(key) set key conm

redis之sorted sets类型及操作

sorted sets类型及操作 sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列的mysql表,一列存value,一列存顺序.操作中key理解为zset的名字. 和set一样sorted set也是string类型元素的集合,不同的是每个元素都会关联一个double类型的score.sorted set的实现是skip list和hash table的混合体.

Redis数据类型:Sorted Sets操作指令

Redis数据类型:Sorted Sets操作指令 Sorted Sets常用操作指令 Sorted Sets,本质是一个有序的Sets,其实在原来的Sets集合中对每一个元素新增了一个属性Score,用于排序. ZADD 将指定的元素及Score添加到集合.如果集合中存在该元素,则更新其Score. 如果集合不存在,会先创建一个集合然后在添加元素及Score. 127.0.0.1:6379> ZADD sortset 1 name (integer) 1 ZRANGE 返回指定下标开始到结束下