PHP 使用redis实现秒杀

时间:2023-03-09 18:11:27
PHP 使用redis实现秒杀

PHP 使用redis实现秒杀

使用redis队列,因为pop操作是原子的,即使有很多用户同时到达,也是依次执行,推荐使用(mysql事务在高并发下性能下降很厉害,文件锁的方式也是)

先将商品库存如队列

  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. ?>

抢购、描述逻辑

    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. //生成唯一订单号
    15. function build_order_no(){
    16. return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    17. }
    18. //记录日志
    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. //模拟下单操作
    26. //下单前判断redis队列库存量
    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. //生成订单
    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. //库存减少
    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('库存减少成功');
    44. }else{
    45. insertLog('库存减少失败');
    46. }   
        1. --
        2. -- 数据库: `big`
        3. --
        4. -- --------------------------------------------------------
        5. --
        6. -- 表的结构 `ih_goods`
        7. --
        8. CREATE TABLE IF NOT EXISTS `ih_goods` (
        9. `goods_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
        10. `cat_id` int(11) NOT NULL,
        11. `goods_name` varchar(255) NOT NULL,
        12. PRIMARY KEY (`goods_id`)
        13. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
        14. --
        15. -- 转存表中的数据 `ih_goods`
        16. --
        17. INSERT INTO `ih_goods` (`goods_id`, `cat_id`, `goods_name`) VALUES
        18. (1, 0, '小米手机');
        19. -- --------------------------------------------------------
        20. --
        21. -- 表的结构 `ih_log`
        22. --
        23. CREATE TABLE IF NOT EXISTS `ih_log` (
        24. `id` int(11) 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. -- 转存表中的数据 `ih_log`
        32. --
        33. -- --------------------------------------------------------
        34. --
        35. -- 表的结构 `ih_order`
        36. --
        37. CREATE TABLE IF NOT EXISTS `ih_order` (
        38. `id` int(11) NOT NULL AUTO_INCREMENT,
        39. `order_sn` char(32) NOT NULL,
        40. `user_id` int(11) NOT NULL,
        41. `status` int(11) NOT NULL DEFAULT '0',
        42. `goods_id` int(11) NOT NULL DEFAULT '0',
        43. `sku_id` int(11) NOT NULL DEFAULT '0',
        44. `price` float NOT NULL,
        45. `addtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
        46. PRIMARY KEY (`id`)
        47. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='订单表' AUTO_INCREMENT=1 ;
        48. --
        49. -- 转存表中的数据 `ih_order`
        50. --
        51. -- --------------------------------------------------------
        52. --
        53. -- 表的结构 `ih_store`
        54. --
        55. CREATE TABLE IF NOT EXISTS `ih_store` (
        56. `id` int(11) NOT NULL AUTO_INCREMENT,
        57. `goods_id` int(11) NOT NULL,
        58. `sku_id` int(10) unsigned NOT NULL DEFAULT '0',
        59. `number` int(10) NOT NULL DEFAULT '0',
        60. `freez` int(11) NOT NULL DEFAULT '0' COMMENT '虚拟库存',
        61. PRIMARY KEY (`id`)
        62. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='库存' AUTO_INCREMENT=2 ;
        63. --
        64. -- 转存表中的数据 `ih_store`
        65. --
        66. INSERT INTO `ih_store` (`id`, `goods_id`, `sku_id`, `number`, `freez`) VALUES
        67. (1, 1, 11, 500, 0);