From 53e094af35e429e32b18b2791c5be10da04b0ebc Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Wed, 12 Dec 2018 23:51:42 +0100 Subject: [PATCH 01/15] Added missing compression - If phpredis is configured to compress key values, the EVAL script's arguments have to be compressed as well by this library. --- classes/mutex/PHPRedisMutex.php | 46 +++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index 0d0afb09..dff30361 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -2,10 +2,9 @@ namespace malkusch\lock\mutex; -use Redis; -use RedisException; use malkusch\lock\exception\LockAcquireException; use malkusch\lock\exception\LockReleaseException; +use RedisException; /** * Mutex based on the Redlock algorithm using the phpredis extension. @@ -26,9 +25,10 @@ class PHPRedisMutex extends RedisMutex * The Redis APIs needs to be connected yet. I.e. Redis::connect() was * called already. * - * @param Redis[] $redisAPIs The Redis connections. - * @param string $name The lock name. - * @param int $timeout The time in seconds a lock expires, default is 3. + * @param array<\Redis|\RedisCluster> $redisAPIs The Redis connections. + * @param string $name The lock name. + * @param int $timeout The time in seconds a lock expires after. + * Default is 3. * * @throws \LengthException The timeout must be greater than 0. */ @@ -38,11 +38,13 @@ public function __construct(array $redisAPIs, string $name, int $timeout = 3) } /** - * @throws LockAcquireException + * {@inheritDoc} + * + * @throws \malkusch\lock\exception\LockAcquireException */ protected function add($redisAPI, string $key, string $value, int $expire): bool { - /** @var Redis $redisAPI */ + /** @var \Redis $redisAPI */ try { // Will set the key, if it doesn't exist, with a ttl of $expire seconds return $redisAPI->set($key, $value, ["nx", "ex" => $expire]); @@ -55,18 +57,34 @@ protected function add($redisAPI, string $key, string $value, int $expire): bool } } + /** + * {@inheritDoc} + * + * @param \Redis|\RedisCluster $redis The Redis or RedisCluster connection. + */ protected function evalScript($redis, string $script, int $numkeys, array $arguments) { - /** @var Redis $redis */ + // Determine if we need to compress eval arguments. + $lzfCompression = false; + if (\defined(\Redis::class . '::COMPRESSION_LZF') && + \Redis::COMPRESSION_LZF === $redis->getOption(\Redis::OPT_COMPRESSION) && + \function_exists('\lzf_compress') + ) { + $lzfCompression = true; + } - /* - * If a serializion mode such as "php" or "igbinary" is enabled, the arguments must be serialized but the keys - * must not. - * - * @issue 14 - */ for ($i = $numkeys, $iMax = \count($arguments); $i < $iMax; $i++) { + /* If a serializion mode such as "php" or "igbinary" is enabled, the arguments must be + * serialized by us, because phpredis does not do this for the eval command. + */ $arguments[$i] = $redis->_serialize($arguments[$i]); + + /* If LZF compression is enabled for the redis connection and the runtime has the LZF + * extension installed, compress the arguments as the final step. + */ + if ($lzfCompression) { + $arguments[$i] = \lzf_compress($arguments[$i]); + } } try { From 82067d9fe1ff1a2b806e63effbeeb8be888da539 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Fri, 19 Apr 2019 22:24:48 +0200 Subject: [PATCH 02/15] Updated documentation of implementation with version hint. --- classes/mutex/PHPRedisMutex.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index dff30361..a8a8819b 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -9,7 +9,11 @@ /** * Mutex based on the Redlock algorithm using the phpredis extension. * - * This implementation requires at least phpredis-2.2.4. + * This implementation requires at least phpredis-2.2.4. If used together with + * the lzf extension, and phpredis is configured to use compression, at least + * phpredis-4.3.0 is required! For reason, see github issue link. + * + * @see https://github.com/phpredis/phpredis/issues/1477 * * @author Markus Malkusch * @license WTFPL From d43798c6eba11d5acb38cd3938f1f5070b3534cd Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Fri, 19 Apr 2019 22:28:24 +0200 Subject: [PATCH 03/15] Changed comment. --- classes/mutex/PHPRedisMutex.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index a8a8819b..e7530213 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -10,8 +10,8 @@ * Mutex based on the Redlock algorithm using the phpredis extension. * * This implementation requires at least phpredis-2.2.4. If used together with - * the lzf extension, and phpredis is configured to use compression, at least - * phpredis-4.3.0 is required! For reason, see github issue link. + * the lzf extension, and phpredis is configured to use lzf compression, at + * least phpredis-4.3.0 is required! For reason, see github issue link. * * @see https://github.com/phpredis/phpredis/issues/1477 * From f6d4e56702ad74414922933456394c7e0fb7511b Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:06:52 +0200 Subject: [PATCH 04/15] Added test for compression. --- classes/mutex/PHPRedisMutex.php | 11 ++--- composer.json | 4 +- tests/mutex/PHPRedisMutexTest.php | 68 +++++++++++++++++++++++-------- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index e7530213..72088078 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -4,6 +4,7 @@ use malkusch\lock\exception\LockAcquireException; use malkusch\lock\exception\LockReleaseException; +use Redis; use RedisException; /** @@ -70,14 +71,14 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum { // Determine if we need to compress eval arguments. $lzfCompression = false; - if (\defined(\Redis::class . '::COMPRESSION_LZF') && - \Redis::COMPRESSION_LZF === $redis->getOption(\Redis::OPT_COMPRESSION) && - \function_exists('\lzf_compress') + if (defined("Redis::COMPRESSION_LZF") && + constant("Redis::COMPRESSION_LZF") === $redis->getOption(Redis::OPT_COMPRESSION) && + function_exists('lzf_compress') ) { $lzfCompression = true; } - for ($i = $numkeys, $iMax = \count($arguments); $i < $iMax; $i++) { + for ($i = $numkeys, $iMax = count($arguments); $i < $iMax; $i++) { /* If a serializion mode such as "php" or "igbinary" is enabled, the arguments must be * serialized by us, because phpredis does not do this for the eval command. */ @@ -87,7 +88,7 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum * extension installed, compress the arguments as the final step. */ if ($lzfCompression) { - $arguments[$i] = \lzf_compress($arguments[$i]); + $arguments[$i] = lzf_compress($arguments[$i]); } } diff --git a/composer.json b/composer.json index 981f8069..02ff9711 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ "psr/log": "^1" }, "require-dev": { + "ext-lzf": "*", "ext-memcached": "*", "ext-pcntl": "*", "ext-pdo_mysql": "*", @@ -62,9 +63,10 @@ "squizlabs/php_codesniffer": "^3.3" }, "suggest": { + "ext-lzf": "To use this library with PHP Redis lzf compression enabled.", "ext-pnctl": "Enables locking with flock without busy waiting in CLI scripts.", - "ext-sysvsem": "Enables locking using semaphores.", "ext-redis": "To use this library with the PHP Redis extension.", + "ext-sysvsem": "Enables locking using semaphores.", "predis/predis": "To use this library with predis." }, "archive": { diff --git a/tests/mutex/PHPRedisMutexTest.php b/tests/mutex/PHPRedisMutexTest.php index eb7831ba..cab3f518 100644 --- a/tests/mutex/PHPRedisMutexTest.php +++ b/tests/mutex/PHPRedisMutexTest.php @@ -41,8 +41,7 @@ protected function setUp() $uri = parse_url($redisUri); // original Redis::set and Redis::eval calls will reopen the connection - $connection = new class extends Redis - { + $connection = new class extends Redis { private $is_closed = false; public function close() @@ -100,6 +99,9 @@ private function closeMinorityConnections() } $numberToClose = ceil(count($this->connections) / 2) - 1; + if (0 >= $numberToClose) { + return; + } foreach ((array) array_rand($this->connections, $numberToClose) as $keyToClose) { $this->connections[$keyToClose]->close(); @@ -132,27 +134,36 @@ public function testEvalScriptFails() } /** - * @param $serialization - * @dataProvider dpSerializationModes + * @dataProvider serializationAndCompressionModes + * + * @param array $serializer Serializer to test. + * @param array $compressor Compressor to test. */ - public function testSynchronizedWorks($serialization) + public function testSerializersAndCompressors($serializer, $compressor) { foreach ($this->connections as $connection) { - $connection->setOption(Redis::OPT_SERIALIZER, $serialization); + $connection->setOption(Redis::OPT_SERIALIZER, $serializer); + $connection->setOption(Redis::OPT_COMPRESSION, $compressor); } - $this->assertSame("test", $this->mutex->synchronized(function (): string { - return "test"; - })); + $this->assertSame( + "test", + $this->mutex->synchronized(function (): string { + return "test"; + }) + ); } public function testResistantToPartialClusterFailuresForAcquiringLock() { $this->closeMinorityConnections(); - $this->assertSame("test", $this->mutex->synchronized(function (): string { - return "test"; - })); + $this->assertSame( + "test", + $this->mutex->synchronized(function (): string { + return "test"; + }) + ); } public function testResistantToPartialClusterFailuresForReleasingLock() @@ -163,21 +174,42 @@ public function testResistantToPartialClusterFailuresForReleasingLock() })); } - public function dpSerializationModes() + public function serializationAndCompressionModes() { if (!class_exists(Redis::class)) { return []; } - $serializers = [ - [Redis::SERIALIZER_NONE], - [Redis::SERIALIZER_PHP], + $options = [ + [Redis::SERIALIZER_NONE, Redis::COMPRESSION_NONE], + [Redis::SERIALIZER_PHP, Redis::COMPRESSION_NONE], ]; if (defined("Redis::SERIALIZER_IGBINARY")) { - $serializers[] = [constant("Redis::SERIALIZER_IGBINARY")]; + $options[] = [ + constant("Redis::SERIALIZER_IGBINARY"), + Redis::COMPRESSION_NONE + ]; + } + + if (defined("Redis::COMPRESSION_LZF")) { + $options[] = [ + Redis::SERIALIZER_NONE, + constant("Redis::COMPRESSION_LZF") + ]; + $options[] = [ + Redis::SERIALIZER_PHP, + constant("Redis::COMPRESSION_LZF") + ]; + + if (defined("Redis::SERIALIZER_IGBINARY")) { + $options[] = [ + constant("Redis::SERIALIZER_IGBINARY"), + constant("Redis::COMPRESSION_LZF") + ]; + } } - return $serializers; + return $options; } } From 02914b69ab5719bc497ab33e266c70bc8f2af4d7 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:11:16 +0200 Subject: [PATCH 05/15] Added lzf in testing environment. --- tests/php-travis.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/php-travis.ini b/tests/php-travis.ini index cb6fa85e..c82bc18d 100644 --- a/tests/php-travis.ini +++ b/tests/php-travis.ini @@ -1,2 +1,3 @@ extension = "memcached.so" +extension = "lzf.so" extension = "redis.so" From c7af9eeb9b440e9a92651e8ffafb2e4fb558894f Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:21:35 +0200 Subject: [PATCH 06/15] Added lzf to travis. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 1131e560..3a0be96e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,7 @@ matrix: - php: nightly before_install: + - pecl install lzf - phpenv config-add tests/php-travis.ini - redis-server --port 63791 & - redis-server --port 63792 & From 835b0581f623cc62b599292dc793ce4048e1c7f1 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:25:58 +0200 Subject: [PATCH 07/15] Fixed travis lzf install. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3a0be96e..01d1a7f4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,7 @@ matrix: - php: nightly before_install: - - pecl install lzf + - yes | pecl install lzf - phpenv config-add tests/php-travis.ini - redis-server --port 63791 & - redis-server --port 63792 & From 2ea641800eb3737b3954b54ae042069187ea488c Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:32:28 +0200 Subject: [PATCH 08/15] Use latest redis, igbinary and lzf extensions. --- .travis.yml | 3 ++- tests/php-travis.ini | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 01d1a7f4..2d692bfb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,8 @@ matrix: - php: nightly before_install: - - yes | pecl install lzf + - pecl update-channels + - yes | pecl install igbinary lzf redis - phpenv config-add tests/php-travis.ini - redis-server --port 63791 & - redis-server --port 63792 & diff --git a/tests/php-travis.ini b/tests/php-travis.ini index c82bc18d..67dbf28c 100644 --- a/tests/php-travis.ini +++ b/tests/php-travis.ini @@ -1,3 +1 @@ extension = "memcached.so" -extension = "lzf.so" -extension = "redis.so" From 053b348530ec405c8626bdebc3d65a6fd2d6963d Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:37:04 +0200 Subject: [PATCH 09/15] Force install latest redis version. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2d692bfb..bebb8f05 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,7 @@ matrix: before_install: - pecl update-channels - - yes | pecl install igbinary lzf redis + - yes | pecl install -f igbinary lzf redis - phpenv config-add tests/php-travis.ini - redis-server --port 63791 & - redis-server --port 63792 & From e02bac8fef77a6df396466a2a1f9ca7080f1150a Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:42:51 +0200 Subject: [PATCH 10/15] Added comment about min phpredis version. --- classes/mutex/PHPRedisMutex.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index 72088078..efdb07aa 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -10,7 +10,7 @@ /** * Mutex based on the Redlock algorithm using the phpredis extension. * - * This implementation requires at least phpredis-2.2.4. If used together with + * This implementation requires at least phpredis-4.0.0. If used together with * the lzf extension, and phpredis is configured to use lzf compression, at * least phpredis-4.3.0 is required! For reason, see github issue link. * From 88c944b17793f2626a8eff9734b7ac257a27c197 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 22:53:59 +0200 Subject: [PATCH 11/15] Added php7.3 to travis. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index bebb8f05..e1008c60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ cache: php: - 7.1 - 7.2 + - 7.3 - nightly env: From a48d435f845e829c9e38aaff04dc3d4dfc3a0636 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sat, 20 Apr 2019 23:08:28 +0200 Subject: [PATCH 12/15] Improved travis script. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e1008c60..72eb32e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,7 +41,7 @@ before_install: - redis-server --port 63793 & install: - - composer install --no-scripts --no-suggest --no-interaction + - composer install --no-ansi --no-progress --no-scripts --no-suggest --no-interaction before_script: - mysql -e 'create database test;' From ffabb4b2f0a6fd9d7c3454f94aebfaff2b01f056 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sun, 21 Apr 2019 16:29:16 +0200 Subject: [PATCH 13/15] Updated documentation for ext-redis usage. --- README.md | 2 ++ classes/mutex/PHPRedisMutex.php | 2 +- composer.json | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dfbe72f4..f0ae8f9a 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ php-lock/lock follows semantic versioning. Read more on [semver.org][1]. - Optionally the [php-pcntl][3] extension to enable locking with `flock()` without busy waiting in CLI scripts. - Optionally `flock()`, `ext-redis`, `ext-pdo_mysql`, `ext-pdo_sqlite`, `ext-pdo_pgsql` or `ext-memcached` can be used as a backend for locks. See examples below. + - If `ext-redis` is used for locking and is configured to use igbinary for serialization or + lzf for compression, additionally `ext-igbinary` and/or `ext-lzf` have to be installed. ---- diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index efdb07aa..bbbc396f 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -72,7 +72,7 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum // Determine if we need to compress eval arguments. $lzfCompression = false; if (defined("Redis::COMPRESSION_LZF") && - constant("Redis::COMPRESSION_LZF") === $redis->getOption(Redis::OPT_COMPRESSION) && + Redis::COMPRESSION_LZF === $redis->getOption(Redis::OPT_COMPRESSION) && function_exists('lzf_compress') ) { $lzfCompression = true; diff --git a/composer.json b/composer.json index 02ff9711..5f045395 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ "psr/log": "^1" }, "require-dev": { + "ext-igbinary": "*", "ext-lzf": "*", "ext-memcached": "*", "ext-pcntl": "*", @@ -63,6 +64,7 @@ "squizlabs/php_codesniffer": "^3.3" }, "suggest": { + "ext-igbinary": "To use this library with PHP Redis igbinary serializer enabled.", "ext-lzf": "To use this library with PHP Redis lzf compression enabled.", "ext-pnctl": "Enables locking with flock without busy waiting in CLI scripts.", "ext-redis": "To use this library with the PHP Redis extension.", From c7516e00c915e008fed245e3e2628283053e97bf Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Tue, 23 Apr 2019 21:17:29 +0200 Subject: [PATCH 14/15] Refactored PHPRedisMutex to extract lzf compression detection. --- classes/mutex/PHPRedisMutex.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index bbbc396f..2705ee2c 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -69,15 +69,6 @@ protected function add($redisAPI, string $key, string $value, int $expire): bool */ protected function evalScript($redis, string $script, int $numkeys, array $arguments) { - // Determine if we need to compress eval arguments. - $lzfCompression = false; - if (defined("Redis::COMPRESSION_LZF") && - Redis::COMPRESSION_LZF === $redis->getOption(Redis::OPT_COMPRESSION) && - function_exists('lzf_compress') - ) { - $lzfCompression = true; - } - for ($i = $numkeys, $iMax = count($arguments); $i < $iMax; $i++) { /* If a serializion mode such as "php" or "igbinary" is enabled, the arguments must be * serialized by us, because phpredis does not do this for the eval command. @@ -87,7 +78,7 @@ function_exists('lzf_compress') /* If LZF compression is enabled for the redis connection and the runtime has the LZF * extension installed, compress the arguments as the final step. */ - if ($lzfCompression) { + if ($this->hasLzfCompression($redis)) { $arguments[$i] = lzf_compress($arguments[$i]); } } @@ -98,4 +89,22 @@ function_exists('lzf_compress') throw new LockReleaseException("Failed to release lock", 0, $e); } } + + /** + * Determines if lzf compression is enabled for the given connection. + * + * @param \Redis|\RedisCluster $redis Redis connection. + * @return bool TRUE if lzf comression is enabled, false otherwise. + */ + private function hasLzfCompression($redis): bool + { + if (\defined('Redis::COMPRESSION_LZF') && + Redis::COMPRESSION_LZF === $redis->getOption(Redis::OPT_COMPRESSION) && + \function_exists('lzf_compress') + ) { + return true; + } + + return false; + } } From 8eb660c9b6dcba0bc5130737f45e632d2e6179d1 Mon Sep 17 00:00:00 2001 From: Willem Stuursma Date: Thu, 25 Apr 2019 16:29:28 +0200 Subject: [PATCH 15/15] Small style changes --- .travis.yml | 2 +- classes/mutex/PHPRedisMutex.php | 30 ++++++++++++++---------------- tests/mutex/PHPRedisMutexTest.php | 3 --- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index 72eb32e8..eb473595 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,7 +41,7 @@ before_install: - redis-server --port 63793 & install: - - composer install --no-ansi --no-progress --no-scripts --no-suggest --no-interaction + - composer install --no-progress --no-scripts --no-suggest --no-interaction before_script: - mysql -e 'create database test;' diff --git a/classes/mutex/PHPRedisMutex.php b/classes/mutex/PHPRedisMutex.php index 2705ee2c..0bc068f9 100644 --- a/classes/mutex/PHPRedisMutex.php +++ b/classes/mutex/PHPRedisMutex.php @@ -27,7 +27,7 @@ class PHPRedisMutex extends RedisMutex /** * Sets the connected Redis APIs. * - * The Redis APIs needs to be connected yet. I.e. Redis::connect() was + * The Redis APIs needs to be connected. I.e. Redis::connect() was * called already. * * @param array<\Redis|\RedisCluster> $redisAPIs The Redis connections. @@ -43,9 +43,7 @@ public function __construct(array $redisAPIs, string $name, int $timeout = 3) } /** - * {@inheritDoc} - * - * @throws \malkusch\lock\exception\LockAcquireException + * @throws LockAcquireException */ protected function add($redisAPI, string $key, string $value, int $expire): bool { @@ -63,19 +61,22 @@ protected function add($redisAPI, string $key, string $value, int $expire): bool } /** - * {@inheritDoc} - * * @param \Redis|\RedisCluster $redis The Redis or RedisCluster connection. + * @throws LockReleaseException */ protected function evalScript($redis, string $script, int $numkeys, array $arguments) { - for ($i = $numkeys, $iMax = count($arguments); $i < $iMax; $i++) { - /* If a serializion mode such as "php" or "igbinary" is enabled, the arguments must be + for ($i = $numkeys; $i < \count($arguments); $i++) { + /* + * If a serialization mode such as "php" or "igbinary" is enabled, the arguments must be * serialized by us, because phpredis does not do this for the eval command. + * + * The keys must not be serialized. */ $arguments[$i] = $redis->_serialize($arguments[$i]); - /* If LZF compression is enabled for the redis connection and the runtime has the LZF + /* + * If LZF compression is enabled for the redis connection and the runtime has the LZF * extension installed, compress the arguments as the final step. */ if ($this->hasLzfCompression($redis)) { @@ -94,17 +95,14 @@ protected function evalScript($redis, string $script, int $numkeys, array $argum * Determines if lzf compression is enabled for the given connection. * * @param \Redis|\RedisCluster $redis Redis connection. - * @return bool TRUE if lzf comression is enabled, false otherwise. + * @return bool TRUE if lzf compression is enabled, false otherwise. */ private function hasLzfCompression($redis): bool { - if (\defined('Redis::COMPRESSION_LZF') && - Redis::COMPRESSION_LZF === $redis->getOption(Redis::OPT_COMPRESSION) && - \function_exists('lzf_compress') - ) { - return true; + if (!\defined('Redis::COMPRESSION_LZF')) { + return false; } - return false; + return Redis::COMPRESSION_LZF === $redis->getOption(Redis::OPT_COMPRESSION); } } diff --git a/tests/mutex/PHPRedisMutexTest.php b/tests/mutex/PHPRedisMutexTest.php index cab3f518..d1cebc4e 100644 --- a/tests/mutex/PHPRedisMutexTest.php +++ b/tests/mutex/PHPRedisMutexTest.php @@ -135,9 +135,6 @@ public function testEvalScriptFails() /** * @dataProvider serializationAndCompressionModes - * - * @param array $serializer Serializer to test. - * @param array $compressor Compressor to test. */ public function testSerializersAndCompressors($serializer, $compressor) {