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; } } } }

 

posted @ 2018-04-22 23:25  岁月静好13543  阅读(8563)  评论(0编辑  收藏  举报
AmazingCounters.com