sortSet

  1. sortedSet是一个可排序的set集合。
  2. SortedSet中的每一个元素都带有一个score属性,可以基于score属性对元素排序,底层的实现是一个跳表(SkipList)加 hash表。
  3. SortedSet具备下列特性:可排序、元素不重复、查询速度快。
  4. 因为SortedSet的可排序特性,经常被用来实现排行榜这样的功能。

ZADD

  1. 向sortSet添加一个或多个成员,或更新其分数。如果key不存在,则创建该key。

  2. 语法:

ZADD key [NX | XX] [GT | LT] [CH] [INCR] score member [score member ...]

XX: 只更新已经存在的元素。不要添加新元素。 NX: 只添加新元素。不要更新已经存在的元素。 LT: 只有当新score小于当前分数时才更新现有元素。这个标志不会阻止添加新元素。 GT: 只有当新score大于当前分数时才更新现有元素。这个标志不会阻止添加新元素。 CH: 将返回值从添加的新元素数修改为更改的元素总数(CH是changed的缩写)。更改的元素是新添加的元素和已经存在的元素更新的分数。因此,在命令行中指定的具有与过去相同分数的元素不会被计算在内。注意:通常’ ZADD ‘的返回值只计算添加的新元素的数量。 INCR: 当指定该选项时,‘ZADD’的行为类似于‘ZINCRBY’。在这种模式下只能指定一个分数-元素对。

  1. 示例:
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 1 "uno"
(integer) 1
redis> ZADD myzset 2 "two" 3 "three"
(integer) 2
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "uno"
4) "1"
5) "two"
6) "2"
7) "three"
8) "3"

ZCARD

  1. 返回已sortSet中成员的数目。

  2. 语法:

ZCARD key
  1. 示例:
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZCARD myzset
(integer) 2

ZCOUNT

  1. 返回排序集中分数在一定范围内的成员的计数。

  2. 语法:

ZCOUNT key min max
  1. 示例:
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZCOUNT myzset -inf +inf
(integer) 3
redis> ZCOUNT myzset (1 3
(integer) 2

ZDIFF

  1. 返回多个排序集之间的差值。

  2. 语法:

ZDIFF numkeys key [key ...] [WITHSCORES]
  1. 示例:
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset1 3 "three"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZDIFF 2 zset1 zset2
1) "three"
redis> ZDIFF 2 zset1 zset2 WITHSCORES
2) "three"
3) "3"

ZDIFFSTORE

  1. 将多个排序集的差值存储在一个键中。

  2. 语法:

ZDIFFSTORE destination numkeys key [key ...]
  1. 示例:
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset1 3 "three"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZDIFFSTORE out 2 zset1 zset2
(integer) 1
redis> ZRANGE out 0 -1 WITHSCORES
1) "three"
2) "3"

ZINCRBY

  1. 增加已sortSet中成员的分数。

  2. 语法:

ZINCRBY key increment member
  1. 示例:
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZINCRBY myzset 2 "one"
"3"
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "two"
2) "2"
3) "one"
4) "3"

ZINTER

  1. 返回多个排序集的相交。

  2. 语法:

ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]]
    [AGGREGATE <SUM | MIN | MAX>] [WITHSCORES]
  1. 示例:
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZADD zset2 3 "three"
(integer) 1
redis> ZINTER 2 zset1 zset2
1) "one"
2) "two"
redis> ZINTER 2 zset1 zset2 WITHSCORES
3) "one"
4) "2"
5) "two"
6) "4"

ZINTERCARD

  1. 返回多个排序集相交的成员数。

  2. 语法:

ZINTERCARD numkeys key [key ...] [LIMIT limit]
  1. 示例:
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZADD zset2 3 "three"
(integer) 1
redis> ZINTER 2 zset1 zset2
1) "one"
2) "two"
redis> ZINTERCARD 2 zset1 zset2
(integer) 2
redis> ZINTERCARD 2 zset1 zset2 LIMIT 1
(integer) 1

ZINTERSTORE

  1. 将多个排序集的相交存储在一个键中。

  2. 语法:

ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight
    [weight ...]] [AGGREGATE <SUM | MIN | MAX>]
  1. 语法:
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZADD zset2 3 "three"
(integer) 1
redis> ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3
(integer) 2
redis> ZRANGE out 0 -1 WITHSCORES
1) "one"
2) "5"
3) "two"
4) "10"

ZLEXCOUNT

  1. 返回字典顺序范围内已sortSet中的成员数。

  2. 语法:

ZLEXCOUNT key min max
  1. 示例:
redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e
(integer) 5
redis> ZADD myzset 0 f 0 g
(integer) 2
redis> ZLEXCOUNT myzset - +
(integer) 7
redis> ZLEXCOUNT myzset [b [f
(integer) 5

ZMPOP

  1. 从一个或多个已排序的集合中返回删除后得分最高或最低的成员。如果弹出最后一个成员,则删除排序集。

  2. 语法:

ZMPOP numkeys key [key ...] <MIN | MAX> [COUNT count]
  1. 示例:
redis> ZMPOP 1 notsuchkey MIN
(error) object of type 'NoneType' has no len()
redis> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis> ZMPOP 1 myzset MIN
1) "myzset"
2) 1) 1) "one"
        1) "1"
