最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP+redis實(shí)現(xiàn)的限制搶購(gòu)防止商品超發(fā)功能詳解

 更新時(shí)間:2019年09月19日 09:24:55   作者:巴八靈  
這篇文章主要介紹了PHP+redis實(shí)現(xiàn)的限制搶購(gòu)防止商品超發(fā)功能,結(jié)合實(shí)例形式分析了PHP+redis通過(guò)數(shù)據(jù)標(biāo)記、日志記錄等操作防止商品搶購(gòu)中的超發(fā)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP+redis實(shí)現(xiàn)的限制搶購(gòu)防止商品超發(fā)功能。分享給大家供大家參考,具體如下:

  • redis不僅僅是單純的緩存,它還有一些特殊的功能,在一些特殊場(chǎng)景上很好用。redis中key的原子自增incrby和判斷key不存在再寫(xiě)入的setnx方法,可以有效的防止超發(fā)。
  • 下面使用兩個(gè)不同的方式來(lái)說(shuō)明利用redis做商品購(gòu)買(mǎi)庫(kù)存數(shù)量限制。
  • 業(yè)務(wù)場(chǎng)景很簡(jiǎn)單,就是限制搶購(gòu)5個(gè)商品,模擬并發(fā)請(qǐng)求搶購(gòu)商品,每搶購(gòu)一次對(duì)應(yīng)redis中的key值增加一次,通過(guò)判斷限購(gòu)的數(shù)量來(lái)限制搶購(gòu),搶購(gòu)成功寫(xiě)入成功日志,失敗寫(xiě)入失敗的信息記錄,通過(guò)記錄的數(shù)量來(lái)判斷是否超發(fā)。

文件index.php

<?php
require_once './myRedis.php';
require_once './function.php';
class sendAward{
  public $conf = [];
  const V1 = 'way1';//版本一
  const V2 = 'way2';//版本二
  const AMOUNTLIMIT = 5;//搶購(gòu)數(shù)量限制
  const INCRAMOUNT = 1;//redis遞增數(shù)量值
  //初始化調(diào)用對(duì)應(yīng)方法執(zhí)行商品發(fā)放
  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 '';
    }
  }
  //搶購(gòu)商品方式一
  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("沒(méi)有搶到商品",$v);
      return;
    }
    $redis->incrby($keyNmae,self::INCRAMOUNT);
    writeLog("搶到商品",$v);
  }
  //搶購(gòu)商品方式二
  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("沒(méi)有搶到商品",$v);
      return;
    }
    writeLog("搶到商品",$v);
  }
}
//實(shí)例化調(diào)用對(duì)應(yīng)執(zhí)行方法
$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
    //設(shè)置密碼
    if(isset($conf['auth'])){
      $this->handler->auth($conf['auth']); //密碼驗(yàn)證
    }
    //選擇數(shù)據(jù)庫(kù)
    if(isset($conf['db'])){
      $this->handler->select($conf['db']);//選擇數(shù)據(jù)庫(kù)2
    }else{
      $this->handler->select(0);//默認(rèn)選擇0庫(kù)
    }
  }
  //獲取key的值
  public function get($name){
    return $this->handler->get($name);
  }
  //設(shè)置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;
  }
  //當(dāng)key不存在的設(shè)置key的值,存在則不設(shè)置
  public function setnx($key,$value){
    return $this->handler->setnx($key,$value);
  }
  //將key的數(shù)值增加指定數(shù)值
  public function incrby($key,$value){
    return $this->handler->incrBy($key,$value);
  }
}

文件function.php

<?php
//獲取商品key名稱
function getKeyName($v)
{
  return "send_goods_".$v;
}
//日志寫(xiě)入方法
function writeLog($msg,$v)
{
  $log = $msg.PHP_EOL;
  file_put_contents("log/$v.log",$log,FILE_APPEND);
}

1.ab工具并發(fā)測(cè)試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 沒(méi)有搶到商品
   8 沒(méi)有搶到商品
   9 沒(méi)有搶到商品
   10 沒(méi)有搶到商品
   11 沒(méi)有搶到商品
   12 沒(méi)有搶到商品

觀察日志發(fā)現(xiàn) 搶到商品的記錄有6條超過(guò)正常的5條,說(shuō)明超發(fā)了

2.ab工具并發(fā)測(cè)試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 沒(méi)有搶到商品
   6 搶到商品
   7 沒(méi)有搶到商品
   8 沒(méi)有搶到商品
   9 沒(méi)有搶到商品
   10 沒(méi)有搶到商品

