<?phpheader (' Content-type:text/html;charset=utf-8 ');
echo Time ();
Class Seckillredis {static protected $validity _time = 30;//valid for 5 minutes
protected $goods _id;
protected $user _queue_key;
protected $goods _number_key;
protected $user _id;
Public function __construct ($goods _id, $area _id, $uid) {
if ($goods _id) {
$this->goods_id= $goods _id;
User status for current commodity queue
$this->user_queue_key= "Goods_". $goods _id. " _user ";
Inventory queue for the current item
$this->goods_number_key= "Goods_". $goods _id;
}
$this->user_id= $uid? $uid: 2;
}
/* Redis link */static public Function Getredis () {
$redis =new Redis ();
$redis->connect (' 127.0.0.1 ', ' 6379 ') or Die (' Can not Content Redis ');
if ($redis) {
return $redis;
}else{
Die (' Can not Content redis! ');
}
}//Initialize queue when adding or changing inventory
Public Function Kucun ($num) {
$redis = $this->getredis ();
$redis->delete ($this->goods_number_key);
$redis->delete ($this->user_queue_key);
for ($i =0; $i < $num; $i + +) {$redis->rpush ($this->goods_number_key, 1); }
}//To determine if the unpaid order has expired, update the number of seconds to kill the product entry public function Poling_set_seckill_redis () {
$redis = $this->getredis ();
exists check if key exists, there is a return of 1, there is no return 0,0 also belongs to the non-existent
if ($redis->exists ($this->user_queue_key) = = True) {//Purge expired usage amount $use _list = $redis->lrange ($this->user_qu Eue_key, 0,-1);
Var_dump ($use _list);
foreach ($use _list as $k + $v) {$data = Json_decode ($v, true); if (Time ()-$data [' time '] > Self:: $validity _time) {//Expiration date deleted $this->returnfree ($k, $data [' uid '], $data [' Num ' ]); } } } }
/* Get free snapping--num: Number of purchases */Public Function Getfree ($num) {if (Empty ($this->user_id)) {return array (' result ' = False, ' Message ' = ' snap message: User ID cannot be null '; } $redis = Self::getredis (); if ($redis->llen ($this->goods_number_key) >= $num) {
for ($i =0; $i < $num; $i + +) {
$result = $redis->lpop ($this->goods_number_key);
}
$result = $redis->lrem ($this->goods_number_key, ' 1 ', $num);
Var_dump ($redis->lrange ($seckill _array[$type] [' Free_key '],0,-1)];
if ($result = = true) {//Add use quantity $index = $redis->rpush ($this->user_queue_key, Json_encode (' uid ' = = $th is->user_id, ' time ' = Time (), ' num ' = $num))-1; return array (' result ' = ' = ', ' index ' = $index); }else{return Array (' result ' = False, ' message ' = = ' snapped message: was robbed '); }
}else{
return array (' result ' = False, ' message ' = ' snapping up information: stock not enough ');
}
}/* Returns idle snapping-$index as key value */Public Function Returnfree ($index, $uid, $num) {$redis = Self::getredis (); $value = $redis->lget ($this->user_queue_key, $index);
Var_dump ($value. ' HH ');
if (!empty ($value)) {$redis->lrem ($this->user_queue_key, $value, 1); Add Idle Quantity
for ($i =0; $i < $num; $i + +) {
$redis->rpush ($this->goods_number_key, 1);
}
$sql statement, stock plus 1 return array (' result ' = true); }else{return Array (' result ' = False, ' message ' = = ' snapped message: No index exists '); } }}
Class index{/* A controller method that needs to control concurrency */public Function GetOrderInfo () {$miaosha =new Seckillredis (' 3 ', ' 123 ', ' 2 ');
$miaosha->kucun (' 100 ');
$miaosha->poling_set_seckill_redis ();
$result = $miaosha->getfree (' 1 ');
return $result;
if ($result [' result '] = = False) {//no chance to return error message ' network busy, please try again '; }//Process database code
}
} $hua = new Index;
Var_dump ($hua->getorderinfo ()). ' <br> ';
$redis =seckillredis::getredis (); Echo $redis->llen (' Goods_3 ');
Var_dump ($redis->lrange (' Goods_3 ', 0,-1));
Var_dump ($redis->lrange (' Goods_3_user ', 0,-1));
PHP Mall seconds kill, can buy multiple items, Redis anti-high concurrency