diff --git a/composer.json b/composer.json index a9507b4d..aa3221ff 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/autoload/locks.global.php b/config/autoload/locks.global.php index 7deb06c7..f76c911e 100644 --- a/config/autoload/locks.global.php +++ b/config/autoload/locks.global.php @@ -1,6 +1,7 @@ [ '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'], ], ]; diff --git a/config/autoload/redis.local.php.local b/config/autoload/redis.local.php.local new file mode 100644 index 00000000..d6256cca --- /dev/null +++ b/config/autoload/redis.local.php.local @@ -0,0 +1,13 @@ + [ + 'servers' => 'tcp://shlink_redis:6379', +// 'servers' => [ +// 'tcp://shlink_redis:6379', +// ], + ], + +]; diff --git a/module/Common/config/cache.config.php b/module/Common/config/cache.config.php new file mode 100644 index 00000000..26f547d6 --- /dev/null +++ b/module/Common/config/cache.config.php @@ -0,0 +1,17 @@ + [ + 'factories' => [ + DoctrineCache\Cache::class => Cache\CacheFactory::class, + Cache\RedisFactory::SERVICE_NAME => Cache\RedisFactory::class, + ], + ], + +]; diff --git a/module/Common/config/dependencies.config.php b/module/Common/config/dependencies.config.php index 88d5d8b6..f5c79b05 100644 --- a/module/Common/config/dependencies.config.php +++ b/module/Common/config/dependencies.config.php @@ -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, diff --git a/module/Common/src/Cache/RedisFactory.php b/module/Common/src/Cache/RedisFactory.php new file mode 100644 index 00000000..c35508f2 --- /dev/null +++ b/module/Common/src/Cache/RedisFactory.php @@ -0,0 +1,30 @@ +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); + } +}