Skip to content

Commit

Permalink
Don't do any caching with TTL is 0
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
Maarten Staa committed Apr 17, 2015
1 parent 511dc65 commit e9fc749
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 13 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit e9fc749

Please sign in to comment.