Skip to content

Commit

Permalink
Add Cached Repository Proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Mar 20, 2022
1 parent 1ff9ab5 commit 55963cd
Show file tree
Hide file tree
Showing 10 changed files with 718 additions and 23 deletions.
43 changes: 43 additions & 0 deletions app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use PUGX\Poser\Poser;
Expand All @@ -25,6 +26,11 @@
use PUGX\Poser\Render\SvgPlasticRender;
use Slim\HttpCache\CacheProvider;
use Slim\Views\Twig;
use Stash\Driver\Composite;
use Stash\Driver\Ephemeral;
use Stash\Driver\FileSystem;
use Stash\Driver\Redis;
use Stash\Pool;
use function DI\autowire;

return static function (ContainerBuilder $containerBuilder): void {
Expand All @@ -44,6 +50,43 @@
new AmqpTransport($settings->getString('queue.dsn'))
);
},
CacheItemPoolInterface::class => function (ContainerInterface $container): Pool {
$settings = $container->get(SettingsInterface::class);

$drivers = [
new Ephemeral()
];

if ($settings->has('cache.redis')) {
$dsn = parse_url($settings->getString('cache.redis'));
$drivers[] = new Redis(
[
'servers' => [
'server' => $dsn['host'] ?? 'localhost',
'port' => $dsn['port'] ?? 6379
]
]
);
}

$drivers[] = new FileSystem(
[
'path' => __DIR__ . '/../var/cache'
]
);

if (count($drivers) > 1) {
$driver = new Composite(
[
'drivers' => $drivers
]
);

return new Pool($driver);
}

return new Pool($drivers[0]);
},
Consumer::class => function (ContainerInterface $container): Consumer {
return new Consumer(
$container->get(Bus::class),
Expand Down
42 changes: 38 additions & 4 deletions app/repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,54 @@
use App\Domain\Package\PackageRepositoryInterface;
use App\Domain\Stats\StatsRepositoryInterface;
use App\Domain\Version\VersionRepositoryInterface;
use App\Infrastructure\Persistence\Dependency\CachedDependencyRepository;
use App\Infrastructure\Persistence\Dependency\PdoDependencyRepository;
use App\Infrastructure\Persistence\Package\CachedPackageRepository;
use App\Infrastructure\Persistence\Package\PdoPackageRepository;
use App\Infrastructure\Persistence\Stats\CachedStatsRepository;
use App\Infrastructure\Persistence\Stats\PdoStatsRepository;
use App\Infrastructure\Persistence\Version\CachedVersionRepository;
use App\Infrastructure\Persistence\Version\PdoVersionRepository;
use DI\ContainerBuilder;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use function DI\autowire;

return static function (ContainerBuilder $containerBuilder): void {
$containerBuilder->addDefinitions(
[
DependencyRepositoryInterface::class => autowire(PdoDependencyRepository::class),
PackageRepositoryInterface::class => autowire(PdoPackageRepository::class),
StatsRepositoryInterface::class => autowire(PdoStatsRepository::class),
VersionRepositoryInterface::class => autowire(PdoVersionRepository::class)
// Dependency
PdoDependencyRepository::class => autowire(PdoDependencyRepository::class),
DependencyRepositoryInterface::class => static function (ContainerInterface $container): CachedDependencyRepository {
return new CachedDependencyRepository(
$container->get(PdoDependencyRepository::class),
$container->get(CacheItemPoolInterface::class)
);
},
// Package
PdoPackageRepository::class => autowire(PdoPackageRepository::class),
PackageRepositoryInterface::class => static function (ContainerInterface $container): CachedPackageRepository {
return new CachedPackageRepository(
$container->get(PdoPackageRepository::class),
$container->get(CacheItemPoolInterface::class)
);
},
// Stats
PdoStatsRepository::class => autowire(PdoStatsRepository::class),
StatsRepositoryInterface::class => static function (ContainerInterface $container): CachedStatsRepository {
return new CachedStatsRepository(
$container->get(PdoStatsRepository::class),
$container->get(CacheItemPoolInterface::class)
);
},
// Version
PdoVersionRepository::class => autowire(PdoVersionRepository::class),
VersionRepositoryInterface::class => static function (ContainerInterface $container): CachedVersionRepository {
return new CachedVersionRepository(
$container->get(PdoVersionRepository::class),
$container->get(CacheItemPoolInterface::class)
);
}
]
);
};
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@
"nyholm/psr7": "^1.5",
"nyholm/psr7-server": "^1.0",
"php-di/php-di": "^6.3",
"psr/cache": "^1.0",
"psr/container": "^1.0",
"psr/log": "^2.0",
"ramsey/collection": "^1.2",
"robmorgan/phinx": "^0.12.10",
"slim/http-cache": "^1.1",
"slim/slim": "^4.9",
"slim/twig-view": "^3.3",
"symfony/console": "^6.0",
"tedivm/stash": "^0.17",
"vlucas/phpdotenv": "^5.4"
},
"require-dev": {
Expand Down
160 changes: 143 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 55963cd

Please sign in to comment.