$redis = new redis();$result = $redis->connect('127.0.0.1',6379);$getnum = $redis->get('getnum');//已抢数量$num = 100;//抢购总数量if($getnum < $num){ $redis->watch('getnum');//监听key $redis->multi();//开缘由务 //插入抢购数据 $redis->hSet(34;userList",'user_id_'.mt_rand(1,999),time()); $redis->set("getnum",$getnum+1);//抢购到+1 $robResult = $redis->exec();//实行事务 if ($robResult) { echo "抢购成功!
<br/>"; echo "剩余数量:" . ($num - ($redis->get("getnum"))) . "<br/>"; echo "用户列表:<pre>"; var_dump($redis->hGetAll("userList")); } else { echo "手气不好,再抢购!
"; exit; }}else{ echo "已售罄"; exit;}