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

What are the incr and hash applications of redis?

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

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about the incr and hash applications of redis. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Incr and hash application incr of redis

For example, the Beijing license plate adopts the process of getting the qualification first and then approving it. There are more than N numbers in the license plate pool, and the page is displayed in the form of ten pages. Each number is followed by a grab button, and one person can only grab one license plate, and the same license plate can only be snatched by one person. Business Model reference (http://num.10010.com/NumApp/chseNumList/init?num=186)

If ($this- > redis_db- > incr ("bj_". $car_no)! = 1) {Let others do it first, click something else} else {/ / grab the race condition, if you do not review the qualification request to withdraw, and clear the incr if (grabbed but not qualified, etc.) {release the right to the id, don't occupy the manger $this- > yredis_db- > del ("bj_". $id) } else {other business A holds the number to return. Other Business B}}

In addition, incr can operate on string type, hash type, and sortedSet type

Blpop

Blpop has an advantage over lpop in that it can prioritize multiple queues.

Blpop pops up according to the order of key. The return value is the listname and specific element values of key, and the block time can be set. The principle is that the service is blocked first.

$date = date ('Ymd', time ()); / / left in and left out, give priority to the general license plate number, and then set a blocking time return $this- > redis- > blpop (self::$_config [' dispatch_normal_list']) after assigning a very good serial number. $date, self::$_config ['dispatch_better_list']. $date, self::$_config ['redis_block_l_pop_time_out']); hsetnx

Sets a field in hash to the specified value, provided that field does not exist. Returns 0 if it exists.

This ensures that when a person can grab only one license plate, but in the process of getting the license plate to pay or other business operations, other people cannot do this (that is, the license plate cannot be bound to someone else). According to the specific business situation, car_no-based hash field and people-based hash field can be set.

Hash_base_people {"zhangsan": "Beijing A888", "lisi": "Beijing A999"} hash_base_car_no {"Beijing A888": "zhangsan", "Beijing A999": "lisi"}

Based on these two hash, you can do more operations about the business, such as viewing the specific binding relationship through hget and so on.

Hdel

With the binding model through hsetnx, when someone pays a series of deposits on a license plate, it means that it can be eliminated forever, which will use hdel. In addition, if operations such as paying a deposit are not done within a specified period of time, the license plate number will be returned to the original list.

/ delete hash $base_people_id_del with people_id as key = $this- > redis- > hdel (self::$_config ['hash_base_people'], $people_id); / / delete hash $base_car_no_del with car_no as key = $this- > redis- > hdel (self::$_config [' hash_base_car_no'], $clue_id); lpush

If there is an entrance, put the license plate that can be snapped by Beijing into a list.

$lpush_res = $redisObj- > lpush ($list_name, $car_no)

The value of list_name can be determined according to the specific value of car_no. For example, I put 6 and 8 in the better_car_no list, the rest in the normal_car_no list, and finally use blpop to specify a priority.

Rpoplpush

Secure queue pop-up mode, such as multiple people operating on an entry button, if there is enough data in the list structure, each person has and only one piece of data will be received, and then do other business operations.

But the problem is, if the original queue has been popped up after using lpop, if the client crashes after getting the element of the pop and before finishing processing the element. At that time, the message disappeared into thin air. If there are no other subsidies (such as binding or recording this pop-up element) that require rigor, you can use rpoplpush to solve this problem. After the client has actually processed the elements of this pop, the message is safely deleted through lrem.

This is how the incr and hash applications of redis are shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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