總結(jié):觀察日志可知搶到商品的日志記錄是5條并沒(méi)有超發(fā),說(shuō)明利用這種方式可以限制住庫(kù)存的數(shù)量。之所以超發(fā)是因?yàn)榉椒ㄒ恢型ㄟ^(guò)加法來(lái)判斷限制條件的同時(shí),并發(fā)一大,就會(huì)越過(guò)這個(gè)判斷條件出現(xiàn)會(huì)超發(fā),redis的在這方面就體現(xiàn)優(yōu)勢(shì)了。

完整代碼github地址

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 如何在舊的PHP系統(tǒng)中使用PHP 5.3之后的庫(kù)

    如何在舊的PHP系統(tǒng)中使用PHP 5.3之后的庫(kù)

    這篇文章主要介紹了如何在舊的PHP系統(tǒng)中使用PHP 5.3之后的庫(kù),需要的朋友可以參考下
    2015-12-12
  • PHP實(shí)現(xiàn)的memcache環(huán)形隊(duì)列類實(shí)例

    PHP實(shí)現(xiàn)的memcache環(huán)形隊(duì)列類實(shí)例

    這篇文章主要介紹了PHP實(shí)現(xiàn)的memcache環(huán)形隊(duì)列類,實(shí)例分析了基于memcache實(shí)現(xiàn)環(huán)形隊(duì)列的方法,涉及memcache緩存及隊(duì)列的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • php檢測(cè)圖片主要顏色的方法

    php檢測(cè)圖片主要顏色的方法

    這篇文章主要介紹了php檢測(cè)圖片主要顏色的方法,涉及php針對(duì)圖片的相關(guān)操作技巧,需要的朋友可以參考下
    2015-07-07
  • 通過(guò)php動(dòng)態(tài)傳數(shù)據(jù)到highcharts

    通過(guò)php動(dòng)態(tài)傳數(shù)據(jù)到highcharts

    本文主要介紹了通過(guò)php動(dòng)態(tài)傳數(shù)據(jù)到highcharts的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • mysql desc(DESCRIBE)命令實(shí)例講解

    mysql desc(DESCRIBE)命令實(shí)例講解

    這篇文章主要介紹了mysql desc(DESCRIBE)命令實(shí)例講解的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • PHP操作MySQL事務(wù)實(shí)例

    PHP操作MySQL事務(wù)實(shí)例

    這篇文章主要介紹了PHP操作MySQL事務(wù)的方法,以實(shí)例的形式較為詳細(xì)的分析了ACID特征,具有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • php利用scws實(shí)現(xiàn)mysql全文搜索功能的方法

    php利用scws實(shí)現(xiàn)mysql全文搜索功能的方法

    這篇文章主要介紹了php利用scws實(shí)現(xiàn)mysql全文搜索功能的方法,可通過(guò)scws分詞插件的擴(kuò)展來(lái)實(shí)現(xiàn)MySQL全文搜索功能,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • PHP實(shí)現(xiàn)找出鏈表中環(huán)的入口節(jié)點(diǎn)

    PHP實(shí)現(xiàn)找出鏈表中環(huán)的入口節(jié)點(diǎn)

    這篇文章主要介紹了PHP實(shí)現(xiàn)找出鏈表中環(huán)的入口節(jié)點(diǎn),涉及php針對(duì)環(huán)形鏈表的遍歷、查找、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • PHP實(shí)現(xiàn)會(huì)員賬號(hào)單唯一登錄的方法分析

    PHP實(shí)現(xiàn)會(huì)員賬號(hào)單唯一登錄的方法分析

    這篇文章主要介紹了PHP實(shí)現(xiàn)會(huì)員賬號(hào)單唯一登錄的方法,結(jié)合實(shí)例形式分析了php基于session與文件讀寫(xiě)的單一用戶登陸限制實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • 生成php程序的php代碼

    生成php程序的php代碼

    前臺(tái)程序不少情況下需要生成.php文件,諸如多用戶的在線日記程序、留言簿以及自助網(wǎng)站程序等等,都不可避免地在與用戶的交互中生成.php程序文件。一般的,所生成的.php文件內(nèi)容并不復(fù)雜,但麻雀雖小五臟俱全,完整的.php文件結(jié)構(gòu)必須得到保證。
    2008-04-04

最新評(píng)論

阿尔山市| 延边| 商南县| 松桃| 嘉善县| 汝城县| 临夏县| 呼伦贝尔市| 酒泉市| 康马县| 弥渡县| 湛江市| 阳朔县| 明光市| 台前县| 二连浩特市| 潜山县| 喀喇| 梅州市| 方正县| 阿拉善右旗| 南汇区| 顺平县| 凭祥市| 辽源市| 古蔺县| 绥宁县| 文化| 卢氏县| 藁城市| 洛南县| 来凤县| 乾安县| 无棣县| 托克逊县| 伊川县| 永胜县| 新竹市| 通许县| 临高县| 毕节市|