使用redis防止商品超發


  • redis不僅僅是單純的緩存,它還有一些特殊的功能,在一些特殊場景上很好用。redis中key的原子自增incrby和判斷key不存在再寫入的setnx方法,可以有效的防止超發。
  • 下面使用兩個不同的方式來說明利用redis做商品購買庫存數量限制。
  • 業務場景很簡單,就是限制搶購5個商品,模擬並發請求搶購商品,每搶購一次對應redis中的key值增加一次,通過判斷限購的數量來限制搶購,搶購成功寫入成功日志,失敗寫入失敗的信息記錄,通過記錄的數量來判斷是否超發。

文件index.php

<?php
require_once './myRedis.php';
require_once './function.php';

class sendAward{
	public $conf = [];
	const V1 = 'way1';//版本一
	const V2 = 'way2';//版本二
	const AMOUNTLIMIT = 5;//搶購數量限制
	const INCRAMOUNT = 1;//redis遞增數量值
	
	//初始化調用對應方法執行商品發放
	public function __construct($conf,$type){
		$this->conf = $conf;
		if(empty($type))
			return '';
		if($type==self::V1){
			$this->way1(self::V1);
		}elseif($type==self::V2){
			$this->way2(self::V2);
		}else{
			return '';
		}
	}
	
	//搶購商品方式一
	protected  function way1($v){
		$redis = new myRedis($this->conf);		
		$keyNmae = getKeyName($v);
		if(!$redis->exists($keyNmae)){
			$redis->set($keyNmae,0);
		}
		$currAmount = $redis->get($keyNmae);
		if(($currAmount+self::INCRAMOUNT)>self::AMOUNTLIMIT){
			writeLog("沒有搶到商品",$v);
			return;
		}
		$redis->incrby($keyNmae,self::INCRAMOUNT);
		writeLog("搶到商品",$v);
	}
	
	//搶購商品方式二
	protected function way2($v){
		$redis = new myRedis($this->conf);
		$keyNmae = getKeyName($v);
		if(!$redis->exists($keyNmae)){
			$redis->setnx($keyNmae,0);
		}
		if($redis->incrby($keyNmae,self::INCRAMOUNT) > self::AMOUNTLIMIT){
			writeLog("沒有搶到商品",$v);
			return;
		}
		writeLog("搶到商品",$v);
	}
			
}

//實例化調用對應執行方法
$type = isset($_GET['v'])?$_GET['v']:'way1';
$conf = [
	'host'=>'192.168.0.214','port'=>'6379',
	'auth'=>'test','db'=>2,
];
new sendAward($conf,$type);


文件myRedis.php

<?php
/**
 * @desc 自定義redis操作類
 * **/
class myRedis{
	public $handler = NULL;
	public function __construct($conf){
		$this->handler = new Redis();
		$this->handler->connect($conf['host'], $conf['port']); //連接Redis
		//設置密碼
		if(isset($conf['auth'])){
			$this->handler->auth($conf['auth']); //密碼驗證
		}
		//選擇數據庫
		if(isset($conf['db'])){
			$this->handler->select($conf['db']);//選擇數據庫2
		}else{
			$this->handler->select(0);//默認選擇0庫
		}
	}

	//獲取key的值
	public function get($name){
		return $this->handler->get($name);
	}
	
	//設置key的值
	public function set($name,$value){
		return $this->handler->set($name,$value);
	}

	//判斷key是否存在
	public function exists($key){
		if($this->handler->exists($key)){
			return true;
		}
		return false;
	}

	//當key不存在的設置key的值,存在則不設置
	public function setnx($key,$value){
		return $this->handler->setnx($key,$value);
	}

	//將key的數值增加指定數值
	public function incrby($key,$value){
		return $this->handler->incrBy($key,$value);
	}
	
}


文件function.php


<?php
//獲取商品key名稱
function getKeyName($v)
{
	return "send_goods_".$v;
}

//日志寫入方法
function writeLog($msg,$v)
{
	$log = $msg.PHP_EOL;
	file_put_contents("log/$v.log",$log,FILE_APPEND);
}

1.ab工具並發測試way1方法


[root@localhost oversend]# ab -c 100 -n 200 http://192.168.0.213:8083/index.php?v=way1
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.0.213 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:        nginx
Server Hostname:        192.168.0.213
Server Port:            8083

Document Path:          /index.php?v=way1
Document Length:        0 bytes

Concurrency Level:      100
Time taken for tests:   0.089 seconds
Complete requests:      200
Failed requests:        0
Write errors:           0
Total transferred:      30600 bytes
HTML transferred:       0 bytes
Requests per second:    2243.13 [#/sec] (mean)
Time per request:       44.581 [ms] (mean)
Time per request:       0.446 [ms] (mean, across all concurrent requests)
Transfer rate:          335.16 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6   2.2      5      17
Processing:     2   28  16.3     25      55
Waiting:        1   26  15.2     24      50
Total:          5   34  16.3     30      60

Percentage of the requests served within a certain time (ms)
  50%     30
  66%     35
  75%     54
  80%     56
  90%     57
  95%     60
  98%     60
  99%     60
 100%     60 (longest request)

v1方法日志分析


[root@localhost log]# less -N way1.log  
      1 搶到商品
      2 搶到商品
      3 搶到商品
      4 搶到商品
      5 搶到商品
      6 搶到商品
      7 沒有搶到商品
      8 沒有搶到商品
      9 沒有搶到商品
     10 沒有搶到商品
     11 沒有搶到商品
     12 沒有搶到商品

觀察日志發現 搶到商品的記錄有6條超過正常的5條,說明超發了

2.ab工具並發測試way2方法

[root@localhost oversend]# ab -c 100 -n 200 http://192.168.0.213:8083/index.php?v=way2
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.0.213 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:        nginx
Server Hostname:        192.168.0.213
Server Port:            8083

Document Path:          /index.php?v=way2
Document Length:        0 bytes

Concurrency Level:      100
Time taken for tests:   0.087 seconds
Complete requests:      200
Failed requests:        0
Write errors:           0
Total transferred:      31059 bytes
HTML transferred:       0 bytes
Requests per second:    2311.68 [#/sec] (mean)
Time per request:       43.259 [ms] (mean)
Time per request:       0.433 [ms] (mean, across all concurrent requests)
Transfer rate:          350.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6   5.4      5      13
Processing:     3   31  16.6     30      70
Waiting:        1   30  16.6     30      70
Total:          5   37  18.5     32      82

Percentage of the requests served within a certain time (ms)
  50%     32
  66%     41
  75%     45
  80%     50
  90%     68
  95%     80
  98%     81
  99%     82
 100%     82 (longest request)

v2方法日志分析

[root@localhost log]# less -N v2.log  
[root@localhost log]# less -N way2.log  
      1 搶到商品
      2 搶到商品
      3 搶到商品
      4 搶到商品
      5 沒有搶到商品
      6 搶到商品
      7 沒有搶到商品
      8 沒有搶到商品
      9 沒有搶到商品
     10 沒有搶到商品

總結:觀察日志可知搶到商品的日志記錄是5條並沒有超發,說明利用這種方式可以限制住庫存的數量。之所以超發是因為方法一中通過加法來判斷限制條件的同時,並發一大,就會越過這個判斷條件出現會超發,redis的在這方面就體現優勢了。

完整代碼github地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM