#113071 Замена predis для работы с sentinel

This commit is contained in:
Александр Рыбкин 2025-07-30 13:56:35 +03:00
commit b39978eca9
3 changed files with 85 additions and 0 deletions

28
composer.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "dominion/laravel-redis-sentinel",
"description": "Множественность серверов sentinel",
"keywords": ["laravel","redis", "sentinel"],
"homepage": "https://gitea.optiweb.ru/public/kuvalda-laravel-redis-sentinel",
"license": "MIT",
"authors": [
{
"name": "Rybkin Sasha",
"email": "ribkin@dominion.ru"
}
],
"require": {
"namoshek/laravel-redis-sentinel": "^0.8|^0.9"
},
"autoload": {
"psr-4": {
"dominion\\laravel-redis-sentinel\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Dominion\\Redis\\Sentinel\\RedisSentinelServiceProvider"
]
}
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Dominion\Redis\Sentinel\Connectors;
use RedisSentinel;
/**
* Allows to connect to a Sentinel driven Redis master using the PhpRedis extension.
*/
class PhpRedisSentinelConnector extends \Namoshek\Redis\Sentinel\Connectors\PhpRedisSentinelConnector
{
private function connectToSentinel(array $config): RedisSentinel
{
$hosts = is_array($config['sentinel_host']) ? $config['sentinel_host'] : [$config['sentinel_host']];
foreach ($hosts as $host)
{
$newConfig = $config;
$newConfig['sentinel_host'] = $host;
$RedisSentinel = parent::connectToSentinel($newConfig);
if($RedisSentinel->ping())
{
break;
}
}
return $RedisSentinel;
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Dominion\Redis\Sentinel;
use Illuminate\Redis\RedisManager;
use Illuminate\Support\ServiceProvider;
use Dominion\Redis\Sentinel\Connectors\PhpRedisSentinelConnector;
/**
* Registers and boots services of the Laravel Redis Sentinel package.
*/
class RedisSentinelServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
$this->app->extend('redis', function (RedisManager $service) {
return $service->extend('dominion-phpredis-sentinel', fn () => new PhpRedisSentinelConnector);
});
}
}