<?php
header("content-type:text/html;charset=utf-8");
$redis = new redis();
$result = $redis->connect('IP', PORT);
$mywatchkey = $redis->get("mywatchkey");
$rob_total = 100; //搶購數(shù)量
if($mywatchkey<$rob_total){
$redis->watch("mywatchkey");
$redis->multi();
//設(shè)置延遲墩崩,方便測試效果氓英。
sleep(5);
//插入搶購數(shù)據(jù)
$redis->hSet("mywatchlist","user_id_".mt_rand(1, 9999),time());
$redis->set("mywatchkey",$mywatchkey+1);
$rob_result = $redis->exec();
if($rob_result){
$mywatchlist = $redis->hGetAll("mywatchlist");
echo "搶購成功!<br/>";
echo "剩余數(shù)量:".($rob_total-$mywatchkey-1)."<br/>";
echo "用戶列表:<pre>";
var_dump($mywatchlist);
}else{
echo "手氣不好鹦筹,再搶購铝阐!";exit;
}
}
?>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者