当前位置:编程学习 > > 正文

phpredis使用场景(php和redis实现秒杀活动的流程)

时间:2022-01-16 00:05:49类别:编程学习

phpredis使用场景

php和redis实现秒杀活动的流程

1 说明

前段时间面试的时候,一直被问到如何设计一个秒杀活动,但是无奈没有此方面的实际经验,所以只好凭着自己的理解和一些资料去设计这么一个程序

主要利用到了redis的string和set,string主要是利用它的k-v结构去对库存进行处理,也可以用list的数据结构来处理商品的库存,set则用来确保用户进行重复的提交

其中我们最主要解决的问题是

-防止并发产生超抢/超卖

2 流程设计

phpredis使用场景(php和redis实现秒杀活动的流程)

3 代码

3.1 服务端代码

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • class miaosha{
  •  
  •  const msg_repeat_user = '请勿重复参与';
  •  const msg_empty_stock = '库存不足';
  •  const msg_key_not_exist = 'key不存在';
  •  
  •  const ip_pool = 'ip_pool';
  •  const user_pool = 'user_pool';
  •  
  •  /** @var redis */
  •  public $redis;
  •  public $key;
  •  
  •  public function __construct($key = '')
  •  {
  •   $this->checkkey($key);
  •   $this->redis = new redis(); //todo 连接池
  •   $this->redis->connect('127.0.0.1');
  •  }
  •  
  •  public function checkkey($key = '')
  •  {
  •   if(!$key) {
  •    throw new exception(self::msg_key_not_exist);
  •   } else {
  •    $this->key = $key;
  •   }
  •  }
  •  
  •  public function setstock($value = 0)
  •  {
  •   if($this->redis->exists($this->key) == 0) {
  •    $this->redis->set($this->key,$value);
  •   }
  •  }
  •  
  •  public function checkip($ip = 0)
  •  {
  •   $skey = $this->key . self::ip_pool;
  •   if(!$ip || $this->redis->sismember($skey,$ip)) {
  •    throw new exception(self::msg_repeat_user);
  •   }
  •  }
  •  
  •  public function checkuser($user = 0)
  •  {
  •   $skey = $this->key . self::user_pool;
  •   if(!$user || $this->redis->sismember($skey,$user)) {
  •    throw new exception(self::msg_repeat_user);
  •   }
  •  }
  •  
  •  public function checkstock($user = 0, $ip = 0)
  •  {
  •   $num = $this->redis->decr($this->key);
  •   if($num < 0 ) {
  •    throw new exception(self::msg_empty_stock);
  •   } else {
  •    $this->redis->sadd($this->key . self::user_pool, $user);
  •    $this->redis->sadd($this->key . self::ip_pool, $ip);
  •    //todo add to mysql
  •    echo 'success' . php_eol;
  •    error_log('success' . $user . php_eol,3,'/var/www/html/demo/log/debug.log');
  •   }
  •  }
  •  
  •  /**
  •   * @note:此种做法不能防止并发
  •   * @func checkstockfail
  •   * @param int $user
  •   * @param int $ip
  •   * @throws exception
  •   */
  •  public function checkstockfail($user = 0,$ip = 0) {
  •   $num = $this->redis->get($this->key);
  •   if($num > 0 ){
  •    $this->redis->sadd($this->key . self::user_pool, $user);
  •    $this->redis->sadd($this->key . self::ip_pool, $ip);
  •    //todo add to mysql
  •    echo 'success' . php_eol;
  •    error_log('success' . $user . php_eol,3,'/var/www/html/demo/log/debug.log');
  •    $num--;
  •    $this->redis->set($this->key,$num);
  •   } else {
  •    throw new exception(self::msg_empty_stock);
  •   }
  •  }
  • }
  • 3.2 客户端测试代码

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • function test()
  • {
  •  try{
  •   $key = 'cup_';
  •   $handler = new miaosha($key);
  •   $handler->setstock(10);
  •   $user = rand(1,10000);
  •   $ip = $user;
  •   $handler->checkip($ip);
  •   $handler->checkuser($user);
  •   $handler->checkstock($user,$ip);
  •  } catch (\exception $e) {
  •   echo $e->getmessage() . php_eol;
  •   error_log('fail' . $e->getmessage() .php_eol,3,'/var/www/html/demo/log/debug.log');
  •  }
  • }
  •  
  • function test2()
  • {
  •  try{
  •   $key = 'cup_';
  •   $handler = new miaosha($key);
  •   $handler->setstock(10);
  •   $user = rand(1,10000);
  •   $ip = $user;
  •   $handler->checkip($ip);
  •   $handler->checkuser($user);
  •   $handler->checkstockfail($user,$ip); //不能防止并发的
  •  } catch (\exception $e) {
  •   echo $e->getmessage() . php_eol;
  •   error_log('fail' . $e->getmessage() .php_eol,3,'/var/www/html/demo/log/debug.log');
  •  }
  • }
  • 4 测试

    测试环境说明

    在服务端代码里面我们有两个函数分别是checkstock和checkstockfail,其中checkstockfail不能在高并发的情况下效果很差,不能在redis层面保证库存为0的时候终止操作。

    我们利用ab工具进行测试

    其中 是配置的虚拟主机名称 flash-sale.php 是我们脚本的名称

  • ?
  • 1
  • 2
  • #第1种情况 500并发下 用客户端的test2()去执行
  •  ab -n 500 -c 100 www.hello.com/flash-sale.php
  • log日志的记录结果:

    phpredis使用场景(php和redis实现秒杀活动的流程)

  • ?
  • 1
  • 2
  • #第2种情况 5000并发下 用客户端的test2()去执行
  •  ab -n 5000 -c 1000 www.hello.com/flash-sale.php
  • log日志的记录结果:

    phpredis使用场景(php和redis实现秒杀活动的流程)

  • ?
  • 1
  • 2
  • #第3种情况 500并发下 用客户端的test()去执行
  •  ab -n 500 -c 100 www.hello.com/flash-sale.php
  • log日志的记录结果:

    phpredis使用场景(php和redis实现秒杀活动的流程)

  • ?
  • 1
  • 2
  • #第4种情况 5000并发下 用客户端的test()去执行
  •  ab -n 5000 -c 1000 www.hello.com/flash-sale.php
  • log日志的记录结果:

    phpredis使用场景(php和redis实现秒杀活动的流程)

    5 总结

    我们从日志中可以很明显的看出第3、4中情况下,可以保证商品的数量总是我们设置的库存值10,但是在情况1、2下,则产生了超卖的现象

    redis来控制并发主要是利用了其api都是原子性操作的优势,从checkstock和checkstockfail中可以看出,一个是直接decr对库存进行减一操作,所以不存在并发的情况,但是另一个方法是将库存值先取出做减一操作然后再重新赋值,这样的话,在并发下,多个进程会读取到多个库存为1的值,因此会产生超卖的情况

    以上所述是小编给大家介绍的php和redis实现秒杀活动的流程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!

    如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

    原文链接:https://segmentfault.com/a/1190000019778733

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