diff --git a/core/lib/Drupal/Core/Routing/RoutePreloader.php b/core/lib/Drupal/Core/Routing/RoutePreloader.php index db1dc95..e7dbc85 100644 --- a/core/lib/Drupal/Core/Routing/RoutePreloader.php +++ b/core/lib/Drupal/Core/Routing/RoutePreloader.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Routing; +use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\State\StateInterface; use Symfony\Component\EventDispatcher\Event; @@ -84,6 +85,7 @@ public function onRequest(KernelEvent $event) { } else { $routes = $this->state->get($key, []); + $this->cache->set($key, $routes, Cache::PERMANENT, ['routes']); } if ($routes) { diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index f79b4e7..23dd704 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Routing; +use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\PathProcessor\InboundPathProcessorInterface; @@ -193,12 +194,15 @@ public function preLoadRoutes($names) { $routes_to_load = array_diff($names, array_keys($this->routes), array_keys($this->serializedRoutes)); if ($routes_to_load) { - if ($cache = $this->cache->get(static::ROUTE_LOAD_CID . hash('sha512', serialize($routes_to_load)))) { + $cid = static::ROUTE_LOAD_CID . hash('sha512', serialize($routes_to_load)); + if ($cache = $this->cache->get($cid)) { $routes = $cache->data; } else { $result = $this->connection->query('SELECT name, route FROM {' . $this->connection->escapeTable($this->tableName) . '} WHERE name IN ( :names[] )', array(':names[]' => $routes_to_load)); $routes = $result->fetchAllKeyed(); + + $this->cache->set($cid, $routes, Cache::PERMANENT, ['routes']); } $this->serializedRoutes += $routes; @@ -349,6 +353,7 @@ public function getAllRoutes() { public function reset() { $this->routes = array(); $this->serializedRoutes = array(); + Cache::invalidateTags(['routes']); } /** diff --git a/core/tests/Drupal/Tests/Core/Routing/RoutePreloaderTest.php b/core/tests/Drupal/Tests/Core/Routing/RoutePreloaderTest.php index 189aa92..92c20f9 100644 --- a/core/tests/Drupal/Tests/Core/Routing/RoutePreloaderTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/RoutePreloaderTest.php @@ -42,12 +42,20 @@ class RoutePreloaderTest extends UnitTestCase { protected $preloader; /** + * The mocked cache. + * + * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cache; + + /** * {@inheritdoc} */ protected function setUp() { $this->routeProvider = $this->getMock('Drupal\Core\Routing\PreloadableRouteProviderInterface'); $this->state = $this->getMock('\Drupal\Core\State\StateInterface'); - $this->preloader = new RoutePreloader($this->routeProvider, $this->state); + $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $this->preloader = new RoutePreloader($this->routeProvider, $this->state, $this->cache); } /**