PHP uses Redis for second-kill

Source: Internet
Author: User
Tags vars

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
  1. <?php
  2. $store = 1000;
  3. $redis =new Redis ();
  4. $result =$redis->connect (' 127.0.0.1 ', 6379);
  5. $res =$redis->llen (' Goods_store ');
  6. echo $res;
  7. $count =$store-$res;
  8. For ($i =0; $i <$count; $i + +) {
  9. $redis->lpush (' Goods_store ', 1);
  10. }
  11. echo $redis->llen (' Goods_store ');
  12. ?>


Snapping, describing logic

[PHP]View PlainCopy
    1. <?php
    2. $conn =mysql_connect ("localhost","big","123456");
    3. if (! $conn) {
    4. echo "Connect failed";
    5. exit;
    6. }
    7. mysql_select_db ("Big",$conn);
    8. mysql_query ("Set names UTF8");
    9. $price = 10;
    10. $user _id=1;
    11. $goods _id=1;
    12. $sku _id=11;
    13. $number = 1;
    14. Generate unique order number
    15. function Build_order_no () {
    16. return date (' Ymd ').   substr (Implode (NULL, array_map (' ord ', str_split (substr (), 7, 13), 1)), 0, 8);
    17. }
    18. Record log
    19. function Insertlog ($event,$type =0) {
    20. Global $conn;
    21. $sql = "INSERT into Ih_log (event,type)
    22. VALUES (' $event ',' $type ');
    23. mysql_query ($sql,$conn);
    24. }
    25. Simulate the order operation
    26. Determine the Redis queue inventory before you order
    27. $redis =new Redis ();
    28. $result =$redis->connect (' 127.0.0.1 ', 6379);
    29. $count =$redis->lpop (' Goods_store ');
    30. if (! $count) {
    31. Insertlog (' error:no store Redis ');
    32. return;
    33. }
    34. Generate order
    35. $order _sn=build_order_no ();
    36. $sql = "INSERT into Ih_order (Order_sn,user_id,goods_id,sku_id,price)
    37. VALUES (' $order _sn ',' $user _id ',' $goods _id ', '$sku _id ',' $price ');
    38. $order _rs=mysql_query ($sql,$conn);
    39. Inventory reduction
    40. $sql ="Update ih_store set number=number-{$number} where sku_id= ' $sku _id '";
    41. $store _rs=mysql_query ($sql,$conn);
    42. if (Mysql_affected_rows ()) {
    43. Insertlog (' Inventory reduction Success ');
    44. }else{
    45. Insertlog (' inventory reduction failed ');
    46. }
        1. --
        2. --database: ' Big '
        3. --
        4. -- --------------------------------------------------------
        5. --
        6. --The structure of the table ' Ih_goods '
        7. --
        8. CREATE TABLE IF not EXISTS ' Ih_goods ' (
        9. ' goods_id ' int (ten) unsigned not NULL auto_increment,
        10. ' cat_id ' int (one) is not NULL,
        11. ' Goods_name ' varchar (255) is not NULL,
        12. PRIMARY KEY (' goods_id ')
        13. ) Engine=myisam DEFAULT Charset=utf8 auto_increment=2;
        14. --
        15. --Dump the data in the table ' Ih_goods '
        16. --
        17. INSERT into ' ih_goods ' (' goods_id ', ' cat_id ', ' goods_name ') VALUES
        18. (1, 0, ' Xiaomi phone ');
        19. -- --------------------------------------------------------
        20. --
        21. --The structure of the table ' Ih_log '
        22. --
        23. CREATE TABLE IF not EXISTS ' Ih_log ' (
        24. ' id ' int (one) not NULL auto_increment,
        25. ' Event ' varchar (255) Not NULL,
        26. ' Type ' tinyint (4) not NULL DEFAULT ' 0 ',
        27. ' Addtime ' timestamp not NULL DEFAULT Current_timestamp,
        28. PRIMARY KEY (' id ')
        29. ) Engine=myisam DEFAULT Charset=utf8 auto_increment=1;
        30. --
        31. --Dump the data in the table ' Ih_log '
        32. --
        33. -- --------------------------------------------------------
        34. --
        35. --The structure of the table ' Ih_order '
        36. --
        37. CREATE TABLE IF not EXISTS ' Ih_order ' (
        38. ' id ' int (one) not NULL auto_increment,
        39. ' Order_sn ' char (+) is not NULL,
        40. ' user_id ' int (one) is not NULL,
        41. ' Status ' int (one) not NULL DEFAULT ' 0 ',
        42. ' goods_id ' int (one) not NULL DEFAULT ' 0 ',
        43. ' sku_id ' int (one) not NULL DEFAULT ' 0 ',
        44. ' Price ' float is not NULL,
        45. ' Addtime ' timestamp not NULL DEFAULT Current_timestamp,
        46. PRIMARY KEY (' id ')
        47. ) Engine=innodb DEFAULT Charset=utf8 comment=' order form ' auto_increment=1;
        48. --
        49. --Dump the data in the table ' Ih_order '
        50. --
        51. -- --------------------------------------------------------
        52. --
        53. --The structure of the table ' Ih_store '
        54. --
        55. CREATE TABLE IF not EXISTS ' Ih_store ' (
        56. ' id ' int (one) not NULL auto_increment,
        57. ' goods_id ' int (one) is not NULL,
        58. ' sku_id ' int (ten) unsigned not NULL DEFAULT ' 0 ',
        59. ' Number ' int (ten) is not NULL DEFAULT ' 0 ',
        60. ' Freez ' int (one) not NULL DEFAULT ' 0 ' COMMENT ' virtual inventory ',
        61. PRIMARY KEY (' id ')
        62. ) Engine=innodb DEFAULT Charset=utf8 comment=' inventory ' auto_increment=2;
        63. --
        64. --Dump the data in the table ' Ih_store '
        65. --
        66. INSERT into ' ih_store ' (' id ', ' goods_id ', ' sku_id ', ' number ', ' Freez ') VALUES
        67. (1, 1, 11, 500, 0);

PHP uses Redis for second-kill

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.