Commit 1fa42ede by 刘旋尧

update

parent 8a049003
......@@ -6,29 +6,23 @@ use Predis;
class Lock
{
public $addr = '';
public $timeout = 200;
private $client;
private $randomStr;
private $timeout = 200;
const LOCK_PREFIX = "LOCK_";
public function __construct($addr = '')
{
if ($addr) {
$this->addr = $addr;
}
if ($this->addr) {
$this->init();
}
}
const LOCK_PREFIX = "LOCK_";
public function init()
public function __construct($addr)
{
parent::init();
$this->client = new Predis\Client($this->addr);
$this->client = new Predis\Client($addr);
$this->randomStr = $this->randomStr();
}
/**
* 加锁
*
* @param [type] $key
* @return boolean
*/
public function lock($key)
{
$rs = $this->client->set(self::LOCK_PREFIX . $key, $this->randomStr, 'NX', 'PX', $this->timeout);
......@@ -37,6 +31,12 @@ class Lock
}
/**
* 解锁
*
* @param [type] $key
* @return void
*/
public function unLock($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