Commit 1fa42ede by 刘旋尧

update

parent 8a049003
...@@ -6,29 +6,23 @@ use Predis; ...@@ -6,29 +6,23 @@ use Predis;
class Lock class Lock
{ {
public $addr = ''; public $timeout = 200;
private $client; private $client;
private $randomStr; private $randomStr;
private $timeout = 200;
const LOCK_PREFIX = "LOCK_";
public function __construct($addr = '') const LOCK_PREFIX = "LOCK_";
{
if ($addr) {
$this->addr = $addr;
}
if ($this->addr) {
$this->init();
}
}
public function init() public function __construct($addr)
{ {
parent::init(); $this->client = new Predis\Client($addr);
$this->client = new Predis\Client($this->addr);
$this->randomStr = $this->randomStr(); $this->randomStr = $this->randomStr();
} }
/**
* 加锁
*
* @param [type] $key
* @return boolean
*/
public function lock($key) public function lock($key)
{ {
$rs = $this->client->set(self::LOCK_PREFIX . $key, $this->randomStr, 'NX', 'PX', $this->timeout); $rs = $this->client->set(self::LOCK_PREFIX . $key, $this->randomStr, 'NX', 'PX', $this->timeout);
...@@ -37,6 +31,12 @@ class Lock ...@@ -37,6 +31,12 @@ class Lock
} }
/**
* 解锁
*
* @param [type] $key
* @return void
*/
public function unLock($key) public function unLock($key)
{ {
$val = $this->client->get(self::LOCK_PREFIX . $key); $val = $this->client->get(self::LOCK_PREFIX . $key);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment