Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sorted sets type and operation of redis

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Sorted sets type and operation

Sorted set is an upgraded version of set, which adds a sequence attribute to set, which can be specified when you add modified elements, and after each assignment, zset automatically re-adjusts the order to the new value. It can be understood as a mysql table with two columns, one column value and one column storage order. In the operation, key is understood as the name of zset.

Like set, sorted set is also a collection of elements of type string, except that each element is associated with a score of type double. The implementation of sorted set is a mixture of skip list and hash table. When elements are added to the collection, an element-to-score mapping is added to the hash table, so given that the cost of getting score for an 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 fetched sequentially. The cost of adding and deleting operations is O (log (N)), which is the same as that of skip list. The skip list implementation of redis uses a two-way linked list, so that elements can be fetched from the tail in reverse order. The most common way to use sorted set should be as an index. We can store the fields to be sorted as score and the id of the object as elements.

Zadd

Add the element member,score to the zset named key for sorting. If the element already exists, update the order of the element according to score

For example, zadd myzset 1 "one"

Zadd myzset 2 "two"

Zrem

Delete the element member in the zset named key

Such as: zrem myzset two

Zrange

View the elements of the specified subscript range

Such as: zrange myzset 0-1 withscores

Zrevrange

Returns all elements of index from start to end in a zset named key (sorted by score from largest to smallest)

Such as: zrerange myzset 0-1 withscores

Zrangebyscore

Returns the elements of the set in which score is in a given interval

Such as: zrangebyscore myzset3 2 3 withscores

Zincrby

If the element member already exists in the zset named key, the score of the element increases increment;. Otherwise, add the element to the collection, and its score value is increment.

For example, zincrby myzset2 2 "one"

Zrank

Returns the ranking of the member element in the zset named key (sorted by score from smallest to largest), that is, the subscript

Such as: zrank myzset3 two

Zrevrank

Returns the ranking of the member element in the zset named key (sorted by score from largest to smallest), that is, the subscript

Zcount

Returns the number of score in the set in a given interval

Such as: zcount myzset3 2 3

Zcard

Returns the number of elements in the collection

Zscore

Returns the score corresponding to a given element

Such as: zscore myzset3 two

Zremrangebyrank

Delete elements in the collection that rank in a given interval

Such as: zremrangebyrank myzset3 3 3

Zremrangebyscore

Delete elements in the collection where score is in a given interval

Such as: zremrangebyscore myzset3 1 2

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report