使用redis乐观锁实现秒杀

header('content-type:text/html;charset=utf-8');
$redis = new Redis();
$result = $redis->open('192.168.1.221', 6379);

//$redis -> set('s_kill_number', 10);

//var_dump($redis -> set( 'b' , 123 ));
//var_dump( $redis -> get('b') );
//var_dump( $result );exit;
//echo "
"; 
  
echo "页面被访问了" . $redis->incr('ci'). '
';
echo '总共抢到了' . $redis->lLen('s_kill') . '
';
echo '剩余数量' . $redis->get('s_kill_number'). '
';

$redis -> watch('s_kill_number');

$num_ber = $redis -> get('s_kill_number');
// echo $num_ber;die;
$redis -> multi();

//print_r( $redis->lGetRange('s_kill',0 , $redis ->lLen('s_kill')));
//var_dump($num_ber);exit;

if ( $num_ber > 0) {
sleep(2);
$redis->decr('s_kill_number');
$redis->lPush('s_kill', rand(1000, 9999));
$redis -> exec();
exit('OK'. '
');
} else {
$redis -> exec();
exit('抢光了'. '
');

}


原链接:http://blog.csdn.net/u013697959/article/details/77477763

你可能感兴趣的:(Redis)