Skip to content

Commit 86709ab

Browse files
author
caoym
committed
fix LocalAutoLock
1 parent c752e60 commit 86709ab

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/Lock/FileLock.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,33 @@
55

66
class FileLock implements LockInterface
77
{
8-
8+
private $file;
99
public function lock($key, $ttl)
1010
{
11+
if($this->locked){
12+
\PhpBoot\abort("relock $key");
13+
}
1114
$path = sys_get_temp_dir().'/lock_252a8fdc9b944af99a9bc53d2aea08f1_'.$key;
12-
if(SafeFileWriter::write($path, json_encode(['time'=>time(), 'ttl'=>$ttl]), false)){
13-
$this->locked = true;
14-
return true;
15-
}else{
15+
$this->file = @fopen($path, 'a');
16+
if (!$this->file || !flock($this->file, LOCK_EX | LOCK_NB)) {
17+
if($this->file){
18+
fclose($this->file);
19+
}
1620
return false;
21+
} else {
22+
$this->locked = true;
1723
}
24+
return true;
1825
}
1926

2027
public function unlock($key)
2128
{
2229
$this->locked or \PhpBoot\abort("unlock unlocked $key");
23-
$path = sys_get_temp_dir().'/lock_252a8fdc9b944af99a9bc53d2aea08f1_'.$key;
24-
$res = @unlink($path);
30+
flock($this->file, LOCK_UN);
31+
fclose($this->file);
32+
$this->file = null;
2533
$this->locked = false;
26-
return $res;
34+
return true;
2735
}
2836
private $locked = false;
2937
}

0 commit comments

Comments
 (0)