Using Redis queues, because the pop operation is atomic, even if a lot of users arrive at the same time, it is recommended to do it in turn (MySQL transaction degrades very much in high concurrency, and file locks are also the way)
Inventory items first as a queue
[PHP]View PlainCopy
- <?php
- $store = 1000;
- $redis =new Redis ();
- $result =$redis->connect (' 127.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 ');
- ?>
Snapping, describing logic
[PHP]View PlainCopy
- <?php
- $conn =mysql_connect ("localhost","big","123456");
- if (! $conn) {
- echo "Connect failed";
- exit;
- }
- mysql_select_db ("Big",$conn);
- mysql_query ("Set names UTF8");
- $price = 10;
- $user _id=1;
- $goods _id=1;
- $sku _id=11;
- $number = 1;
- Generate unique order number
- function Build_order_no () {
- return date (' Ymd '). substr (Implode (NULL, array_map (' ord ', str_split (substr (), 7, 13), 1)), 0, 8);
- }
- Record log
- function Insertlog ($event,$type =0) {
- Global $conn;
- $sql = "INSERT into Ih_log (event,type)
- VALUES (' $event ',' $type ');
- mysql_query ($sql,$conn);
- }
- Simulate the order operation
- Determine the Redis queue inventory before you order
- $redis =new Redis ();
- $result =$redis->connect (' 127.0.0.1 ', 6379);
- $count =$redis->lpop (' Goods_store ');
- if (! $count) {
- Insertlog (' error:no store Redis ');
- return;
- }
- Generate order
- $order _sn=build_order_no ();
- $sql = "INSERT into Ih_order (Order_sn,user_id,goods_id,sku_id,price)
- VALUES (' $order _sn ',' $user _id ',' $goods _id ', '$sku _id ',' $price ');
- $order _rs=mysql_query ($sql,$conn);
- Inventory reduction
- $sql ="Update ih_store set number=number-{$number} where sku_id= ' $sku _id '";
- $store _rs=mysql_query ($sql,$conn);
- if (Mysql_affected_rows ()) {
- Insertlog (' Inventory reduction Success ');
- }else{
- Insertlog (' inventory reduction failed ');
- }
-
- --
- --database: ' Big '
- --
- -- --------------------------------------------------------
- --
- --The structure of the table ' Ih_goods '
- --
- CREATE TABLE IF not EXISTS ' Ih_goods ' (
- ' goods_id ' int (ten) unsigned not NULL auto_increment,
- ' cat_id ' int (one) is not NULL,
- ' Goods_name ' varchar (255) is not NULL,
- PRIMARY KEY (' goods_id ')
- ) Engine=myisam DEFAULT Charset=utf8 auto_increment=2;
- --
- --Dump the data in the table ' Ih_goods '
- --
- INSERT into ' ih_goods ' (' goods_id ', ' cat_id ', ' goods_name ') VALUES
- (1, 0, ' Xiaomi phone ');
- -- --------------------------------------------------------
- --
- --The structure of the table ' Ih_log '
- --
- CREATE TABLE IF not EXISTS ' Ih_log ' (
- ' id ' int (one) not NULL auto_increment,
- ' Event ' varchar (255) Not NULL,
- ' Type ' tinyint (4) not NULL DEFAULT ' 0 ',
- ' Addtime ' timestamp not NULL DEFAULT Current_timestamp,
- PRIMARY KEY (' id ')
- ) Engine=myisam DEFAULT Charset=utf8 auto_increment=1;
- --
- --Dump the data in the table ' Ih_log '
- --
- -- --------------------------------------------------------
- --
- --The structure of the table ' Ih_order '
- --
- CREATE TABLE IF not EXISTS ' Ih_order ' (
- ' id ' int (one) not NULL auto_increment,
- ' Order_sn ' char (+) is not NULL,
- ' user_id ' int (one) is not NULL,
- ' Status ' int (one) not NULL DEFAULT ' 0 ',
- ' goods_id ' int (one) not NULL DEFAULT ' 0 ',
- ' sku_id ' int (one) not NULL DEFAULT ' 0 ',
- ' Price ' float is not NULL,
- ' Addtime ' timestamp not NULL DEFAULT Current_timestamp,
- PRIMARY KEY (' id ')
- ) Engine=innodb DEFAULT Charset=utf8 comment=' order form ' auto_increment=1;
- --
- --Dump the data in the table ' Ih_order '
- --
- -- --------------------------------------------------------
- --
- --The structure of the table ' Ih_store '
- --
- CREATE TABLE IF not EXISTS ' Ih_store ' (
- ' id ' int (one) not NULL auto_increment,
- ' goods_id ' int (one) is not NULL,
- ' sku_id ' int (ten) unsigned not NULL DEFAULT ' 0 ',
- ' Number ' int (ten) is not NULL DEFAULT ' 0 ',
- ' Freez ' int (one) not NULL DEFAULT ' 0 ' COMMENT ' virtual inventory ',
- PRIMARY KEY (' id ')
- ) Engine=innodb DEFAULT Charset=utf8 comment=' inventory ' auto_increment=2;
- --
- --Dump the data in the table ' Ih_store '
- --
- INSERT into ' ih_store ' (' id ', ' goods_id ', ' sku_id ', ' number ', ' Freez ') VALUES
- (1, 1, 11, 500, 0);
PHP uses Redis for second-kill