From 87e7dc1c7da535be78339b9887ec00581cd504e2 Mon Sep 17 00:00:00 2001 From: Ilyas Salikhov Date: Tue, 18 Jun 2024 18:05:09 +0300 Subject: [PATCH] Redis 3 compability (method Redis::expire) --- Cache/Redis.php | 16 +++++++++++++++- Makefile | 6 ++++++ docker-compose.yml | 3 +++ tests/Cache/RedisTest.php | 3 +++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Cache/Redis.php b/Cache/Redis.php index 99e4fff..9fd5705 100644 --- a/Cache/Redis.php +++ b/Cache/Redis.php @@ -9,6 +9,7 @@ class Redis extends \Redis protected $stopwatch; protected $stopwatchAdditionalTags = []; protected $serverName; + protected int $expireMethodArgumentsCount; public function addWatchedServer( $host, @@ -18,6 +19,15 @@ public function addWatchedServer( $this->serverName = $host . (6379 == $port ? '' : ':' . $port); $this->pconnect($host, $port, $timeout); + + // для совместимости с Redis 3 + $expireMethodReflection = new \ReflectionMethod(\Redis::class, 'expire'); + $this->expireMethodArgumentsCount = $expireMethodReflection->getNumberOfParameters(); + if ($this->expireMethodArgumentsCount < 2 || $this->expireMethodArgumentsCount > 3) { + throw new \RuntimeException( + 'Redis::expire method has wrong number of arguments ' . $this->expireMethodArgumentsCount . ' instead of 2 or 3' + ); + } } public function setStopwatch(Stopwatch $stopwatch): void @@ -138,7 +148,11 @@ public function expire($key, $expire, $mode = null) $e = $this->getStopwatchEvent('expire'); } - $result = parent::expire($key, $expire, $mode); + if (2 === $this->expireMethodArgumentsCount) { + $result = parent::expire($key, $expire); + } else { + $result = parent::expire($key, $expire, $mode); + } if ($this->stopwatch) { $e->stop(); diff --git a/Makefile b/Makefile index 52cd47a..b31d390 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,12 @@ endif PHP_CONSOLE_DEPS=vendor +up: docker-compose.yml + @docker compose up -d --build --remove-orphans --quiet-pull + +stop: + @docker compose stop + vendor: composer.json @$(PHP) composer install -o -n --no-ansi @touch vendor || true diff --git a/docker-compose.yml b/docker-compose.yml index 3250442..e6ac0e5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,9 @@ version: '3.4' services: + redis: + image: redis:3.2-alpine + php: build: context: . diff --git a/tests/Cache/RedisTest.php b/tests/Cache/RedisTest.php index 0659712..08002ba 100644 --- a/tests/Cache/RedisTest.php +++ b/tests/Cache/RedisTest.php @@ -17,5 +17,8 @@ class RedisTest extends TestCase public function testInit(): void { $redis = new Redis(); + $redis->addWatchedServer('redis'); + + $redis->expire('a', 1, null); } }