Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

我的redis 的 watch 和 get 调换位置为啥会出错呢 ?

$redis = new 
edis();
$result = $redis->connect('127.0.0.1', 6379);

$redis->watch("mywatchkey");
$mywatchkey = $redis->get("mywatchkey");

$stock_total = 100; //抢购数量
if($mywatchkey < $stock_total) {
    $redis->multi();
    $redis->set("mywatchkey", 1 + $mywatchkey);
    $rob_result = $redis->exec();
    $redis->lPush('user_list', $user_id);
    if ($rob_result) {
        $user_id = 'user_id_' . rand(1, 999999);
      
        
        echo "抢购成功!<br/>";
        echo "剩余数量:".($rob_total-$mywatchkey-1)."<br/>";
        echo "用户列表:<pre>";
        var_dump($mywatchlist);
    } else {
        file_put_contents('./redis_fail_log', "{$user_id}
", FILE_APPEND);
    }
}

我用压力测试, 如果把 $mywatchkey = $redis>get("mywatchkey"); 放到 $redis->watch("mywatchkey"); 之前 , 最后 打印 user_list 的个数就会超出100 ,不知道为啥 。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...