diff --git a/src/Router.php b/src/Router.php index f9a1faf..2c5e1b4 100644 --- a/src/Router.php +++ b/src/Router.php @@ -72,6 +72,15 @@ public function __construct(Dispatcher $events, Container $container = null) */ public function cache($filename, Closure $callback, $cacheMinutes = 1440) { + // If $cacheMinutes is 0 or lower, there is no need to cache anything. + if ($cacheMinutes <= 0) { + // Call closure to define routes that should be cached. + call_user_func($callback, $this); + + // No cache key. + return null; + } + $cacher = $this->container['cache']; $cacheKey = $this->getCacheKey($filename); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 2c92bc4..4dc530a 100755 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -160,6 +160,19 @@ public function testCacheRoutes() $this->assertEquals(1, $router->getRoutes()->count(), 'Routes must be obtained from cache'); } + public function testCacheRoutesNoTtl() + { + $router = $this->getRouter(); + + $key = $router->cache(__FILE__, function () use ($router) { + $router->get('/', 'HomeController@actionIndex'); + }, 0); + + $this->assertNull($key, 'Cache key should be null with TTL=0'); + $this->assertFalse($this->app->cache->has($key), 'Key should not be stored in cache'); + $this->assertEquals(1, $router->getRoutes()->count(), 'Route must be added to router'); + } + public function testAllMethodsWorks() { $methods = array('get', 'post', 'put', 'patch', 'delete');