File tree Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Original file line number Diff line number Diff line change 5
5
6
6
class FileLock implements LockInterface
7
7
{
8
-
8
+ private $ file ;
9
9
public function lock ($ key , $ ttl )
10
10
{
11
+ if ($ this ->locked ){
12
+ \PhpBoot \abort ("relock $ key " );
13
+ }
11
14
$ 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
+ }
16
20
return false ;
21
+ } else {
22
+ $ this ->locked = true ;
17
23
}
24
+ return true ;
18
25
}
19
26
20
27
public function unlock ($ key )
21
28
{
22
29
$ 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 ;
25
33
$ this ->locked = false ;
26
- return $ res ;
34
+ return true ;
27
35
}
28
36
private $ locked = false ;
29
37
}
You can’t perform that action at this time.
0 commit comments