Ordered collection
Sorted set and set are also collections of elements of type string, but each element is associated with a double type of score. The implementation of the sorted set is a mix of skip list and hash table when the element is added to the collection, an element to the score map is added to the hash table, so the cost of getting the score for a given element is O (1), Another score-to-element mapping is added to the skip list and sorted by score, so the elements in the collection can be ordered sequentially. Add, the cost of the delete operation is the same as the cost of O (log (N)) and skip list, and the Redis skip list implementation uses a doubly linked list so that the elements can be taken from the tail in reverse order. The most common use of sorted set is to use it as an index. We can store the fields to be sorted as score, the object's ID when the element is stored.
The following is the sorted set-related command:
Zadd Key Score Member |
Adds an element to the collection, the element exists in the collection, and updates the corresponding score |
Zrem Key Member |
Deletes the specified element, 1 indicates success, if the element does not exist return 0 |
Zincrby Key INCR Member |
Increase the score value of the corresponding member, then move the element and keep the skip list in order. Returns the updated score value |
Zrank Key Member |
Returns the rank (subscript) of the specified element in the collection, in which the elements are sorted by score from small to large |
Zrevrank Key Member |
Ditto, but the elements in the collection are sorted by score from large to small |
Zrange Key Start end |
Similar to the Lrange operation to specify the elements of the interval from the collection. Returns an ordered result |
Zrevrange Key Start end |
Ibid., return results are in reverse order score |
Zrangebyscore Key min Max |
Returns the elements of a set in a given interval score |
Zcount Key min Max |
Returns the number of score in a set in a given interval |
Zcard Key |
Returns the number of elements in the collection |
Zscore key Element |
Returns the score corresponding to the given element |
Zremrangebyrank Key min Max |
Deletes an element in a collection that is ranked in a given interval |
Zremrangebyscore Key min Max |
Deletes an element in a collection that score at a given interval |
Redis-sort Set