From d698f19531c9870622274afc1d44a3ecfdaa5d47 Mon Sep 17 00:00:00 2001 From: Flavio Heleno Date: Tue, 22 Mar 2022 20:36:29 -0300 Subject: [PATCH] Add cache kill switch for CLI --- app/dependencies.php | 20 ++++++++++++-------- app/repositories.php | 33 +++++++++++++++++++++++++++++---- app/settings.php | 7 ++++--- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/app/dependencies.php b/app/dependencies.php index 575125de..8bbf691c 100644 --- a/app/dependencies.php +++ b/app/dependencies.php @@ -27,6 +27,7 @@ use PUGX\Poser\Render\SvgPlasticRender; use Slim\HttpCache\CacheProvider; use Slim\Views\Twig; +use Stash\Driver\BlackHole; use Stash\Driver\Composite; use Stash\Driver\Ephemeral; use Stash\Driver\FileSystem; @@ -54,6 +55,13 @@ CacheItemPoolInterface::class => function (ContainerInterface $container): Pool { $settings = $container->get(SettingsInterface::class); + // disables cache by using a black hole driver + if ($settings->has('cache') === false || $settings->getBool('cache.enabled', false) === false) { + return new Pool( + new BlackHole() + ); + } + $drivers = [ new Ephemeral() ]; @@ -79,17 +87,13 @@ ] ); - if (count($drivers) > 1) { - $driver = new Composite( + return new Pool( + new Composite( [ 'drivers' => $drivers ] - ); - - return new Pool($driver); - } - - return new Pool($drivers[0]); + ) + ); }, Consumer::class => function (ContainerInterface $container): Consumer { return new Consumer( diff --git a/app/repositories.php b/app/repositories.php index 487f1071..25588251 100644 --- a/app/repositories.php +++ b/app/repositories.php @@ -1,6 +1,7 @@ autowire(PdoDependencyRepository::class), - DependencyRepositoryInterface::class => static function (ContainerInterface $container): CachedDependencyRepository { + DependencyRepositoryInterface::class => static function (ContainerInterface $container): DependencyRepositoryInterface { + $settings = $container->get(SettingsInterface::class); + + if ($settings->has('cache') === false || $settings->getBool('cache.enabled', false) === false) { + return $container->get(PdoDependencyRepository::class); + } + return new CachedDependencyRepository( $container->get(PdoDependencyRepository::class), $container->get(CacheItemPoolInterface::class) @@ -31,7 +38,13 @@ }, // Package PdoPackageRepository::class => autowire(PdoPackageRepository::class), - PackageRepositoryInterface::class => static function (ContainerInterface $container): CachedPackageRepository { + PackageRepositoryInterface::class => static function (ContainerInterface $container): PackageRepositoryInterface { + $settings = $container->get(SettingsInterface::class); + + if ($settings->has('cache') === false || $settings->getBool('cache.enabled', false) === false) { + return $container->get(PdoPackageRepository::class); + } + return new CachedPackageRepository( $container->get(PdoPackageRepository::class), $container->get(CacheItemPoolInterface::class) @@ -39,7 +52,13 @@ }, // Stats PdoStatsRepository::class => autowire(PdoStatsRepository::class), - StatsRepositoryInterface::class => static function (ContainerInterface $container): CachedStatsRepository { + StatsRepositoryInterface::class => static function (ContainerInterface $container): StatsRepositoryInterface { + $settings = $container->get(SettingsInterface::class); + + if ($settings->has('cache') === false || $settings->getBool('cache.enabled', false) === false) { + return $container->get(PdoStatsRepository::class); + } + return new CachedStatsRepository( $container->get(PdoStatsRepository::class), $container->get(CacheItemPoolInterface::class) @@ -47,7 +66,13 @@ }, // Version PdoVersionRepository::class => autowire(PdoVersionRepository::class), - VersionRepositoryInterface::class => static function (ContainerInterface $container): CachedVersionRepository { + VersionRepositoryInterface::class => static function (ContainerInterface $container): VersionRepositoryInterface { + $settings = $container->get(SettingsInterface::class); + + if ($settings->has('cache') === false || $settings->getBool('cache.enabled', false) === false) { + return $container->get(PdoVersionRepository::class); + } + return new CachedVersionRepository( $container->get(PdoVersionRepository::class), $container->get(CacheItemPoolInterface::class) diff --git a/app/settings.php b/app/settings.php index 5f90b61b..9ec5734b 100644 --- a/app/settings.php +++ b/app/settings.php @@ -4,16 +4,17 @@ use App\Application\Settings\Settings; use App\Application\Settings\SettingsInterface; use DI\ContainerBuilder; -use Monolog\Logger; +use Psr\Log\LogLevel; return static function (ContainerBuilder $containerBuilder): void { // Global Settings Object $containerBuilder->addDefinitions( [ - SettingsInterface::class => function () { + SettingsInterface::class => static function (): SettingsInterface { return new Settings( [ 'cache' => [ + 'enabled' => PHP_SAPI !== 'cli', 'redis' => "redis://${_ENV['REDIS_HOST']}:${_ENV['REDIS_PORT']}" ], 'db' => [ @@ -28,7 +29,7 @@ 'logger' => [ 'name' => 'slim-app', 'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log', - 'level' => Logger::DEBUG, + 'level' => LogLevel::DEBUG, ] ] );