diff --git a/src/Routing/Middleware/RoutingMiddleware.php b/src/Routing/Middleware/RoutingMiddleware.php index 7c9fd29162f..9db53779d8f 100644 --- a/src/Routing/Middleware/RoutingMiddleware.php +++ b/src/Routing/Middleware/RoutingMiddleware.php @@ -32,11 +32,6 @@ */ class RoutingMiddleware { - /** - * Name of the default cache configuration name used to store routes collection - */ - const DEFAULT_ROUTER_CACHE_CONFIG = '_cake_router_'; - /** * Key used to store the route collection in the cache engine */ @@ -49,14 +44,24 @@ class RoutingMiddleware */ protected $app; + /** + * The cache configuration name to use for route collection caching, + * null to disable caching + * + * @var string + */ + protected $cacheConfig; + /** * Constructor * * @param \Cake\Http\BaseApplication $app The application instance that routes are defined on. + * @param string|null $cacheConfig The cache config name to use or null to disable routes cache */ - public function __construct(BaseApplication $app = null) + public function __construct(BaseApplication $app = null, $cacheConfig = null) { $this->app = $app; + $this->cacheConfig = $cacheConfig; } /** @@ -85,14 +90,10 @@ protected function loadRoutes() */ protected function buildRouteCollection() { - $isRouterCacheEnabled = Configure::read('Router.cache'); - if (Cache::enabled() && $isRouterCacheEnabled) { - $routesCacheConfig = Configure::read('Router.cacheConfig', static::DEFAULT_ROUTER_CACHE_CONFIG); - + if (Cache::enabled() && $this->cacheConfig !== null) { return Cache::remember(static::ROUTE_COLLECTION_CACHE_KEY, function () { - return $this->prepareRouteCollection(); - }, $routesCacheConfig); + }, $this->cacheConfig); } return $this->prepareRouteCollection(); diff --git a/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php b/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php index ba0e885385e..ce85e894911 100644 --- a/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php +++ b/tests/TestCase/Routing/Middleware/RoutingMiddlewareTest.php @@ -465,26 +465,26 @@ public function scopedMiddlewareUrlProvider() */ public function testCacheRoutes() { - Configure::write('Router.cache', true); - Cache::setConfig('_cake_router_', [ + $cacheConfigName = '_cake_router_'; + Cache::setConfig($cacheConfigName, [ 'engine' => 'File', 'path' => TMP, ]); Router::$initialized = false; $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']); $response = new Response(); - $next = function ($req, $res) { - $routeCollection = Cache::read('routeCollection', '_cake_router_'); + $next = function ($req, $res) use ($cacheConfigName) { + $routeCollection = Cache::read('routeCollection', $cacheConfigName); $this->assertInstanceOf(RouteCollection::class, $routeCollection); return $res; }; $app = new Application(CONFIG); - $middleware = new RoutingMiddleware($app); + $middleware = new RoutingMiddleware($app, $cacheConfigName); $middleware($request, $response, $next); - Cache::clear(false, '_cake_router_'); - Cache::drop('_cake_router_'); + Cache::clear(false, $cacheConfigName); + Cache::drop($cacheConfigName); } /** @@ -494,26 +494,27 @@ public function testCacheRoutes() */ public function testCacheNotUsedIfCacheDisabled() { - Configure::write('Router.cache', true); + $cacheConfigName = '_cake_router_'; Cache::disable(); - Cache::setConfig('_cake_router_', [ + Cache::setConfig($cacheConfigName, [ 'engine' => 'File', 'path' => TMP, ]); Router::$initialized = false; $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/articles']); $response = new Response(); - $next = function ($req, $res) { - $routeCollection = Cache::read('routeCollection', '_cake_router_'); + $next = function ($req, $res) use ($cacheConfigName) { + $routeCollection = Cache::read('routeCollection', $cacheConfigName); $this->assertFalse($routeCollection); return $res; }; $app = new Application(CONFIG); - $middleware = new RoutingMiddleware($app); + $middleware = new RoutingMiddleware($app, $cacheConfigName); $middleware($request, $response, $next); - Cache::drop('_cake_router_'); + Cache::clear(false, $cacheConfigName); + Cache::drop($cacheConfigName); Cache::enable(); } @@ -527,8 +528,6 @@ public function testCacheConfigNotFound() $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('The "notfound" cache configuration does not exist'); - Configure::write('Router.cache', true); - Configure::write('Router.cacheConfig', 'notfound'); Cache::setConfig('_cake_router_', [ 'engine' => 'File', 'path' => TMP, @@ -540,7 +539,7 @@ public function testCacheConfigNotFound() return $res; }; $app = new Application(CONFIG); - $middleware = new RoutingMiddleware($app); + $middleware = new RoutingMiddleware($app, 'notfound'); $middleware($request, $response, $next); Cache::drop('_cake_router_');