Skip to content

Commit

Permalink
Add action caching
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Aug 22, 2022
1 parent 0ec3b66 commit c262890
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 499 deletions.
9 changes: 8 additions & 1 deletion src/Application/Action/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PackageHealth\PHP\Domain\Exception\DomainRecordNotFoundException;
use PackageHealth\PHP\Domain\Exception\DomainValidationException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -15,6 +16,7 @@
abstract class AbstractAction {
protected LoggerInterface $logger;
protected CacheProvider $cacheProvider;
protected CacheItemPoolInterface $cacheItemPool;
protected ServerRequestInterface $request;
protected ResponseInterface $response;

Expand All @@ -23,9 +25,14 @@ abstract class AbstractAction {
*/
protected array $args;

public function __construct(LoggerInterface $logger, CacheProvider $cacheProvider) {
public function __construct(
LoggerInterface $logger,
CacheProvider $cacheProvider,
CacheItemPoolInterface $cacheItemPool
) {
$this->logger = $logger;
$this->cacheProvider = $cacheProvider;
$this->cacheItemPool = $cacheItemPool;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Application/Action/Package/AbstractPackageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PackageHealth\PHP\Application\Action\AbstractAction;
use PackageHealth\PHP\Domain\Package\PackageRepositoryInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Slim\HttpCache\CacheProvider;

Expand All @@ -14,9 +15,10 @@ abstract class AbstractPackageAction extends AbstractAction {
public function __construct(
LoggerInterface $logger,
CacheProvider $cacheProvider,
CacheItemPoolInterface $cacheItemPool,
PackageRepositoryInterface $packageRepository
) {
parent::__construct($logger, $cacheProvider);
parent::__construct($logger, $cacheProvider, $cacheItemPool);
$this->packageRepository = $packageRepository;
}
}
112 changes: 51 additions & 61 deletions src/Application/Action/Package/ListPackageVersionsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PackageHealth\PHP\Domain\Version\VersionCollection;
use PackageHealth\PHP\Domain\Version\VersionNotFoundException;
use PackageHealth\PHP\Domain\Version\VersionRepositoryInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Slim\HttpCache\CacheProvider;
Expand All @@ -21,11 +22,12 @@ final class ListPackageVersionsAction extends AbstractPackageAction {
public function __construct(
LoggerInterface $logger,
CacheProvider $cacheProvider,
CacheItemPoolInterface $cacheItemPool,
PackageRepositoryInterface $packageRepository,
VersionRepositoryInterface $versionRepository
) {
parent::__construct($logger, $cacheProvider, $packageRepository);
$this->versionRepository = $versionRepository;
parent::__construct($logger, $cacheProvider, $cacheItemPool, $packageRepository);
$this->versionRepository = $versionRepository;
}

protected function action(): ResponseInterface {
Expand All @@ -35,81 +37,69 @@ protected function action(): ResponseInterface {
$project = $this->resolveStringArg('project');
PackageValidator::assertValidProject($project);

$twig = Twig::fromRequest($this->request);
$item = $this->cacheItemPool->getItem("/view/listPackageVersions/{$vendor}/{$project}");
$html = $item->get();
if ($item->isHit() === false) {
$twig = Twig::fromRequest($this->request);

$packageCol = $this->packageRepository->find(
[
'name' => "{$vendor}/{$project}"
],
1
);

if ($packageCol->isEmpty()) {
throw new PackageNotFoundException();
}

$this->logger->debug("Package '{$vendor}/{$project}' version list was viewed.");
$packageCol = $this->packageRepository->find(
[
'name' => "{$vendor}/{$project}"
],
1
);

$package = $packageCol->first();
$taggedCol = $this->versionRepository->find(
query: [
'package_id' => $package->getId(),
'release' => true
],
orderBy: [
'created_at' => 'DESC',
'number' => 'ASC'
]
);
if ($packageCol->isEmpty()) {
throw new PackageNotFoundException();
}

$developCol = $this->versionRepository->find(
query: [
'package_id' => $package->getId(),
'release' => false
],
orderBy: [
'created_at' => 'DESC',
'number' => 'ASC'
]
);
$package = $packageCol->first();

if (count($taggedCol)) {
$lastModified = array_reduce(
$taggedCol
->map(
function (Version $version): int {
$lastModified = $version->getUpdatedAt() ?? $version->getCreatedAt();

return $lastModified->getTimestamp();
}
)
->toArray(),
'max',
0
$taggedCol = $this->versionRepository->find(
query: [
'package_id' => $package->getId(),
'release' => true
],
orderBy: [
'created_at' => 'DESC'
]
);

$this->response = $this->cacheProvider->withLastModified(
$this->response,
$lastModified
);
$this->response = $this->cacheProvider->withEtag(
$this->response,
hash('sha1', (string)$lastModified)
$developCol = $this->versionRepository->find(
query: [
'package_id' => $package->getId(),
'release' => false
],
orderBy: [
'created_at' => 'DESC'
]
);
}

return $this->respondWithHtml(
$twig->fetch(
$this->logger->debug("Package '{$vendor}/{$project}' version list was rendered.");
$html = $twig->fetch(
'package/list.twig',
[
'package' => $package,
'tagged' => $taggedCol,
'tagged' => $taggedCol,
'develop' => $developCol,
'app' => [
'version' => $_ENV['VERSION']
]
]
)
);

$item->set($html);
$item->expiresAfter(3600);

$this->cacheItemPool->save($item);
}

$this->logger->debug("Package '{$vendor}/{$project}' version list was viewed.");
$this->response = $this->cacheProvider->withEtag(
$this->response,
hash('sha1', $html)
);

return $this->respondWithHtml($html);
}
}
52 changes: 21 additions & 31 deletions src/Application/Action/Package/ListPackagesAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,36 @@

final class ListPackagesAction extends AbstractPackageAction {
protected function action(): ResponseInterface {
$packageCol = $this->packageRepository->findPopular();
$twig = Twig::fromRequest($this->request);
$item = $this->cacheItemPool->getItem('/view/listPackages');
$html = $item->get();
if ($item->isHit() === false) {
$twig = Twig::fromRequest($this->request);

$this->logger->debug('Packages list was viewed.');

if (count($packageCol)) {
$lastModified = array_reduce(
$packageCol
->map(
function (Package $package): int {
$lastModified = $package->getUpdatedAt() ?? $package->getCreatedAt();

return $lastModified->getTimestamp();
}
)
->toArray(),
'max',
0
);

$this->response = $this->cacheProvider->withLastModified(
$this->response,
$lastModified
);
$this->response = $this->cacheProvider->withEtag(
$this->response,
hash('sha1', (string)$lastModified)
);
}
$packageCol = $this->packageRepository->findPopular();

return $this->respondWithHtml(
$twig->fetch(
$this->logger->debug('Packages list was rendered.');
$html = $twig->fetch(
'index.twig',
[
'packages' => $packageCol,
'app' => [
'version' => $_ENV['VERSION']
]
]
)
);

$item->set($html);
$item->expiresAfter(3600);

$this->cacheItemPool->save($item);
}

$this->logger->debug('Packages list was viewed.');
$this->response = $this->cacheProvider->withEtag(
$this->response,
hash('sha1', $html)
);

return $this->respondWithHtml($html);
}
}
Loading

0 comments on commit c262890

Please sign in to comment.