redis实战
pessmistic-lock.php
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
<?php
/**
* redis实战
*
* 实现悲观锁机制
*
* @author <https://github.com/wuxiumu>
*/
$timeout = 5000;
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
do {
$microtime = microtime(true) * 1000;
$microtimeout = $microtime+$timeout+1;
// 上锁
$isLock = $redis->setnx('lock.count', $microtimeout);
if (!$isLock) {
$getTime = $redis->get('lock.count');
if ($getTime > $microtime) {
// 睡眠 降低抢锁频率 缓解redis压力
usleep(5000);
// 未超时继续等待
continue;
}
// 超时,抢锁,可能有几毫秒级时间差可忽略
$previousTime = $redis->getset('lock.count', $microtimeout);
if ((int)$previousTime < $microtime) {
break;
}
}
} while (!$isLock);
$count = $redis->get('count')? : 0;
// file_put_contents('/var/log/count.log.1', ($count+1));
// 业务逻辑
echo "执行count加1操作~ \n\n";
$redis->set('count', $count+1);
// 删除锁
$redis->del('lock.count');
// 打印count值
$count = $redis->get('count');
echo "count值为:$count \n";