tp5以上直接复制粘贴即可, 需要修改命名空间
快速使用 (new RedisLock ())->lock($key, $expire = 5, $num = 0) 返回true代表加锁成功
,可以执行后面代码,否则失败; 支持重试$num次, 加锁成功后$expire秒后锁自动释放, 防止死锁 ;
(new RedisLock ())->unlock($key) 解锁;
<?php
namespace 你的命名空间;
use think\Cache;
class RedisLock {
private $_redis;
public function __construct() {
$handler = Cache::handler();
return $this->_redis = $handler->handler();
}
/**
* 获取锁
* @param String $key 锁标识
* @param Int $expire 锁过期时间
* @param Int $num 重试次数
* @return Boolean
*/
public function lock($key, $expire = 5, $num = 0){
$is_lock = $this->_redis->setnx($key, time()+$expire);
if(!$is_lock) {
//获取锁失败则重试{$num}次
for($i = 0; $i < $num; $i++){
$is_lock = $this->_redis->setnx($key, time()+$expire);
if($is_lock){
break;
}
sleep(1);
}
}
// 不能获取锁
if(!$is_lock){
// 判断锁是否过期
$lock_time = $this->_redis->get($key);
// 锁已过期,删除锁,重新获取
if(time()>$lock_time){
$this->unlock($key);
$is_lock = $this->_redis->setnx($key, time()+$expire);
}
}
return $is_lock? true : false;
}
/**
* 释放锁
* @param String $key 锁标识
* @return Boolean
*/
public function unlock($key){
return $this->_redis->del($key);
}
}