A Redis ordered collection is a collection of elements of type string, as well as a collection, and does not allow duplicate members.
The difference is that each element is associated with a double-type fraction. Redis is a small-to-large ordering of the members in a collection by fractions.
The members of an ordered collection are unique, but fractions (score) can be duplicated.
The collection is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1). The maximum number of members in the collection is 232-1 (4294967295, each of which can store 40多亿个 members).
Instance
- Redis 127.0. 0.1:6379> zadd w3ckey 1 redis
- (integer) 1
- Redis 127.0. 0.1:6379> zadd w3ckey 2 mongodb
- (integer) 1
- Redis 127.0. 0.1:6379> zadd w3ckey 3 mysql
- (integer) 1
- Redis 127.0. 0.1:6379> zadd w3ckey 3 mysql
- (integer) 0
- Redis 127.0. 0.1:6379> zadd w3ckey 4 mysql
- (integer) 0
- Redis 127.0. 0.1:6379> zrange w3ckey 0 withscores
- 1) "Redis"
- 2) "1"
- 3) "MongoDB"
- 4) "2"
- 5) "MySQL"
- 6) "4"
In the above example we add three values to the ordered set of Redis and associate the scores with the command Zadd .
Redis ordered Collection command
The following table lists the basic commands for the Redis ordered collection:
Serial Number |
Command and Description |
1 |
Zadd key Score1 member1 [Score2 member2] adds one or more members to an ordered collection, or updates the fraction of an existing member |
2 |
Zcard Key gets the number of members of an ordered collection |
3 |
Zcount Key min Max calculates the number of members that specify interval fractions in an ordered set |
4 |
Zincrby key increment member a specified member's score plus an increment in an ordered collection increment |
5 |
Zinterstore destination Numkeys key [key ...] computes the intersection of one or more ordered sets given and stores the result set in the new ordered collection key |
6 |
Zlexcount Key min Max calculates the number of members within a specified dictionary interval in an ordered set |
7 |
Zrange key start stop [Withscores] returns an ordered set of members within a specified range by an index interval |
8 |
Zrangebylex key min Max [LIMIT offset Count] Returns the members of an ordered collection through a dictionary interval |
9 |
Zrangebyscore key min Max [withscores] [LIMIT] Returns the members of an ordered set in a specified range by fractions |
10 |
Zrank Key member returns the index of the specified member in the Ordered collection |
11 |
Zrem key member [member ...] removes one or more members from an ordered collection |
12 |
Zremrangebylex key min Max removes all members of the given dictionary interval in the ordered collection |
13 |
Zremrangebyrank key Start stop removes all members of the given rank range in the ordered collection |
14 |
Zremrangebyscore key min Max removes all members of a given fractional interval in an ordered collection |
15 |
Zrevrange key start stop [Withscores] returns the member within the specified interval of an ordered set, through the index, the score from the high end |
16 |
Zrevrangebyscore key Max min [Withscores] returns the members within the specified fractional interval in an ordered set, sorted from highest to lowest |
17 |
Zrevrank Key member returns the rank of the specified member in an ordered collection, ordered set members are descending by fractional value (from large to small) |
18 |
Zscore Key member returns the fractional value of an ordered set of members |
19 |
Zunionstore destination Numkeys key [key ...] calculates the set of a given one or more ordered sets and stores them in a new key |
20 |
Zscan key cursor [MATCH pattern] [count count] iterates over elements in an ordered collection (including element members and element scores) |
Redis ordered set (sorted set)