@@ -8,20 +8,20 @@ discard block |
||
8 | 8 | */ |
9 | 9 | class LocalAutoLock |
10 | 10 | { |
11 | - static function lock($key, $seconds, callable $success, callable $error=null){ |
|
11 | + static function lock($key, $seconds, callable $success, callable $error = null) { |
|
12 | 12 | $key = 'lock:'.$key; |
13 | - if(function_exists('apc_add')){ |
|
13 | + if (function_exists('apc_add')) { |
|
14 | 14 | $lock = new ApcLock(); |
15 | - }else{ |
|
15 | + }else { |
|
16 | 16 | $lock = new FileLock(); |
17 | 17 | } |
18 | - try{ |
|
19 | - if(!isset(self::$currentLock[$key])){ |
|
18 | + try { |
|
19 | + if (!isset(self::$currentLock[$key])) { |
|
20 | 20 | self::$currentLock[$key] = 0; |
21 | 21 | } |
22 | - if(self::$currentLock[$key] == 0){ //未加锁 |
|
23 | - if(!$lock->lock($key, $seconds)){ //加锁失败 |
|
24 | - if($error){ |
|
22 | + if (self::$currentLock[$key] == 0) { //未加锁 |
|
23 | + if (!$lock->lock($key, $seconds)) { //加锁失败 |
|
24 | + if ($error) { |
|
25 | 25 | return $error(); |
26 | 26 | } |
27 | 27 | return; |
@@ -29,37 +29,37 @@ discard block |
||
29 | 29 | } |
30 | 30 | //嵌套加锁 |
31 | 31 | self::$currentLock[$key]++; |
32 | - }catch (\Exception $e){ |
|
33 | - if($error){ |
|
32 | + }catch (\Exception $e) { |
|
33 | + if ($error) { |
|
34 | 34 | return $error(); |
35 | 35 | } |
36 | 36 | return; |
37 | 37 | } |
38 | 38 | $res = null; |
39 | - try{ |
|
39 | + try { |
|
40 | 40 | $res = $success(); |
41 | - }catch (\Exception $e){ |
|
41 | + }catch (\Exception $e) { |
|
42 | 42 | self::$currentLock[$key]--; |
43 | - if(self::$currentLock[$key] == 0){ |
|
44 | - try{ |
|
43 | + if (self::$currentLock[$key] == 0) { |
|
44 | + try { |
|
45 | 45 | $lock->unlock($key); |
46 | - }catch (\Exception $e){ |
|
46 | + }catch (\Exception $e) { |
|
47 | 47 | |
48 | 48 | } |
49 | 49 | } |
50 | 50 | throw $e; |
51 | 51 | } |
52 | 52 | self::$currentLock[$key]--; |
53 | - if(self::$currentLock[$key] == 0){ |
|
54 | - try{ |
|
53 | + if (self::$currentLock[$key] == 0) { |
|
54 | + try { |
|
55 | 55 | $lock->unlock($key); |
56 | - }catch (\Exception $e){ |
|
56 | + }catch (\Exception $e) { |
|
57 | 57 | |
58 | 58 | } |
59 | 59 | } |
60 | 60 | return $res; |
61 | 61 | } |
62 | 62 | |
63 | - private $cache=[]; |
|
64 | - static private $currentLock=[]; |
|
63 | + private $cache = []; |
|
64 | + static private $currentLock = []; |
|
65 | 65 | } |
66 | 66 | \ No newline at end of file |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | $key = 'lock:'.$key; |
13 | 13 | if(function_exists('apc_add')){ |
14 | 14 | $lock = new ApcLock(); |
15 | - }else{ |
|
15 | + } else{ |
|
16 | 16 | $lock = new FileLock(); |
17 | 17 | } |
18 | 18 | try{ |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | } |
30 | 30 | //嵌套加锁 |
31 | 31 | self::$currentLock[$key]++; |
32 | - }catch (\Exception $e){ |
|
32 | + } catch (\Exception $e){ |
|
33 | 33 | if($error){ |
34 | 34 | return $error(); |
35 | 35 | } |
@@ -38,12 +38,12 @@ discard block |
||
38 | 38 | $res = null; |
39 | 39 | try{ |
40 | 40 | $res = $success(); |
41 | - }catch (\Exception $e){ |
|
41 | + } catch (\Exception $e){ |
|
42 | 42 | self::$currentLock[$key]--; |
43 | 43 | if(self::$currentLock[$key] == 0){ |
44 | 44 | try{ |
45 | 45 | $lock->unlock($key); |
46 | - }catch (\Exception $e){ |
|
46 | + } catch (\Exception $e){ |
|
47 | 47 | |
48 | 48 | } |
49 | 49 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | if(self::$currentLock[$key] == 0){ |
54 | 54 | try{ |
55 | 55 | $lock->unlock($key); |
56 | - }catch (\Exception $e){ |
|
56 | + } catch (\Exception $e){ |
|
57 | 57 | |
58 | 58 | } |
59 | 59 | } |
@@ -9,10 +9,10 @@ |
||
9 | 9 | public function lock($key, $ttl) |
10 | 10 | { |
11 | 11 | $path = sys_get_temp_dir().'/lock_252a8fdc9b944af99a9bc53d2aea08f1/'.$key; |
12 | - if(SafeFileWriter::write($path, json_encode(['time'=>time(), 'ttl'=>$ttl]), false)){ |
|
12 | + if (SafeFileWriter::write($path, json_encode(['time'=>time(), 'ttl'=>$ttl]), false)) { |
|
13 | 13 | $this->locked = true; |
14 | 14 | return true; |
15 | - }else{ |
|
15 | + }else { |
|
16 | 16 | return false; |
17 | 17 | } |
18 | 18 | } |
@@ -12,7 +12,7 @@ |
||
12 | 12 | if(SafeFileWriter::write($path, json_encode(['time'=>time(), 'ttl'=>$ttl]), false)){ |
13 | 13 | $this->locked = true; |
14 | 14 | return true; |
15 | - }else{ |
|
15 | + } else{ |
|
16 | 16 | return false; |
17 | 17 | } |
18 | 18 | } |
@@ -16,15 +16,15 @@ |
||
16 | 16 | * @param boolean $overwrite 是否覆盖已有文件 |
17 | 17 | * @return boolean |
18 | 18 | */ |
19 | - static public function write($path, $data, $overwrite = true){ |
|
19 | + static public function write($path, $data, $overwrite = true) { |
|
20 | 20 | $path = str_replace('\\', '/', $path); |
21 | 21 | $fileDir = dirname($path); |
22 | 22 | $tmpFile = @tempnam($fileDir); |
23 | 23 | false !== @file_put_contents($tmpFile, $data) or \PhpBoot\abort("write to file: $tmpFile failed"); |
24 | - if($overwrite){ |
|
24 | + if ($overwrite) { |
|
25 | 25 | @unlink($path); //删除原始文件 |
26 | 26 | } |
27 | - if(!@rename($tmpFile, $path)){ |
|
27 | + if (!@rename($tmpFile, $path)) { |
|
28 | 28 | @unlink($tmpFile); //删除原始文件 |
29 | 29 | \PhpBoot\abort("write to file: $tmpFile failed"); |
30 | 30 | return false; |