mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 01:03:13 +08:00
Created RedisFactory which will create the redis adapter for the redis lock
This commit is contained in:
17
module/Common/config/cache.config.php
Normal file
17
module/Common/config/cache.config.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common;
|
||||
|
||||
use Doctrine\Common\Cache as DoctrineCache;
|
||||
|
||||
return [
|
||||
|
||||
'dependencies' => [
|
||||
'factories' => [
|
||||
DoctrineCache\Cache::class => Cache\CacheFactory::class,
|
||||
Cache\RedisFactory::SERVICE_NAME => Cache\RedisFactory::class,
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
@@ -3,7 +3,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common;
|
||||
|
||||
use Doctrine\Common\Cache as DoctrineCache;
|
||||
use GeoIp2\Database\Reader;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use Monolog\Logger;
|
||||
@@ -20,7 +19,6 @@ return [
|
||||
'dependencies' => [
|
||||
'factories' => [
|
||||
GuzzleClient::class => InvokableFactory::class,
|
||||
DoctrineCache\Cache::class => Cache\CacheFactory::class,
|
||||
Filesystem::class => InvokableFactory::class,
|
||||
Reader::class => ConfigAbstractFactory::class,
|
||||
|
||||
|
||||
30
module/Common/src/Cache/RedisFactory.php
Normal file
30
module/Common/src/Cache/RedisFactory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\Cache;
|
||||
|
||||
use Predis\Client as PredisClient;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
use function array_shift;
|
||||
use function count;
|
||||
use function is_array;
|
||||
use function is_string;
|
||||
|
||||
class RedisFactory
|
||||
{
|
||||
public const SERVICE_NAME = 'Shlinkio\Shlink\Common\Cache\Redis';
|
||||
|
||||
public function __invoke(ContainerInterface $container): PredisClient
|
||||
{
|
||||
$redisConfig = $container->get('config')['redis'] ?? [];
|
||||
$servers = $redisConfig['servers'];
|
||||
|
||||
if (is_array($servers) && count($servers) === 1) {
|
||||
$servers = array_shift($servers);
|
||||
}
|
||||
|
||||
$options = is_string($servers) ? null : ['cluster' => 'redis'];
|
||||
return new PredisClient($servers, $options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user