Skip to content

Commit

Permalink
Redis 3 compability (method Redis::expire)
Browse files Browse the repository at this point in the history
  • Loading branch information
muxx committed Jun 18, 2024
1 parent 2079ebc commit 87e7dc1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Cache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Redis extends \Redis
protected $stopwatch;
protected $stopwatchAdditionalTags = [];
protected $serverName;
protected int $expireMethodArgumentsCount;

public function addWatchedServer(
$host,
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
version: '3.4'

services:
redis:
image: redis:3.2-alpine

php:
build:
context: .
Expand Down
3 changes: 3 additions & 0 deletions tests/Cache/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ class RedisTest extends TestCase
public function testInit(): void
{
$redis = new Redis();
$redis->addWatchedServer('redis');

$redis->expire('a', 1, null);
}
}

0 comments on commit 87e7dc1

Please sign in to comment.