diff --git a/src/Illuminate/CachedRouting/Router.php b/src/Illuminate/CachedRouting/Router.php index 4c5566a2..f7125203 100644 --- a/src/Illuminate/CachedRouting/Router.php +++ b/src/Illuminate/CachedRouting/Router.php @@ -82,7 +82,7 @@ public function cache($filename, Closure $callback, $cacheMinutes = 1440) return null; } - $cacher = $this->container['cache']; + $cacher = $this->getRouteCacher(); $cacheKey = $this->getCacheKey($filename); // Check if the current route group is cached. @@ -113,9 +113,20 @@ public function cache($filename, Closure $callback, $cacheMinutes = 1440) */ public function clearCache($filename) { - $cacher = $this->container['cache']; + $this->getRouteCacher()->forget($this->getCacheKey($filename)); + } - $cacher->forget($this->getCacheKey($filename)); + /** + * Get the cache store used to persist compiled routes. + * + * Always the local "file" store so that booting the application (web or + * artisan) never depends on a networked cache such as Redis. + * + * @return \Illuminate\Cache\Repository + */ + protected function getRouteCacher() + { + return $this->container['cache']->driver('file'); } /** diff --git a/tests/CachedRouting/RoutingIntegrationTest.php b/tests/CachedRouting/RoutingIntegrationTest.php index a826358c..fa06ebb3 100755 --- a/tests/CachedRouting/RoutingIntegrationTest.php +++ b/tests/CachedRouting/RoutingIntegrationTest.php @@ -37,6 +37,7 @@ use Illuminate\Cache\CacheManager; use Illuminate\CachedRouting\Router; use Illuminate\Config\Repository; +use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Application; use Illuminate\Session\SessionManager; use Illuminate\Support\Facades\Facade; @@ -51,6 +52,8 @@ class RoutingIntegrationTest extends TestCase */ protected ?Application $app = null; + protected static ?string $cachePath = null; + /** * Setup the test environment. */ @@ -61,6 +64,13 @@ protected function setUp(): void } } + protected function tearDown(): void + { + if (self::$cachePath !== null) { + (new Filesystem)->deleteDirectory(self::$cachePath); + } + } + /** * Refresh the application instance. */ @@ -83,8 +93,10 @@ protected function refreshApplication(): void $this->app['config'] = new Repository($loader, $this->app['env']); + $this->app['files'] = new Filesystem; $this->app['cache'] = new CacheManager($this->app); - $this->app['config']['cache.driver'] = 'array'; + $this->app['config']['cache.driver'] = 'file'; + $this->app['config']['cache.path'] = self::$cachePath = sys_get_temp_dir() . '/l42x-route-cache-' . uniqid(); $this->app['session'] = new SessionManager($this->app); $this->app['config']['session.driver'] = 'array'; @@ -150,7 +162,6 @@ public function testCacheRoutesNoTtl(): void }, 0); static::assertNull($key, 'Cache key should be null with TTL=0'); - static::assertFalse($this->app->cache->has($key), 'Key should not be stored in cache'); static::assertEquals(1, $router->getRoutes()->count(), 'Route must be added to router'); }