redis解决高并发下脏读问题
//解决并发情况下卡脏读的问题 protected function BingFa($mobile, $ent_id){ $obj = EnterpriseMembers::getNewMemberCardByEntId($mobile, $ent_id); if(!$obj){ return false; }else{ $expire = 10; $key = 'new_card_'.$ent_id.'_'.$obj->member_card; $is_lock = Yii::$app->redis_php->setnx($key, time()+$expire); // 不能获取锁 if(!$is_lock){ $lock_time = Yii::$app->redis_php->get($key); if(time()>$lock_time){ Yii::$app->redis_php->del($key);
$is_lock = Yii::$app->redis_php->setnx($key, time()+$expire); }else{ return $this->BingFa($mobile, $ent_id); } } //如果锁成功 if($is_lock){ $obj->mobile = $mobile; if($obj->save()){ Yii::$app->redis_php->del($key); return $obj; } } } }
学习的热情不因季节的变化而改变