簡單秒殺php
<?phphtml
header("content-type:text/html;charset=utf-8"); redis
$redis = new redis(); spa
$result = $redis->connect('127.0.0.1', 6379);htm
$watchkey = $redis->get("watchkey"); utf-8
$rob_total = 10;get
if($watchkey<$rob_total){ it
$redis->watch("watchkey"); 秒殺
$redis->multi(); im
$redis->hSet("watchlist","user_id_".mt_rand(1, 9999),time());
$redis->incr("watchkey");
$rob_result = $redis->exec();
if($rob_result){
$watchlist = $redis->hGetAll("watchlist");
echo "搶購成功!<br/>";
echo "剩餘數量:".($rob_total-$watchkey-1)."<br/>";
echo "用戶列表:<pre>";
var_dump($watchlist);
}else{
echo "手氣很差,再搶購!";
}
}else{
echo "已被搶購完!";
}