利用 Redis 鎖解決高並發問題


這里我們主要利用 Redis 的 setnx 的命令來處理高並發。

 

setnx 有兩個參數。第一個參數表示鍵。第二個參數表示值。如果當前鍵不存在,那么會插入當前鍵,將第二個參數做為值。返回 1。如果當前鍵存在,那么會返回 0 。

 

創建庫存表

CREATE TABLE `storage` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `number` int(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

 

設置初始庫存為10

 

創建訂單表

CREATE TABLE `order` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `number` int(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

測試不用鎖的時候

$pdo = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root');$sql="select `number` from storage where id=1 limit 1";$res = $pdo->query($sql)->fetch();$number = $res['number'];if($number>0){ $sql ="insert into `order` VALUES (null,$number)";
$order_id = $pdo->query($sql); if($order_id) {
$sql="update storage set `number`=`number`-1 WHERE id=1"; $pdo->query($sql); }}

 

ab 測試模擬並發,發現庫存是正確的。

mysql> select * from storage;+----+--------+| id | number |+----+--------+| 1 | 0 |+----+--------+1 row in set (0.00 sec)

在來看訂單表

mysql> select * from `order`;+----+--------+| id | number |+----+--------+| 1 | 10 || 2 | 10 || 3 | 9 || 4 | 7 || 5 | 6 || 6 | 5 || 7 | 5 || 8 | 5 || 9 | 4 || 10 | 1 |+----+--------+10 rows in set (0.00 sec)

發現存在幾個訂單都是操作的同一個庫存數據,這樣就可能引起超賣的情況。

 

修改代碼加入 redis 鎖進行數據控制

<?php/** * Created by PhpStorm. * User: daisc * Date: 2018/7/23 * Time: 14:45 */class Lock{ private static $_instance ; private $_redis; private function __construct(){ $this->_redis = new Redis(); $this->_redis ->connect('127.0.0.1'); } public static function getInstance(){ if(self::$_instance instanceof self) { return self::$_instance; } return self::$_instance = new self(); }
/** * @function 加鎖 * @param $key 鎖名稱 * @param $expTime 過期時間 */ public function set($key,$expTime){ //初步加鎖 $isLock = $this->_redis->setnx($key,time()+$expTime); if($isLock) { return true; } else { //加鎖失敗的情況下。判斷鎖是否已經存在,如果鎖存在切已經過期,那么刪除鎖。進行重新加鎖 $val = $this->_redis->get($key); if($val&&$val<time()) { $this->del($key); return $this->_redis->setnx($key,time()+$expTime); } return false; } }

/** * @param $key 解鎖 */ public function del($key){ $this->_redis->del($key); }
}


$pdo = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root');$lockObj = Lock::getInstance();//判斷是能加鎖成功if($lock = $lockObj->set('storage',10)){ $sql="select `number` from storage where id=1 limit 1"; $res = $pdo->query($sql)->fetch(); $number = $res['number']; if($number>0) { $sql ="insert into `order` VALUES (null,$number)";
$order_id = $pdo->query($sql); if($order_id) {
$sql="update storage set `number`=`number`-1 WHERE id=1"; $pdo->query($sql); } } //解鎖 $lockObj->del('storage');
}else{ //加鎖不成功執行其他操作。}

 

再次進行 ab 測試,查看測試結果

mysql> select * from `order`;+----+--------+| id | number |+----+--------+| 1 | 10 || 2 | 9 || 3 | 8 || 4 | 7 || 5 | 6 || 6 | 5 || 7 | 4 || 8 | 3 || 9 | 2 || 10 | 1 |+----+--------+10 rows in set (0.00 sec)

發現訂單表沒有操作同一個庫存數據的情況。所以利用 redis 鎖是可以有效的處理高並發的。

 

這里在加鎖的時候其實是可以不需要判斷過期時間的,這里我們為了避免造成死鎖,所以加一個過期時間的判斷。當過期的時候主動刪除該鎖。

出處:http://u6.gg/s2mCt


免責聲明!

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



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