秒杀案例:
header("content-type:text/html;charset=utf-8");
$redis = new redis();
$result = $redis->connect('216.83.54.83', 6379);
$mywatchkey = $redis->get("mywatchkey");
$rob_total = 100; //抢购数量
if($mywatchkey<$rob_total){
$redis->watch("mywatchkey");
$redis->multi();
//设置延迟,方便测试效果。
sleep(5);
//插入抢购数据
$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 "抢购成功!";
echo "剩余数量:".($rob_total-$mywatchkey-1);
echo "用户列表:";
var_dump($mywatchlist);
}
else{
echo "手气不好,再抢购!";exit;
}
}
根据这个原理,我们可以watch 来写商品的购物车,支付订单之前watch 商品的标志位(商品数量等于0的时候变化),标志位变化则支付失败。
未经允许不得转载:任鹏个人博客 » Redis案例:商品秒杀,购物车
最新评论
Forex wiki. https://lt.forex-stock-bitcoin-brokers.com
Magnificent items from you, man. I have take note your stuff
Following on from the 3rd March Meetings held by economic de
It is remarkable, rather valuable message dfgdlfg2131.32
一般都会有一个沙盒期的,过了沙盒期就会慢慢放出来
百度不收录是应为是新站的原因吗?
The spike in consumer prices that left inflation at a four-d