Skip to content

Commit

Permalink
Add Router::clearCache function.
Browse files Browse the repository at this point in the history
Closes #15.
  • Loading branch information
Maarten Staa committed May 10, 2016
1 parent 47df3d8 commit 6a32309
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ public function cache($filename, Closure $callback, $cacheMinutes = 1440)
return $cacheKey;
}

/**
* Clear the cached data for the given routes file.
*
* @param string $filename
*/
public function clearCache($filename)
{
$cacher = $this->container['cache'];

$cacher->forget($this->getCacheKey($filename));
}

/**
* Get the key under which the routes cache for the given file should be stored.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,22 @@ public function testCanUseResource()
});
$this->assertEquals(8, $router->getRoutes()->count(), 'Routes must be obtained from cache');
}

public function testCanClearCache()
{
// Create a controller class.
$controllerName = str_shuffle('abcdefghijklmnopqrstuvwxyz');
eval('class ' . $controllerName . ' extends Illuminate\Routing\Controller { }');

// First, define a route.
$router = $this->getRouter();
$key = $router->cache(__FILE__, function () use ($router, $controllerName) {
$router->get('/{foo}/{bar}', $controllerName . '@getIndex')->where('foo', '\w+')->where('bar', '\d+');
});
$this->assertTrue($this->app->cache->has($key), 'Routes must be in cache');

// Next, clear it.
$router->clearCache(__FILE__);
$this->assertFalse($this->app->cache->has($key), 'Routes must no longer be cached');
}
}

0 comments on commit 6a32309

Please sign in to comment.