Skip to content

Commit

Permalink
Merge pull request #1326 from bolt/bugfix/invalidate-localized-cache
Browse files Browse the repository at this point in the history
Invalidate localized menu cache
  • Loading branch information
bobdenotter committed Apr 23, 2020
2 parents 1fe909b + c8f046a commit 3c421fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
17 changes: 4 additions & 13 deletions src/Event/Subscriber/ContentSaveSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,29 @@

namespace Bolt\Event\Subscriber;

use Bolt\Configuration\Config;
use Bolt\Event\ContentEvent;
use Bolt\Log\LoggerTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

class ContentSaveSubscriber implements EventSubscriberInterface
{
use LoggerTrait;

public const PRIORITY = 100;

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

/** @var Config */
private $config;

public function __construct(CacheInterface $cache, Config $config)
public function __construct(TagAwareCacheInterface $cache)
{
$this->cache = $cache;
$this->config = $config;
}

public function postSave(ContentEvent $event): ContentEvent
{
// Make sure we flush the cache for the menu's
$menus = $this->config->get('menu')->keys()->all();
foreach ($menus as $menu) {
$this->cache->delete('frontendmenu_' . $menu);
}
$this->cache->delete('backendmenu');
$this->cache->invalidateTags(['backendmenu', 'frontendmenu']);

// Saving an entry in the log.
$context = [
Expand Down
8 changes: 6 additions & 2 deletions src/Menu/CachedBackendMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

final class CachedBackendMenuBuilder implements BackendMenuBuilderInterface
{
Expand All @@ -18,7 +20,7 @@ final class CachedBackendMenuBuilder implements BackendMenuBuilderInterface
/** @var RequestStack */
private $requestStack;

public function __construct(BackendMenuBuilderInterface $menuBuilder, CacheInterface $cache, RequestStack $requestStack)
public function __construct(BackendMenuBuilderInterface $menuBuilder, TagAwareCacheInterface $cache, RequestStack $requestStack)
{
$this->cache = $cache;
$this->menuBuilder = $menuBuilder;
Expand All @@ -29,7 +31,9 @@ public function buildAdminMenu(): array
{
$locale = $this->requestStack->getCurrentRequest()->getLocale();
$cacheKey = 'backendmenu_' . $locale;
return $this->cache->get($cacheKey, function () {
return $this->cache->get($cacheKey, function (ItemInterface $item) {
$item->tag('backendmenu');

return $this->menuBuilder->buildAdminMenu();
});
}
Expand Down
19 changes: 14 additions & 5 deletions src/Menu/CachedFrontendMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@

namespace Bolt\Menu;

use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

final class CachedFrontendMenuBuilder implements FrontendMenuBuilderInterface
{
/** @var CacheInterface */
/** @var TagAwareCacheInterface */
private $cache;

/** @var FrontendMenuBuilderInterface */
private $menuBuilder;

public function __construct(FrontendMenuBuilderInterface $menuBuilder, CacheInterface $cache)
/** @var Request */
private $request;

public function __construct(FrontendMenuBuilderInterface $menuBuilder, TagAwareCacheInterface $cache, RequestStack $requestStack)
{
$this->cache = $cache;
$this->menuBuilder = $menuBuilder;
$this->request = $requestStack->getCurrentRequest();
}

public function buildMenu(?string $name = null): array
{
$key = 'frontendmenu_' . ($name ?: 'main');
$key = 'frontendmenu_' . ($name ?: 'main') . '_' . $this->request->getLocale();

return $this->cache->get($key, function (ItemInterface $item) use ($name) {
$item->tag('frontendmenu');

return $this->cache->get($key, function () use ($name) {
return $this->menuBuilder->buildMenu($name);
});
}
Expand Down

0 comments on commit 3c421fd

Please sign in to comment.