English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Redis有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。
The difference is that each element is associated with a double type of score. Redis sorts the members in the set from small to large through scores.
The members of the ordered set are unique, but the score (score) can be repeated.
The collection is implemented by hash table, so the complexity of adding, deleting, and searching is O(1) 232 - 1 (4294967295, each collection can store40 to 1 billion members).
redis 127.0.0.1:6379> ZADD w3codeboxkey 1 redis (integer) 1 redis 127.0.0.1:6379> ZADD w3codeboxkey 2 mongodb (integer) 1 redis 127.0.0.1:6379> ZADD w3codeboxkey 3 mysql (integer) 1 redis 127.0.0.1:6379> ZADD w3codeboxkey 3 mysql (integer) 0 redis 127.0.0.1:6379> ZADD w3codeboxkey 4 mysql (integer) 0 redis 127.0.0.1:6379> ZRANGE w3codeboxkey 0 10 WITHSCORES 1) "redis" 2) "1" 3) "mongodb" 4) "2" 5) "mysql" 6) "4"
In the above example, we use the command ZADD Three values were added to the redis ordered set and associated with scores.
The following table lists the basic commands of redis ordered set:
Serial number | Command and description |
---|---|
1 | ZADD key score1 member1 [score2 member2] Adds one or more members to the ordered set, or updates the score of existing members |
2 | ZCARD key Gives the number of members in the ordered set |
3 | ZCOUNT key min max Calculates the number of members in the specified score range in the ordered set |
4 | ZINCRBY key increment member Increases the score of the specified member in the ordered set by the increment |
5 | ZINTERSTORE destination numkeys key [key ...] Calculates the intersection of one or more ordered sets and stores the result set in a new ordered set destination |
6 | ZLEXCOUNT key min max Calculates the number of members in the specified dictionary range in the ordered set |
7 | ZRANGE key start stop [WITHSCORES] Returns members in the specified range by index in the ordered set |
8 | ZRANGEBYLEX key min max [LIMIT offset count] Returns members in the ordered set by dictionary range |
9 | ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] Returns members in the specified range by score in the ordered set |
10 | ZRANK key member Returns the index of the specified member in the ordered set |
11 | ZREM key member [member ...] Remove one or more members from the sorted set |
12 | ZREMRANGEBYLEX key min max Remove all members in the specified dictionary range from the sorted set |
13 | ZREMRANGEBYRANK key start stop Remove all members in the specified rank range from the sorted set |
14 | ZREMRANGEBYSCORE key min max Remove all members in the specified score range from the sorted set |
15 | ZREVRANGE key start stop [WITHSCORES] Return members in the specified range in the sorted set, by index, sorted from high to low |
16 | ZREVRANGEBYSCORE key max min [WITHSCORES] Return members in the specified score range in the sorted set, sorted from high to low |
17 | ZREVRANK key member Return the rank of the specified member in the sorted set, sorted by score in descending order (from largest to smallest) |
18 | ZSCORE key member Return the score of the member in the sorted set |
19 | ZUNIONSTORE destination numkeys key [key ...] Compute the union of one or more sorted sets and store the result in a new key |
20 | ZSCAN key cursor [MATCH pattern] [COUNT count] Iterate over elements in a sorted set (including element members and element scores) |