Like the set type, sorted set is also a collection of string-type elements, because the elements are ordered, so you can add, delete, and update elements in an ordered set with very fast speed (O (log (N)), and it is also good at sorting.
Application scenario: Get the top n posts in the site with the highest clicks, and so on.
For a detailed explanation of each of the following commands, first draw up a scenario, assuming that the sorted set records the number of clicks per article in the website, if key is Arthits, then:
1. "Zadd Key v K" adds an element to the key, K is the key, and V is the value. For example: Zadd arthits 99 12 for an article with an ID of 12 clicks for 99 times.
2. The "Zrange Key start End" is sorted from small to large according to the value of V to get the element between start to and.
Note: 0 means the first element,-1 represents the last element,-2 represents the penultimate element, and so on, if you want to get the first to the bottom of the third element, the command is: Zrange key 0-3.
3. "Zrevrange Key start end" above, according to the value of V from large to small sorting to get the element between start-to. You can easily remove the top n posts with the highest number of clicks.
4. "Zremrangebyrank Key start End" Deletes the elements in the collection. Sorted by the order of V from small to large, if you want to delete the first value in the key collection, run Zremrangebyrank arthits 0 0; Delete the first 3 values: Zremrangebyrank arthits 0 2.
5. "Zcard" returns the number of elements in the key collection.
6. The "Zrank key K" return value K is ranked in the set key, in order of V from small to large. First Name returns 0, second returns 1, and so on.
7. "Zrevrank key K" Ditto, the difference is, according to the V from large to small order. You can easily remove the most-clicked article.
8. "Zscore key K" takes out the value v corresponding to K in the set key.
9. "Zrem key K" deletes the specified element in the collection.
10. "Zincrby key num k" adds num to the element K in the set key, and the value is for an integral type. For example Zincrby arthits 3 12, add 3 clicks to an article with an ID of 12. At this point the result of Zscore Arthits 12 is 99+3 is 102.
Redis ordered set Add, delete and test the presence members of O (1) (fixed time, regardless of the number of elements contained within the collection). The maximum length of a list is 232-1 elements (4294967295, more than 4 billion sets of each element).
Example
Redis 127.0.0.1:6379> Zadd Tutorials 1 Redis
(integer) 1
Redis 127.0.0.1:6379> Zadd Tutorials 2 MongoDB
(integer) 1
Redis 127.0.0.1:6379> zadd Tutorials 3 MySQL
(integer) 1
Redis 127.0.0.1:6379> zadd Tutorials 3 MySQL
(integer) 0
Redis 127.0.0.1:6379> Zadd Tutorials 4 MySQL
(integer) 0
Redis 127.0.0.1:6379> Zrange Tutorials 0 withscores
1) "Redis"
2) "1"
3) "MongoDB"
4) "2"
5) "MySQL"
6) "4"
In the above example three values are inserted by the command Zadd to sort the collection in Redis and name tutorials.
Redis ordered set command
The following table lists the basic commands for Redis ordered collections:
Ordinal Order and description
1 zadd key Score1 member1 [Score2 member2]
Add one or more members to an ordered collection, or update the scores of existing members
2 Zcard Key
Gets the number of members in 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
Adds an increment to the fraction of a specified member in an ordered set increment
5 Zinterstore Destination Numkeys key [key ...]
Computes the intersection of the given one or more ordered sets and stores the result set in the new ordered set key
6 zlexcount Key min max
Calculates the number of members in a specified dictionary interval in an ordered set
7 zrange key Start stop [Withscores]
Returns an ordered set of members within a specified interval through an index interval
8 zrangebylex key min Max [LIMIT offset Count]
Returns the members of an ordered set through a dictionary interval
9 zrangebyscore Key min Max [withscores] [LIMIT]
Returns the members within a specified interval of an ordered set by a fraction
Ten Zrank Key member
Returns the index of a specified member in an ordered collection
One Zrem key member [member ...]
Remove one or more members from an ordered collection
Zremrangebylex Key min Max
Removes all members of a given dictionary interval in an ordered collection
Zremrangebyrank Key Start stop
Removes all members of a given ranking interval in an ordered collection
Zremrangebyscore Key min Max
Removes all members from a given fractional interval in an ordered set
Zrevrange key start stop [Withscores]
Returns the members of a specified interval in an ordered set by indexing the score from the high end
Zrevrangebyscore key Max min [Withscores]
Returns the members of a specified fractional interval in an ordered set, sorted from highest to lowest score
Zrevrank Key Member
Returns the rank of a specified member in an ordered set, with ordered set members descending by fractional values (from large to small)
Zscore Key Member
Returns the fractional value of a member in an ordered set
Zunionstore destination Numkeys key [key ...]
Calculates the set of one or more ordered sets of a given set and stores them in a new key
Zscan key cursor [MATCH pattern] [count Count]
An element in an ordered set of iterations (including element members and element scores)