Skip to content

Commit

Permalink
修改错误单词&简化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xsplus authored and TIGERB committed Mar 4, 2020
1 parent d9d231f commit 15ae336
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 48 deletions.
38 changes: 38 additions & 0 deletions redis/pessimistic-lock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* redis实战
*
* 实现悲观锁机制
*
* @author TIGERB <https://github.com/TIGERB>
* @example php pessimistic-lock.php
*/

$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);

$timeout = 3;
$lockKey = 'lock.count';
do {
// 上锁
$isLock = $redis->set($lockKey, '1', ['nx', 'ex' => $timeout]); // 避免死锁,设置 $timeout
if (!$isLock) {
// 睡眠 降低抢锁频率 缓解redis压力
echo "资源繁忙,正在重试...". microtime(true) . PHP_EOL;
usleep(5000);
} else {
break;
}
} while (!$isLock);

$key = 'count';
// 执行业务逻辑
echo "执行count加1操作~" . PHP_EOL;
$redis->incr($key);

// 删除锁
$redis->del($lockKey);

// 打印 count 值
$count = $redis->get($key);
echo "count值为:{$count} " . PHP_EOL;
48 changes: 0 additions & 48 deletions redis/pessmistic-lock.php

This file was deleted.

0 comments on commit 15ae336

Please sign in to comment.