原子操作、Hashset 不知道这样写能扛得住多少,我没测过 1. [代码] [PHP]代码 ?php$redis = new Redis();$redis-connect('127.0.0.1', 6379);$UserID = (int) $_GET['UserID'];$ActivityID = (int) $_GET['ActivityID'];// 库存检
不知道这样写 能扛得住多少,我没测过
1. [代码][PHP]代码
<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $UserID = (int) $_GET['UserID']; $ActivityID = (int) $_GET['ActivityID']; // 库存检查 if ($redis->decr('Amount') < 0) { $redis->incr('Amount'); echo '{ "exception" : "InvalidNumberException" }'; exit; } // 直接入库 $NoRepeat = $redis->hSet('UserList', $UserID, sprintf('{ "ActivityID" : %d, "Milliseconds" : "%.0f" }', $ActivityID, round(microtime(true) * 1000)) ); if (!$NoRepeat) { // 还原库存 $redis->incr('Amount'); echo '{ "exception" : "SaveInvalidException" }'; exit; } echo sprintf('{ "errno" : 0 , "data" : { "UserID" : %d, "Milliseconds": %.0f } }', $UserID, round(microtime(true) * 1000)); exit;