Home > Backend Development > PHP Tutorial > Redis classic case sharing product flash sale and shopping cart

Redis classic case sharing product flash sale and shopping cart

巴扎黑
Release: 2023-03-15 09:46:01
Original
1976 people have browsed it


Summary: Product flash sale case

Second sale case:

<?php  
    header("content-type:text/html;charset=utf-8");  
    $redis = new redis();  
    $result = $redis->connect('10.10.10.119', 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 "抢购成功!<br/>";  
            echo "剩余数量:".($rob_total-$mywatchkey-1)."<br/>";  
            echo "用户列表:<pre class="brush:php;toolbar:false">";  
            var_dump($mywatchlist);  
        }else{  
            echo "手气不好,再抢购!";exit;  
        }  
    }
Copy after login

According to this principle, we can watch to write the shopping cart of the product, and watch the flag of the product before paying the order (changes when the quantity of the product is equal to 0 ), the payment fails if the flag changes.


The above is the detailed content of Redis classic case sharing product flash sale and shopping cart. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template