Recently, I was engaged in a second kill activity, considering the performance and response speed, using redis. When writing, I paid special attention to the prevention of supersending. the cas (checkandset) optimistic lock based on redis theory should be able to prevent this problem, but it still appeared, i'm confused. please help me ,... recently, I was engaged in a second kill activity, considering the performance and response speed, using redis. When writing, I paid special attention to the prevention of supersending. the cas (check and set) optimistic lock based on redis theory should be able to prevent this problem, but it still appeared, I am confused. please help me. the specific code is roughly as follows:
Connect ('10. 10.10.119 ', 6379); $ mywatchkey = $ redis-> get ("mywatchkey"); $ rob_total = 100; // quantity of purchases if ($ mywatchkey <$ rob_total) {$ redis-> watch ("mywatchkey"); $ redis-> multi (); // sets the latency to facilitate testing. Sleep (5); // Insert the snapped up data $ 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 "snapped up!
"; Echo" remaining quantity: ". ($ rob_total-$ mywatchkey-1 )."
"; Echo" user list:"; Var_dump ($ mywatchlist);} else {echo" bad luck, buy again! "; Exit ;}}?>