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