diff --git a/.php_cs b/.php_cs deleted file mode 100644 index c784750..0000000 --- a/.php_cs +++ /dev/null @@ -1,56 +0,0 @@ -files() - ->name('*.php') - ->exclude('Fixtures') - ->in(__DIR__.'/src') - ->in(__DIR__.'/tests') -; - -return Symfony\CS\Config::create() - ->setUsingCache(true) - //->setUsingLinter(false) - ->setRiskyAllowed(true) - ->setRules(array( - '@PSR2' => true, - 'binary_operator_spaces' => true, - 'blank_line_before_return' => true, - 'header_comment' => array('header' => $header), - 'include' => true, - 'long_array_syntax' => true, - 'method_separation' => true, - 'no_blank_lines_after_class_opening' => true, - 'no_blank_lines_after_phpdoc' => true, - 'no_blank_lines_between_uses' => true, - 'no_duplicate_semicolons' => true, - 'no_extra_consecutive_blank_lines' => true, - 'no_leading_import_slash' => true, - 'no_leading_namespace_whitespace' => true, - 'no_trailing_comma_in_singleline_array' => true, - 'no_unused_imports' => true, - 'object_operator_without_whitespace' => true, - 'phpdoc_align' => true, - 'phpdoc_indent' => true, - 'phpdoc_no_access' => true, - 'phpdoc_no_package' => true, - 'phpdoc_order' => true, - 'phpdoc_scalar' => true, - 'phpdoc_trim' => true, - 'phpdoc_type_to_var' => true, - 'psr0' => true, - 'single_blank_line_before_namespace' => true, - 'spaces_cast' => true, - 'standardize_not_equals' => true, - 'ternary_operator_spaces' => true, - 'trailing_comma_in_multiline_array' => true, - 'whitespacy_lines' => true, - )) - ->finder($finder) -; diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..6a4a60b --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,52 @@ +setRiskyAllowed(true) + ->setRules([ + '@PSR2' => true, + '@Symfony' => true, + 'array_syntax' => ['syntax' => 'short'], + 'combine_consecutive_unsets' => true, + // one should use PHPUnit methods to set up expected exception instead of annotations + 'general_phpdoc_annotation_remove' => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'], + 'header_comment' => array('header' => $header), + 'heredoc_to_nowdoc' => true, + 'no_extra_consecutive_blank_lines' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'], + 'no_unreachable_default_argument_value' => true, + 'no_useless_else' => true, + 'no_useless_return' => true, + 'ordered_class_elements' => true, + 'ordered_imports' => true, + 'php_unit_strict' => true, + 'phpdoc_add_missing_param_annotation' => true, + 'no_trailing_comma_in_singleline_array' => true, //单行数组最后一个元素不添加逗号 + 'phpdoc_order' => true, + 'psr4' => true, + 'strict_comparison' => false, + 'strict_param' => false, //这里设置为true,发现in_array方法会默认加上第3个参数为true,这使得in_array会对前两个参数值的类型也会做严格的校验,建议设置为false + 'binary_operator_spaces' => ['default' => 'align_single_space_minimal'], + //'binary_operator_spaces' => true, + 'concat_space' => ['spacing' => 'one'], + 'no_empty_statement' => true, + 'simplified_null_return' => true, + 'no_extra_consecutive_blank_lines' => true, + 'pre_increment' => false, //设置为false,$i++ 不会变成 ++$i + 'native_function_invocation' => false, //in_array不会加前缀\ + 'cast_spaces' => ['space' => 'single'], + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->name('*.php') + ->exclude('extend') + ->exclude('vendor') + ->exclude('FormBase') + ->in(__DIR__) + ) + ->setUsingCache(false); \ No newline at end of file diff --git a/README.md b/README.md index 8c4f1d8..028299d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,17 @@ # redisLock -### +使用redis实现分布式锁 +### Installation -### 关于代码格式 +待补充 + + +### 开发指南 + +###### 代码格式 +使用PHP-CS-Fixer格式化代码 +``` +php-cs-fixer fix . +``` +- 参考:https://github.com/FriendsOfPHP/PHP-CS-Fixer \ No newline at end of file diff --git a/composer.json b/composer.json index 5b01605..cd76e9b 100644 --- a/composer.json +++ b/composer.json @@ -21,5 +21,8 @@ "psr-4": { "tim1116\\redisLock\\": "src/" } + }, + "require-dev": { + "eaglewu/swoole-ide-helper": "dev-master" } } diff --git a/src/RedisLock.php b/src/RedisLock.php index ff6e1b8..de5e0b3 100644 --- a/src/RedisLock.php +++ b/src/RedisLock.php @@ -1,7 +1,9 @@ redisSetting = $setting; } - private function checkKey(string $key):bool - { - if (empty($key)) { - return false; - } - return true; - } - public function lock(string $key, int $expire) { if (!$this->checkKey($key)) { - throw new \InvalidArgumentException("key error,unexpected empty"); + throw new \InvalidArgumentException('key error,unexpected empty'); } if ($expire <= 0) { - throw new \InvalidArgumentException("expire time error"); + throw new \InvalidArgumentException('expire time error'); } $redis = $this->connect(); $key = Util::lockKey($key); @@ -45,6 +39,7 @@ public function lock(string $key, int $expire) if (!$isLocked) { return false; } + return true; } @@ -52,18 +47,19 @@ public function lock(string $key, int $expire) public function unLock(string $key) { if (!$this->checkKey($key)) { - throw new \InvalidArgumentException("key error,unexpected empty"); + throw new \InvalidArgumentException('key error,unexpected empty'); } $redis = $this->connect(); + return $redis->del(Util::lockKey($key)); } /** - * 连接redis + * 连接redis. * * @throws \RedisException */ - public function connect():\Redis + public function connect(): \Redis { $redis = new \Redis(); $redis->connect($this->redisSetting->host, $this->redisSetting->port, $this->redisSetting->timeout); @@ -73,10 +69,17 @@ public function connect():\Redis $redis->select($this->redisSetting->dbindex); if ($this->redisSetting->prefix) { $redis->setOption(\Redis::OPT_PREFIX, $this->redisSetting->prefix); - } + return $redis; } + private function checkKey(string $key): bool + { + if (empty($key)) { + return false; + } + return true; + } } diff --git a/src/RedisSetting.php b/src/RedisSetting.php index 721fb60..6200119 100644 --- a/src/RedisSetting.php +++ b/src/RedisSetting.php @@ -1,33 +1,34 @@ setConfig($config); } - - public function setConfig(Array $config) + public function setConfig(array $config) { if (isset($config['host'])) { $this->host = $config['host']; @@ -52,6 +53,5 @@ public function setConfig(Array $config) if (isset($config['prefix'])) { $this->prefix = $config['prefix']; } - } } diff --git a/src/Util.php b/src/Util.php index 7f1dbff..81bd84f 100644 --- a/src/Util.php +++ b/src/Util.php @@ -1,8 +1,13 @@ '192.168.199.101', 'port' => 6379, @@ -17,9 +18,5 @@ 'dbindex' => 6, ]; $obj = RedisLock::getInstance(new RedisSetting($config)); -//$isLock = $obj->lock("aaa",100); -//var_dump($isLock); - -echo "unlock"; -var_dump($obj->unLock("aaa")); +$isLock = $obj->lock('aaa', 100); var_dump($isLock); diff --git a/tests/test2.php b/tests/test2.php new file mode 100644 index 0000000..df97e89 --- /dev/null +++ b/tests/test2.php @@ -0,0 +1,39 @@ + '192.168.199.101', + 'port' => 6379, + 'password' => '123456', + 'dbindex' => 6, +]; + +$key = 'process'; + +// 多进程抢占锁资源 +for ($n = 1; $n <= 5; ++$n) { + $process = new Process(function () use ($n, $config, $key) { + $obj = RedisLock::getInstance(new RedisSetting($config)); + $isLock = $obj->lock($key, 2); + if ($isLock) { + echo $n . ' 进程抢占到锁'; + echo PHP_EOL; + } + }); + $process->start(); +} +for ($n = 5; --$n;) { + $status = Process::wait(true); + echo "Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}" . PHP_EOL; +}