Second-kill code
Public function redis () {$ store = 100; $ redis = new \ Redis (); $ result = $ redis-> connect ('2017. 0.0.1 ', 6379); $ res = $ redis-> llen ('goods _ store'); echo $ res; $ count = $ store-$ res; for ($ I = 0; $ I <$ count; $ I ++) {$ redis-> lpush ('goods _ store', 1 );} echo $ redis-> llen ('goods _ store');} public function go () {$ redis = new \ Redis (); $ result = $ redis-> connect ('2017. 0.0.1 ', 6379); $ count = $ redis-> lpop ('goods _ store'); if (! $ Count) {$ this-> insertLog ('error: no store redis'); echo "seckilling ends"; exit ;} // inventory reduction $ is_true = M ("Store", "ih _")-> where ('Id = 1 and number> 0 ') -> setDec ('number'); if ($ is_true) {$ this-> insertLog ('inventory reduction successfully '. I ("user_id"), 1); echo "seckilling successful";} else {$ this-> insertLog ('inventory reduction failed '. I ("user_id"), 2); echo "second kill failed" ;}} public function insertLog ($ event, $ type = 0) {$ model = M ("Log ", "ih _"); $ data ["event"] = $ event; $ data ["type"] = $ type; $ model-> add ($ data );}