Home  >  Article  >  Database  >  Parse incr and hash applications of redis

Parse incr and hash applications of redis

藏色散人
藏色散人forward
2021-09-18 16:46:202863browse

incr

For example, Beijing license plates adopt a process of grabbing first and then approving the qualifications. There are N numbers in the license plate pool, and the page is displayed in ten items per page. There is a grab button after each number, and one person can only grab one license plate, and the same license plate can only be grabbed by one person. Business model reference (http://num.10010.com/NumApp/chseNumList/init?num=186)

 if ($this->redis_db->incr("bj_".$car_no) != 1) {
      让别人先下手了,点别的去~
  }else{
       //抢到竞态条件,如果不复核资质要求退出,并清除incr
       if(抢到了但是没资质等){
         释放对此id的竟态权,别占茅坑
         $this->yredis_db->del("bj_".$id);
       }else{
         其他业务A
         抱得号码归...
         其他业务B
     }
}

In addition,incrCan be operated on string type, hash type, sortedSet type

blpop

blpopOne advantage over lpop is that it can perform priority operations on multiple queues.
blpop will pop up in sequence according to the order of key. The return value is the listname and specific element value of key, and block# can be set. ## Time, the principle is to block first and serve first.

        $date = date('Ymd', time());
        //左进左出 ,优先分配一般的车牌号码,然后在分配非常好的连号号码,设置一个阻塞时间
        return $this->redis->blpop(self::$_config['dispatch_normal_list'] . $date, self::$_config['dispatch_better_list'] . $date, self::$_config['redis_block_l_pop_time_out']);
hsetnx

Set a

field in hash to the specified value, provided that field does not exist. If present, returns 0. This ensures that one person can only grab one license plate, but during the process of grabbing the license plate to perform payment or other business operations, other people cannot do this (that is, this license plate cannot be bound to other people). Depending on the specific business situation, a hash field based on car_no and a hash field based on people can be set.

hash_base_people {"zhangsan":"京A888","lisi":"京A999"}
hash_base_car_no {"京A888":"zhangsan","京A999":"lisi"}
Based on these two hashes, you can do more business-related operations, such as checking the specific binding relationship through hget, etc.

hdel

With the binding model through hsetnx, when someone pays a deposit for a certain license plate, etc., it means that it can be eliminated forever. This will be used hdel. In addition, if no operations such as paying a deposit are performed within the specified time, the license plate number will be returned to the original list.

 //删除以people_id为key的hash
 $base_people_id_del = $this->redis->hdel(self::$_config['hash_base_people'], $people_id);

 //删除以car_no为key的hash
 $base_car_no_del = $this->redis->hdel(self::$_config['hash_base_car_no'], $clue_id);
lpush

If there is an entrance, put the license plates that can be snapped in 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, if there are 6 and 8, I will put them in the

better_car_no list, and the others in the normal_car_no list. Finally, I can use blpop to specify a sequence. priority.

rpoplpush

Safe queue pop-up mode, for example, N multiple people operate an entrance button. If there is enough data in the list structure, each person will have and only one piece of data will be received. Do other business operations after receiving it.

But the problem is that after using
lpop, the original queue has been popped. If the client obtains the pop element in the middle and before completing the processing of this element, The client crashed. At this time, the message disappeared out of thin air. If there are no other supplementary measures (such as binding or recording the pop-up element) that require strict requirements, you can use rpoplpush to solve this problem. After the client actually processes the pop element, this message is safely deleted through lrem.

Recommended study: "

redis video tutorial"

The above is the detailed content of Parse incr and hash applications of redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete