Skip to content

Commit

Permalink
Add cache kill switch for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Mar 23, 2022
1 parent 20c91c3 commit d698f19
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
20 changes: 12 additions & 8 deletions app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
];
Expand All @@ -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(
Expand Down
33 changes: 29 additions & 4 deletions app/repositories.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
declare(strict_types = 1);

use App\Application\Settings\SettingsInterface;
use App\Domain\Dependency\DependencyRepositoryInterface;
use App\Domain\Package\PackageRepositoryInterface;
use App\Domain\Stats\StatsRepositoryInterface;
Expand All @@ -23,31 +24,55 @@
[
// Dependency
PdoDependencyRepository::class => 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)
);
},
// 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)
);
},
// 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)
);
},
// 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)
Expand Down
7 changes: 4 additions & 3 deletions app/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -28,7 +29,7 @@
'logger' => [
'name' => 'slim-app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => Logger::DEBUG,
'level' => LogLevel::DEBUG,
]
]
);
Expand Down

0 comments on commit d698f19

Please sign in to comment.