Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,48 @@ jobs:
extensions: redis-5.3.7
coverage: "${{ matrix.coverage }}"

- name: "Allow legacy Symfony branches in CI"
run: composer config audit.block-insecure false

- name: "Install dependencies"
uses: ramsey/composer-install@v2
env:
SYMFONY_REQUIRE: "${{ matrix.symfony-version }}"

- name: "Run tests"
run: composer test

current-tests:
runs-on: ubuntu-24.04
name: "Current tests"
services:
redis:
image: redis:7-alpine
ports:
- 6379:6379
strategy:
fail-fast: false
matrix:
php-version:
- '8.2'
- '8.3'
- '8.4'
symfony-version:
- '6.4.*'
- '7.4.*'
coverage: [ 'none' ]
steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
tools: flex
php-version: "${{ matrix.php-version }}"
extensions: redis
coverage: "${{ matrix.coverage }}"

- name: "Install dependencies"
uses: ramsey/composer-install@v2
env:
Expand All @@ -69,6 +111,9 @@ jobs:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
- '8.4'
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
Expand All @@ -77,6 +122,7 @@ jobs:
uses: "shivammathur/setup-php@v2"
with:
php-version: ${{ matrix.php-version }}
tools: flex
coverage: "none"

- name: "Cache dependencies installed with composer"
Expand All @@ -86,6 +132,9 @@ jobs:
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"

- name: "Allow legacy Symfony branches in CI"
run: composer config audit.block-insecure false

- name: "Install dependencies"
uses: ramsey/composer-install@v2

Expand Down
6 changes: 6 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
->in(__DIR__ . '/tests')
;

if (PHP_VERSION_ID < 80000) {
$finder->notName('RedisPhp8.php');
}

$rules = [
'@Symfony' => true,
'@Symfony:risky' => true,

// exceptions
'single_line_throw' => false,
'modernize_strpos' => false,
'trailing_comma_in_multiline' => false,

// php file
'concat_space' => ['spacing' => 'one'],
Expand Down
227 changes: 11 additions & 216 deletions Cache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,222 +2,17 @@

namespace Intaro\PinbaBundle\Cache;

use Intaro\PinbaBundle\Stopwatch\Stopwatch;
require_once __DIR__ . '/RedisStopwatchTrait.php';

$redisVersion = phpversion('redis');
if (PHP_VERSION_ID >= 80000 && is_string($redisVersion) && version_compare($redisVersion, '6.0.0', '>=')) {
require_once __DIR__ . '/RedisPhp8.php';
class_alias(RedisPhp8::class, RedisBase::class);
} else {
require_once __DIR__ . '/RedisLegacy.php';
class_alias(RedisLegacy::class, RedisBase::class);
}

class Redis extends \Redis
class Redis extends RedisBase
{
protected $stopwatch;
protected $stopwatchAdditionalTags = [];
protected $serverName;
protected int $expireMethodArgumentsCount;

public function addWatchedServer(
$host,
$port = 6379,
$timeout = 5
): void {
$this->serverName = $host . (6379 == $port ? '' : ':' . $port);

$this->pconnect($host, $port, $timeout);

// для совместимости с Redis 5
$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
{
$this->stopwatch = $stopwatch;
}

public function setStopwatchTags(array $tags): void
{
$this->stopwatchAdditionalTags = $tags;
}

protected function getStopwatchEvent($methodName)
{
$tags = $this->stopwatchAdditionalTags;
$tags['group'] = 'redis::' . $methodName;

if ($this->serverName) {
$tags['server'] = $this->serverName;
}

return $this->stopwatch->start($tags);
}

public function get($key)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('get');
}

$result = parent::get($key);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function mGet(array $keys)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('mGet');
}

$result = parent::mGet($keys);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function exists($key, ...$other_keys)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('exists');
}

$result = parent::exists($key, ...$other_keys);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function set($key, $var, $opts = null)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('set');
}

$result = parent::set($key, $var, $opts);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function setex($key, $var, $expire)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('setex');
}

$result = parent::setex($key, $var, $expire);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function mSetNx($v)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('mSetNx');
}

$result = parent::mSetNx($v);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function expire($key, $expire, $mode = null)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('expire');
}

if (2 === $this->expireMethodArgumentsCount) {
$result = parent::expire($key, $expire);
} else {
$result = parent::expire($key, $expire, $mode);
}

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function exec()
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('exec');
}

$result = parent::exec();

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function delete($key, ...$other_keys)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('delete');
}

$result = parent::delete($key, ...$other_keys);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function sMembers($key)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('sMembers');
}

$result = parent::sMembers($key);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}

public function sAdd($tag, $id, ...$other_values)
{
if ($this->stopwatch) {
$e = $this->getStopwatchEvent('sAdd');
}

$result = parent::sAdd($tag, $id, ...$other_values);

if ($this->stopwatch) {
$e->stop();
}

return $result;
}
}
Loading
Loading