Created RedisFactory which will create the redis adapter for the redis lock

This commit is contained in:
Alejandro Celaya 2019-08-07 14:17:15 +02:00
parent dcfb12f454
commit 1e294fe1bc
6 changed files with 70 additions and 3 deletions

View file

@ -31,6 +31,7 @@
"monolog/monolog": "^1.21",
"ocramius/proxy-manager": "^2.0",
"phly/phly-event-dispatcher": "^1.0",
"predis/predis": "^1.1",
"shlinkio/shlink-installer": "^1.2.1",
"symfony/console": "^4.3",
"symfony/filesystem": "^4.3",

View file

@ -1,6 +1,7 @@
<?php
declare(strict_types=1);
use Shlinkio\Shlink\Common\Cache\RedisFactory;
use Symfony\Component\Lock;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
@ -13,13 +14,20 @@ return [
'dependencies' => [
'factories' => [
Lock\Store\FlockStore::class => ConfigAbstractFactory::class,
Lock\Store\RedisStore::class => ConfigAbstractFactory::class,
Lock\Factory::class => ConfigAbstractFactory::class,
],
'aliases' => [
// With this config, a user could alias 'lock_store' => 'redis_lock_store' to override the default
'lock_store' => Lock\Store\FlockStore::class,
'redis_lock_store' => Lock\Store\RedisStore::class,
],
],
ConfigAbstractFactory::class => [
Lock\Store\FlockStore::class => ['config.locks.locks_dir'],
Lock\Factory::class => [Lock\Store\FlockStore::class],
Lock\Store\RedisStore::class => [RedisFactory::SERVICE_NAME],
Lock\Factory::class => ['lock_store'],
],
];

View file

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
return [
'redis' => [
'servers' => 'tcp://shlink_redis:6379',
// 'servers' => [
// 'tcp://shlink_redis:6379',
// ],
],
];

View 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,
],
],
];

View file

@ -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,

View 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);
}
}