When registering, the user is assigned a unique number, which is prone to concurrency, leading to the problem of login and exit after the user successfully registers. The first method is considered: put a thousand pieces of data in the redis queue, so that every time one is registered, one will be popped from the queue. The data is not enough to fetch again. Finally, my colleague proposed a method of locking redis data. After trying it, it really worked, so I used the second method. Method 1: Add queue // $redis = \Yii::$app->redis; // $no_queue_key = 'user_no_list'; // $getnoListRedis = $redis->rpop($no_queue_key); // if(empty( $getnoListRedis)){ // $no = Nodata::find()->where(['uid' => 0])->limit(1000)->all(); // foreach( $no as $_v ){ // $res = $redis->lpush($no_queue_key,$_v->no); // } // $getnoListRedis = $redis->rpop($no_queue_key); // } //dd($getnoListRedis ); Method 2: add redis lock to the current data // take no in a loop while($no = No::find()->where(['uid' => 0])->limit(1)->one()){ $key = 'allocateNo:lockKey:'. $ no- >id; // If the lock is successful, get the data and exit the loop if(\Yii::$app->redis->setnx($key,'1')){ //Assign redis first after confirming that it is not locked. Lock first \Yii::$app->redis->expire($key, 30); //Set the lock time, depending on your business scenario, generally an interface request does not exceed 60 seconds break; } // Otherwise Wait 100 milliseconds usleep(100); }
Redis solves concurrent registration to generate unique code
Guess you like
Origin blog.csdn.net/hechenhongbo/article/details/121927755
Recommended
Ranking