In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use zset in redis. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
In Redis, zset is an upgraded version of set. It adds an order attribute to set. This attribute can be specified when adding and modifying elements. After each specification, zset will automatically adjust the order according to the new value. You can understand that there are two columns of mysql table, one column store value, one column store order. Key is understood to be the name of zset in the operation.
With ordered sets (zset), you can add, delete, and update elements very quickly (O(log(N))). Because elements are ordered at insertion time, a range of elements is quickly obtained by score or position. Access to intermediate elements of ordered collections is also very fast, so you can use ordered collections as a smart list with no duplicate members. In this list, you can easily access anything you need: ordered elements, quick existential testing, quick access to intermediate elements of a collection!
In short, you can do a lot of tasks well with ordered sets that are difficult to do in other databases.
First, let's add some data:
> zadd member_list 10 a 3 b 1 c 4 d 7 e(integer) 5#returns 5, i.e. 5 members were successfully added into the collection. Now try adding duplicate elements> zadd member_list 9 a 8 f(integer) 1#This step returns 1 instead of 2 because the element a already exists in the collection and will not be added successfully.> zrange member_list 0 6 WITHSCORES1) "c"2) 1.03) "b"4) 3.05) "d"6) 4.07) "e"8) 7.09) "f"10) 8.011) "a"12) 9.0
As you can see, the rule for sorting ordered sets is that the denominator ranks first, with the smaller denominator ranking first in the set.
A leaderboard that can be used for a large online game. Whenever a player's score changes, the ZADD command can be executed to update the player's score, and then the user information of the score TOP TEN can be obtained through the ZRANGE command. Of course, we can also use the ZRANK command to obtain player ranking information through username. Finally, we'll use a combination of ZRANGE and ZRANK commands to quickly get information about other users who have similar points to a player.
Sorted-Sets types can also be used to build index data.
How to use zset in redis is shared here. I hope the above content can be of some help to everyone and learn more. If you think the article is good, you can share it so that more people can see it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.