PHP redis 分布式锁 实现

<?php
/**
* redis 分布式锁
*/
class RedisLock{
private $config;
private $redis;
public function __construct($config = []){
$this->config = $config ? $config : ['host' => '127.0.0.1', 'port' => 6379];
$this->redis = $this->connect();
}
public function connect(){
$redis = new \Redis();
$redis->connect($this->config['host'], $this->config['port']);
return $redis;
}

/**
* @param $scene 锁场景
* @param $expire 锁有效期
* @return bool
*/
public function lock($scene = null , $expire = 10){
if (!$scene || !$expire){
return false;
}
// 生成随机值,锁标识
$lockId = md5(uniqid());
$result = $this->redis->set($scene, $lockId, ['NX', 'EX' => $expire]) ;

if($result)
return $lockId;
else
return $result;
}

/**
* 解锁
*/
public function unLock($scene, $lockId){

$lua = <<<SCRIPT
local key=KEYS[1]
local value=ARGV[1]
if(redis.call('get', key) == value)
then
return redis.call('del', key)
end
SCRIPT;
// 执行lua脚本
return $this->redis->eval($lua, [$scene, $lockId], 1);
}
}

//实现
$lock = new \Tests\Feature\RedisLock();
//第一次加锁
$res = $lock->Lock("test", 30);
var_dump($res); // 返回lockId
echo "<br>";
//第二次加锁
$res1 = $lock->Lock("test", 25);
var_dump($res1); //加锁失败 false
//解锁
if($res){
$lock->unLock("test", $res);
}


posted @ 2023-02-10 17:09  COCO歧  阅读(54)  评论(0编辑  收藏  举报