Home > Database > Redis > How to use zset in redis

How to use zset in redis

(*-*)浩
Release: 2019-06-17 15:34:53
Original
4958 people have browsed it

Zset in Redis is an upgraded version of set. It adds a sequence attribute on the basis of set. This attribute can be specified when adding and modifying elements. After each specification, zset will automatically re-acquire the new value. Adjust the order. You can understand the mysql table with two columns, one column stores the value and one column stores the order. During the operation, key is understood as the name of zset.

How to use zset in redis

Using a sorted set (zset), you can add, delete and update elements very quickly (O(log(N))). Because the elements are sorted when inserted, a range of elements can be quickly obtained by score or position. Accessing the middle elements of a sorted set is also very fast, so you can use the sorted set as a smart list without duplicate members. In this list, you can easily access anything you need: ordered elements, quick existence tests, quick access to middle elements of the set! (Recommended learning: Redis Video Tutorial)

In short, using ordered sets you can well complete many tasks that are difficult to achieve in other databases.

At first, let’s add some data:

> zadd member_list 10 a 3 b 1 c 4 d 7 e
(integer) 5
#返回5,即成功加了5个进入集合。现在试试添加重复的元素
> zadd member_list 9 a 8 f
(integer) 1
#这个步骤,返回了1,而不是2,是因为a这个元素已经存在于集合当中了,不会添加成功。
> zrange member_list 0 6 WITHSCORES
1) "c"
2) 1.0
3) "b"
4) 3.0
5) "d"
6) 4.0
7) "e"
8) 7.0
9) "f"
10) 8.0
11) "a"
12) 9.0
Copy after login

As you can see, the rule for sorting ordered sets is based on the size of the denominator. The smaller the denominator, the smaller the denominator is in the front of the set

Can be used for the score ranking list of a large-scale online game. Whenever the player's score changes, you can execute the ZADD command to update the player's score, and then use the ZRANGE command to obtain the user information of the TOP TEN points. Of course, we can also use the ZRANK command to obtain the player's ranking information through username. Finally, we will use the ZRANGE and ZRANK commands in combination to quickly obtain information about other users with similar points to a certain player.
Sorted-Sets type can also be used to build index data.

For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!

The above is the detailed content of How to use zset in redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template