初始化项目
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user