laravel redis乐观锁

 public function secKill(){
        $data=DB::table('product')->find('1');
        $data=json_decode(json_encode($data,true),true);
        if($data['store']>0){
            sleep(1);
            $res=DB::table('product')->where('id',1)->decrement('store',1);
            if($res){
                echo '更新库存成功';
            }
            return;
        }
        echo '没有库存';
    }
 public function watchRedis(){
        Redis::watch('count');//监视这个key
//        Redis::set('count',10);
        $count=Redis::get('count');//获取销售量
//        var_dump($count);die;
//        Redis::del('count');die;
        $num=2; //模拟库存
        if($count>=$num){
            exit('活动结束');
        }
        Redis::multi();
        Redis::set('count',$count+1);
        sleep(1);//模拟业务逻辑
        $res=Redis::exec();//按命令先后顺序执行,监视的数据如果修改了,就执行命令
        if($res){
            $data=DB::table('products')->find('1');
            $data=json_decode(json_encode($data,true),true);
            if($data['store']>0){
                sleep(1);
                $res=DB::table('products')->where('id',1)->decrement('store',1);
                if($res){
                    echo '更新库存成功';
                }
                return;
            }
            echo '没有库存';
        }
    }

排他锁 共享锁

start TRANSACTION

SELECT * FROM `products`  where id=1 FOR UPDATE

SELECT * FROM `products`  where id=1 LOCK in SHARE MODE

UPDATE `product` set price = 55 WHERE id =1

delete from `product` WHERE id =1

COMMIT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值