redis> ZRANGE myzset 0 -1 WITHSCORES
3) "two"
4) "2"
5) "three"
6) "3"
redis> ZMPOP 1 myzset MAX COUNT 10
7) "myzset"
8) 1) 1) "three"
        2) "3"
    2) 1) "two"
        1) "2"
redis> ZADD myzset2 4 "four" 5 "five" 6 "six"
(integer) 3
redis> ZMPOP 2 myzset myzset2 MIN COUNT 10
9) "myzset2"
10) 1) 1) "four"
        1) "4"
    1) 1) "five"
        1) "5"
    2) 1) "six"
        1) "6"
redis> ZRANGE myzset 0 -1 WITHSCORES
(empty array)
redis> ZMPOP 2 myzset myzset2 MAX COUNT 10
(error) object of type 'NoneType' has no len()
redis> ZRANGE myzset2 0 -1 WITHSCORES
(empty array)
redis> EXISTS myzset myzset2
(integer) 0

ZMSCORE

  1. 返回已sortSet中一个或多个成员的分数。

  2. 语法:

ZMSCORE key member [member ...]
  1. 示例:
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZMSCORE myzset "one" "two" "nofield"
1) "1"
2) "2"
3) (nil)

ZPOPMAX

  1. 从已排序的集合中删除得分最高的成员后返回它们。如果弹出最后一个成员,则删除排序集。
ZPOPMAX key [count]
  1. 示例:
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZPOPMAX myzset
1) "three"
2) "3"

ZPOPMIN

  1. 从已排序的集合中删除得分最低的成员后返回它们。如果弹出最后一个成员,则删除排序集。

  2. 语法:

ZPOPMIN key [count]
  1. 示例:
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZPOPMIN myzset
1) "one"
2) "1"

ZRANDMEMBER

  1. 从已排序的集合中返回一个或多个随机成员。

  2. 语法:

ZRANDMEMBER key [count [WITHSCORES]]
  1. 示例:
redis> ZADD dadi 1 uno 2 due 3 tre 4 quattro 5 cinque 6 sei
(integer) 6
redis> ZRANDMEMBER dadi
"quattro"
redis> ZRANDMEMBER dadi
"due"
redis> ZRANDMEMBER dadi -5 WITHSCORES
1) "uno"
2) "1"
3) "tre"
4) "3"
5) "quattro"
6) "4"
7) "cinque"
8) "5"
9) "cinque"
10) "5"

ZRANGE

  1. 返回索引范围内已sortSet中的成员。

  2. 语法:

ZRANGE key start stop [BYSCORE | BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
  1. 示例:
redis> ZADD myzset 1 "one" 2 "two" 3 "three"
(integer) 3
redis> ZRANGE myzset 0 -1
1) "one"
2) "two"
3) "three"
redis> ZRANGE myzset 2 3
4) "three"
redis> ZRANGE myzset -2 -1
5) "two"
6) "three"

ZRANGEBYLEX

  1. 返回字典顺序范围内已sortSet中的成员。

  2. 语法:

ZRANGEBYLEX key min max [LIMIT offset count]
  1. 示例:
redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
(integer) 7
redis> ZRANGEBYLEX myzset - [c
1) "a"
2) "b"
3) "c"
redis> ZRANGEBYLEX myzset - (c
4) "a"
5) "b"
redis> ZRANGEBYLEX myzset [aaa (g
6) "b"
7) "c"
8) "d"
9) "e"
10) "f"

ZRANGEBYSCORE

  1. Returns members in a sorted set within a range of scores.

ZRANGESTORE

  1. tores a range of members from sorted set in a key.

ZRANK

  1. Returns the index of a member in a sorted set ordered by ascending scores.

ZREM

  1. Removes one or more members from a sorted set. Deletes the sorted set if all members were removed.

ZREMRANGEBYLEX

  1. Removes members in a sorted set within a lexicographical range. Deletes the sorted set if all members were removed.

ZREMRANGEBYRANK

  1. Removes members in a sorted set within a range of indexes. Deletes the sorted set if all members were removed.

ZREMRANGEBYSCORE

  1. Removes members in a sorted set within a range of scores. Deletes the sorted set if all members were removed.

ZREVRANGE

  1. Returns members in a sorted set within a range of indexes in reverse order.

ZREVRANGEBYLEX

  1. Returns members in a sorted set within a lexicographical range in reverse order.

ZREVRANGEBYSCORE

  1. Returns members in a sorted set within a range of scores in reverse order.

ZREVRANK

  1. Returns the index of a member in a sorted set ordered by descending scores.

ZSCAN

  1. Iterates over members and scores of a sorted set.

ZSCORE

  1. Returns the score of a member in a sorted set.

ZUNION

  1. Returns the union of multiple sorted sets.

ZUNIONSTORE

  1. Stores the union of multiple sorted sets in a key.

BZMPOP

  1. Removes and returns a member by score from one or more sorted sets. Blocks until a member is available otherwise. Deletes the sorted set if the last element was popped.

BZPOPMAX

  1. Removes and returns the member with the highest score from one or more sorted sets. Blocks until a member available otherwise. Deletes the sorted set if the last element was popped.

BZPOPMIN

  1. Removes and returns the member with the lowest score from one or more sorted sets. Blocks until a member is available otherwise. Deletes the sorted set if the last element was popped.