lock-lock.php
redis=$redis;
}
public function lock($key,$retry=2,$timeout=1,$ex=10){
$ok=false;
$value=session_create_id();
while ( $retry-- ){
$ok=$this->redis->set($key,$value,[‘nx‘,‘ex‘=>$ex]);
if($ok){
$this->lock_id[$key]=$value;
break;
}
var_dump("等待获取锁");
sleep(1); //阻塞进程的
}
return $ok;
}
public function unlock($key){
//直接删除key了
if(isset($this->lock_id[$key])){
$lock=$this->lock_id[$key];
if($this->redis->get($key) == $lock){
return $this->redis->delete($key);
}
}
return false;
}
}