简单秒杀
<?php
header("content-type:text/html;charset=utf-8");
$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
$watchkey = $redis->get("watchkey");
$rob_total = 10;
if($watchkey<$rob_total){
$redis->watch("watchkey");
$redis->multi();
$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 "已被抢购完!";
}