Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache setLink in Canonical #2887

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/Canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

class Canonical
{
Expand Down Expand Up @@ -44,13 +47,21 @@ class Canonical
/** @var RouterInterface */
private $router;

public function __construct(Config $config, UrlGeneratorInterface $urlGenerator, RequestStack $requestStack, RouterInterface $router, string $defaultLocale)
/** @var Stopwatch */
private $stopwatch;

/** @var TagAwareCacheInterface */
private $cache;

public function __construct(Config $config, UrlGeneratorInterface $urlGenerator, RequestStack $requestStack, RouterInterface $router, string $defaultLocale, Stopwatch $stopwatch, TagAwareCacheInterface $cache)
{
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->request = $requestStack->getCurrentRequest() ?? Request::createFromGlobals();
$this->defaultLocale = $defaultLocale;
$this->router = $router;
$this->stopwatch = $stopwatch;
$this->cache = $cache;

$this->init();
}
Expand Down Expand Up @@ -173,8 +184,23 @@ public function setPath(?string $route = null, array $params = []): void
$this->path = $this->generateLink($route, $params, false);
}

public function generateLink(?string $route, ?array $params, $canonical = false): ?string
public function generateLink(?string $route, ?array $params, $canonical = false): string
{
$cacheKey = 'bolt.generateLink_' . md5($route . implode('-', $params) . (string) $canonical);

$link = $this->cache->get($cacheKey, function (ItemInterface $item) use ($route, $params, $canonical) {
$item->tag('routes');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bobdenotter I see these popping up in a few places now. Should we create an enum-type class like that?

https://github.com/bolt/core/blob/master/src/Enum/UserStatus.php

Especially since it could be that extensions also may need to clear cache after performing some operations?

Leaving it here as a general comment for all relate PRs 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea! I'll do this in a separate PR, though.


return $this->generateLinkCacheHelper($route, $params, $canonical);
});

return $link;
}

private function generateLinkCacheHelper(?string $route, ?array $params, $canonical = false): string
{
$this->stopwatch->start('bolt.GenerateLink');

$removeDefaultLocaleOnCanonical = $this->config->get('general/localization/remove_default_locale_on_canonical', true);
$hasDefaultLocale = isset($params['_locale']) && $params['_locale'] === $this->defaultLocale;

Expand All @@ -197,15 +223,19 @@ public function generateLink(?string $route, ?array $params, $canonical = false)
}

try {
return $this->urlGenerator->generate(
$link = $this->urlGenerator->generate(
$route,
$params,
$canonical ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
);
} catch (InvalidParameterException | MissingMandatoryParametersException | RouteNotFoundException | \TypeError $e) {
// Just use the current URL /shrug
return $canonical ? $this->request->getUri() : $this->request->getPathInfo();
$link = $canonical ? $this->request->getUri() : $this->request->getPathInfo();
}

$this->stopwatch->stop('bolt.GenerateLink');

return $link;
}

private function routeRequiresParam(string $route, string $param): bool
Expand Down