30 lines
681 B
PHP
30 lines
681 B
PHP
<?php
|
|
declare (strict_types=1);
|
|
namespace app\common\lock;
|
|
use Predis\Client;
|
|
/**
|
|
* 订单服务类
|
|
* Class RedisLock
|
|
* @package app\common\lock
|
|
*/
|
|
class RedisLock
|
|
{
|
|
protected $redis;
|
|
protected $lockKey;
|
|
protected $timeout;
|
|
|
|
public function __construct($lockKey, $timeout = 30) {
|
|
$this->redis = new Client(config('redis'));
|
|
$this->lockKey = $lockKey;
|
|
$this->timeout = $timeout;
|
|
}
|
|
|
|
public function acquireLock() {
|
|
$result = $this->redis->set($this->lockKey, 'locked', 'NX', 'EX', $this->timeout);
|
|
return $result == 'OK';
|
|
}
|
|
|
|
public function releaseLock() {
|
|
$this->redis->del($this->lockKey);
|
|
}
|
|
} |