php 数据库文件锁

public function record_add($data){
        $p_file = $data['goods_id'].$data['money']."temp.lock";
         
        $fp = fopen($p_file, 'a+');
        // 2. 判断是否已存在文件锁,存在(上锁失败)则退出
        if (!flock($fp, LOCK_EX + LOCK_NB)) {
            exit;
        }
        // 3. 上锁
        flock($fp, LOCK_EX + LOCK_NB);
        $where=[
            ['goods_id','eq',$data['goods_id']],
            // ['money','>=',$data['money']]
        ];
        $a=\app\common\model\AuctionRecord::where($where)->order('money desc')->find();

        if($a['money']>=$data['money']){
            exception('当前已有人出价更高');
        }

        if($data['userid'] == $a['userid']){
            exception('您当前已是最高价');
        }

        $whee=[
            ['userid','eq',$data['userid']],
            ['is_default','eq',1]
        ];

        $wh=[
            ['goods_id','eq',$data['goods_id']],
        ];
        \app\common\model\AuctionRecord::where($wh)->update(['is_delete'=>1]);

        $ret=\app\common\model\AuctionRecord::create($data);


        // 6. 解锁文件
        flock($fp, LOCK_UN);
        // 7. 关闭文件
        fclose($fp);
        return $ret;
    }

 

你可能感兴趣的:(php 数据库文件锁)